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