Fixed grammars

This commit is contained in:
Erez Shinan 2017-02-24 00:45:34 +02:00
parent 5236e4a32a
commit fbba305a9e
3 changed files with 24 additions and 4 deletions

View File

@ -24,11 +24,11 @@ json_grammar = r"""
object : "{" [pair ("," pair)*] "}"
pair : string ":" value
number: FLOAT
number: SIGNED_NUMBER
string : ESCAPED_STRING
%import common.ESCAPED_STRING
%import common.FLOAT
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS

View File

@ -3,13 +3,18 @@
//
DIGIT: "0".."9"
HEXDIGIT: "a".."f"|"A".."F"|DIGIT
INT: DIGIT+
DECIMAL: INT ("." INT)?
SIGNED_INT: ["+"|"-"] INT
DECIMAL: INT "." INT? | "." INT
// float = /-?\d+(\.\d+)?([eE][+-]?\d+)?/
FLOAT: "-"? DECIMAL (("e"|"E")("+"|"-")? INT)?
_EXP: ("e"|"E") SIGNED_INT
FLOAT: INT _EXP | DECIMAL _EXP?
NUMBER: FLOAT | INT
SIGNED_NUMBER: ["+"|"-"] NUMBER
//
// Strings

View File

@ -18,6 +18,7 @@ logging.basicConfig(level=logging.INFO)
from lark.lark import Lark
from lark.common import GrammarError, ParseError
from lark.lexer import LexError
__path__ = os.path.dirname(__file__)
def _read(n, *args):
@ -276,6 +277,20 @@ def _make_parser_test(PARSER):
x = g.parse('Hello HelloWorld')
self.assertSequenceEqual(x.children, ['HelloWorld'])
# def test_string_priority(self):
# g = _Lark("""start: (A | /a?bb/)+
# A: "a" """)
# x = g.parse('abb')
# self.assertEqual(len(x.children), 2)
# # This parse raises an exception because the lexer will always try to consume
# # "a" first and will never match the regular expression
# # This behavior is subject to change!!
# # Thie won't happen with ambiguity handling.
# g = _Lark("""start: (A | /a?ab/)+
# A: "a" """)
# self.assertRaises(LexError, g.parse, 'aab')
def test_undefined_rule(self):
self.assertRaises(GrammarError, _Lark, """start: a""")