def test_diag(self): with unittest.mock.patch('liblet.antlr.warn') as mock_warn: Ambig = ANTLR(r""" grammar Ambig; stat: expr ';' | ID '(' ')' ';' ; expr: ID '(' ')' | INT ; INT : [0-9]+ ; ID : [a-zA-Z]+ ; WS : [ \t\r\n]+ -> skip ; """) text = 'f();' Ambig.context(text, 'stat', diag=True) args, kwargs = mock_warn.call_args errs = "line 1:3 reportAttemptingFullContext d=0 (stat), input='f();'\nline 1:3 reportAmbiguity d=0 (stat): ambigAlts={1, 2}, input='f();'" self.assertEqual(args[0], errs)
def test_trace(self): buf = StringIO() with redirect_stdout(buf): Ambig = ANTLR(r""" grammar Ambig; stat: expr ';' | ID '(' ')' ';' ; expr: ID '(' ')' | INT ; INT : [0-9]+ ; ID : [a-zA-Z]+ ; WS : [ \t\r\n]+ -> skip ; """) text = 'f();' Ambig.context(text, 'stat', trace=True) trace = dedent(""" enter stat, LT(1)=f enter expr, LT(1)=f consume [@0,0:0='f',<5>,1:0] rule expr consume [@1,1:1='(',<2>,1:1] rule expr consume [@2,2:2=')',<3>,1:2] rule expr exit expr, LT(1)=; consume [@3,3:3=';',<1>,1:3] rule stat exit stat, LT(1)=<EOF> """)[1:] # remove first blank line self.assertEqual(buf.getvalue(), trace)
def test_tree_str(self): Bad = ANTLR(r""" grammar Bad ; start: expr ; expr: ID | expr OP ID | expr '*' ID; OP: '+' | '-' ; ID: [a-z]; """) tree = Bad.context( 'z-a+a*x', 'start', as_string=True, ) self.assertEqual(tree, "(start (expr (expr (expr (expr z) - a) + a) * x))")