-
Notifications
You must be signed in to change notification settings - Fork 0
/
pj.py
328 lines (260 loc) · 11.3 KB
/
pj.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#TODO: pogledati može li se upotrebljivo ukrasti pprinter od astpretty
import enum, types, collections, contextlib
def identifikator(znak): return znak.isalnum() or znak == '_'
def pogledaj(mem, token):
try: return mem[token.sadržaj]
except KeyError: raise token.nedeklaracija() from None
# TODO: bolji API: Greška(poruka, pozicija ili token ili AST...)
# ali ostaviti i lex.greška() i parser.greška() for convenience
class Greška(Exception): """Baza za sve greške vezane uz poziciju u kodu."""
class LeksičkaGreška(Greška): """Greška nastala prilikom leksičke analize."""
class SintaksnaGreška(Greška): """Greška nastala prilikom sintaksne analize."""
class SemantičkaGreška(Greška):"""Greška nastala prilikom semantičke analize."""
class GreškaIzvođenja(Greška): """Greška nastala prilikom izvođenja."""
@contextlib.contextmanager
def očekivano(tip_greške):
try: yield
except tip_greške as e: print(type(e).__name__, e, sep=': ')
else: raise Greška('{} nije dignuta'.format(tip_greške.__name__))
class Tokenizer:
def __init__(self, string):
self.pročitani, self.buffer, self.stream = [], None, iter(string)
self.početak = self.i, self.j = 1, 0
@property
def pozicija(self): return self.i, self.j
@property
def sadržaj(self): return ''.join(self.pročitani)
def čitaj(self):
"""Čita sljedeći znak iz buffera ili stringa."""
znak = self.buffer or next(self.stream, '')
self.pročitani.append(znak)
self.buffer = None
if znak == '\n':
self.gornji_j = self.j
self.i += 1
self.j = 0
else: self.j += 1
return znak
def vrati(self):
"""Poništava čitanje zadnjeg pročitanog znaka."""
assert not self.buffer
self.buffer = self.pročitani.pop()
if self.j: self.j -= 1
else:
self.j = self.gornji_j
self.i -= 1
del self.gornji_j
def pogledaj(self):
"""'Viri' u sljedeći znak, 'bez' čitanja."""
znak = self.čitaj()
self.vrati()
return znak
def slijedi(self, znak):
"""Čita sljedeći znak ako i samo ako je jednak očekivanom."""
return self.čitaj() == znak or self.vrati()
def zvijezda(self, uvjet):
"""Čita Kleene* (nula ili više) znakova koji zadovoljavaju uvjet."""
while uvjet(self.čitaj()): pass
self.vrati()
def plus(self, uvjet):
"""Čita Kleene+ (jedan ili više) znakova koji zadovoljavaju uvjet."""
prvi = self.čitaj()
if not uvjet(prvi): raise self.greška('očekivan ' + uvjet.__name__)
self.zvijezda(uvjet)
def pročitaj(self, znak):
"""Čita zadani znak, ili prijavljuje leksičku grešku."""
if znak != self.čitaj():
raise self.greška('očekivano {!r}'.format(znak))
def pročitaj_do(self, znak, uključivo=True):
"""Čita sve znakove do zadanog znaka."""
self.zvijezda(lambda z: z and z != znak)
if self.pogledaj() != znak:
raise self.greška('{!r} nije pronađen'.format(znak))
if uključivo: self.pročitaj(znak)
def greška(self, info=''):
"""Konstruira leksičku grešku koja se treba prijaviti s raise."""
if self.buffer: self.čitaj()
poruka = 'Redak {}, stupac {}: '.format(*self.pozicija)
zadnji = self.pročitani.pop()
opis = 'znak {!r}'.format(zadnji) if zadnji else 'kraj ulaza'
poruka += 'neočekivan {}'.format(opis)
if info: poruka += ' (' + info + ')'
return LeksičkaGreška(poruka)
def token(self, tip):
"""Odašilje token."""
t = Token(tip, self.sadržaj)
t.početak = self.početak
t.kraj = self.pozicija
self.zanemari()
return t
def literal(self, odakle, case=True):
"""Odašilje doslovni token ako ga nađe, ili vrstu zadanu argumentom."""
t = self.sadržaj if case else self.sadržaj.casefold()
def p(odakle):
if isinstance(odakle, enum.EnumMeta):
for e in odakle:
if e.value == t or getattr(e.value, 'literal', None) == t:
return e
nađen = p(odakle) or p(type(odakle))
if nađen: return self.token(nađen)
elif isinstance(type(odakle), enum.EnumMeta): return self.token(odakle)
else: raise self.greška()
def zanemari(self):
"""Resetira pročitano."""
self.pročitani.clear()
self.početak = self.pozicija
class E(enum.Enum): # Everywhere
"""Često korišteni tipovi tokena, neovisno o konkretnom jeziku."""
KRAJ = None # End
GREŠKA = '\x00' # Error
PRAZNO = ' ' # Empty
VIŠAK = '' # Extra
class Token(collections.namedtuple('TokenTuple', 'tip sadržaj')):
# TODO: razmisliti je li Token unhashable, ili hashiran samo kao TokenTuple
"""Klasa koja predstavlja tokene."""
def __new__(cls, tip, sadržaj=None):
if sadržaj is None:
sadržaj = tip.value
if isinstance(sadržaj, type): sadržaj = sadržaj.literal
if isinstance(tip.value, type): cls = tip.value
return super().__new__(cls, tip, sadržaj)
def __init__(self, *args):
# if self.tip is E.GREŠKA: prijavi grešku na početku tokena, ne na kraju
self.uspoređeni = set()
self.razriješen = False
def __repr__(self):
ime, sadržaj = self.tip.name, self.sadržaj
if sadržaj not in {ime, ''}: ime += repr(self.sadržaj)
return ime
def __xor__(self, tip):
"""Vraća sebe (istina) ako je zadanog tipa, inače None (laž)."""
if not isinstance(tip, set): tip = {tip}
self.uspoređeni |= tip
if self.tip in tip:
self.razriješen = True
return self
def je(self, *tipovi):
self.uspoređeni.update(tipovi)
if self.tip in tipovi:
self.razriješen = True
return self
def neočekivan(self, info=''):
"""Konstruira sintaksnu grešku: neočekivan tip tokena."""
poruka = 'Redak {}, stupac {}: neočekivan token {!r}'
if info: poruka += ' (' + info + ')'
očekivano = ' ili '.join(t.name for t in self.uspoređeni if t!=self.tip)
if očekivano: poruka += '\nOčekivano: ' + očekivano
i, j = getattr(self, 'početak', '??')
return SintaksnaGreška(poruka.format(i, j, self))
if False:
def redeklaracija(self, prvi=None):
"""Konstruira semantičku grešku redeklaracije."""
i, j = getattr(self, 'početak', '??')
poruka = 'Redak {}, stupac {}: redeklaracija {!r}'.format(i, j, self)
if prvi is not None:
info = 'Prva deklaracija je bila ovdje: redak {}, stupac {}'
poruka += '\n' + info.format(*prvi.početak)
return SemantičkaGreška(poruka)
def nedeklaracija(self, dodatak=''):
"""Konstruira semantičku grešku nedeklariranog simbola."""
i, j = getattr(self, 'početak', '??')
poruka = 'Redak {}, stupac {}: nedeklarirano {!r}'.format(i, j, self)
if dodatak: poruka += ' ' + dodatak.join('()')
return SemantičkaGreška(poruka)
def problem(self, info):
"""Konstruira grešku izvođenja."""
poruka = 'Redak {}, stupac {}: {!r}: {}'
i, j = getattr(self, 'početak', '??')
return GreškaIzvođenja(poruka.format(i, j, self, info))
def krivi_tip(self, *tipovi):
"""Konstruira semantičku grešku."""
poruka = 'Redak {}, stupac {}: {!r}: tipovi ne odgovaraju: '
poruka += ', '.join(map(str, tipovi))
i, j = getattr(self, 'početak', '??')
return SemantičkaGreška(poruka.format(i, j, self))
@classmethod
def kraj(cls):
"""Oznaka kraja niza tokena."""
t = cls(E.KRAJ, '')
t.početak = t.kraj = 'zadnji', 0
t.razriješen = False
return t
class Parser:
def __init__(self, tokeni):
self.buffer, self.stream = None, iter(tokeni)
self.zadnji, self.KRAJ = None, Token.kraj()
def čitaj(self):
"""Čitanje sljedećeg tokena iz buffera ili inicijalnog niza."""
token = self.buffer
if token is None:
if self.zadnji is not None and not self.zadnji.razriješen:
raise self.greška()
token = next(self.stream, self.KRAJ)
self.buffer = None
self.zadnji = token
return token
def vrati(self):
"""Poništavanje čitanja zadnjeg pročitanog tokena."""
assert not self.buffer, 'Buffer je pun'
self.buffer = self.zadnji
pogledaj = Tokenizer.pogledaj
def pročitaj(self, *tipovi):
"""Čita jedan od dozvoljenih simbola, ili javlja sintaksnu grešku."""
token = self.čitaj()
if token ^ set(tipovi): return token
self.vrati()
raise self.greška()
def slijedi(self, *tipovi):
"""Čita sljedeći token samo ako je odgovarajućeg tipa."""
return self.zadnji if self.čitaj().je(*tipovi) else self.vrati()
def __rshift__(self, tip):
"""Čita sljedeći token samo ako je odgovarajućeg tipa."""
return self.zadnji if self.čitaj() ^ tip else self.vrati()
def vidi(self, *tipovi): return self.pogledaj().je(*tipovi)
def __ge__(self, tip): return self.pogledaj() ^ tip
def greška(self): return self.zadnji.neočekivan()
@classmethod
def parsiraj(klasa, tokeni):
parser = klasa(tokeni)
try: rezultat = parser.start()
except NoneInAST: raise parser.greška() # BUG: nije dovoljno precizno!
else:
parser.pročitaj(E.KRAJ)
return rezultat
elementarni = str, int, bool
class NoneInAST(Exception): pass
def AST_adapt(component):
if isinstance(component, (Token, AST0, elementarni)): return component
elif isinstance(component, (tuple, list)):
if None in component: raise NoneInAST('Neobuhvaćen slučaj!')
return ListaAST(component)
elif isinstance(component, dict):
if None in component or None in component.values():
raise NoneInAST('Neobuhvaćen slučaj!')
return RječnikAST(component.items())
elif component is None: raise NoneInAST('Neobuhvaćen slučaj!')
else: raise TypeError('Nepoznat tip komponente {}'.format(type(component)))
class AST0:
"""Bazna klasa za sva apstraktna sintaksna stabla."""
def __xor__(self, tip):
return isinstance(tip, type) and isinstance(self, tip)
def je(self, *tipovi): return isinstance(self, tipovi)
class Atom(Token, AST0): """Atomarni token kao apstraktno stablo."""
class ListaAST(tuple):
def __repr__(self): return repr(list(self))
class RječnikAST(tuple):
def __repr__(self): return repr(dict(self))
class Nenavedeno(AST0):
"""Atribut koji nije naveden."""
def __bool__(self): return False
def __repr__(self): return type(self).__name__.join('<>')
nenavedeno = Nenavedeno()
def AST(atributi):
AST2 = collections.namedtuple('AST2', atributi)
# AST2.__new__.__defaults__ = tuple(nenavedeno for field in AST2._fields)
class AST1(AST2, AST0):
def __new__(cls, *args, **kw):
new_args = [AST_adapt(arg) for arg in args]
new_kw = {k: AST_adapt(v) for k, v in kw.items()}
return super().__new__(cls, *new_args, **new_kw)
return AST1