Python pyparsing.alphanums() Examples

The following are 19 code examples of pyparsing.alphanums(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyparsing , or try the search function .
Example #1
Source File: sql_graphviz.py    From auptimizer with GNU General Public License v3.0 6 votes vote down vote up
def grammar():
    parenthesis = Forward()
    parenthesis <<= "(" + ZeroOrMore(CharsNotIn("()") | parenthesis) + ")"

    field_def = OneOrMore(Word(alphanums + "_\"'`:-") | parenthesis)
    field_def.setParseAction(field_act)

    tablename_def = ( Word(alphas + "`_") | QuotedString("\"") )

    field_list_def = field_def + ZeroOrMore(Suppress(",") + field_def)
    field_list_def.setParseAction(field_list_act)

    create_table_def = Literal("CREATE") + "TABLE" + tablename_def.setResultsName("tableName") + "(" + field_list_def.setResultsName("fields") + ")" + ";"
    create_table_def.setParseAction(create_table_act)

    add_fkey_def = Literal("FOREIGN") + "KEY" + "(" + Word(alphanums).setResultsName("keyName") + ")" + "REFERENCES" + Word(alphanums).setResultsName("fkTable") + "(" + Word(alphanums + "_").setResultsName("fkCol") + ")" + Optional(Literal("DEFERRABLE")) + ";"
    add_fkey_def.setParseAction(add_fkey_act)

    other_statement_def = OneOrMore(CharsNotIn(";")) + ";"
    other_statement_def.setParseAction(other_statement_act)

    comment_def = "--" + ZeroOrMore(CharsNotIn("\n"))
    comment_def.setParseAction(other_statement_act)

    return OneOrMore(comment_def | create_table_def | add_fkey_def | other_statement_def) 
Example #2
Source File: interpreter.py    From design-patterns with MIT License 6 votes vote down vote up
def parse(input_string):
        word = Word(alphanums)
        command = Group(OneOrMore(word))
        token = Suppress("->")
        device = Group(OneOrMore(word))
        argument = Group(OneOrMore(word))
        event = command + token + device + Optional(token + argument)
        parse_results = event.parseString(input_string)
        cmd = parse_results[0]
        dev = parse_results[1]
        cmd_str = "_".join(cmd)
        dev_str = "_".join(dev)
        try:
            val = parse_results[2]
        except IndexError:
            val_str = None
        else:
            val_str = "_".join(val)
        return cmd_str, dev_str, val_str 
Example #3
Source File: utilities.py    From pyactr with GNU General Public License v3.0 5 votes vote down vote up
def getrule():
    """
    Using pyparsing, get rule out of a string.
    """
    arrow = pp.Literal("==>")
    buff = pp.Word(pp.alphas, "".join([pp.alphanums, "_"]))
    special_valueLHS = pp.oneOf([x for x in _LHSCONVENTIONS.keys()])
    end_buffer = pp.Literal(">")
    special_valueRHS = pp.oneOf([x for x in _RHSCONVENTIONS.keys()])
    chunk = getchunk()
    rule_reader = pp.Group(pp.OneOrMore(pp.Group(special_valueLHS + buff + end_buffer + pp.Group(pp.Optional(chunk))))) + arrow + pp.Group(pp.OneOrMore(pp.Group(special_valueRHS + buff + end_buffer + pp.Group(pp.Optional(chunk)))))
    return rule_reader 
Example #4
Source File: core.py    From fusesoc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def parse(self, flags):
        _flags = []
        for k, v in flags.items():
            if v == True:
                _flags.append(k)
            elif v in [False, None]:
                pass
            else:
                _flags.append(k + "_" + v)

        def cb_conditional(s, l, t):
            if (t.cond in _flags) != (t.negate == "!"):
                return t.expr
            else:
                return []

        word = Word(alphanums + ":<>.[]_-,=~/^~")
        conditional = Forward()
        conditional << (
            Optional("!")("negate")
            + word("cond")
            + Suppress("?")
            + Suppress("(")
            + OneOrMore(conditional ^ word)("expr")
            + Suppress(")")
        ).setParseAction(cb_conditional)
        string = word
        string_list = OneOrMore(conditional ^ string)
        s = " ".join(string_list.parseString(self.__str__()))
        logger.debug(
            "Parsing '{}' with flags {} => {}".format(self.__str__(), str(_flags), s)
        )
        return s 
Example #5
Source File: utilities.py    From pyactr with GNU General Public License v3.0 5 votes vote down vote up
def getchunk():
    """
    Using pyparsing, create chunk reader for chunk strings.
    """
    slot = pp.Word("".join([pp.alphas, "_"]), "".join([pp.alphanums, "_"]))
    special_value = pp.Group(pp.oneOf([ACTRVARIABLE, "".join([ACTRNEG, ACTRVARIABLE]), ACTRNEG, VISIONGREATER, VISIONSMALLER, "".join([VISIONGREATER, ACTRVARIABLE]), "".join([VISIONSMALLER, ACTRVARIABLE])])\
            + pp.Word("".join([pp.alphanums, "_", '"', "'"])))
    strvalue = pp.QuotedString('"', unquoteResults=False)
    strvalue2 = pp.QuotedString("'", unquoteResults=False)
    varvalue = pp.Word("".join([pp.alphanums, "_"]))
    value = varvalue | special_value | strvalue | strvalue2
    chunk_reader = pp.OneOrMore(pp.Group(slot + value))
    return chunk_reader 
Example #6
Source File: modulefilter.py    From wfuzz with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        if PYPARSING:
            category = Word(alphas + "_-*", alphanums + "_-*")
            operator = oneOf("and or ,")
            neg_operator = "not"
            elementRef = category
            definition = elementRef + ZeroOrMore(operator + elementRef)
            nestedformula = Group(Suppress(Optional(Literal("("))) + definition + Suppress(Optional(Literal(")"))))
            neg_nestedformula = Optional(neg_operator) + nestedformula
            self.finalformula = neg_nestedformula + ZeroOrMore(operator + neg_nestedformula)

            elementRef.setParseAction(self.__compute_element)
            neg_nestedformula.setParseAction(self.__compute_neg_formula)
            nestedformula.setParseAction(self.__compute_formula)
            self.finalformula.setParseAction(self.__myreduce) 
Example #7
Source File: _importer.py    From tcconfig with MIT License 5 votes vote down vote up
def __parse_tc_filter_network(text):
        network_pattern = pp.SkipTo("{:s}=".format(Tc.Param.DST_NETWORK), include=True) + pp.Word(
            pp.alphanums + "." + "/"
        )

        return network_pattern.parseString(text)[-1] 
Example #8
Source File: stringparser.py    From bob with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        # create parsing grammer
        sQStringLiteral = pyparsing.QuotedString("'")
        sQStringLiteral.setParseAction(
            lambda s, loc, toks: StringLiteral(s, loc, toks, False))

        dQStringLiteral = pyparsing.QuotedString('"', '\\')
        dQStringLiteral.setParseAction(
            lambda s, loc, toks: StringLiteral(s, loc, toks, True))

        stringLiteral = sQStringLiteral | dQStringLiteral

        functionCall = pyparsing.Forward()
        functionArg = stringLiteral | functionCall
        functionCall << pyparsing.Word(pyparsing.alphas, pyparsing.alphanums+'-') + \
            pyparsing.Suppress('(') + \
            pyparsing.Optional(functionArg +
                pyparsing.ZeroOrMore(pyparsing.Suppress(',') + functionArg)) + \
            pyparsing.Suppress(')')
        functionCall.setParseAction(
            lambda s, loc, toks: FunctionCall(s, loc, toks))

        predExpr = pyparsing.infixNotation(
            stringLiteral ^ functionCall ,
            [
                ('!',  1, pyparsing.opAssoc.RIGHT, lambda s, loc, toks: NotOperator(s, loc, toks)),
                ('<',  2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryStrOperator)),
                ('<=', 2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryStrOperator)),
                ('>',  2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryStrOperator)),
                ('>=', 2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryStrOperator)),
                ('==', 2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryStrOperator)),
                ('!=', 2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryStrOperator)),
                ('&&', 2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryBoolOperator)),
                ('||', 2, pyparsing.opAssoc.LEFT,  infixBinaryOp(BinaryBoolOperator))
            ])

        self.__ifgrammer = predExpr 
