Python pyparsing.ZeroOrMore() Examples
The following are 23
code examples of pyparsing.ZeroOrMore().
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: parsers.py From rekall with GNU General Public License v2.0 | 7 votes |
def anything_beetween(opener_and_closer): """Builds a (pyparsing) parser for the content inside delimiters. Args: opener_and_closer: a string containing two elements: opener and closer Returns: A (pyparsing) parser for the content inside delimiters. """ opener = pyparsing.Literal(opener_and_closer[0]) closer = pyparsing.Literal(opener_and_closer[1]) char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer)) other_chars = unicode(string.printable).translate(char_removal_mapping) word_without_delimiters = pyparsing.Word(other_chars).setName( "other_chars") anything = pyparsing.Forward() delimited_block = opener + anything + closer # pylint: disable=expression-not-assigned anything << pyparsing.ZeroOrMore( word_without_delimiters.setName("word_without_delimiters") | delimited_block.setName("delimited_block") ) # Combine all the parts into a single string. return pyparsing.Combine(anything)
Example #2
Source File: parser.py From pdblp with MIT License | 6 votes |
def _parse(mystr): LBRACE, RBRACE, EQUAL = map(pp.Suppress, "{}=") field = pp.Word(pp.printables + ' ', excludeChars='[]=') field.addParseAction(pp.tokenMap(str.rstrip)) string = pp.dblQuotedString().setParseAction(pp.removeQuotes) number = pp.pyparsing_common.number() date_expr = pp.Regex(r'\d\d\d\d-\d\d-\d\d') time_expr = pp.Regex(r'\d\d:\d\d:\d\d\.\d\d\d') nan = pp.Keyword('nan') scalar_value = (string | date_expr | time_expr | number | nan) list_marker = pp.Suppress("[]") value_list = pp.Forward() jobject = pp.Forward() memberDef1 = pp.Group(field + EQUAL + scalar_value) memberDef2 = pp.Group(field + EQUAL + jobject) memberDef3 = pp.Group(field + list_marker + EQUAL + LBRACE + value_list + RBRACE) memberDef = memberDef1 | memberDef2 | memberDef3 value_list <<= (pp.delimitedList(scalar_value, ",") | pp.ZeroOrMore(pp.Group(pp.Dict(memberDef2)))) value_list.setParseAction(lambda t: [pp.ParseResults(t[:])]) members = pp.OneOrMore(memberDef) jobject <<= pp.Dict(LBRACE + pp.ZeroOrMore(memberDef) + RBRACE) # force empty jobject to be a dict jobject.setParseAction(lambda t: t or {}) parser = members parser = pp.OneOrMore(pp.Group(pp.Dict(memberDef))) return parser.parseString(mystr)
Example #3
Source File: rule.py From CWR-DataApi with MIT License | 6 votes |
def _apply_modifiers(rule, modifiers): if 'grouped' in modifiers: rule = pp.Group(rule) if 'optional' in modifiers: rule = pp.Optional(rule) else: for modifier in modifiers: if modifier.startswith('at_least'): times = rule_at_least.parseString(modifier)[0] if times > 0: rule_multiple = rule for _ in range(1, times): rule_multiple = rule_multiple + rule rule = rule_multiple + pp.ZeroOrMore(rule) else: rule = pp.Optional(pp.ZeroOrMore(rule)) return rule
Example #4
Source File: sql_graphviz.py From auptimizer with GNU General Public License v3.0 | 6 votes |
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 #5
Source File: c_parser.py From rekall with GNU General Public License v2.0 | 6 votes |
def _type_reference(self): """A reference to a type. The type may be already defined in place or just referred by name. """ identifier = ( self._typeof_expression() # Inline struct definition. # e.g. struct { int x; } foo; | self._struct_definition_possibly_with_fields() | self._enum_definition() | self._numeric_type_identifier() | self._compound_type_identifier() | self._identifier() ) return ( pyparsing.ZeroOrMore(_VOLATILE) + identifier ).setParseAction(self._create_type_reference)
Example #6
Source File: c_parser.py From rekall with GNU General Public License v2.0 | 6 votes |
def _maybe_attributes(self): """Possibly match some attributes. The syntax of attributes is described here: https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html """ return pyparsing.Group( pyparsing.ZeroOrMore( _ATTRIBUTE + _DOUBLE_OPEN_PARENTHESIS + pyparsing.delimitedList( pyparsing.Group( self._identifier()("name") + pyparsing.Optional( _OPEN_PARENTHESIS + parsers.anything_beetween("()")("args") + _CLOSE_PARENTHESIS ) ) ) + _DOUBLE_CLOSE_PARENTHESIS ).setParseAction(self._make_attribute) )("attributes")
Example #7
Source File: yara_support.py From rekall with GNU General Public License v2.0 | 6 votes |
def anything_beetween(opener_and_closer): """Builds a (pyparsing) parser for the content inside delimiters. Args: opener_and_closer: a string containing two elements: opener and closer Returns: A (pyparsing) parser for the content inside delimiters. """ opener = pyparsing.Literal(opener_and_closer[0]) closer = pyparsing.Literal(opener_and_closer[1]) char_removal_mapping = dict.fromkeys(list(map(ord, opener_and_closer))) other_chars = str(string.printable).translate(char_removal_mapping) word_without_delimiters = pyparsing.Word(other_chars).setName( "other_chars") anything = pyparsing.Forward() delimited_block = opener + anything + closer # pylint: disable=expression-not-assigned anything << pyparsing.ZeroOrMore( word_without_delimiters.setName("word_without_delimiters") | delimited_block.setName("delimited_block") ) # Combine all the parts into a single string. return pyparsing.Combine(anything)
Example #8
Source File: ldap3mock.py From privacyidea with GNU Affero General Public License v3.0 | 6 votes |
def _parse_filter(): op = pyparsing.oneOf('! & |') lpar = pyparsing.Literal('(').suppress() rpar = pyparsing.Literal(')').suppress() k = pyparsing.Word(pyparsing.alphanums) # NOTE: We may need to expand on this list, but as this is not a real # LDAP server we should be OK. # Value to contain: # numbers, upper/lower case letters, astrisk, at symbol, minus, full # stop, backslash or a space v = pyparsing.Word(pyparsing.alphanums + "-*@.\\ äöü") rel = pyparsing.oneOf("= ~= >= <=") expr = pyparsing.Forward() atom = pyparsing.Group(lpar + op + expr + rpar) \ | pyparsing.Combine(lpar + k + rel + v + rpar) expr << atom + pyparsing.ZeroOrMore( expr ) return expr
Example #9
Source File: expression_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
def _base_or_array_expression(self): array_indices = pyparsing.ZeroOrMore( _OPEN_BRACKETS + self.expression + _CLOSE_BRACKETS ) return ( self._base_expression() + pyparsing.Group(array_indices) ).setParseAction(self._create_base_or_array_expression)
Example #10
Source File: header_parsing.py From dm_control with Apache License 2.0 | 5 votes |
def _nested_if_else(if_, pred, else_, endif, match_if_true, match_if_false): """Constructs a parser for (possibly nested) if...(else)...endif blocks.""" ifelse = pp.Forward() ifelse << pp.Group( # pylint: disable=expression-not-assigned if_ + pred("predicate") + pp.ZeroOrMore(match_if_true | ifelse)("if_true") + pp.Optional(else_ + pp.ZeroOrMore(match_if_false | ifelse)("if_false")) + endif) return ifelse # Some common string patterns to suppress. # ------------------------------------------------------------------------------
Example #11
Source File: header_parsing.py From dm_control with Apache License 2.0 | 5 votes |
def _nested_scopes(opening, closing, body): """Constructs a parser for (possibly nested) scopes.""" scope = pp.Forward() scope << pp.Group( # pylint: disable=expression-not-assigned opening + pp.ZeroOrMore(body | scope)("members") + closing) return scope
Example #12
Source File: htmlTableParser.py From pyparsing with MIT License | 5 votes |
def table_row(start_tag, end_tag): body = start_tag.tag_body body.addParseAction(pp.tokenMap(str.strip), pp.tokenMap(strip_html)) row = pp.Group( tr.suppress() + pp.ZeroOrMore(start_tag.suppress() + body + end_tag.suppress()) + tr_end.suppress() ) return row
Example #13
Source File: parser.py From pyprover with Apache License 2.0 | 5 votes |
def tokenlist(sep, item): """Creates a list of tokens matching the item.""" return item + ZeroOrMore(sep + item) + Optional(sep)
Example #14
Source File: modulefilter.py From wfuzz with GNU General Public License v2.0 | 5 votes |
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 #15
Source File: stringparser.py From bob with GNU General Public License v3.0 | 5 votes |
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 #16
Source File: c_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
def _enum_definition(self): """Detect an enum definition. e.g. enum foo { OPTION_1: 1 + 2, OPTION_2 } """ return ( _ENUM + pyparsing.Optional(self._identifier())("enum_name") + _OPEN_CURLY + pyparsing.ZeroOrMore( pyparsing.Group( self._identifier()("name") + pyparsing.Optional( _EQUALS # This allows us to get even invalid expressions. + pyparsing.SkipTo(pyparsing.Word(",}"))("expression") ) + pyparsing.Optional(_COMMA) ) )("fields") + _CLOSE_CURLY + self._maybe_attributes()("attributes") ).setParseAction(self._process_enum_definition)
Example #17
Source File: c_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
def _program(self): return pyparsing.ZeroOrMore( self._element() | self._typedef() ).setParseAction(self._make_prog)
Example #18
Source File: expression_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
def _integer(self): integer = self._hexadecimal_as_string() | self._decimal_as_string() # Python does not care about suffixes so we just drop them. possible_suffix = pyparsing.Literal('u') | 'U' | 'll' | 'LL' | 'l' | 'L' maybe_suffix = ( pyparsing.ZeroOrMore(possible_suffix) ).suppress() return ( integer + maybe_suffix ).setParseAction(util.action(lambda x: int(x, base=0)))
Example #19
Source File: expression_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
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 #20
Source File: expansions.py From pyjsgf with MIT License | 5 votes |
def _make_matcher_element(self): # Define an extra parse action for the child's matcher element. def f(tokens): if tokens.asList(): # Add current match values to the _repetitions_matched list. self._repetitions_matched.append(save_current_matches(self.child)) # Wipe current match values for the next repetition (if any). self.child.reset_for_new_match() return tokens # Get the child's matcher element and add the extra parse action. child_element = self.child.matcher_element.addParseAction(f) # Determine the parser element type to use. type_ = pyparsing.ZeroOrMore if self.is_optional else pyparsing.OneOrMore # Handle the special case of a repetition ancestor, e.g. ((a b)+)+ rep = self.repetition_ancestor if rep: # Check if there are no other branches. c = rep.child only_branch = True while c is not self: if len(c.children) > 1: only_branch = False break else: c = c.children[0] # Use an And element instead if self is the only branch because # it makes no sense to repeat a repeat like this! if only_branch: type_ = pyparsing.And child_element = [child_element] return self._set_matcher_element_attributes(type_(child_element))
Example #21
Source File: shparsers.py From stash with MIT License | 4 votes |
def __init__(self, debug=False): self.debug = debug self.logger = logging.getLogger('StaSh.Parser') escaped = pp.Combine("\\" + pp.Word(pp.printables + ' ', exact=1)).setParseAction(self.escaped_action) escaped_oct = pp.Combine("\\" + pp.Word('01234567', max=3)).setParseAction(self.escaped_oct_action) escaped_hex = pp.Combine("\\x" + pp.Word('0123456789abcdefABCDEF', exact=2)).setParseAction(self.escaped_hex_action) # Some special uq_word is needed, e.g. &3 for file descriptor of Pythonista interactive prompt uq_word = (pp.Literal('&3') | pp.Word(_WORD_CHARS)).setParseAction(self.uq_word_action) bq_word = pp.QuotedString('`', escChar='\\', unquoteResults=False).setParseAction(self.bq_word_action) dq_word = pp.QuotedString('"', escChar='\\', unquoteResults=False).setParseAction(self.dq_word_action) sq_word = pp.QuotedString("'", escChar='\\', unquoteResults=False).setParseAction(self.sq_word_action) # The ^ operator means longest match (as opposed to | which means first match) word = pp.Combine(pp.OneOrMore(escaped ^ escaped_oct ^ escaped_hex ^ uq_word ^ bq_word ^ dq_word ^ sq_word))\ .setParseAction(self.word_action) identifier = pp.Word(pp.alphas + '_', pp.alphas + pp.nums + '_').setParseAction(self.identifier_action) assign_op = pp.Literal('=').setParseAction(self.assign_op_action) assignment_word = pp.Combine(identifier + assign_op + word).setParseAction(self.assignment_word_action) punctuator = pp.oneOf('; &').setParseAction(self.punctuator_action) pipe_op = pp.Literal('|').setParseAction(self.pipe_op_action) io_redirect_op = pp.oneOf('>> >').setParseAction(self.io_redirect_op_action) io_redirect = (io_redirect_op + word)('io_redirect') # The optional ' ' is a workaround to a possible bug in pyparsing. # The position of cmd_word after cmd_prefix is always reported 1 character ahead # of the correct value. cmd_prefix = (pp.OneOrMore(assignment_word) + pp.Optional(' '))('cmd_prefix') cmd_suffix = (pp.OneOrMore(word)('args') + pp.Optional(io_redirect)) ^ io_redirect modifier = pp.oneOf('! \\') cmd_word = (pp.Combine(pp.Optional(modifier) + word) ^ word)('cmd_word').setParseAction(self.cmd_word_action) simple_command = \ (cmd_prefix + pp.Optional(cmd_word) + pp.Optional(cmd_suffix)) \ | (cmd_word + pp.Optional(cmd_suffix)) simple_command = pp.Group(simple_command) pipe_sequence = simple_command + pp.ZeroOrMore(pipe_op + simple_command) pipe_sequence = pp.Group(pipe_sequence) complete_command = pp.Optional(pipe_sequence + pp.ZeroOrMore(punctuator + pipe_sequence) + pp.Optional(punctuator)) # --- special parser for inside double quotes uq_word_in_dq = pp.Word(pp.printables.replace('`', ' ').replace('\\', ''))\ .setParseAction(self.uq_word_action) word_in_dq = pp.Combine(pp.OneOrMore(escaped ^ escaped_oct ^ escaped_hex ^ bq_word ^ uq_word_in_dq)) # --- self.parser = complete_command.parseWithTabs().ignore(pp.pythonStyleComment) self.parser_within_dq = word_in_dq.leaveWhitespace() self.next_word_type = ShParser._NEXT_WORD_CMD self.tokens = [] self.parts = []
Example #22
Source File: jsLiteralParse.py From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License | 4 votes |
def jsParse(inStr): # This disaster is a context-free grammar parser for parsing javascript object literals. # It needs to be able to handle a lot of the definitional messes you find in in-the-wild # javascript object literals. # Unfortunately, Javascript is /way/ more tolerant then JSON when it comes to object literals # so we can't just parse objects using python's `json` library. TRUE = pp.Keyword("true").setParseAction( pp.replaceWith(True) ) FALSE = pp.Keyword("false").setParseAction( pp.replaceWith(False) ) NULL = pp.Keyword("null").setParseAction( pp.replaceWith(None) ) jsonString = pp.quotedString.setParseAction( pp.removeQuotes ) jsonNumber = pp.Combine( pp.Optional('-') + ( '0' | pp.Word('123456789',pp.nums) ) + pp.Optional( '.' + pp.Word(pp.nums) ) + pp.Optional( pp.Word('eE',exact=1) + pp.Word(pp.nums+'+-',pp.nums) ) ) jsonObject = pp.Forward() jsonValue = pp.Forward() jsonDict = pp.Forward() jsonArray = pp.Forward() jsonElements = pp.Forward() rawText = pp.Regex('[a-zA-Z_$][0-9a-zA-Z_$]*') commaToNull = pp.Word(',,', exact=1).setParseAction(pp.replaceWith(None)) jsonElements << pp.ZeroOrMore(commaToNull) + pp.Optional(jsonObject) + pp.ZeroOrMore((pp.Suppress(',') + jsonObject) | commaToNull) jsonValue << ( jsonString | jsonNumber | TRUE | FALSE | NULL ) dictMembers = pp.delimitedList( pp.Group( (rawText | jsonString) + pp.Suppress(':') + (jsonValue | jsonDict | jsonArray))) jsonDict << ( pp.Dict( pp.Suppress('{') + pp.Optional(dictMembers) + pp.ZeroOrMore(pp.Suppress(',')) + pp.Suppress('}') ) ) jsonArray << ( pp.Group(pp.Suppress('[') + pp.Optional(jsonElements) + pp.Suppress(']') ) ) jsonObject << (jsonValue | jsonDict | jsonArray) jsonComment = pp.cppStyleComment jsonObject.ignore( jsonComment ) def convertDict(s, l, toks): return dict(toks.asList()) def convertNumbers(s,l,toks): n = toks[0] try: return int(n) except ValueError: return float(n) jsonNumber.setParseAction(convertNumbers) jsonDict.setParseAction(convertDict) # jsonObject.setDebug() jsonObject.parseString('"inStr"').pop() return jsonObject.parseString(inStr).pop() # Stolen from http://stackoverflow.com/a/12017573/268006
Example #23
Source File: c_parser.py From rekall with GNU General Public License v2.0 | 4 votes |
def _type_instance(self): """A type declaration. The modifiers of a typedef: struct s *P[]; ^^^^<- The type instance. """ type_instance = ( # Function pointer (*f)(int foobar) pyparsing.ZeroOrMore(_STAR) + _OPEN_PARENTHESIS + pyparsing.Optional(_STAR("function_pointer")) + self._identifier()("type_instance_name") + _CLOSE_PARENTHESIS + parsers.anything_in_parentheses()("function_args") ) | ( # Function object f(foo bar *) pyparsing.ZeroOrMore(_STAR) + self._identifier()("type_instance_name") + parsers.anything_in_parentheses()("function_args") ) | ( # Simple form: *foo[10]; pyparsing.ZeroOrMore(_STAR)("type_pointer") + self._identifier()("type_instance_name") # Possibly array: [] , [][] + pyparsing.ZeroOrMore( _OPEN_BRACKET + pyparsing.SkipTo(_CLOSE_BRACKET)( "brackets_with_expression_inside*") + _CLOSE_BRACKET) # Bitfields: int x: 7; + pyparsing.Optional( _COLON + pyparsing.SkipTo( _SEMICOLON | _COMMA)("bitfield") ) ) return pyparsing.Group( type_instance + self._maybe_attributes() )