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
|
from typing import List, cast, Optional
from more_itertools import peekable
from tabla import Token, LexToken
from parse.base import BaseParser
from errors import Error
from parse.type import ParseType
from parse.ident import ParseIdent
from parse.expr import ParseExpr, Expr
from astree.decl import DeclGlobal, DeclFunc, Decl
class ParseDecl:
def __init__(self, parser: BaseParser):
self.parser = parser
def decl_global(self) -> (DeclGlobal | Error):
# Tipo
_type = ParseType(self.parser)._type()
if type(_type) is Error:
return _type
# Identificador
ident = ParseIdent(self.parser).ident()
if type(ident) is Error:
return ident
# =
init: Optional[Expr] = None
eq = self.parser._try(Token.EQUAL)
if eq:
# Expresión
init = ParseExpr(self.parser).expr()
if type(init) is Error:
return init
return DeclGlobal(ident = ident,
_type = _type,
init = init)
def decl_func(self) -> (DeclFunc | Error):
# funcion
tok = self.parser.want(Token.FUNCTION)
if type(tok) is Error:
return tok
# Tipo
_type = ParseType(self.parser)._type()
if type(_type) is Error:
return _type
# Identificador
ident = ParseIdent(self.parser).ident()
if type(ident) is Error:
return ident
# Prototipo
proto = ParseType(self.parser).prototype(_type)
if type(proto) is Error:
return proto
init = ParseExpr(self.parser).compound_expr()
if type(init) is Error:
return init
return DeclFunc(ident = ident,
prototype = proto,
body = init)
# Parses a declaration.
def decl(self) -> (Decl | Error):
toks = [Token.BOOLEAN, Token.CHAR, Token.INT, Token.STRING, Token.VOID, Token.FUNCTION]
_next = self.parser.want(*toks)
decl: Optional[Decl] = None
if type(_next) is Error:
return _next
elif _next.tipo is Token.FUNCTION:
self.parser.unlex()
decl = self.decl_func()
else:
self.parser.unlex()
decl = self.decl_global()
if type(decl) is Error:
return decl
# ;
semicolon = self.parser.want(Token.SEMICOLON)
if type(semicolon) is Error:
return semicolon
return decl
# Parses the declarations for a sub-parser.
def decls(self) -> (List[Decl] | Error):
decls: List[Decl] = []
while not self.parser.peek(Token.EOF):
decl = self.decl()
if type(decl) is Error:
return decl
decls.append(decl)
return decls
|