Example #9
Source File: parsers.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def identifier():
    return pyparsing.Word(pyparsing.alphas + '_', pyparsing.alphanums + '_') 
Example #10
Source File: expression_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _identifier_with_dots(self):
        return pyparsing.Word(
            pyparsing.alphas + '_.', pyparsing.alphanums + '_.') 
Example #11
Source File: expression_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def XXXX_cast_expression(self):
        """A function returning a parser for parsing cast expressions.

        Args:
            expression: a pyparsing parser for parsing an expression to be cast.

        Returns:
            A (pyparsing) parser for parsing cast expressions.
        """
        word = pyparsing.Word(pyparsing.alphanums + '_*[]')
        nested = pyparsing.Forward().setName("nested")
        nested << pyparsing.Combine(
            pyparsing.Literal('(').suppress()
            + pyparsing.Combine(
                pyparsing.ZeroOrMore(self._integer() | word | nested))
            + pyparsing.Literal(')').suppress()
        )
        typeof_expression = (
            _OPEN_PARENTHESIS
            + pyparsing.Keyword('typeof')
            + nested("typeof_arg")
            + _CLOSE_PARENTHESIS
        )

        type_expression = (
            typeof_expression
            | nested("simple_type")
        )
        return (
            type_expression
            + ~(_PLUS | _MINUS)
            + self.expression("expression")
        ).setParseAction(self._create_cast_expression) 
