Begin refactoring how the ParsingContext is used by the Parser.
Chris Pressey
4 years ago
32 | 32 | def __str__(self): |
33 | 33 | return "Symbols: {}\nStatics: {}\nTypedefs: {}\nConsts: {}".format(self.symbols, self.statics, self.typedefs, self.consts) |
34 | 34 | |
35 | def lookup(self, name): | |
35 | def fetch(self, name): | |
36 | 36 | if name in self.statics: |
37 | 37 | return self.statics[name].model |
38 | 38 | if name in self.symbols: |
50 | 50 | self.scanner.syntax_error(msg) |
51 | 51 | |
52 | 52 | def lookup(self, name): |
53 | model = self.context.lookup(name) | |
53 | model = self.context.fetch(name) | |
54 | 54 | if model is None: |
55 | 55 | self.syntax_error('Undefined symbol "{}"'.format(name)) |
56 | 56 | return model |
57 | ||
58 | def declare(self, name, symentry): | |
59 | if self.context.fetch(name): | |
60 | self.syntax_error('Symbol "%s" already declared' % name) | |
61 | self.context.symbols[name] = symentry | |
57 | 62 | |
58 | 63 | # --- grammar productions |
59 | 64 | |
69 | 74 | typenames.extend(self.context.typedefs.keys()) |
70 | 75 | while self.scanner.on(*typenames): |
71 | 76 | defn = self.defn() |
72 | name = defn.name | |
73 | if self.context.lookup(name): | |
74 | self.syntax_error('Symbol "%s" already declared' % name) | |
75 | self.context.symbols[name] = SymEntry(defn, defn.location) | |
77 | self.declare(defn.name, SymEntry(defn, defn.location)) | |
76 | 78 | defns.append(defn) |
77 | 79 | while self.scanner.on('define', 'routine'): |
78 | 80 | if self.scanner.consume('define'): |
82 | 84 | else: |
83 | 85 | routine = self.legacy_routine() |
84 | 86 | name = routine.name |
85 | if self.context.lookup(name): | |
86 | self.syntax_error('Symbol "%s" already declared' % name) | |
87 | self.context.symbols[name] = SymEntry(routine, routine.location) | |
87 | self.declare(name, SymEntry(routine, routine.location)) | |
88 | 88 | routines.append(routine) |
89 | 89 | self.scanner.check_type('EOF') |
90 | 90 | |
301 | 301 | c = {} |
302 | 302 | for defn in statics: |
303 | 303 | name = defn.name |
304 | if self.context.lookup(name): | |
304 | if self.context.fetch(name): | |
305 | 305 | self.syntax_error('Symbol "%s" already declared' % name) |
306 | 306 | c[name] = SymEntry(defn, defn.location) |
307 | 307 | return c |
333 | 333 | elif forward: |
334 | 334 | name = self.scanner.token |
335 | 335 | self.scanner.scan() |
336 | loc = self.context.lookup(name) | |
336 | loc = self.context.fetch(name) | |
337 | 337 | if loc is not None: |
338 | 338 | return loc |
339 | 339 | else: |