Example #1
0
class TestCommonHelperExpressions(PyparsingExpressionTestCase):
    tests = [
        PpTestSpec(
            desc = "A comma-delimited list of words",
            expr = pp.delimitedList(pp.Word(pp.alphas)),
            text = "this, that, blah,foo,   bar",
            expected_list = ['this', 'that', 'blah', 'foo', 'bar'],
        ),
        PpTestSpec(
            desc = "A counted array of words",
            expr = pp.OneOrMore(pp.countedArray(pp.Word('ab'))),
            text = "2 aaa bbb 0 3 abab bbaa abbab",
            expected_list = [['aaa', 'bbb'], [], ['abab', 'bbaa', 'abbab']],
        ),
        PpTestSpec(
            desc = "skipping comments with ignore",
            expr = (pp.pyparsing_common.identifier('lhs')
                    + '='
                    + pp.pyparsing_common.fnumber('rhs')).ignore(pp.cppStyleComment),
            text = "abc_100 = /* value to be tested */ 3.1416",
            expected_list = ['abc_100', '=', 3.1416],
            expected_dict = {'lhs': 'abc_100', 'rhs': 3.1416},
        ),
        PpTestSpec(
            desc = "some pre-defined expressions in pyparsing_common, and building a dotted identifier with delimted_list",
            expr = (pp.pyparsing_common.number("id_num")
                    + pp.delimitedList(pp.pyparsing_common.identifier, '.', combine=True)("name")
                    + pp.pyparsing_common.ipv4_address("ip_address")
                    ),
            text = "1001 www.google.com 192.168.10.199",
            expected_list = [1001, 'www.google.com', '192.168.10.199'],
            expected_dict = {'id_num': 1001, 'name': 'www.google.com', 'ip_address': '192.168.10.199'},
        ),
        PpTestSpec(
            desc = "using oneOf (shortcut for Literal('a') | Literal('b') | Literal('c'))",
            expr = pp.OneOrMore(pp.oneOf("a b c")),
            text = "a b a b b a c c a b b",
            expected_list = ['a', 'b', 'a', 'b', 'b', 'a', 'c', 'c', 'a', 'b', 'b'],
        ),
        PpTestSpec(
            desc = "parsing nested parentheses",
            expr = pp.nestedExpr(),
            text = "(a b (c) d (e f g ()))",
            expected_list = [['a', 'b', ['c'], 'd', ['e', 'f', 'g', []]]],
        ),
        PpTestSpec(
            desc = "parsing nested braces",
            expr = (pp.Keyword('if')
                    + pp.nestedExpr()('condition')
                    + pp.nestedExpr('{', '}')('body')),
            text = 'if ((x == y) || !z) {printf("{}");}',
            expected_list = ['if', [['x', '==', 'y'], '||', '!z'], ['printf(', '"{}"', ');']],
            expected_dict = {'condition': [[['x', '==', 'y'], '||', '!z']],
                             'body': [['printf(', '"{}"', ');']]},
        ),
    ]
Example #2
0
    def _parse_ascii_data(self, header_tokens, file_mmap, buffer_offset,
                          aggregate_data_types):
        """
        Parse the data portion of a PLY file assuming it uses ASCII format.

        :param header_tokens:
        :param file_mmap:
        :param buffer_offset:
        :param aggregate_data_types:
        :return:
        """
        # Define the grammar of the body
        number = pyparsing_common.number()
        body_expr = list()
        for element in header_tokens.elements:
            sequences = list()
            for prop in element.properties:
                for variable in prop:
                    if "index_type" in variable:
                        sequences.append(countedArray(number))
                    else:
                        sequences.append(number(variable.name))

            element_data = Group(And(sequences))
            body_expr.append(Group(element_data * element.count)(element.name))

        ascii_grammar = And(body_expr)

        # Tokenize the body data
        body_tokens = ascii_grammar.parseString(
            file_mmap[buffer_offset:].decode("ascii"), parseAll=True)

        # Convert the data to arrays.
        element_data = dict()
        for name, dtype in aggregate_data_types.items():
            element_data[name] = array.array(dtype,
                                             self._flatten(body_tokens[name]))

        return element_data
