Пример #1
0
    def setUp(self):
        super().setUp()

        g = GrammarBuilder()
        g.number_literal = Regexp(r'-?(?:[1-9]\d*|0)(?:\.\d*)?(?:[eE][+-]?\d+)?')
        g.string_literal = Regexp(r'"(?:[^"]|\\(?:["\\nbfrt]|u[0-9a-fA-F]{4}))*"')
        g.array = '[' + flatten(repeat(g.expr, separator=drop(','))) + ']'
        g.object_ = '{' + flatten(repeat(flatten(g.string_literal + ':' + g.expr), separator=',')) + '}'
        g.expr = flatten(g.number_literal | g.string_literal | g.array | g.object_)
        g.whitespace = Regexp('\\s+')

        self.grammar = g(start=g.expr, tokenize=[ignore(g.whitespace)], drop_terminals=True)
Пример #2
0
    def setUp(self):
        super().setUp()

        g = GrammarBuilder()
        g.number_literal = Regexp(r'-?(?:[1-9]\d*|0)(?:\.\d*)?(?:[eE][+-]?\d+)?') >= float
        g.string_literal = Regexp(r'"(?:[^"]|\\(?:["\\nbfrt]|u[0-9a-fA-F]{4}))*"') >= (lambda s: s[1:-1])
        g.array = '[' + flatten(repeat(g.expr, separator=',')) + ']' >= list
        g.object_item = g.string_literal + ':' + g.expr >= tuple
        g.object_ = '{' + flatten(repeat(g.object_item, separator=',')) + '}' >= dict
        g.boolean = keep('true') | keep('false') >= (lambda s: s == 'true')
        g.null = keep('null') >= const(None)
        g.expr = flatten(g.number_literal | g.string_literal | g.array | g.object_ | g.boolean | g.null)
        g.whitespace = Regexp('\\s+')
        self.grammar = g(start=g.expr, tokenize=[ignore(g.whitespace)], drop_terminals=True)
Пример #3
0
 def setUp(self):
     super().setUp()
     g = GrammarBuilder()
     g.xy = Terminal('x') | Terminal('y')
     g.yx = Terminal('x') | Terminal('y')
     g.start = flatten(Lookahead('x') + g.xy) | g.yx
     self.grammar = g(start=g.start)
Пример #4
0
 def setUp(self):
     super().setUp()
     g = GrammarBuilder()
     g.xy = Terminal('x') | Terminal('y')
     g.yx = Terminal('x') | Terminal('y')
     g.start = flatten(Lookahead('x') + g.xy) | g.yx
     self.grammar = g(start=g.start)
Пример #5
0
    def setUp(self):
        super().setUp()

        g = GrammarBuilder()
        g.number_literal = Regexp(
            r'-?(?:[1-9]\d*|0)(?:\.\d*)?(?:[eE][+-]?\d+)?')
        g.string_literal = Regexp(
            r'"(?:[^"]|\\(?:["\\nbfrt]|u[0-9a-fA-F]{4}))*"')
        g.array = '[' + flatten(repeat(g.expr, separator=drop(','))) + ']'
        g.object_ = '{' + flatten(
            repeat(flatten(g.string_literal + ':' + g.expr),
                   separator=',')) + '}'
        g.expr = flatten(g.number_literal | g.string_literal | g.array
                         | g.object_)
        g.whitespace = Regexp('\\s+')

        self.grammar = g(start=g.expr,
                         tokenize=[ignore(g.whitespace)],
                         drop_terminals=True)
Пример #6
0
    def setUp(self):
        signed = uncurry(lambda ops, x: x * product(-1 for s in ops if s == '-'))

        g = GrammarBuilder()
        g.number = Regexp(r'\d+') >= float
        g.atom = g.number | flatten('(' + g.expr + ')' >= itemgetter(0))
        g.signed = repeat(keep('+') | keep('-')) + g.atom >= signed
        g.product_expr = +repeat(g.signed, separator='*') >= product
        g.expr = +repeat(g.product_expr, separator='+') >= sum
        g.whitespace = builtins.horizontal_whitespace
        self.grammar = g(start=g.expr, tokenize=[ignore(g.whitespace)], drop_terminals=True)
Пример #7
0
    def setUp(self):
        super().setUp()

        g = GrammarBuilder()
        g.number_literal = Regexp(
            r'-?(?:[1-9]\d*|0)(?:\.\d*)?(?:[eE][+-]?\d+)?') >= float
        g.string_literal = Regexp(
            r'"(?:[^"]|\\(?:["\\nbfrt]|u[0-9a-fA-F]{4}))*"') >= (
                lambda s: s[1:-1])
        g.array = '[' + flatten(repeat(g.expr, separator=',')) + ']' >= list
        g.object_item = g.string_literal + ':' + g.expr >= tuple
        g.object_ = '{' + flatten(repeat(g.object_item,
                                         separator=',')) + '}' >= dict
        g.boolean = keep('true') | keep('false') >= (lambda s: s == 'true')
        g.null = keep('null') >= const(None)
        g.expr = flatten(g.number_literal | g.string_literal | g.array
                         | g.object_ | g.boolean | g.null)
        g.whitespace = Regexp('\\s+')
        self.grammar = g(start=g.expr,
                         tokenize=[ignore(g.whitespace)],
                         drop_terminals=True)
Пример #8
0
    def setUp(self):
        signed = uncurry(
            lambda ops, x: x * product(-1 for s in ops if s == '-'))

        g = GrammarBuilder()
        g.number = Regexp(r'\d+') >= float
        g.atom = g.number | flatten('(' + g.expr + ')' >= itemgetter(0))
        g.signed = repeat(keep('+') | keep('-')) + g.atom >= signed
        g.product_expr = +repeat(g.signed, separator='*') >= product
        g.expr = +repeat(g.product_expr, separator='+') >= sum
        g.whitespace = builtins.horizontal_whitespace
        self.grammar = g(start=g.expr,
                         tokenize=[ignore(g.whitespace)],
                         drop_terminals=True)
Пример #9
0
 def setUp(self):
     super().setUp()
     g = GrammarBuilder()
     g.ab = flatten(Terminal('A') | Terminal('B'))
     g.seq = repeat(g.ab)
     self.grammar = g(start=g.seq)
Пример #10
0
 def setUp(self):
     super().setUp()
     g = GrammarBuilder()
     g.ab = flatten(Terminal('A') | Terminal('B'))
     g.seq = repeat(g.ab)
     self.grammar = g(start=g.seq)