|
| 1 | +class JSONLexer: |
| 2 | + def __init__(self, json_string): |
| 3 | + self.json_string = json_string |
| 4 | + self.tokens = [] |
| 5 | + self.current_position = 0 |
| 6 | + |
| 7 | + def tokenize(self): |
| 8 | + if len(self.json_string) > 0: |
| 9 | + while self.current_position < len(self.json_string): |
| 10 | + current_char = self.json_string[self.current_position] |
| 11 | + |
| 12 | + if current_char.isspace(): |
| 13 | + self.current_position += 1 |
| 14 | + elif current_char == "{": |
| 15 | + self.tokens.append(("LBRACE", "{")) |
| 16 | + self.current_position += 1 |
| 17 | + elif current_char == "}": |
| 18 | + self.tokens.append(("RBRACE", "}")) |
| 19 | + self.current_position += 1 |
| 20 | + elif current_char == "[": |
| 21 | + self.tokens.append(("LBRACKET", "[")) |
| 22 | + self.current_position += 1 |
| 23 | + elif current_char == "]": |
| 24 | + self.tokens.append(("RBRACKET", "]")) |
| 25 | + self.current_position += 1 |
| 26 | + elif current_char == ",": |
| 27 | + self.tokens.append(("COMMA", ",")) |
| 28 | + self.current_position += 1 |
| 29 | + elif current_char == ":": |
| 30 | + self.tokens.append(("COLON", ":")) |
| 31 | + self.current_position += 1 |
| 32 | + elif current_char.isdigit() or current_char == "-": |
| 33 | + number, success, consumed = self.lex_number() |
| 34 | + if success: |
| 35 | + self.tokens.append(("NUMBER", number)) |
| 36 | + self.current_position += consumed |
| 37 | + else: |
| 38 | + print(f"Error Tokenizing: {number}") |
| 39 | + return 1 |
| 40 | + elif current_char == '"': |
| 41 | + str, success, consumed = self.lex_string() |
| 42 | + if success: |
| 43 | + self.tokens.append(("STRING", str)) |
| 44 | + self.current_position += consumed |
| 45 | + else: |
| 46 | + print(f"Error Tokenizing: {str}") |
| 47 | + return 1 |
| 48 | + elif self.json_string.startswith("true", self.current_position): |
| 49 | + self.tokens.append(("TRUE", "true")) |
| 50 | + self.current_position += 4 |
| 51 | + elif self.json_string.startswith("false", self.current_position): |
| 52 | + self.tokens.append(("FALSE", "false")) |
| 53 | + self.current_position += 5 |
| 54 | + elif self.json_string.startswith("null", self.current_position): |
| 55 | + self.tokens.append(("NULL", "null")) |
| 56 | + self.current_position += 4 |
| 57 | + else: |
| 58 | + print(f"Unexpected character: {current_char}") |
| 59 | + return 1 |
| 60 | + |
| 61 | + return self.tokens |
| 62 | + else: |
| 63 | + return 1 |
| 64 | + |
| 65 | + def lex_string(self): |
| 66 | + start_index = self.current_position + 1 |
| 67 | + chars_count = 2 # Includes the opening and closing quotes |
| 68 | + str_ = "" |
| 69 | + escaped = False |
| 70 | + |
| 71 | + for char in self.json_string[start_index:]: |
| 72 | + if char == "\\" and not escaped: |
| 73 | + escaped = True |
| 74 | + elif char == '"' and not escaped: |
| 75 | + return str_, True, chars_count |
| 76 | + elif escaped and char not in ["\\", '"', "b", "f", "n", "r", "t", "u", "/"]: |
| 77 | + break |
| 78 | + elif char in ["\t", "\n"]: |
| 79 | + break |
| 80 | + else: |
| 81 | + escaped = False |
| 82 | + |
| 83 | + str_ = str_ + char |
| 84 | + chars_count += 1 |
| 85 | + |
| 86 | + return str_, False, chars_count |
| 87 | + |
| 88 | + def lex_number(self): |
| 89 | + start_index = self.current_position |
| 90 | + chars_count = 0 |
| 91 | + num_string = "" |
| 92 | + |
| 93 | + # Check for number starting with 0 which is not a float |
| 94 | + if len(self.json_string[start_index:]) > 1: |
| 95 | + if self.json_string[start_index] == "0" and self.json_string[ |
| 96 | + start_index + 1 |
| 97 | + ] not in [".", ","]: |
| 98 | + return num_string, False, chars_count |
| 99 | + |
| 100 | + for char in self.json_string[start_index:]: |
| 101 | + if not char.isdigit() and char not in ["-", ".", "e", "E", "+"]: |
| 102 | + break |
| 103 | + num_string = num_string + char |
| 104 | + chars_count += 1 |
| 105 | + |
| 106 | + try: |
| 107 | + num = ( |
| 108 | + int(num_string) |
| 109 | + if "." not in num_string and "e" not in num_string.lower() |
| 110 | + else float(num_string) |
| 111 | + ) |
| 112 | + return num, True, chars_count |
| 113 | + except ValueError: |
| 114 | + return num_string, False, chars_count |
| 115 | + |
| 116 | + |
| 117 | +class JSONParser: |
| 118 | + def __init__(self, json_string): |
| 119 | + self.tokens = JSONLexer(json_string).tokenize() |
| 120 | + self.current_token = None |
| 121 | + self.current_index = 0 |
| 122 | + |
| 123 | + def parse(self): |
| 124 | + if self.tokens == 1: |
| 125 | + print(f"Error Parsing: Token: {self.current_token} Err: Not JSON") |
| 126 | + return 1 |
| 127 | + else: |
| 128 | + self.current_token = self.tokens[0] |
| 129 | + |
| 130 | + if self.current_token[0] not in ["LBRACE", "LBRACKET"]: |
| 131 | + print(f"Error Parsing: Token: {self.current_token} Err: Not JSON") |
| 132 | + return 1 |
| 133 | + else: |
| 134 | + result = self.parse_value() |
| 135 | + |
| 136 | + # Check if values exist after closing brace or bracket |
| 137 | + if self.current_index + 1 != len(self.tokens): |
| 138 | + return 1 |
| 139 | + else: |
| 140 | + return result |
| 141 | + |
| 142 | + def parse_value(self): |
| 143 | + if self.current_token[0] == "STRING": |
| 144 | + return 0 |
| 145 | + elif self.current_token[0] == "FALSE": |
| 146 | + return 0 |
| 147 | + elif self.current_token[0] == "TRUE": |
| 148 | + return 0 |
| 149 | + elif self.current_token[0] == "NULL": |
| 150 | + return 0 |
| 151 | + elif self.current_token[0] == "NUMBER": |
| 152 | + return 0 |
| 153 | + elif self.current_token[0] == "LBRACE": |
| 154 | + return self.parse_object() |
| 155 | + elif self.current_token[0] == "LBRACKET": |
| 156 | + return self.parse_array() |
| 157 | + else: |
| 158 | + return 1 |
| 159 | + |
| 160 | + def parse_object(self): |
| 161 | + self.next_token() # Move past the brace token |
| 162 | + |
| 163 | + while self.current_token != None and self.current_token[0] != "RBRACE": |
| 164 | + if self.current_token[0] == "STRING": |
| 165 | + self.next_token() |
| 166 | + else: |
| 167 | + print(f"Error Parsing: Token: {self.current_token} Err: Key Error") |
| 168 | + return 1 |
| 169 | + |
| 170 | + if self.current_token[0] == "COLON": |
| 171 | + self.next_token() |
| 172 | + else: |
| 173 | + print(f"Error Parsing: Token: {self.current_token} Err: Colon Error") |
| 174 | + return 1 |
| 175 | + |
| 176 | + if self.parse_value() == 0: |
| 177 | + self.next_token() |
| 178 | + else: |
| 179 | + print(f"Error Parsing: Token: {self.current_token} Err: Value Error") |
| 180 | + return 1 |
| 181 | + |
| 182 | + res = self.parse_comma() |
| 183 | + if res == 1: |
| 184 | + print(f"Error Parsing: Token: {self.current_token} Err: Extra Comma") |
| 185 | + return 1 |
| 186 | + |
| 187 | + if ( |
| 188 | + "COMMA" not in self.tokens[self.current_index - 1] |
| 189 | + and self.current_token != None |
| 190 | + ): |
| 191 | + return 0 |
| 192 | + else: |
| 193 | + print(f"Error Parsing: Token: {self.current_token} Err: Missing Values") |
| 194 | + return 1 |
| 195 | + |
| 196 | + def parse_array(self): |
| 197 | + self.next_token() # Move past the bracket token |
| 198 | + |
| 199 | + while self.current_token != None and self.current_token[0] != "RBRACKET": |
| 200 | + if self.parse_value() == 0: |
| 201 | + self.next_token() |
| 202 | + else: |
| 203 | + print(f"Error Parsing: Token: {self.current_token} Err: Value Error") |
| 204 | + return 1 |
| 205 | + |
| 206 | + res = self.parse_comma() |
| 207 | + if res == 1: |
| 208 | + print(f"Error Parsing: Token: {self.current_token} Err: Extra Comma") |
| 209 | + return 1 |
| 210 | + |
| 211 | + if ( |
| 212 | + "COMMA" not in self.tokens[self.current_index - 1] |
| 213 | + and self.current_token != None |
| 214 | + ): |
| 215 | + return 0 |
| 216 | + else: |
| 217 | + print(f"Error Parsing: Token: {self.current_token} Err: Missing Values") |
| 218 | + return 1 |
| 219 | + |
| 220 | + def parse_comma(self): |
| 221 | + if self.current_token != None: |
| 222 | + if self.current_token[0] == "COMMA": |
| 223 | + self.next_token() |
| 224 | + |
| 225 | + return 0 |
| 226 | + else: |
| 227 | + return 1 |
| 228 | + |
| 229 | + # Move to next token |
| 230 | + def next_token(self): |
| 231 | + self.current_index += 1 |
| 232 | + if self.current_index < len(self.tokens): |
| 233 | + self.current_token = self.tokens[self.current_index] |
| 234 | + else: |
| 235 | + self.current_token = None |
0 commit comments