|
| 1 | +""" |
| 2 | +Provides location tracking in parser. |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import Callable, Optional |
| 6 | + |
| 7 | +import mathics_scanner |
| 8 | +from mathics_scanner.location import ( |
| 9 | + EVAL_METHODS, |
| 10 | + ContainerKind, |
| 11 | + SourceRange, |
| 12 | + SourceRange2, |
| 13 | +) |
| 14 | + |
| 15 | +from mathics.core.parser.ast import Node |
| 16 | + |
| 17 | + |
| 18 | +def track_location(func: Callable) -> Callable: |
| 19 | + """Python decorator for a parse method that adds location tracking |
| 20 | + on non-leaf-nodes when TRACK_LOCATION is set. Otherwise, we just |
| 21 | + run the parse method. |
| 22 | +
|
| 23 | + """ |
| 24 | + |
| 25 | + def wrapper(self, *args) -> Optional[Node]: |
| 26 | + if not mathics_scanner.location.TRACK_LOCATIONS: |
| 27 | + return func(self, *args) |
| 28 | + |
| 29 | + # Save location information |
| 30 | + # For expressions which make their way to |
| 31 | + # FunctionApplyRule, saving a position here is |
| 32 | + # extraneous because the FunctionApplyRule is |
| 33 | + # the position. But deal with this redundancy |
| 34 | + # after the dust settles, and we have experience |
| 35 | + # on what is desired. |
| 36 | + start_column = self.tokeniser.pos |
| 37 | + start_line = self.feeder.lineno |
| 38 | + parsed_node = func(self, *args) |
| 39 | + |
| 40 | + if parsed_node is not None and hasattr(parsed_node, "location"): |
| 41 | + if self.feeder.container_kind == ContainerKind.PYTHON: |
| 42 | + parsed_node.location = self.feeder.container |
| 43 | + if self.feeder.container not in EVAL_METHODS: |
| 44 | + EVAL_METHODS.add(parsed_node.location) |
| 45 | + else: |
| 46 | + end_pos = self.tokeniser.pos |
| 47 | + end_line = self.feeder.lineno |
| 48 | + if start_line == end_line: |
| 49 | + parsed_node.location = SourceRange2( |
| 50 | + start_line=start_line, |
| 51 | + start_pos=start_column, |
| 52 | + end_pos=end_pos, |
| 53 | + container=self.feeder.container_index, |
| 54 | + ) |
| 55 | + else: |
| 56 | + parsed_node.location = SourceRange( |
| 57 | + start_line=start_line, |
| 58 | + start_pos=start_column, |
| 59 | + end_line=end_line, |
| 60 | + end_pos=end_pos, |
| 61 | + container=self.feeder.container_index, |
| 62 | + ) |
| 63 | + |
| 64 | + return parsed_node |
| 65 | + |
| 66 | + return wrapper |
| 67 | + |
| 68 | + |
| 69 | +def track_token_location(func: Callable) -> Callable: |
| 70 | + """Python decorator for a parse method that adds location |
| 71 | + tracking to leaf-nodes, i.e. tokens. This happens though only TRACK_LOCATION is set. Otherwise, we just run the |
| 72 | + normal parse method. |
| 73 | +
|
| 74 | + """ |
| 75 | + |
| 76 | + def wrapper(self, token) -> Optional[Node]: |
| 77 | + if not mathics_scanner.location.TRACK_LOCATIONS: |
| 78 | + return func(self, token) |
| 79 | + |
| 80 | + # Save location information |
| 81 | + # For expressions which make their way to |
| 82 | + # FunctionApplyRule, saving a position here is |
| 83 | + # extraneous because the FunctionApplyRule is |
| 84 | + # the position. But deal with this redundancy |
| 85 | + # after the dust settles, and we have experience |
| 86 | + # on what is desired. |
| 87 | + start_column = token.pos |
| 88 | + start_line = self.feeder.lineno |
| 89 | + parsed_node = func(self, token) |
| 90 | + |
| 91 | + if ( |
| 92 | + self.feeder.container_kind == ContainerKind.PYTHON |
| 93 | + and self.feeder.container not in EVAL_METHODS |
| 94 | + ): |
| 95 | + location = self.feeder.container |
| 96 | + EVAL_METHODS.add(location) |
| 97 | + |
| 98 | + end_line = self.feeder.lineno |
| 99 | + if start_line == end_line: |
| 100 | + parsed_node.location = SourceRange2( |
| 101 | + start_line=start_line, |
| 102 | + start_pos=start_column, |
| 103 | + end_pos=start_column + len(token.text) - 1, |
| 104 | + container=self.feeder.container_index, |
| 105 | + ) |
| 106 | + else: |
| 107 | + parsed_node.location = SourceRange( |
| 108 | + start_line=start_line, |
| 109 | + start_pos=start_column, |
| 110 | + end_line=self.feeder.lineno, |
| 111 | + end_pos=start_column + len(token.text) - 1, |
| 112 | + container=self.feeder.container_index, |
| 113 | + ) |
| 114 | + return parsed_node |
| 115 | + |
| 116 | + return wrapper |
0 commit comments