Example #12
Source File: expression_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _type_property(self):
        return (
            _TYPE_PROPERTY_KEYWORD
            + _OPEN_PARENTHESIS
            + pyparsing.Word(pyparsing.alphanums + ' _*[]')
            + _CLOSE_PARENTHESIS
        ).setParseAction(self._create_sizeof_type) 
Example #13
Source File: expression_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _cast_transformer(self):
        """Removes obvious casts."""
        return pyparsing.Combine(
            pyparsing.Regex(r"\([^()]*\)").suppress()
            + (pyparsing.Word(pyparsing.alphanums + "_")
               | pyparsing.Literal("(")),
            adjacent=False) 
Example #14
Source File: preprocessing_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def static_function(self):
        return (
            (pyparsing.Keyword("static") | pyparsing.Keyword("inline"))
            + pyparsing.OneOrMore(pyparsing.Word(pyparsing.alphanums + "_*&"))
            + parsers.anything_in_parentheses()
            + parsers.anything_in_curly()
        ).suppress() 
Example #15
Source File: searchparser.py    From phpsploit with GNU General Public License v3.0 4 votes vote down vote up
def parser(self):
        """
        This function returns a parser.
        The grammar should be like most full text search engines (Google, Tsearch, Lucene).
        
        Grammar:
        - a query consists of alphanumeric words, with an optional '*' wildcard
          at the end of a word
        - a sequence of words between quotes is a literal string
        - words can be used together by using operators ('and' or 'or')
        - words with operators can be grouped with parenthesis
        - a word or group of words can be preceded by a 'not' operator
        - the 'and' operator precedes an 'or' operator
        - if an operator is missing, use an 'and' operator
        """
        operatorOr = Forward()
        
        operatorWord = Group(Combine(Word(alphanums) + Suppress('*'))).setResultsName('wordwildcard') | \
                            Group(Word(alphanums)).setResultsName('word')
        
        operatorQuotesContent = Forward()
        operatorQuotesContent << (
            (operatorWord + operatorQuotesContent) | operatorWord
        )
        
        operatorQuotes = Group(
            Suppress('"') + operatorQuotesContent + Suppress('"')
        ).setResultsName("quotes") | operatorWord
        
        operatorParenthesis = Group(
            (Suppress("(") + operatorOr + Suppress(")"))
        ).setResultsName("parenthesis") | operatorQuotes

        operatorNot = Forward()
        operatorNot << (Group(
            Suppress(Keyword("not", caseless=True)) + operatorNot
        ).setResultsName("not") | operatorParenthesis)

        operatorAnd = Forward()
        operatorAnd << (Group(
            operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd
        ).setResultsName("and") | Group(
            operatorNot + OneOrMore(~oneOf("and or") + operatorAnd)
        ).setResultsName("and") | operatorNot)
        
        operatorOr << (Group(
            operatorAnd + Suppress(Keyword("or", caseless=True)) + operatorOr
        ).setResultsName("or") | operatorAnd)

        return operatorOr.parseString 