Example #3
0
class TestCommonHelperExpressions(PyparsingExpressionTestCase):
    tests = [
        PpTestSpec(
            desc="A comma-delimited list of words",
            expr=pp.delimitedList(pp.Word(pp.alphas)),
            text="this, that, blah,foo,   bar",
            expected_list=["this", "that", "blah", "foo", "bar"],
        ),
        PpTestSpec(
            desc="A counted array of words",
            expr=pp.Group(pp.countedArray(pp.Word("ab")))[...],
            text="2 aaa bbb 0 3 abab bbaa abbab",
            expected_list=[["aaa", "bbb"], [], ["abab", "bbaa", "abbab"]],
        ),
        PpTestSpec(
            desc="skipping comments with ignore",
            expr=(pp.pyparsing_common.identifier("lhs") + "=" +
                  pp.pyparsing_common.fnumber("rhs")).ignore(
                      pp.cppStyleComment),
            text="abc_100 = /* value to be tested */ 3.1416",
            expected_list=["abc_100", "=", 3.1416],
            expected_dict={
                "lhs": "abc_100",
                "rhs": 3.1416
            },
        ),
        PpTestSpec(
            desc=
            "some pre-defined expressions in pyparsing_common, and building a dotted identifier with delimted_list",
            expr=(pp.pyparsing_common.number("id_num") + pp.delimitedList(
                pp.pyparsing_common.identifier, ".", combine=True)("name") +
                  pp.pyparsing_common.ipv4_address("ip_address")),
            text="1001 www.google.com 192.168.10.199",
            expected_list=[1001, "www.google.com", "192.168.10.199"],
            expected_dict={
                "id_num": 1001,
                "name": "www.google.com",
                "ip_address": "192.168.10.199",
            },
        ),
        PpTestSpec(
            desc=
            "using oneOf (shortcut for Literal('a') | Literal('b') | Literal('c'))",
            expr=pp.oneOf("a b c")[...],
            text="a b a b b a c c a b b",
            expected_list=[
                "a", "b", "a", "b", "b", "a", "c", "c", "a", "b", "b"
            ],
        ),
        PpTestSpec(
            desc="parsing nested parentheses",
            expr=pp.nestedExpr(),
            text="(a b (c) d (e f g ()))",
            expected_list=[["a", "b", ["c"], "d", ["e", "f", "g", []]]],
        ),
        PpTestSpec(
            desc="parsing nested braces",
            expr=(pp.Keyword("if") + pp.nestedExpr()("condition") +
                  pp.nestedExpr("{", "}")("body")),
            text='if ((x == y) || !z) {printf("{}");}',
            expected_list=[
                "if",
                [["x", "==", "y"], "||", "!z"],
                ["printf(", '"{}"', ");"],
            ],
            expected_dict={
                "condition": [[["x", "==", "y"], "||", "!z"]],
                "body": [["printf(", '"{}"', ");"]],
            },
        ),
    ]
