我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用pyparsing.nestedExpr()。
def expression(self): expression = pyparsing.Forward() # (1 + (2 + 3)) nested_expression = pyparsing.nestedExpr( "(", ")", expression).setParseAction(self._combine_lists) # FOO(2 , 3) function_call = ( _TOKEN().setResultsName("function") + _OPEN_PARENTHESIS() + pyparsing.delimitedList( pyparsing.Combine(expression, adjacent=False, joinString=" "), delim=",").setResultsName("func_args") + _CLOSE_PARENTHESIS() ) expression << pyparsing.OneOrMore( function_call.setParseAction(self._is_known_function) | pyparsing.Group(nested_expression) | _TOKEN() | _NOT_TOKEN() ) return pyparsing.Combine(expression, adjacent=False, joinString=" ")
def jena_graph(java_file, args): ''' Starts Main.java in the same folder and converts it into a query tree, then into a nested list ''' graph = '' #Makes the call to start Main.java - gets output of file via System.out.println(string) cmd = ["java", "-cp", os.environ.get('CLASSPATH'), java_file, args] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() for line in stdout: graph += line try: res_graph = pp.nestedExpr(opener='(', closer=')').parseString(graph) res_graph = res_graph.asList() except: print "pyparse err", sys.exc_info()[0], graph res_graph = -1 return res_graph
def jena_graph(java_file, args): ''' Starts Main.java in the same folder and converts it into a query tree, then into a nested list ''' graph = '' #Makes the call to start Main.java - gets output of file via System.out.println(string) cmd = ["java", "-cp", "/Users/david/libs/jena/lib/*:.", java_file, args] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() for line in stdout: graph += line try: res_graph = pp.nestedExpr(opener='(', closer=')').parseString(graph) res_graph = res_graph.asList() except: print "pyparse err", graph, args res_graph = -1 return res_graph
def __init__(self, rule): self.rule = rule self.opener = self.rule['opener'] self.closer = self.rule['closer'] self.columns = self.rule.get('columns', -1) nested = nestedExpr(opener=self.opener, closer=self.closer, content=CharsNotIn(self.opener + self.closer)) if self.columns < 0: self.nested = OneOrMore(nested) else: self.nested = nested * self.columns + Or([CharsNotIn('\n'), Empty()])
def __init__(self): nest = pypar.nestedExpr g = pypar.Forward() nestedParens = nest('(', ')') nestedBrackets = nest('[', ']') nestedCurlies = nest('{', '}') nest_grammar = nestedParens | nestedBrackets | nestedCurlies parens = "(){}[]" letters = ''.join([x for x in pypar.printables if x not in parens]) word = pypar.Word(letters) g = pypar.OneOrMore(word | nest_grammar) self.grammar = g
def __init__(self): self.enclosed = pyp.Forward() nestedAngles = pyp.nestedExpr('<', '>', content=self.enclosed) self.enclosed << (pyp.Word('_' + pyp.alphanums) | '{' | '}' | '(' | ')' | '*' | '&' | ',' | pyp.Word("::") | nestedAngles)
def OperationsSchema(v): if isinstance(v, six.text_type): try: v = pyparsing.OneOrMore( pyparsing.nestedExpr()).parseString(v).asList()[0] except pyparsing.ParseException as e: api.abort(400, {"cause": "Invalid operations", "reason": "Fail to parse the operations string", "detail": six.text_type(e)}) return voluptuous.Schema(voluptuous.Any(*OperationsSchemaBase), required=True)(v)
def parse_parentheses(line): try: return nestedExpr('(', ')').parseString(line).asList() except ParseException: print "Exception on line:", line
def parse_list_of_lists(xs): """ Parse a list of list of integers using pyparsing """ enclosed = pa.Forward() # Parser to be defined later natural = pa.Word(pa.nums) # Natural Number # Nested Grammar nestedBrackets = pa.nestedExpr(pa.Suppress('['), pa.Suppress(']'), content=enclosed) enclosed << (natural | pa.Suppress(',') | nestedBrackets) try: rs = enclosed.parseString(xs).asList()[0] return list(map(lambda x: list(map(int, x)), rs)) except pa.ParseException: raise RuntimeError("Invalid Macro states Specification")