Skip to content

Commit 68987f0

Browse files
committed
WIP - add location decorator
1 parent 82618f0 commit 68987f0

3 files changed

Lines changed: 14 additions & 30 deletions

File tree

mathics/builtin/trace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ class TrackLocations(Predefined):
520520
summary_text = "track source-text locations in evaluation"
521521

522522
def evaluate(self, evaluation: Evaluation) -> Symbol:
523-
print(mathics_scanner.position.MATHICS3_PATHS)
524-
return from_bool(mathics_scanner.position.TRACK_LOCATIONS)
523+
print(mathics_scanner.location.MATHICS3_PATHS)
524+
return from_bool(mathics_scanner.location.TRACK_LOCATIONS)
525525

526526
def eval_set(self, value, evaluation):
527527
"""Set[$TrackLocations, value_]"""

mathics/core/expression.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,9 @@ def rest_range(indices):
12561256
head, *elements, elements_properties=self.elements_properties
12571257
)
12581258

1259+
if hasattr(self, "location"):
1260+
new.location = self.location
1261+
12591262
# Step 3: Now, process the attributes of head
12601263
# If there are sequence, flatten them if the attributes allow it.
12611264
if (

mathics/core/parser/parser.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
String,
3232
Symbol,
3333
)
34+
from mathics.core.parser.location import track_location
3435
from mathics.core.parser.operators import (
3536
all_operators,
3637
binary_operators,
@@ -98,7 +99,7 @@ def __init__(self):
9899
"DifferentialD",
99100
]
100101
)
101-
self.loctation = None
102+
self.location = None
102103

103104
def backtrack(self, pos):
104105
"""
@@ -169,36 +170,12 @@ def parse(self, feeder) -> Optional[Node]:
169170
self.bracket_depth = 0
170171
self.box_depth = 0
171172

173+
# Advance past white space and comments to assist location tracking.
172174
self.tokeniser._skip_blank()
173-
start_pos = self.tokeniser.pos
174-
start_line = feeder.lineno
175175

176-
parsed_node = self.parse_e()
177-
if parsed_node is None:
178-
return None
179-
180-
# Save location information
181-
# For expressions which make their way to
182-
# FunctionApplyRule, saving a position here is
183-
# extraneous because the FunctionApplyRule an get
184-
# the position. But deal with this redundancy
185-
# after the dust settles, and we have experience
186-
# on what is desired.
187-
if self.feeder.container_kind == ContainerKind.PYTHON:
188-
self.location = feeder.container
189-
else:
190-
end_pos = self.tokeniser.pos
191-
end_line = feeder.lineno
192-
self.location = SourceRange(
193-
start_line=start_line,
194-
start_pos=start_pos,
195-
end_line=end_line,
196-
end_pos=end_pos,
197-
container=feeder.container_index,
198-
)
199-
200-
return parsed_node
176+
return self.parse_e()
201177

178+
@track_location
202179
def parse_e(self) -> Optional[Node]:
203180
"""
204181
Parse the single top-level or "start" expression.
@@ -214,6 +191,7 @@ def parse_e(self) -> Optional[Node]:
214191
else:
215192
return None
216193

194+
@track_location
217195
def parse_binary_operator(
218196
self, expr1, token: Token, expr1_precedence: int
219197
) -> Optional[Node]:
@@ -385,6 +363,7 @@ def parse_box_operator(
385363

386364
return result
387365

366+
@track_location
388367
def parse_comparison(
389368
self, expr1, token: Token, expr1_precedence: int
390369
) -> Optional[Node]:
@@ -440,6 +419,7 @@ def parse_comparison(
440419
expr1 = Node(tag, expr1, expr2).flatten()
441420
return expr1
442421

422+
@track_location
443423
def parse_expr(self, precedence: int) -> Optional[Node]:
444424
"""
445425
Parse an expression returning an AST Node tree for this.
@@ -533,6 +513,7 @@ def parse_expr(self, precedence: int) -> Optional[Node]:
533513
result = new_result
534514
return result
535515

516+
@track_location
536517
def parse_p(self):
537518
"""Parse a "p_"-tagged expression.
538519
"p_" tags include prefix operators, left-bracketed expressions

0 commit comments

Comments
 (0)