Skip to content

Commit b949dc4

Browse files
committed
Token now behaves more like a string; Fixes for exception reporting
1 parent 3023c0e commit b949dc4

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lark_cython/lark_cython.pyx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,21 @@ cdef class Token:
5151
def __repr__(self):
5252
return 'Token(%r, %r)' % (self.type, self.value)
5353

54+
def __str__(self):
55+
return self.value
56+
5457
cdef __deepcopy__(self, memo):
5558
return Token(self.type, self.value, self.start_pos, self.line, self.column)
5659

57-
def __eq__(self, Token_or_str other):
58-
if isinstance(other, Token) and self.type != other.type:
59-
return False
60+
def __eq__(self, other):
61+
if isinstance(other, Token):
62+
return self.type == other.type and self.value == other.value
63+
64+
if isinstance(other, str):
65+
return self.value == other
66+
67+
return NotImplemented
6068

61-
return str.__eq__(self, other)
6269

6370
def __hash__(self):
6471
return hash(self.value)
@@ -384,8 +391,8 @@ from lark.exceptions import UnexpectedCharacters, UnexpectedInput, UnexpectedTok
384391
cdef class ParseConf:
385392
__slots__ = 'parse_table', 'callbacks', 'start', 'start_state', 'end_state', 'states'
386393

387-
cdef parse_table
388-
cdef int start_state, end_state
394+
cdef public parse_table
395+
cdef public int start_state, end_state
389396
cdef dict states, callbacks
390397
cdef str start
391398

@@ -403,9 +410,9 @@ cdef class ParseConf:
403410
cdef class ParserState:
404411
__slots__ = 'parse_conf', 'lexer', 'state_stack', 'value_stack'
405412

406-
cdef ParseConf parse_conf
407-
cdef LexerThread lexer # LexerThread
408-
cdef list value_stack, state_stack
413+
cdef public ParseConf parse_conf
414+
cdef public LexerThread lexer # LexerThread
415+
cdef public list value_stack, state_stack
409416

410417
def __init__(self, parse_conf, lexer, state_stack=None, value_stack=None):
411418
self.parse_conf = parse_conf

0 commit comments

Comments
 (0)