Python pyparsing 模块,nestedExpr() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用pyparsing.nestedExpr()

项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
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=" ")
项目:tf-sparql    作者:derdav3    | 项目源码 | 文件源码
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
项目:tf-sparql    作者:derdav3    | 项目源码 | 文件源码
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
项目:python-agent    作者:sandabuliu    | 项目源码 | 文件源码
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()])
项目:NLPre    作者:NIHOPA    | 项目源码 | 文件源码
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
项目:NLPre    作者:NIHOPA    | 项目源码 | 文件源码
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
项目:gdb-pretty-frame-cpp    作者:philtweir    | 项目源码 | 文件源码
def __init__(self):
        self.enclosed = pyp.Forward()
        nestedAngles = pyp.nestedExpr('<', '>', content=self.enclosed)
        self.enclosed << (pyp.Word('_' + pyp.alphanums) | '{' | '}' |
                          '(' | ')' | '*' | '&' | ',' | pyp.Word("::") |
                          nestedAngles)
项目:gnocchi    作者:gnocchixyz    | 项目源码 | 文件源码
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)
项目:idasec    作者:RobinDavid    | 项目源码 | 文件源码
def parse_parentheses(line):
    try:
        return nestedExpr('(', ')').parseString(line).asList()
    except ParseException:
        print "Exception on line:", line
项目:qmflows-namd    作者:SCM-NV    | 项目源码 | 文件源码
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")