forked from kylemartinezolodin/Programming-Languages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
178 lines (148 loc) · 5.89 KB
/
Copy pathlexer.py
File metadata and controls
178 lines (148 loc) · 5.89 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
import sys, enum
# THIS CLASS' MAIN ROLE IS TO TOKENIZE THE SOURCE CODE OF THE CUSTOM LANGUAGE
class Lexer:
def __init__(self, input):
self.source = input + '\n' # Source code to lex as a string. Append a newline to simplify lexing/parsing the last token/statement.
self.curChar = '' # Current character in the string.
self.curPos = -1 # Current position in the string.
# FOR COMPILATION ABORT PURPOSES
self.curLine = 0
self.curCol = 0
# DURING INITIALIZATION
self.nextChar()
# Process the next character.
def nextChar(self):
self.curPos += 1
self.curLine += 1
if self.curPos >= len(self.source):
self.curChar = '\0' # EOF
else:
self.curChar = self.source[self.curPos]
# Return the lookahead character.
def peek(self):
if self.curPos + 1 >= len(self.source):
return '\0'
return self.source[self.curPos+1]
# Invalid token found, print error message and exit.
def abort(self, message):
sys.exit("Lexing error @ line " +str(self.curLine) +", col " +str(self.curCol) +"\n" + message)
# Skip whitespace except newlines, which we will use to indicate the end of a statement.
def skipWhitespace(self):
while self.curChar == ' ' or self.curChar == '\t' or self.curChar == '\r':
self.nextChar()
# Skip comments in the code.
def skipComment(self):
pass
# ANG BIDA SA LEXER
# Return the next token.
def getToken(self):
self.skipWhitespace() # REMEMBER getToken IS CALLED AFTER A RECENT getToken CALL WHERE GI KAON NIYA TANANG CHAR AFTER A CERTAIN TOKEN, THUS A NEXT getToken CALL IS UNCHARTED
token = None
# NEWLINE HANDLER
if self.curChar == '\n':
self.curLine += 1
self.curCol = 0
token = Token(self.curChar, TokenType.NEWLINE)
# End-of-file HANDLER
elif self.curChar == '\0':
token = Token('', TokenType.EOF)
# OPERATOR HANDLER
# Check the first character of this token to see if we can decide what it is.
# If it is a multiple character operator (e.g., !=), number, identifier, or keyword then we will process the rest.
elif self.curChar == '=':
token = Token(self.curChar, TokenType.EQ)
elif self.curChar == '+':
token = Token(self.curChar, TokenType.PLUS)
elif self.curChar == '-':
token = Token(self.curChar, TokenType.MINUS)
elif self.curChar == '*':
token = Token(self.curChar, TokenType.ASTERISK)
elif self.curChar == '/':
token = Token(self.curChar, TokenType.SLASH)
# NUBER HANDLER
elif self.curChar.isdigit():
# Leading character is a digit, so this must be a number.
# Get all consecutive digits and decimal if there is one.
startPos = self.curPos
while self.peek().isdigit():
self.nextChar()
if self.peek() == '.': # Decimal!
self.nextChar()
# Must have at least one digit after decimal.
if not self.peek().isdigit():
# Error!
self.abort("Illegal character in number.")
while self.peek().isdigit():
self.nextChar()
tokText = self.source[startPos : self.curPos + 1] # Get the substring.
token = Token(tokText, TokenType.NUMBER)
# IDENTIFIER HANDLER
elif self.curChar.isalpha(): # IF IT IS ALPHANUMERIC
# Leading character is a letter, so this must be an identifier or a keyword.
# Get all consecutive alpha numeric characters.
startPos = self.curPos
while self.peek().isalnum():
self.nextChar()
# Check if the token is in the list of keywords.
tokText = self.source[startPos : self.curPos + 1] # Get the substring.
keyword = Token.checkIfKeyword(tokText)
if keyword == None: # Identifier
token = Token(tokText, TokenType.IDENT)
else: # Keyword
token = Token(tokText, keyword)
# UNKNOWN TOKEN HANDLING!
else:
# MESSAGE FORMAT:
# Unknown token!
# Starting character [INT CODE] is 97 as "a"
self.abort("Unknown token! \nStarting character [INT CODE] is " +str(ord(self.curChar)) +" as \"" +self.curChar +"\"" )
self.nextChar()
return token
# THIS CLASS' MAIN ROLE IS TO STRUCTURE THE TOKEN, CONTAINING TOKEN TEXT AND THE TYPE OF TOKEN.
class Token:
def __init__(self, tokenText, tokenKind):
self.text = tokenText # The token's actual text. Used for identifiers, strings, and numbers.
self.kind = tokenKind # The TokenType that this token is classified as.
@staticmethod # STATIC PARA CALLABLE SIYA BISAG WALAY BUHATAY OG INSTANCE ANI NGA CLASS
def checkIfKeyword(tokenText):
for kind in TokenType:
# Relies on all keyword enum values being 1XX.
if kind.name == tokenText and kind.value >= 100 and kind.value < 200:
return kind
return None
# THIS CLASS' MAIN ROLE IS TO CLASSIFY THE TOKEN, WHERE ALL RESERVED WORD ARE DEFINED HERE
# MAG SABOT SA GURO TAS RESERVED WORD GAMITON SA ATO LANGUAGE
class TokenType(enum.Enum):
EOF = -1
NEWLINE = 0
NUMBER = 1
IDENT = 2
STRING = 3
# Keywords.
START = 101
STOP = 102
INT = 103
PRINT = 104
# LABEL = 101
# GOTO = 102
# PRINT = 103
# INPUT = 104
# LET = 105
# IF = 106
# THEN = 107
# ENDIF = 108
# WHILE = 109
# REPEAT = 110
# ENDWHILE = 111
# Operators.
EQ = 201
PLUS = 202
MINUS = 203
ASTERISK = 204
SLASH = 205
EQEQ = 206
NOTEQ = 207
LT = 208
LTEQ = 209
GT = 210
GTEQ = 211