1+ from lark import Lark , Tree
2+ import lark_cython
3+
4+
5+ def test_minimal ():
6+ parser = Lark ('!start: "a" "b"' , parser = 'lalr' , _plugins = lark_cython .plugins )
7+ res = parser .parse ("ab" )
8+ assert isinstance (res , Tree )
9+ assert all (isinstance (t , lark_cython .Token ) for t in res .children )
10+ assert [t .value for t in res .children ] == ['a' , 'b' ]
11+
12+ def test_no_placeholders ():
13+ parser = Lark ('!start: "a" ["b"]' , parser = 'lalr' , _plugins = lark_cython .plugins , maybe_placeholders = True )
14+
15+ assert len (parser .parse ("a" ).children ) == 2
16+ assert len (parser .parse ("ab" ).children ) == 2
17+
18+ parser = Lark ('!start: "a" ["b"]' , parser = 'lalr' , _plugins = lark_cython .plugins , maybe_placeholders = False )
19+
20+ assert len (parser .parse ("a" ).children ) == 1
21+ assert len (parser .parse ("ab" ).children ) == 2
22+
23+ def test_start ():
24+ parser = Lark ('!x: "a" "b"' , parser = 'lalr' , _plugins = lark_cython .plugins , start = 'x' )
25+ res = parser .parse ("ab" )
26+ assert [t .value for t in res .children ] == ['a' , 'b' ]
27+
28+ def test_lexer_callbacks ():
29+ comments = []
30+
31+ parser = Lark ("""
32+ start: INT*
33+
34+ COMMENT: /#.*/
35+
36+ %import common (INT, WS)
37+ %ignore COMMENT
38+ %ignore WS
39+ """ , parser = "lalr" , _plugins = lark_cython .plugins , lexer_callbacks = {'COMMENT' : comments .append })
40+
41+
42+ res = parser .parse ("""
43+ 1 2 3 # hello
44+ # world
45+ 4 5 6
46+ """ )
47+
48+ assert isinstance (res .children [0 ], lark_cython .Token )
49+ assert isinstance (comments [0 ], lark_cython .Token )
50+ assert len (comments ) == 2
0 commit comments