Skip to content

Commit f2084e8

Browse files
committed
Fixed support for the InteractiveParser
1 parent 2382f5e commit f2084e8

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

lark_cython/lark_cython.pyx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cdef class Token:
3434
self.end_column = end_column
3535
self.end_pos = end_pos
3636

37-
cdef update(self, type_: Optional[str]=None, value: Optional[Any]=None):
37+
cpdef update(self, type_: Optional[str]=None, value: Optional[Any]=None):
3838
return Token.new_borrow_pos(
3939
type_ if type_ is not None else self.type,
4040
value if value is not None else self.value,
@@ -45,7 +45,7 @@ cdef class Token:
4545
def new_borrow_pos(cls, type_: str, value: Any, borrow_t: 'Token'):
4646
return cls(type_, value, borrow_t.start_pos, borrow_t.line, borrow_t.column, borrow_t.end_line, borrow_t.end_column, borrow_t.end_pos)
4747

48-
cdef __reduce__(self):
48+
def __reduce__(self):
4949
return (self.__class__, (self.type, self.value, self.start_pos, self.line, self.column))
5050

5151
def __repr__(self):
@@ -77,7 +77,7 @@ cdef class LexerState:
7777
cdef public LineCounter line_ctr
7878
cdef public object last_token
7979

80-
def __cinit__(self, text, line_ctr, last_token=None):
80+
def __init__(self, text, line_ctr, last_token=None):
8181
self.text = text
8282
self.line_ctr = line_ctr
8383
self.last_token = last_token
@@ -91,6 +91,8 @@ cdef class LexerState:
9191
cdef __copy__(self):
9292
return type(self)(self.text, copy(self.line_ctr), self.last_token)
9393

94+
_Token = Token
95+
9496
cdef class LineCounter:
9597
__slots__ = 'char_pos', 'line', 'column', 'line_start_pos', 'newline_char'
9698

@@ -190,7 +192,7 @@ cdef class Lexer:
190192
return LexerState(text, line_ctr)
191193

192194
cpdef make_lexer_thread(self, str text):
193-
return LexerThread(self, text)
195+
return LexerThread.from_text(self, text)
194196

195197
cdef class BasicLexer(Lexer):
196198

@@ -362,20 +364,21 @@ cdef class LexerThread:
362364
"""A thread that ties a lexer instance and a lexer state, to be used by the parser"""
363365

364366
cdef Lexer lexer
365-
cdef object state
367+
cdef LexerState state
366368

367-
def __cinit__(self, lexer, text):
369+
def __init__(self, lexer, LexerState lexer_state):
368370
self.lexer = lexer
369-
self.state = lexer.make_lexer_state(text)
371+
self.state = lexer_state
372+
373+
@classmethod
374+
def from_text(cls, lexer, text):
375+
return cls(lexer, lexer.make_lexer_state(text))
370376

371-
cdef next_token(self, ParserState parser_state):
377+
def next_token(self, ParserState parser_state):
372378
return self.lexer.next_token(self.state, parser_state)
373379

374380
def __copy__(self):
375-
copied = object.__new__(LexerThread)
376-
copied.lexer = self.lexer
377-
copied.state = copy(self.state)
378-
return copied
381+
return type(self)(self.lexer, copy(self.state))
379382

380383
####
381384

@@ -411,7 +414,7 @@ cdef class ParserState:
411414
__slots__ = 'parse_conf', 'lexer', 'state_stack', 'value_stack'
412415

413416
cdef public ParseConf parse_conf
414-
cdef public LexerThread lexer # LexerThread
417+
cdef public object lexer # LexerThread
415418
cdef public list value_stack, state_stack
416419

417420
def __init__(self, parse_conf, lexer, state_stack=None, value_stack=None):
@@ -441,7 +444,7 @@ cdef class ParserState:
441444
def copy(self):
442445
return copy(self)
443446

444-
cdef feed_token(self, Token token, bint is_end=False):
447+
cpdef feed_token(self, Token token, bint is_end=False):
445448
cdef:
446449
list state_stack = self.state_stack
447450
list value_stack = self.value_stack
@@ -463,7 +466,8 @@ cdef class ParserState:
463466
try:
464467
action, arg = states[state][token.type]
465468
except KeyError:
466-
expected = {s for s in states[state].keys() if s.isupper()}
469+
# expected = {s for s in states[state].keys() if s.isupper()}
470+
expected = set(filter(str.isupper, states[state].keys()))
467471
raise UnexpectedToken(token, expected, state=self, interactive_parser=None)
468472

469473
assert arg != end_state
@@ -513,7 +517,7 @@ cdef class _Parser:
513517
return self.parse_from_state(parser_state)
514518

515519

516-
cdef parse_from_state(self, ParserState state):
520+
cpdef parse_from_state(self, ParserState state):
517521
# Main LALR-parser loop
518522
cdef Token token
519523
cdef Token end_token

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def cythonize(*args, **kwargs):
2323

2424
ext_modules = cythonize('lark_cython/*.pyx'), # accepts a glob pattern
2525
requires = ['Cython'],
26-
install_requires = ['lark>=1.1.0', 'cython>=0.29.0', 'Cython>=0.29.0'],
26+
install_requires = ['lark>=1.1.2', 'cython>=0.29.0', 'Cython>=0.29.0'],
2727
setup_requires=['Cython'],
2828

2929
author = "Erez Shinan",

0 commit comments

Comments
 (0)