-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathscriptlex.py
162 lines (134 loc) · 3.43 KB
/
scriptlex.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from ply import *
keywords = (
'for', 'dir', 'desc', 'in', 'end', 'not', 'and', 'or', 'to', 'by', 'import',
'name', 'with', 'macros',
'at', 'as', 'on', 'facing', 'rotated', 'align', 'here', 'the_end', 'the_nether', 'overworld',
'move', 'create', 'tell', 'title', 'subtitle', 'actionbar',
'reset', 'clock', 'function', 'if', 'unless', 'then', 'do', 'else', 'switch', 'case', 'default',
'return', 'while', 'macro', 'block', 'block_data', 'block_tag', 'entity_tag', 'item_tag', 'define', 'array', 'remove', 'success', 'result',
'shaped', 'recipe', 'keys', 'eyes', 'feet', 'advancement', 'loot_table', 'predicate', "item_modifier",
'push', 'pop', 'true', 'false',
)
tokens = keywords + (
'COMMAND',
'LEQ','GEQ','GT','LT','EQUALEQUAL','DOLLAR','DOT','COLON','SEMICOLON',
'PLUSEQUALS','MINUSEQUALS','TIMESEQUALS','DIVIDEEQUALS','MODEQUALS','PLUSPLUS','MINUSMINUS',
'EQUALS','PLUS','MINUS','TIMES','DIVIDE','MOD','REF',
#'POWEREMPTY',
'POWER',
'LPAREN','RPAREN','COMMA','DECIMAL','FLOAT','HEX','BINARY','FUNCTIONID','ID','NEWLINE','LBRACK','RBRACK','LCURLY','RCURLY',
'ATID', 'NOT', 'TILDEEMPTY', 'TILDE',
'NORMSTRING', 'COMMENT', 'PRINT'
)
def t_None(t):
r'None'
t.value = "0"
t.type = "DECIMAL"
return t
def t_False(t):
r'False'
t.value = "0"
t.type = "DECIMAL"
return t
def t_True(t):
r'True'
t.value = "1"
t.type = "DECIMAL"
return t
def t_FUNCTIONID(t):
r'[A-Za-z_][A-Za-z0-9_]*\('
t.value = t.value[:-1]
return t
def t_ID(t):
r'[A-Za-z_][A-Za-z0-9_]*'
if t.value in keywords:
t.type = t.value
return t
def t_COMMAND(t):
r'(?m:^\s*\/.+)'
t.lexer.lineno += t.value.count('\n')
t.value = t.value.strip()
return t
def t_WHITESPACE(t):
r'[ \t]'
def t_ATID(t):
r'@[A-Za-z_][A-Za-z0-9_]*'
t.value = t.value[1:]
return t
t_PRINT = r'\$print\('
t_EQUALEQUAL = r'=='
t_LEQ = r'<='
t_GEQ = r'>='
t_LT = r'<'
t_GT = r'>'
t_DOLLAR = r'\$'
t_COMMA = r'\,'
t_PLUSEQUALS = r'\+='
t_MINUSEQUALS = r'-='
t_TIMESEQUALS = r'\*='
t_MODEQUALS = r'\%='
t_EQUALS = r'='
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_PLUSPLUS = r'\+\+'
t_MINUSMINUS = r'--'
t_DOT = r'\.'
t_COLON = r'\:'
t_NOT = r'!'
t_REF = r'&'
t_SEMICOLON = r';'
#def t_POWEREMPTY(t):
# r'\^[ \t]'
# t.value = "^"
# return t
t_POWER = r'\^'
def t_TILDEEMPTY(t):
r'~[ \t]'
t.value = "~"
return t
t_TILDE = r'~'
t_FLOAT = r'\d+\.\d+'
t_DECIMAL = r'\d+'
def t_HEX(t):
r'0x[0-9A-Fa-f]+'
t.value = str(int(t.value, 16))
return t
def t_BINARY(t):
r'0b[01]+'
t.value = str(int(t.value, 2))
return t
def t_DIVIDEEQUALS(t):
r'/='
return t
def t_DIVIDE(t):
r'/'
return t
t_MOD = r'\%'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACK = r'\['
t_RBRACK = r'\]'
t_LCURLY = r'\{'
t_RCURLY = r'\}'
t_ignore = '\r'
def t_NEWLINE(t):
r'\n'
t.lexer.lineno += 1
return t
def t_NORMSTRING(t):
r'("((\\.)|[^"\n])*")|(\'((\\.)|[^\'\n])*\')'
return t
def t_COMMENT(t):
r'\#.+'
return t
def t_error(t):
print(f'Illegal character "{t.value[0]}" was skipped at line {t.lexer.lineno}')
t.lexer.skip(1)
# Compute column.
# input is the input text string
# token is a token instance
def find_column(input, token):
line_start = input.rfind('\n', 0, token.lexpos) + 1
return (token.lexpos - line_start) + 1
lexer = lex.lex(debug=0)