def parse_rest_case_data(rest_case_data): data = {} listing = p.Suppress(p.Literal("(")) + p.Word( p.alphanums).setResultsName("listing") + p.Suppress( p.Literal("Listing)")) additional_info = p.And([ p.SkipTo(p.Literal(": ")), p.Suppress(p.Literal(": ")), p.SkipTo(p.White(min=2) | p.StringEnd()), ]) rest_case_data_detail = p.And([ p.SkipTo(listing).setResultsName("address"), listing, p.SkipTo(p.LineStart() + p.Word(p.nums)).setResultsName("additional_info"), p.SkipTo(p.StringEnd()).setResultsName("rest_case_data") ]) for key, value in rest_case_data_detail.parseString( rest_case_data).asDict().items(): if key == "address": data['address'] = value[0].strip() elif key == "additional_info": additional_info = p.ZeroOrMore( p.Group(additional_info)).parseString(value[0]) data.update(dict(additional_info.asList())) else: data[key] = value.strip() return data
def _compile_sequence(self, element, src_state, dst_state, grammar, kaldi_rule, fst): children = element.children if len(children) > 1: # Handle Repetition elements differently as a special case. is_rep = isinstance(element, elements_.Repetition) if is_rep and element.optimize: # Repetition... # Insert new states, so back arc only affects child s1 = fst.add_state() s2 = fst.add_state() fst.add_arc(src_state, s1, None) # NOTE: to avoid creating an un-decodable epsilon loop, we must not allow an all-epsilon child here (compile_graph_agf should check) matcher = self.compile_element(children[0], s1, s2, grammar, kaldi_rule, fst) # NOTE: has_eps_path is ~3-5x faster than matcher.parseString() inside try/except block if not fst.has_eps_path(s1, s2): fst.add_arc(s2, s1, fst.eps_disambig, fst.eps) # back arc fst.add_arc(s2, dst_state, None) return pp.OneOrMore(matcher) else: # Cannot do optimize path, because of epsilon loop, so finish up with Sequence path self._log.warning( "%s: Cannot optimize Repetition element, because its child element can match empty string; falling back inefficient non-optimize path!" % self) states = [src_state, s2] + [ fst.add_state() for i in range(len(children) - 2) ] + [dst_state] matchers = [matcher] for i, child in enumerate(children[1:], start=1): s1 = states[i] s2 = states[i + 1] matchers.append( self.compile_element(child, s1, s2, grammar, kaldi_rule, fst)) return pp.And(tuple(matchers)) else: # Sequence... # Insert new states for individual children elements states = [src_state] + [ fst.add_state() for i in range(len(children) - 1) ] + [dst_state] matchers = [] for i, child in enumerate(children): s1 = states[i] s2 = states[i + 1] matchers.append( self.compile_element(child, s1, s2, grammar, kaldi_rule, fst)) return pp.And(tuple(matchers)) elif len(children) == 1: return self.compile_element(children[0], src_state, dst_state, grammar, kaldi_rule, fst) else: # len(children) == 0 return pp.Empty()
def __get_date_common_parser_part() -> pyparsing.ParserElement: # This only checks for <name>=<value> and returns a list of 2-tuples. The # tuples are expected to be validated elsewhere. return pyparsing.OneOrMore( pyparsing.Group( pyparsing.And( [ # name # It can by any string containing any characters except # whitespace (token separator), '=' (name-value separator) # and "()" (brackets). pyparsing.Regex(r"[^=\s()]+").setName("<date part name>"), # Suppress is needed so the '=' doesn't pollute the # resulting structure produced automatically by pyparsing. pyparsing.Suppress( # no spaces allowed around the "=" pyparsing.Literal("=").leaveWhitespace() ), # value # It can by any string containing any characters except # whitespace (token separator) and "()" (brackets). pyparsing.Regex(r"[^\s()]+").setName("<date part value>"), ] ) ) )
def parse_first_case_line(first_case_line): data = {"case_order": first_case_line[0]} gender = p.Suppress(p.Literal("(")) + p.Word( p.alphas, exact=1).setResultsName("gender") + p.Suppress( p.Literal(")")) dob = p.Suppress( p.Literal("DOB:")) + date.setResultsName("dob") + p.Suppress( p.Literal("Age:")) + p.Word(p.nums).setResultsName("age") linked_case = p.Suppress(p.Literal("LINKED CASE")) provisional = p.Suppress(p.Literal("PROVISIONAL")) first_case_line_detail = p.And([ p.SkipTo(p.White(" ", min=10) ^ gender).setResultsName("name"), p.Optional(gender), p.Optional(dob), p.Optional(linked_case), p.Optional(provisional), p.Word(p.nums), ]) for key, value in first_case_line_detail.parseString( first_case_line[1]).asDict().items(): data[key] = value.strip() return data
def make_and(self, alternative): tokens = [ self.make_rule(token) for token in re.split(r'\s+', alternative) ] if len(tokens) > 1: return pp.And(tokens) else: return tokens[0]
def expr(cls): parts = [i.expr() for i in cls.comps] atom = pp.MatchFirst(parts) resp = pp.And([ Method.expr(), base.Sep, Path.expr(), pp.ZeroOrMore(base.Sep + atom) ]) resp = resp.setParseAction(cls) return resp
def expr(cls): parts = [i.expr() for i in cls.comps] atom = pp.MatchFirst(parts) resp = pp.And([ pp.MatchFirst([ WS.expr() + pp.Optional(base.Sep + StatusCode.expr()), StatusCode.expr(), ]), pp.ZeroOrMore(base.Sep + atom) ]) resp = resp.setParseAction(cls) return resp
def parser_for_string(self, string): if string == '': return pp.Empty() if hasattr(string, 'match'): return pp.Regex(string) tokens = string.split() token_parsers = [self.parser_for_token(t) for t in tokens] parser = pp.And(token_parsers) if not self.name.endswith('ʹ'): parser.setParseAction(parse_action) #print('Grammar for', string, 'is', repr(parser)) return parser
def as_pp_parser(self) -> pp.ParserElement: pos_args = [i.pp_parser for i in self.pos_args] + [rest_parser.copy()] opt_args = [i.pp_parser for i in self.opt_args] if opt_args: optionals = pp.Or(opt_args) args = intersperse_parser(pos_args, optionals) else: args = pp.And(pos_args) args.setParseAction(update_dict) cw = pp.CaselessKeyword( self.command_word)("command").setParseAction(lambda x: self.func) if self.add_help: args = (self.help_arg.pp_parser | args).setParseAction(lambda x: x[0]) return (cw + args("options")).streamline()
def __init__(self, parser, public='commands'): self.parser = parser self.public = public self.rules = {} self.commands = Commands(self) ruleName = pp.Combine( pp.Suppress('<') + pp.Word(pp.alphanums + '_.') + pp.Suppress('>')) ruleName.setParseAction(lambda toks: self[toks[0]]) expr = pp.Forward() seq = pp.delimitedList(expr, delim=pp.Empty()) seq.setParseAction(lambda toks: pp.And(toks.asList()) if len(toks.asList()) > 1 else None) self.rule = alt = pp.delimitedList(seq, delim='|') alt.setParseAction(lambda toks: pp.Or(toks.asList()) if len(toks.asList()) > 1 else None) groupExpr = pp.Suppress('(') + alt + pp.Suppress(')') groupExpr.setParseAction(lambda toks: Grouping(toks[0])) word = pp.Word(pp.alphanums + r".&'\"") word.setParseAction(lambda toks: pp.Keyword(toks[0])) token = groupExpr | ruleName | word optExpr = pp.Suppress('[') + alt + pp.Suppress(']') optExpr.setParseAction(lambda toks: pp.Optional(toks[0])) zeroOrMore = token + pp.Suppress(pp.Literal('*')) zeroOrMore.setParseAction(lambda toks: pp.ZeroOrMore(toks[0])) oneOrMore = token + pp.Suppress(pp.Literal('+')) oneOrMore.setParseAction(lambda toks: pp.OneOrMore(toks[0])) elem = zeroOrMore | oneOrMore | optExpr | token tagged = elem + pp.Combine(pp.Suppress('/') + pp.Word(pp.alphanums)).setResultsName('tag') tagged.setParseAction(self.parseExpr) expr << (tagged | elem)
def parse_heading_block(heading_block): heading_block_string = " ".join(heading_block.asList()) heading_block_detail = p.And([ p.Suppress(p.SkipTo(date)), date("date"), p.SkipTo(p.Literal("LJA:")).setResultsName("court"), p.Suppress(p.Literal("LJA:")), p.SkipTo(p.Literal("CMU:")).setResultsName("LJA"), p.Suppress(p.Literal("CMU:")), p.SkipTo(p.Literal("Session:")).setResultsName("CMU"), p.Suppress(p.Literal("Session:")), p.SkipTo(p.Literal("Panel:")).setResultsName("Session"), p.Suppress(p.Literal("Panel:")), p.SkipTo(p.Literal("Courtroom:")).setResultsName("Panel"), p.Suppress(p.Literal("Courtroom:")), p.SkipTo(p.Literal("Block:")).setResultsName("Courtroom"), p.Suppress(p.Literal("Block:")), p.SkipTo(p.StringEnd()).setResultsName("Block") ]) data = heading_block_detail.parseString(heading_block_string).asDict() return {key: value.strip() for key, value in data.items()}
def parsing_and(self): return pyparsing.And(self.parsing_items)
Do Not Edit! --> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> <TITLE>Bookmarks</TITLE>""", """<!DOCTYPE NETSCAPE-Bookmark-file-1> <!-- This is an automatically generated file. It will be read and overwritten. Do Not Edit! --> <TITLE>Bookmarks</TITLE>""", """<!DOCTYPE NETSCAPE-Bookmark-file-1> <html><head><!-- This is an automatically generated file. It will be read and overwritten. DO NOT EDIT! --><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Bookmarks</title></head> """ ] #headers = [ pp.And( pp.Literal(line.strip()) for line in head.splitlines()) for head in headers] # requires furthers changes to serialisation headers2 = [ pp.Combine(pp.And([ pp.Literal(line.strip()) + pp.ZeroOrMore(pp.White()) for line in head.splitlines() ]), adjacent=False) for head in headers ] #header1 startH1 = pp.Literal("<H1") endH1 = pp.Literal("/H1>") H1 = pp.Combine(startH1 + pp.SkipTo(endH1) + endH1) #bookmark tokens startBM = pp.Literal("<DT><A HREF=") | pp.Literal("<DT><A FEEDURL=") endBM = pp.Literal("</A>") + pp.Optional( pp.ZeroOrMore(pp.White()) + pp.Literal("<DD>") + pp.SkipTo(pp.ZeroOrMore(pp.White()) + "<") ) #^ (pp.Literal("</A>")+pp.Literal("<DD>")+pp.SkipTo(pp.LineEnd()))
def _make_matcher_element(self): # Return an And element using each child's matcher element. return pyparsing.And([child.matcher_element for child in self.children])\ .setParseAction(self._parse_action)
query = pyparsing.And([ pyparsing.Group( pyparsing.Optional( pyparsing.Or([ alphanumerals, pyparsing.OneOrMore(pyparsing.Word(pyparsing.nums)) ]))), pyparsing.Group( pyparsing.Optional( pyparsing.Or( [pyparsing.Literal('chord'), pyparsing.Literal('melody')]))), pyparsing.Group( pyparsing.ZeroOrMore( pyparsing.And( [pyparsing.Group(pyparsing.Optional(augmentedtimes)), times]))), pyparsing.Group( pyparsing.ZeroOrMore( pyparsing.And([ notes, pyparsing.Group(pyparsing.Optional(augmentednotes)), pyparsing.Group(pyparsing.Optional(octave)) ]))), pyparsing.Group( pyparsing.Optional( pyparsing.Or([ pyparsing.Literal('rest'), pyparsing.Literal('notes'), pyparsing.Literal('note'), pyparsing.Literal('melody') ]))), pyparsing.Group( pyparsing.Optional( pyparsing.And([ alphanumerals, pyparsing.Or( [pyparsing.Literal('note'), pyparsing.Literal('notes')]), pyparsing.Literal('melody') ]))), pyparsing.Group( pyparsing.Optional( pyparsing.And([ pyparsing.Literal("on the word ""), pyparsing.ZeroOrMore(pyparsing.Word(pyparsing.alphas)), pyparsing.Literal("!"") ]))), pyparsing.Group( pyparsing.Optional( pyparsing.And([passage, pyparsing.Literal('passage')]))), pyparsing.Group( pyparsing.Optional( pyparsing.And([ pyparsing.Or([ pyparsing.Literal('in bars'), pyparsing.Literal('in measures') ]), pyparsing.OneOrMore(pyparsing.Word(pyparsing.nums)), pyparsing.Or([pyparsing.Literal('-'), pyparsing.Literal('to')]), pyparsing.OneOrMore(pyparsing.Word(pyparsing.nums)) ]))), pyparsing.Group( pyparsing.Optional( pyparsing.And([ pyparsing.Literal('in'), pyparsing.OneOrMore(pyparsing.Word(pyparsing.nums)), pyparsing.Literal('/'), pyparsing.OneOrMore(pyparsing.Word(pyparsing.nums)), pyparsing.Literal('time') ]), )), pyparsing.Group( pyparsing.Optional( pyparsing.And( [pyparsing.Literal('in the'), clef, pyparsing.Literal('clef')]))), pyparsing.Group( pyparsing.Optional( pyparsing.And([pyparsing.Literal('in the'), instruments]))) ])
pyparsing.Literal("="), pyparsing.Literal(">"), pyparsing.Literal("<"), pyparsing.Literal("!="), pyparsing.Literal(">="), pyparsing.Literal("<=") ]) identifiers = pyparsing.Word(pyparsing.alphanums) expressions = pyparsing.Group(identifiers) equations = pyparsing.Group( pyparsing.And([ identifiers, operators, identifiers ]) ) expressionconjunctions = pyparsing.Literal(",") compoundexpressions = pyparsing.Group( pyparsing.And([ expressions, pyparsing.Optional( pyparsing.OneOrMore( pyparsing.And([ expressionconjunctions, expressions ])
if a in "fst": mode += 1 if a == "f": arguments.append(Flag.parser) else: arguments.append(Register.parser) elif a == "o": mode += 2 arguments.append(immediate_offset_parser) elif a == "b": mode += 2 arguments.append(immediate_parser) elif a == "w": arguments.append(immediate_word_parser) if len(arguments) != 0: instructions.append((pyparsing.CaselessKeyword(name) + pyparsing.Group(pyparsing.And(arguments))).setParseAction((lambda c, m, n: (lambda s,l,t: [c(m, n, t[1])]))(cls, mode, num))) else: instructions.append(pyparsing.CaselessKeyword(name).setParseAction((lambda c, m, n: (lambda s,l,t: [c(m, n, [])]))(cls, mode, num))) parser = pyparsing.ZeroOrMore(LabelAbs.parser + pyparsing.Suppress(pyparsing.Literal(":"))) + pyparsing.Optional(pyparsing.Or(instructions)) #parse input file labels = {} instructions = [] offset = 0 with open(sys.argv[1]) as f: line_num=0 for l in f.read().split("\n"): line_num += 1 if len(l) == 0 or l[0] == "#": continue try:
def countedPlanes(toks): n_planes = toks[0] iplane_to_cm << pp.And([pp.Literal(',').suppress(), positiveInteger] * n_planes)('iplane_to_cm')
def countedRegions(toks): n = toks[0] region_definitions << pp.And([region] * n)
def _make_matcher_element(self): # Wrap the parser element for the referenced rule's root expansion so that # the current match value for the NamedRuleRef is also set. return pyparsing.And([self.referenced_rule.expansion.matcher_element ]).setParseAction(self._parse_action)
#!/usr/bin/env python import pyparsing as pp import json import psyco pp.ParserElement.enablePackrat() psyco.full() pL = pp.Literal pK = pp.Keyword pS = lambda *args: pp.And(args) pP = lambda str: pp.And([pL(x) for x in str.split()]) pO = pp.Optional pG = pp.Group pI = pp.Suppress pOoM = pp.OneOrMore class Commands(set): def __init__(self, rules): self.rules = rules def __setitem__(self, name, value): self.rules[name] = value self.add(name) @property def parser(self): return pp.Or([self.rules[name] for name in self]) class Grouping(pp.TokenConverter):
def __get_rule_parser() -> pyparsing.ParserElement: # This function defines the rule grammar # How to add new rule expressions: # 1 Create new grammar rules in a way similar to existing rsc_expr and # op_expr. Use setName for better description of a grammar when printed. # Use setResultsName for an easy access to parsed parts. # 2 Create new classes in expression_part module, probably one for each # type of expression. Those are data containers holding the parsed data # independent of the parser. # 3 Create builders for the new classes and connect them to created # grammar rules using setParseAction. # 4 Add the new expressions into simple_expr definition. # 5 Test and debug the whole thing. node_attr_unary_expr = pyparsing.And([ # operator pyparsing.Or([ pyparsing.CaselessKeyword(op) for op in _token_to_node_expr_unary_op ]).setResultsName("operator"), # attribute name # It can by any string containing any characters except whitespace # (token separator) and "()" (brackets). pyparsing.Regex(r"[^\s()]+").setName("<attribute name>" ).setResultsName("attr_name"), ]) node_attr_unary_expr.setParseAction(__build_node_attr_unary_expr) node_attr_binary_expr = pyparsing.And([ # attribute name # It can by any string containing any characters except whitespace # (token separator) and "()" (brackets). pyparsing.Regex(r"[^\s()]+").setName("<attribute name>" ).setResultsName("attr_name"), # operator pyparsing.Or([ pyparsing.CaselessKeyword(op) for op in _token_to_node_expr_binary_op ]).setResultsName("operator"), # attribute type pyparsing.Optional( pyparsing.Or([ pyparsing.CaselessKeyword(type_) for type_ in _token_to_node_expr_type ])).setName("<attribute type>").setResultsName("attr_type"), # attribute value # It can by any string containing any characters except whitespace # (token separator) and "()" (brackets). pyparsing.Regex(r"[^\s()]+").setName("<attribute value>" ).setResultsName("attr_value"), ]) node_attr_binary_expr.setParseAction(__build_node_attr_binary_expr) date_unary_expr = pyparsing.And([ pyparsing.CaselessKeyword("date"), # operator pyparsing.Or([ pyparsing.CaselessKeyword(op) for op in _token_to_date_expr_unary_op ]).setResultsName("operator"), # date # It can by any string containing any characters except whitespace # (token separator) and "()" (brackets). # The actual value should be validated elsewhere. pyparsing.Regex(r"[^\s()]+").setName("<date>").setResultsName("date"), ]) date_unary_expr.setParseAction(__build_date_unary_expr) date_inrange_expr = pyparsing.And([ pyparsing.CaselessKeyword("date"), pyparsing.CaselessKeyword("in_range"), # date # It can by any string containing any characters except whitespace # (token separator) and "()" (brackets). # The actual value should be validated elsewhere. # The Regex matches 'to'. In order to prevent that, FollowedBy is # used. pyparsing.Optional( pyparsing.And([ pyparsing.Regex(r"[^\s()]+").setName( "[<date>]").setResultsName("date1"), pyparsing.FollowedBy(pyparsing.CaselessKeyword("to")), ])), pyparsing.CaselessKeyword("to"), pyparsing.Or([ # date # It can by any string containing any characters except # whitespace (token separator) and "()" (brackets). # The actual value should be validated elsewhere. pyparsing.Regex(r"[^\s()]+").setName("<date>").setResultsName( "date2"), # duration pyparsing.And([ pyparsing.CaselessKeyword("duration"), __get_date_common_parser_part().setResultsName("duration"), ]), ]), ]) date_inrange_expr.setParseAction(__build_date_inrange_expr) datespec_expr = pyparsing.And([ pyparsing.CaselessKeyword("date-spec"), __get_date_common_parser_part().setResultsName("datespec"), ]) datespec_expr.setParseAction(__build_datespec_expr) rsc_expr = pyparsing.And([ pyparsing.CaselessKeyword("resource"), # resource name # Up to three parts seperated by ":". The parts can contain any # characters except whitespace (token separator), ":" (parts # separator) and "()" (brackets). pyparsing.Regex( r"(?P<standard>[^\s:()]+)?:(?P<provider>[^\s:()]+)?:(?P<type>[^\s:()]+)?" ).setName("<resource name>"), ]) rsc_expr.setParseAction(__build_rsc_expr) op_interval = pyparsing.And([ pyparsing.CaselessKeyword("interval"), # no spaces allowed around the "=" pyparsing.Literal("=").leaveWhitespace(), # interval value: number followed by a time unit, no spaces allowed # between the number and the unit thanks to Combine being used pyparsing.Combine( pyparsing.And([ pyparsing.Word(pyparsing.nums), pyparsing.Optional(pyparsing.Word(pyparsing.alphas)), ]) ).setName("<integer>[<time unit>]").setResultsName("interval_value"), ]) op_expr = pyparsing.And([ pyparsing.CaselessKeyword("op"), # operation name # It can by any string containing any characters except whitespace # (token separator) and "()" (brackets). Operations are defined in # agents' metadata which we do not have access to (e.g. when the # user sets operation "my_check" and doesn't even specify agent's # name). pyparsing.Regex(r"[^\s()]+").setName("<operation name>" ).setResultsName("name"), pyparsing.Optional(op_interval).setResultsName("interval"), ]) op_expr.setParseAction(__build_op_expr) # Ordering matters here as the first expression which matches wins. This is # mostly not an issue as the expressions don't overlap and the grammar is # not ambiguous. There are, exceptions, however: # 1) date gt something # This can be either a date_unary_expr or a node_attr_binary_expr. We # want it to be a date expression. If the user wants it to be a node # attribute expression, they can do it like this: 'date gt <type> # something' where <type> is an item of _token_to_node_expr_type. That # way, both date and node attribute expression can be realized. simple_expr = pyparsing.Or([ date_unary_expr, date_inrange_expr, datespec_expr, node_attr_unary_expr, node_attr_binary_expr, rsc_expr, op_expr, ]) # See pyparsing examples # https://github.com/pyparsing/pyparsing/blob/master/examples/simpleBool.py # https://github.com/pyparsing/pyparsing/blob/master/examples/eval_arith.py bool_operator = pyparsing.Or( [pyparsing.CaselessKeyword("and"), pyparsing.CaselessKeyword("or")]) bool_expr = pyparsing.infixNotation( simple_expr, # By putting both "and" and "or" in one tuple we say they have the same # priority. This is consistent with legacy pcs parsers. And it is how # it should be, they work as a glue between "simple_expr"s. [(bool_operator, 2, pyparsing.opAssoc.LEFT, __build_bool_tree)], ) return pyparsing.Or([bool_expr, simple_expr])
class TestWhitespaceMethods(PyparsingExpressionTestCase): tests = [ # These test the single-element versions PpTestSpec( desc="The word foo", expr=pp.Literal("foo").ignore_whitespace(), text=" foo ", expected_list=["foo"], ), PpTestSpec( desc="The word foo", expr=pp.Literal("foo").leave_whitespace(), text=" foo ", expected_fail_locn=0, ), PpTestSpec( desc="The word foo", expr=pp.Literal("foo").ignore_whitespace(), text="foo", expected_list=["foo"], ), PpTestSpec( desc="The word foo", expr=pp.Literal("foo").leave_whitespace(), text="foo", expected_list=["foo"], ), # These test the composite elements PpTestSpec( desc= "If we recursively leave whitespace on the parent, this whitespace-dependent grammar will succeed, even if the children themselves skip whitespace", expr=pp.And([ pp.Literal(" foo").ignore_whitespace(), pp.Literal(" bar").ignore_whitespace(), ]).leave_whitespace(recursive=True), text=" foo bar", expected_list=[" foo", " bar"], ), # PpTestSpec( desc= "If we recursively ignore whitespace in our parsing, this whitespace-dependent grammar will fail, even if the children themselves keep whitespace", expr=pp.And([ pp.Literal(" foo").leave_whitespace(), pp.Literal(" bar").leave_whitespace(), ]).ignore_whitespace(recursive=True), text=" foo bar", expected_fail_locn=1, ), PpTestSpec( desc= "If we leave whitespace on the parent, but it isn't recursive, this whitespace-dependent grammar will fail", expr=pp.And([ pp.Literal(" foo").ignore_whitespace(), pp.Literal(" bar").ignore_whitespace(), ]).leave_whitespace(recursive=False), text=" foo bar", expected_fail_locn=5, ), # These test the Enhance classes PpTestSpec( desc= "If we recursively leave whitespace on the parent, this whitespace-dependent grammar will succeed, even if the children themselves skip whitespace", expr=pp.Optional( pp.Literal(" foo").ignore_whitespace()).leave_whitespace( recursive=True), text=" foo", expected_list=[" foo"], ), # PpTestSpec( desc= "If we ignore whitespace on the parent, but it isn't recursive, parsing will fail because we skip to the first character 'f' before the internal expr can see it", expr=pp.Optional( pp.Literal(" foo").leave_whitespace()).ignore_whitespace( recursive=True), text=" foo", expected_list=[], ), # PpTestSpec( # desc="If we leave whitespace on the parent, this whitespace-dependent grammar will succeed, even if the children themselves skip whitespace", # expr=pp.Optional(pp.Literal(" foo").ignoreWhitespace()).leaveWhitespace( # recursive=False # ), # text=" foo", # expected_list=[] # ), ]
def countedPoints(toks): n = toks[0] points << pp.And([point] * n)
import pyparsing identifiers = pyparsing.Word(pyparsing.alphanums) integers = pyparsing.Combine( pyparsing.And([ pyparsing.Optional( pyparsing.Or([pyparsing.Literal('+'), pyparsing.Literal('-')])), pyparsing.Word(pyparsing.nums) ])) datatypes = pyparsing.Combine( pyparsing.And([ pyparsing.Or([ pyparsing.Literal('char'), pyparsing.Literal('int'), pyparsing.Literal('long'), pyparsing.Literal('double') ]), pyparsing.ZeroOrMore(pyparsing.Literal('*')) ])) domains = pyparsing.Group( pyparsing.And([ datatypes, pyparsing.Optional( pyparsing.And([ pyparsing.Literal('[').suppress(), pyparsing.Or([integers, identifiers]), pyparsing.ZeroOrMore(
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2/24/17 10:46 AM # @Author : Jackling import pyparsing as pp # simple example print '--------------' letterDigit = pp.And([pp.Word(pp.alphas, exact=1), pp.Word(pp.nums, exact=1)]) print letterDigit.parseString('x5') digitsLetters = pp.Word(pp.nums) + pp.Word(pp.alphas) print digitsLetters.parseString('23skiddoo') # caselessliteral print '--------------' ni = pp.CaselessLiteral('Ni') print ni.parseString('Ni') print ni.parseString('NI') print ni.parseString('nI') print ni.parseString('ni') # OneOrMore print '--------------' name = pp.Word(pp.alphas) number = pp.Word(pp.nums) nameNoOrBang = pp.Or([name, number, pp.Literal('!')]) print nameNoOrBang.parseString('Brian') print nameNoOrBang.parseString('73') print nameNoOrBang.parseString('!') several = pp.OneOrMore(nameNoOrBang)
def countedLayers(toks): n = toks[0] layer_definitions << pp.And([layer] * n)