Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
164 lines (120 loc) · 4.5 KB
/
Copy pathparser.py
File metadata and controls
164 lines (120 loc) · 4.5 KB
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
"""
AST Node definitions and Parser for mathematical expressions.
Implements a Recursive Descent parser.
"""
from dataclasses import dataclass
from typing import List, Union
from expression_parser.tokenizer import Token, TokenType, TokenizerError, Tokenizer
class ParseError(Exception):
def __init__(self, message: str, position: int):
super().__init__(f"Parse error at position {position}: {message}")
self.position = position
# AST Node Definitions
@dataclass
class ASTNode:
pass
@dataclass
class NumberNode(ASTNode):
value: Union[int, float]
@dataclass
class VariableNode(ASTNode):
name: str
@dataclass
class UnaryOpNode(ASTNode):
operator: str
operand: ASTNode
@dataclass
class BinaryOpNode(ASTNode):
left: ASTNode
operator: str
right: ASTNode
@dataclass
class FunctionCallNode(ASTNode):
name: str
args: List[ASTNode]
class Parser:
def __init__(self, tokens: List[Token]):
self.tokens = tokens
self.pos = 0
def _current_token(self) -> Token:
if self.pos < len(self.tokens):
return self.tokens[self.pos]
return self.tokens[-1]
def _consume(self, expected_type: TokenType) -> Token:
tok = self._current_token()
if tok.type != expected_type:
raise ParseError(
f"Expected token type '{expected_type.name}', found '{tok.type.name}' ('{tok.value}')",
tok.position
)
self.pos += 1
return tok
def parse(self) -> ASTNode:
if self._current_token().type == TokenType.EOF:
raise ParseError("Empty expression", 0)
node = self._expr()
if self._current_token().type != TokenType.EOF:
tok = self._current_token()
raise ParseError(f"Unexpected token '{tok.value}' after full parse", tok.position)
return node
def _expr(self) -> ASTNode:
"""expr -> term ((PLUS | MINUS) term)*"""
node = self._term()
while self._current_token().type in (TokenType.PLUS, TokenType.MINUS):
tok = self._current_token()
self.pos += 1
right = self._term()
node = BinaryOpNode(left=node, operator=tok.value, right=right)
return node
def _term(self) -> ASTNode:
"""term -> factor ((MULTIPLY | DIVIDE | MODULO) factor)*"""
node = self._factor()
while self._current_token().type in (TokenType.MULTIPLY, TokenType.DIVIDE, TokenType.MODULO):
tok = self._current_token()
self.pos += 1
right = self._factor()
node = BinaryOpNode(left=node, operator=tok.value, right=right)
return node
def _factor(self) -> ASTNode:
"""factor -> unary ((POWER) factor)?"""
node = self._unary()
if self._current_token().type == TokenType.POWER:
tok = self._current_token()
self.pos += 1
right = self._factor() # Right associative
node = BinaryOpNode(left=node, operator=tok.value, right=right)
return node
def _unary(self) -> ASTNode:
"""unary -> (PLUS | MINUS) unary | primary"""
tok = self._current_token()
if tok.type in (TokenType.PLUS, TokenType.MINUS):
self.pos += 1
operand = self._unary()
return UnaryOpNode(operator=tok.value, operand=operand)
return self._primary()
def _primary(self) -> ASTNode:
"""primary -> NUMBER | IDENTIFIER ( '(' args ')' )? | '(' expr ')' """
tok = self._current_token()
if tok.type == TokenType.NUMBER:
self.pos += 1
return NumberNode(value=tok.value)
if tok.type == TokenType.IDENTIFIER:
name = tok.value
self.pos += 1
if self._current_token().type == TokenType.LPAREN:
self.pos += 1
args = []
if self._current_token().type != TokenType.RPAREN:
args.append(self._expr())
while self._current_token().type == TokenType.COMMA:
self.pos += 1
args.append(self._expr())
self._consume(TokenType.RPAREN)
return FunctionCallNode(name=name, args=args)
return VariableNode(name=name)
if tok.type == TokenType.LPAREN:
self.pos += 1
node = self._expr()
self._consume(TokenType.RPAREN)
return node
raise ParseError(f"Unexpected token '{tok.value}'", tok.position)