| 1 | import py |
| 2 | from rpython.rlib.parsing.parsing import PackratParser, Rule, Nonterminal |
| 3 | from rpython.rlib.parsing.parsing import Symbol, ParseError |
| 4 | from rpython.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function |
| 5 | from rpython.rlib.parsing.deterministic import LexerError |
| 6 | from rpython.rlib.parsing.tree import RPythonVisitor |
| 7 | |
| 8 | class TestDictError(object): |
| 9 | dictebnf = """ |
| 10 | QUOTED_STRING: "'[^\\']*'"; |
| 11 | IGNORE: " |\n"; |
| 12 | data: <dict> | <QUOTED_STRING> | <list>; |
| 13 | dict: ["{"] (dictentry [","])* dictentry ["}"]; |
| 14 | dictentry: QUOTED_STRING [":"] data; |
| 15 | list: ["["] (data [","])* data ["]"]; |
| 16 | """ |
| 17 | |
| 18 | def setup_class(cls): |
| 19 | regexs, rules, ToAST = parse_ebnf(cls.dictebnf) |
| 20 | cls.ToAST = ToAST |
| 21 | parse = make_parse_function(regexs, rules, eof=True) |
| 22 | cls.parse = staticmethod(parse) |
| 23 | |
| 24 | def test_lexererror(self): |
| 25 | excinfo = py.test.raises(LexerError, self.parse, """ |
| 26 | { |
| 27 | 'type': 'SCRIPT',$# |
| 28 | 'funDecls': '', |
| 29 | 'length': '1', |
| 30 | }""") |
| 31 | msg = excinfo.value.nice_error_message("<stdin>") |
| 32 | print msg |
| 33 | assert msg == """\ |
| 34 | File <stdin>, line 3 |
| 35 | 'type': 'SCRIPT',$# |
| 36 | ^ |
| 37 | LexerError""" |
| 38 | |
| 39 | def test_parseerror(self): |
| 40 | source = """ |
| 41 | { |
| 42 | 'type': 'SCRIPT', |
| 43 | 'funDecls': '', |
| 44 | 'length':: '1', |
| 45 | }""" |
| 46 | excinfo = py.test.raises(ParseError, self.parse, source) |
| 47 | error = excinfo.value.errorinformation |
| 48 | source_pos = excinfo.value.source_pos |
| 49 | assert source_pos.lineno == 4 |
| 50 | assert source_pos.columnno == 13 |
| 51 | msg = excinfo.value.nice_error_message("<stdin>", source) |
| 52 | print msg |
| 53 | assert msg == """\ |
| 54 | File <stdin>, line 5 |
| 55 | 'length':: '1', |
| 56 | ^ |
| 57 | ParseError: expected '{', 'QUOTED_STRING' or '['""" |