Python pyparsing 模块,nestedExpr() 实例源码
我们从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, 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, "/Users/david/libs/jena/lib/*:.", 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):
nest = pypar.nestedExpr
g = pypar.Forward()
nestedParens = 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 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")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。