Example #4
0
    def __init__(self):
        #pp.ParserElement.setDefaultWhitespaceChars(' \t\r')
        # this messes with geda_pcb_parsing and unittest

        self.gschem_int = pp.Combine(pp.Optional(pp.Literal('-')) +
                                     pp.Word(pp.nums))
        self.gschem_str = pp.Word(pp.printables)
        self.gschem_linestr = pp.Regex('.*') + pp.lineEnd.suppress()
        
        self.gschem_version = (
            pp.lineStart.suppress() + 
            pp.Literal('v').suppress() + 
            self.gschem_int.setResultsName('version') + 
            self.gschem_int.setResultsName('fileversion') + 
            pp.lineEnd.suppress()
        )
        self.gschem_version.setParseAction(
            lambda toks: GschemVersion(toks.asDict()))


        def mk_int(toks):
            return int(toks[0])

        self.int_nl = pp.Word(pp.nums) + pp.lineEnd.suppress()
        self.int_nl.setParseAction(mk_int)

        self.gschem_text = (
            pp.lineStart.suppress() + 
            pp.Literal('T').suppress() + 
            self.gschem_int.setResultsName('x') + 
            self.gschem_int.setResultsName('y') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('size') + 
            self.gschem_int.setResultsName('visibility') + 
            self.gschem_int.setResultsName('show_name_value') + 
            self.gschem_int.setResultsName('angle') + 
            self.gschem_int.setResultsName('alignment') + 
            pp.countedArray(self.gschem_linestr,
                            intExpr=self.int_nl).setResultsName('children')
        )
        self.gschem_text.setParseAction(lambda toks: GschemText(toks.asDict()))
        #int_nl.setDebug(True)
        #gschem_text.setDebug(True)
        
        self.gschem_attr = (
            pp.lineStart.suppress() + 
            pp.Literal('T').suppress() + 
            self.gschem_int.setResultsName('x') + 
            self.gschem_int.setResultsName('y') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('size') + 
            self.gschem_int.setResultsName('visibility') + 
            self.gschem_int.setResultsName('show_name_value') + 
            self.gschem_int.setResultsName('angle') + 
            self.gschem_int.setResultsName('alignment') + 
            pp.countedArray(self.gschem_linestr,
                            intExpr=self.int_nl).setResultsName('children')
        )
        self.gschem_attr.setParseAction(lambda toks: GschemText(toks.asDict()))
        
        self.gschem_attr_list = pp.Optional(
            pp.Literal('{\n').suppress() + 
            pp.OneOrMore(self.gschem_attr).setResultsName('attrib') +
            pp.Literal('}\n').suppress()
        )

        self.gschem_line = (
            pp.lineStart.suppress() + 
            pp.Literal('L').suppress() + 
            self.gschem_int.setResultsName('x1') + 
            self.gschem_int.setResultsName('y1') + 
            self.gschem_int.setResultsName('x2') + 
            self.gschem_int.setResultsName('y2') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('width') + 
            self.gschem_int.setResultsName('capstyle') + 
            self.gschem_int.setResultsName('dashstyle') + 
            self.gschem_int.setResultsName('dashlength') + 
            self.gschem_int.setResultsName('dashspace') + 
            pp.lineEnd.suppress()
        )
        self.gschem_line.setParseAction(lambda toks: GschemLine(toks.asDict()))

        self.gschem_box = (
            pp.lineStart.suppress() + 
            pp.Literal('B').suppress() + 
            self.gschem_int.setResultsName('x') + 
            self.gschem_int.setResultsName('y') + 
            self.gschem_int.setResultsName('width') + 
            self.gschem_int.setResultsName('height') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('width') + 
            self.gschem_int.setResultsName('capstyle') + 
            self.gschem_int.setResultsName('dashstyle') + 
            self.gschem_int.setResultsName('dashlength') + 
            self.gschem_int.setResultsName('dashspace') + 
            self.gschem_int.setResultsName('filltype') + 
            self.gschem_int.setResultsName('fillwidth') + 
            self.gschem_int.setResultsName('angle1') + 
            self.gschem_int.setResultsName('pitch1') + 
            self.gschem_int.setResultsName('angle2') + 
            self.gschem_int.setResultsName('pitch2') + 
            pp.lineEnd.suppress()
        )
        self.gschem_box.setParseAction(lambda toks: GschemBox(toks.asDict()))

        self.gschem_circle = (
            pp.lineStart.suppress() + 
            pp.Literal('V').suppress() + 
            self.gschem_int.setResultsName('x') + 
            self.gschem_int.setResultsName('y') +
            self.gschem_int.setResultsName('radius') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('width') + 
            self.gschem_int.setResultsName('capstyle') + 
            self.gschem_int.setResultsName('dashstyle') + 
            self.gschem_int.setResultsName('dashlength') + 
            self.gschem_int.setResultsName('dashspace') + 
            self.gschem_int.setResultsName('filltype') + 
            self.gschem_int.setResultsName('fillwidth') + 
            self.gschem_int.setResultsName('angle1') + 
            self.gschem_int.setResultsName('pitch1') + 
            self.gschem_int.setResultsName('angle2') + 
            self.gschem_int.setResultsName('pitch2') + 
            pp.lineEnd.suppress()
        )
        self.gschem_circle.setParseAction(
            lambda toks: GschemCircle(toks.asDict()))

        self.gschem_arc = (
            pp.lineStart.suppress() + 
            pp.Literal('A').suppress() + 
            self.gschem_int.setResultsName('x') + 
            self.gschem_int.setResultsName('y') + 
            self.gschem_int.setResultsName('radius') + 
            self.gschem_int.setResultsName('startangle') + 
            self.gschem_int.setResultsName('sweepangle') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('width') + 
            self.gschem_int.setResultsName('capstyle') + 
            self.gschem_int.setResultsName('dashstyle') + 
            self.gschem_int.setResultsName('dashlength') + 
            self.gschem_int.setResultsName('dashspace') + 
            pp.lineEnd.suppress()
        )
        self.gschem_arc.setParseAction(lambda toks: GschemArc(toks.asDict()))


        self.gschem_component = (
            pp.lineStart.suppress() + 
            pp.Literal('C').suppress() + 
            self.gschem_int.setResultsName('x') + 
            self.gschem_int.setResultsName('y') + 
            self.gschem_int.setResultsName('selectable') + 
            self.gschem_int.setResultsName('angle') + 
            self.gschem_int.setResultsName('mirror') + 
            pp.Regex('\S+.*').setResultsName('basename') + 
            pp.lineEnd.suppress() + 
            self.gschem_attr_list
        )
        self.gschem_component.setParseAction(
            lambda toks: GschemComponent(toks.asDict()))

        self.gschem_net = (
            pp.lineStart.suppress() + 
            pp.Literal('N').suppress() + 
            self.gschem_int.setResultsName('x1') + 
            self.gschem_int.setResultsName('y1') + 
            self.gschem_int.setResultsName('x2') + 
            self.gschem_int.setResultsName('y2') + 
            self.gschem_int.setResultsName('color') + 
            pp.lineEnd.suppress() + 
            self.gschem_attr_list
        )
        self.gschem_net.setParseAction(lambda toks: GschemNet(toks.asDict()))

        self.gschem_pin = (
            pp.lineStart.suppress() + 
            pp.Literal('P').suppress() + 
            self.gschem_int.setResultsName('x1') + 
            self.gschem_int.setResultsName('y1') + 
            self.gschem_int.setResultsName('x2') + 
            self.gschem_int.setResultsName('y2') + 
            self.gschem_int.setResultsName('color') + 
            self.gschem_int.setResultsName('pintype') +
            self.gschem_int.setResultsName('whichend') + 
            pp.lineEnd.suppress() + 
            self.gschem_attr_list
        )
        self.gschem_pin.setParseAction(lambda toks: GschemPin(toks.asDict()))

        def f(toks):
            #print 'DEBUG', toks.asDict()
            #return GschemNet(toks.asDict())
            try:
                r = GschemFile(toks.asDict())
            except :
                import traceback
                traceback.print_exc()
            return r 
        #gschem_net.setParseAction(f)


        self.gschem_file = (
            self.gschem_version.setResultsName('ver') + 
            pp.ZeroOrMore(pp.Or([
                self.gschem_net,
                self.gschem_component,
                self.gschem_pin,
                self.gschem_line,
                self.gschem_box,
                self.gschem_circle,
                self.gschem_arc,
                self.gschem_text])).setResultsName('children') + 
            pp.stringEnd
        )
        self.gschem_file.setParseAction(lambda toks: GschemFile(toks.asDict()))