def test_basic(self): p = Lark(""" start: "a" "b" "c" "d" %ignore " " """) res = list(p.lex("abc cba dd")) assert res == list('abccbadd') res = list(p.lex("abc cba dd", dont_ignore=True)) assert res == list('abc cba dd')
def main(): fin = open("grammar.lark") grammar = fin.read() fin.close() parser = Lark(grammar, parser="lalr") #parser = Lark ( grammar, parser = "lalr", lexer = "contextual" ) #parser = Lark ( grammar ) count = 1 testfile = "./test%s.txt" % count while os.path.exists(testfile): print("\n\n--- Parsing file: %s ---\n" % testfile) fin = open(testfile) try: buf = fin.read() print(buf) # Debug lexer print("Lexer: ", [x for x in parser.lex(buf)], "\n") tree = parser.parse(buf) print(tree, "\n\n") print(tree.pretty()) finally: fin.close() count += 1 testfile = "./test%s.txt" % count
class LexerJson(QsciLexerCustom): def __init__(self, parent=None): super().__init__(parent) self.create_parser() self.create_styles() def create_styles(self): deeppink = QColor(249, 38, 114) khaki = QColor(230, 219, 116) mediumpurple = QColor(174, 129, 255) mediumturquoise = QColor(81, 217, 205) yellowgreen = QColor(166, 226, 46) lightcyan = QColor(213, 248, 232) darkslategrey = QColor(39, 40, 34) styles = { 0: mediumturquoise, 1: mediumpurple, 2: yellowgreen, 3: deeppink, 4: khaki, 5: lightcyan } for style, color in styles.items(): self.setColor(color, style) self.setPaper(darkslategrey, style) self.setFont(self.parent().font(), style) self.token_styles = { "COLON": 5, "COMMA": 5, "LBRACE": 5, "LSQB": 5, "RBRACE": 5, "RSQB": 5, "FALSE": 0, "NULL": 0, "TRUE": 0, "STRING": 4, "NUMBER": 1, } def create_parser(self): grammar = ''' anons: ":" "{" "}" "," "[" "]" TRUE: "true" FALSE: "false" NULL: "NULL" %import common.ESCAPED_STRING -> STRING %import common.SIGNED_NUMBER -> NUMBER %import common.WS %ignore WS ''' self.lark = Lark(grammar, parser=None, lexer='standard') # All tokens: print([t.name for t in self.lark.parser.lexer.tokens]) def defaultPaper(self, style): return QColor(39, 40, 34) def language(self): return "Json" def description(self, style): return {v: k for k, v in self.token_styles.items()}.get(style, "") def styleText(self, start, end): self.startStyling(start) text = self.parent().text()[start:end] last_pos = 0 try: for token in self.lark.lex(text): ws_len = token.pos_in_stream - last_pos if ws_len: self.setStyling(ws_len, 0) # whitespace token_len = len(bytearray(token, "utf-8")) self.setStyling(token_len, self.token_styles.get(token.type, 0)) last_pos = token.pos_in_stream + token_len except Exception as e: print(e)
lang_file = open("calculator.lark") calc_parser = Lark(lang_file.read(), start='chain') values = [10, '+', 3, '*', 2, '^', 5, '+', 2] #text = '10 + 3 * 2 ^ 5 + 2' text = '9 ^ 3 ^ 2 ^ 2' # for tk in calc_parser.lex('10 + 2 + 3'): # print(repr(tk)) tree = calc_parser.parse(text) print(tree.pretty()) lexed = [] for tk in calc_parser.lex(text): lexed.append((tk[0:], tk.type)) while len(lexed) > 1: print(lexed) higher = find_highest_precedence(lexed) idx = lexed.index(higher) res = evaluate(lexed[idx - 1][0], lexed[idx][0], lexed[idx + 1][0]) lexed.insert(idx - 1, res) for i in range(3): lexed.pop(idx) print(lexed[0][0])