Returns a list of tokens obtained by lexical analysis of the specified statement.
(self, stmt)
| 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 == '"': |
| 98 | c = self.__get_next_char() # Advance past terminating quote |
| 99 | break |
| 100 | |
| 101 | # Process numbers |
| 102 | elif c.isdigit() or c == ".": |
| 103 | if c == ".": |
| 104 | token.category = Token.UNSIGNEDFLOAT |
no test coverage detected