-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.py
More file actions
53 lines (42 loc) · 1.45 KB
/
Lexer.py
File metadata and controls
53 lines (42 loc) · 1.45 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import ply.lex as lex
from .SimpleShellSyntaxError import *
from . import ASTTool
TOKENS = (
'TOKEN',
'REDIRECT' # 在这处理一下重定向符
)
class Lexer:
# 设定捕捉的tokens的列表
tokens = TOKENS
literals = [';', '|']
# 定义捕捉分词的函数,使用正则表达式捕捉,并进行一些简单的预处理
# 重定向符
def t_REDIRECT(self, t):
"""(<|[12]?>{1,2})(([^ "';\|\t\n]+)|("[^"\t\n]+")|('[^'\t\n]+'))?"""
return t
# 一般的token,包括命令、参数、路径
def t_TOKEN(self, t):
"""([^ "';\|\t\n]+)|("[^"\t\n]+")|('[^'\t\n]+')"""
ASTTool.del_single_quo(t.value) # 把包围的单引号删掉,如果有,这样就不用后续处理单引号的情况了
return t
# 定义一个捕捉\n的函数,用来做行数统计
def t_newline(self, t):
r'\n+'
t.lexer.lineno += len(t.value)
# 设置忽略空格和tab键
t_ignore = ' \t'
# 错误处理
def t_error(self, t):
raise SimpleShellSyntaxError("Illegal character '%s'" % t.value[0])
# print("Illegal character '%s'" % t.value[0])
# t.lexer.skip(1)
# 构建分词器
def build(self, **kwargs):
self.lex = lex.lex(module=self, **kwargs)
if __name__ == '__main__':
lexer = Lexer()
lexer.build()
data = input('输入要解析的代码:')
lexer.lex.input(data)
for tok in lexer.lex:
print(tok)