MCPcopy Create free account
hub / github.com/RetiredWizard/PyDOS / Lexer

Class Lexer

PyBasic/lexer.py:40–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38
39
40class Lexer:
41
42 def __init__(self):
43
44 self.__column = 0 # Current column number
45 self.__stmt = '' # Statement string being processed
46
47 def tokenize(self, stmt):
48 """Returns a list of tokens obtained by
49 lexical analysis of the specified
50 statement.
51
52 """
53 self.__stmt = stmt
54 self.__column = 0
55
56 # Establish a list of tokens to be
57 # derived from the statement
58 tokenlist = []
59
60 # Process every character until we
61 # reach the end of the statement string
62 c = self.__get_next_char()
63 while c != '':
64
65 # Skip any preceding whitespace
66 while c.isspace():
67 c = self.__get_next_char()
68
69 # Construct a token, column count already
70 # incremented
71 token = Token(self.__column - 1, None, '')
72
73 # Process strings
74 if c == '"':
75 token.category = Token.STRING
76
77 # Consume all of the characters
78 # until we reach the terminating
79 # quote. Do not store the quotes
80 # in the lexeme
81 c = self.__get_next_char() # Advance past opening quote
82
83 # We explicitly support empty strings
84 if c == '"':
85 # String is empty, leave lexeme as ''
86 # and advance past terminating quote
87 c = self.__get_next_char()
88
89 else:
90 while True:
91 token.lexeme += c # Append the current char to the lexeme
92 c = self.__get_next_char()
93
94 if c == '':
95 raise SyntaxError("Mismatched quotes")
96
97 if c == '"':

Callers 3

mainFunction · 0.90
readDataMethod · 0.90
getprogramMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected