from pyparsing import class Lexer def __init__ self program star Regex

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from pyparsing import *
class Lexer:
def __init__(self, program):
star = Regex("\*").setResultsName('star')
oper_left = Regex("\(").setResultsName('oper_left')
oper_right = Regex("\)").setResultsName('oper_right')
text = Regex("\"[a-z*(+)]+\"").setResultsName('text')
identify = Regex("[A-Z]p|[A-Z]").setResultsName('ident')
lexems = OneOrMore(Group(identify) | Group(oper_left) | Group(oper_right) | Group(text) | Group(star))
lexems = lexems.parseString(program)
self.lexems = list(reversed([lexem.items()[0] for lexem in lexems]))
def next_token(self):
return self.lexems.pop() if self.lexems else False
def cur_token(self):
return self.lexems[-1] if self.lexems else False
def cur_token_is_star(self):
return self.lexems[-1][0] == 'star' if self.lexems else False
def cur_token_is_ident(self):
return self.lexems[-1][0] == 'ident' if self.lexems else False
def cur_token_is_text(self):
return self.lexems[-1][0] == 'text' if self.lexems else False
def cur_token_is_oper_left(self):
return self.lexems[-1][0] == 'oper_left' if self.lexems else False
def cur_token_is_oper_right(self):
return self.lexems[-1][0] == 'oper_right' if self.lexems else False