Example #16
Source File: interpreter.py    From Mastering-Python-Design-Patterns-Second-Edition with MIT License 4 votes vote down vote up
def main():  
    word = Word(alphanums)  
    command = Group(OneOrMore(word))  
    token = Suppress("->")  
    device = Group(OneOrMore(word))  
    argument = Group(OneOrMore(word))  
    event = command + token + device + Optional(token + argument)  
 
    gate = Gate()  
    garage = Garage()  
    airco = Aircondition()  
    heating = Heating()  
    boiler = Boiler()  
    fridge = Fridge()  

    tests = ('open -> gate',  
             'close -> garage',  
             'turn on -> air condition',  
             'turn off -> heating',  
             'increase -> boiler temperature -> 5 degrees',  
             'decrease -> fridge temperature -> 2 degrees')  

    open_actions = {'gate':gate.open, 
                    'garage':garage.open, 
                    'air condition':airco.turn_on,  
                    'heating':heating.turn_on, 
                    'boiler temperature':boiler.increase_temperature,  
                    'fridge temperature':fridge.increase_temperature}  
    close_actions = {'gate':gate.close, 
                     'garage':garage.close, 
                     'air condition':airco.turn_off,  
                     'heating':heating.turn_off, 
                     'boiler temperature':boiler.decrease_temperature,  
                     'fridge temperature':fridge.decrease_temperature}  

    for t in tests:  
        if len(event.parseString(t)) == 2: # no argument  
            cmd, dev = event.parseString(t)  
            cmd_str, dev_str = ' '.join(cmd), ' '.join(dev)  
            if 'open' in cmd_str or 'turn on' in cmd_str:  
                open_actions[dev_str]()  
            elif 'close' in cmd_str or 'turn off' in cmd_str:  
                close_actions[dev_str]()  
        elif len(event.parseString(t)) == 3: # argument  
            cmd, dev, arg = event.parseString(t)  
            cmd_str = ' '.join(cmd)
            dev_str = ' '.join(dev)
            arg_str = ' '.join(cmd)
            num_arg = 0  
            try:  
                # extract the numeric part
                num_arg = int(arg_str.split()[0])  
            except ValueError as err:  
                print(f"expected number but got: '{arg_str[0]}'")  
            if 'increase' in cmd_str and num_arg > 0:  
                open_actions[dev_str](num_arg)  
            elif 'decrease' in cmd_str and num_arg > 0:  
                close_actions[dev_str](num_arg) 
Example #17
Source File: searchparser.py    From patzilla with GNU Affero General Public License v3.0 4 votes vote down vote up
def parser(self):
        """
        This function returns a parser.
        The grammar should be like most full text search engines (Google, Tsearch, Lucene).

        Grammar:
        - a query consists of alphanumeric words, with an optional '*' wildcard
          at the end of a word
        - a sequence of words between quotes is a literal string
        - words can be used together by using operators ('and' or 'or')
        - words with operators can be grouped with parenthesis
        - a word or group of words can be preceded by a 'not' operator
        - the 'and' operator precedes an 'or' operator
        - if an operator is missing, use an 'and' operator
        """
        operatorOr = Forward()

        operatorWord = Word(wordchars).setResultsName('value')

        operatorQuotesContent = Forward()
        operatorQuotesContent << (
            (operatorWord + operatorQuotesContent) | operatorWord
            )

        operatorQuotes = Group(
            Suppress('"') + operatorQuotesContent + Suppress('"')
        ).setResultsName("quotes") | operatorWord

        prefix = (Word(alphanums).setResultsName('index') + Word('=').setResultsName('binop'))
        operatorParenthesis = Group(
            Optional(prefix) +
            (Suppress("(") + operatorOr + Suppress(")"))
        ).setResultsName("parenthesis") | Group(prefix + operatorQuotes).setResultsName('term') | operatorQuotes

        operatorNot = Forward()
        operatorNot << (Group(
            Suppress(Keyword("not", caseless=True)) + operatorNot
        ).setResultsName("not") | operatorParenthesis)

        operatorAnd = Forward()
        operatorAnd << (Group(
            operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd
        ).setResultsName("and") | Group(
            operatorNot + OneOrMore(~oneOf("and or", caseless=True) + operatorAnd)
        ).setResultsName("and") | operatorNot)

        operatorProximity = Forward()
        operatorProximity << (Group(
            operatorParenthesis + Suppress(Literal("near,")) + Word(nums).setResultsName('distance') + operatorParenthesis
        ).setResultsName("near") | Group(
            operatorParenthesis + Suppress(Literal("span,")) + Word(nums).setResultsName('distance') + operatorParenthesis
        ).setResultsName("span") | operatorAnd)

        operatorOr << (Group(
            operatorProximity + Suppress(Keyword("or", caseless=True)) + operatorOr
        ).setResultsName("or") | operatorProximity)

        return operatorOr.parseString 
