-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexer.py
More file actions
176 lines (157 loc) · 3.85 KB
/
Copy pathlexer.py
File metadata and controls
176 lines (157 loc) · 3.85 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
from ply import lex
# MOST OF THIS IS TOLEN FROM KYOS LAB, I'll go through it setting up more of what's actually to stay
# List of token names. This is always required
tokens = [
"NUMBER",
"ID",
"PLUS",
"MINUS",
"TIMES",
"DIVIDE",
"MOD",
"POW",
"LT",
"LEQ",
"GT",
"GEQ",
"EQOP",
"NEQ",
"AND",
"OR",
"NOT",
"SEMICOL",
"PERIOD",
"COMMA",
"EQ",
"LPAREN",
"RPAREN",
"LBRACK",
"RBRACK",
"LBRACE",
"RBRACE",
"SQUIGGLY",
"PEQ",
"COL",
"MEQ",
"TEQ",
"DEQ",
"PP",
"MM",
"QMARK",
"STRING"
]
# Reserved words which should not match any IDs we need to add this
reserved = {
"real": "REAL",
"waifu": "WAIFU",
"husbando": "HUSBANDO",
"catgirl": "CATGIRL",
"catboy": "CATBOY",
"senpai": "SENPAI",
"kouhai": "KOUHAI",
"chan": "CHAN",
"kun": "KUN",
"san": "SAN",
"sama": "SAMA",
"yokai": "YOKAI",
"owo": "OWO",
"uwu": "UWU",
"desu": "DESU",
"harem": "HAREM",
"nani": "NANI",
"noU": "NOU",
"whileU": "WHILEU",
"iStudied": "ISTUDIED",
"shi": "SHI",
"baka": "BAKA",
"loli": "LOLI"
}
# Add reserved names to list of tokens
tokens += list(reserved.values())
class OwOScriptLexer():
# A string containing ignored characters (spaces and tabs)
t_ignore = " \t"
# Regular expression rule with some action code
t_PP = r"\+\+"
t_MM = r"--"
t_PLUS = r"\+"
t_MINUS = r"-"
t_TIMES = r"\*"
t_DIVIDE = r"/"
t_MOD = r"\%"
t_POW = r"\*\*"
t_LEQ = r"\<="
t_LT = r"\<"
t_GEQ = r"\>="
t_GT = r"\>"
t_SQUIGGLY = r"\~"
t_EQOP = r"\=="
t_NEQ = r"\!="
t_AND = r"\&&"
t_OR = r"\|\|"
t_NOT = r"\!"
t_SEMICOL = r";"
t_PERIOD = r"\."
t_COMMA = r","
t_EQ = r"\="
t_LPAREN = r"\("
t_RPAREN = r"\)"
t_LBRACE = r"\{"
t_RBRACE = r"\}"
t_LBRACK = r"\["
t_RBRACK = r"\]"
t_PEQ = r"\+="
t_MEQ = r"\-="
t_TEQ = r"\*="
t_DEQ = r"\/="
t_COL = r"\:"
t_QMARK = r"\?"
# A regular expression rule with some action code
def t_NUMBER(self, t):
# This needs to be like dynamic, it should be a float unless integer, but numbers are like a single thing so
r"([0-9]*[.])?[0-9]+"
try:
t.value = int(t.value)
except ValueError:
t.value = float(t.value)
return t
def t_ID(self, t):
r"[a-zA-Z_][a-zA-Z_0-9]*"
t.type = reserved.get(t.value, "ID") # Check for reserved words
return t
def t_STRING(self, t):
r"\"(\\.|[^\"])*\""
return t
# Define a rule so we can track line numbers. DO NOT MODIFY
def t_newline(self, t):
r"\n+"
t.lexer.lineno += len(t.value)
# Error handling rule. DO NOT MODIFY
def t_error(self, t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer. DO NOT MODIFY
def build(self, **kwargs):
self.tokens = tokens
self.lexer = lex.lex(module=self, **kwargs)
# Test the output. DO NOT MODIFY
def test(self, data):
self.lexer.input(data)
while True:
tok = self.lexer.token()
if not tok:
break
print(tok)
m = OwOScriptLexer()
m.build()
# Main function. DO NOT MODIFY
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Take in the OwOScript source code and perform lexical analysis.")
parser.add_argument("FILE", help="Input file with OwOScript source code")
args = parser.parse_args()
f = open(args.FILE, "r")
data = f.read()
f.close()
m.test(data)