-
-
Save MegaIng/c6abba4d9be87473d8d586734f2b39c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Iterator | |
from lark import Lark, Token | |
from lark.common import LexerConf | |
from lark.lexer import Lexer, BasicLexer, LexerState | |
class RecursiveLexerThread: | |
def __init__(self, lexer: Lexer, text: str): | |
print(lexer, text) | |
self.lexer = lexer | |
self.state_stack = [LexerState(text)] | |
def lex(self, parser_state): | |
while self.state_stack: | |
lexer_state = self.state_stack[-1] | |
lex = self.lexer.lex(lexer_state, parser_state) | |
try: | |
token = next(lex) | |
except StopIteration: | |
self.state_stack.pop() # We are done with this file | |
else: | |
if token.type == "_INCLUDE": | |
name = token.value.split()[-1] # get just the string | |
name = name[1:-1] # Remove " | |
self.state_stack.append(LexerState(test_files[name])) | |
yield token # The parser still expects this token either way | |
parser = Lark(r""" | |
start: (statement|_INCLUDE)+ | |
statement: NAME "=" value -> assignment | |
!?value: (value ("+"|"-"))? mul | |
!?mul: (mul ("*"|"/"))? atom | |
?atom: NAME -> variable | |
| NUMBER -> number | |
| "(" value ")" | |
_INCLUDE.1: "INCLUDE" /\s+/ STRING | |
%import common.CNAME -> NAME | |
%import common.SIGNED_INT -> NUMBER | |
%import common.ESCAPED_STRING -> STRING | |
%ignore /\s+/ | |
""", _plugins={ | |
"LexerThread": RecursiveLexerThread | |
}, parser="lalr") | |
test_files = { | |
"a": """ | |
pi = 31415/10000 | |
""", | |
"b": """ | |
INCLUDE "a" | |
tau = pi * 2 | |
""" | |
} | |
tree = parser.parse(""" | |
INCLUDE "b" | |
result = tau | |
""") | |
print(tree.pretty()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curious, why didn't you make RecurisveLexerThread inherit from the LexerThread?