Example #18
Source File: interpreter.py    From Python_Master-the-Art-of-Design-Patterns with MIT License 4 votes vote down vote up
def main():
    word = Word(alphanums)
    command = Group(OneOrMore(word))
    token = Suppress("->")
    device = Group(OneOrMore(word))
    argument = Group(OneOrMore(word))
    event = command + token + device + Optional(token + argument)

    gate = Gate()
    garage = Garage()
    airco = Aircondition()
    heating = Heating()
    boiler = Boiler()
    fridge = Fridge()

    tests = ('open -> gate',
             'close -> garage',
             'turn on -> aircondition',
             'turn off -> heating',
             'increase -> boiler temperature -> 5 degrees',
             'decrease -> fridge temperature -> 2 degrees')

    open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on,
                  'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature,
                  'fridge temperature':fridge.increase_temperature}
    close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off,
                   'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature,
                   'fridge temperature':fridge.decrease_temperature}

    for t in tests:
        if len(event.parseString(t)) == 2: # no argument
            cmd, dev = event.parseString(t)
            cmd_str, dev_str = ' '.join(cmd), ' '.join(dev)
            if 'open' in cmd_str or 'turn on' in cmd_str:
                open_actions[dev_str]()
            elif 'close' in cmd_str or 'turn off' in cmd_str:
                close_actions[dev_str]()
        elif len(event.parseString(t)) == 3: # argument
            cmd, dev, arg = event.parseString(t)
            cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg)
            num_arg = 0
            try:
                num_arg = int(arg_str.split()[0]) # extract the numeric part
            except ValueError as err:
                print("expected number but got: '{}'".format(arg_str[0]))
            if 'increase' in cmd_str and num_arg > 0:
                open_actions[dev_str](num_arg)
            elif 'decrease' in cmd_str and num_arg > 0:
                close_actions[dev_str](num_arg) 
Example #19
Source File: interpreter.py    From Python_Master-the-Art-of-Design-Patterns with MIT License 4 votes vote down vote up
def main():
    word = Word(alphanums)
    command = Group(OneOrMore(word))
    token = Suppress("->")
    device = Group(OneOrMore(word))
    argument = Group(OneOrMore(word))
    event = command + token + device + Optional(token + argument)

    gate = Gate()
    garage = Garage()
    airco = Aircondition()
    heating = Heating()
    boiler = Boiler()
    fridge = Fridge()

    tests = ('open -> gate',
             'close -> garage',
             'turn on -> aircondition',
             'turn off -> heating',
             'increase -> boiler temperature -> 20 degrees',
             'decrease -> fridge temperature -> 6 degree')

    open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on,
                  'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature,
                  'fridge temperature':fridge.increase_temperature}
    close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off,
                   'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature,
                   'fridge temperature':fridge.decrease_temperature}

    for t in tests:
        if len(event.parseString(t)) == 2: # no argument
            cmd, dev = event.parseString(t)
            cmd_str, dev_str = ' '.join(cmd), ' '.join(dev)
            if 'open' in cmd_str or 'turn on' in cmd_str:
                open_actions[dev_str]()
            elif 'close' in cmd_str or 'turn off' in cmd_str:
                close_actions[dev_str]()
        elif len(event.parseString(t)) == 3: # argument
            cmd, dev, arg = event.parseString(t)
            cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg)
            num_arg = 0
            try:
                num_arg = int(arg_str.split()[0]) # extract the numeric part
            except ValueError as err:
                print("expected number but got: '{}'".format(arg_str[0]))
            if 'increase' in cmd_str and num_arg > 0:
                open_actions[dev_str](num_arg)
            elif 'decrease' in cmd_str and num_arg > 0:
                close_actions[dev_str](num_arg)