Skip to content

Commit

Permalink
Merge pull request #1294 from lark-parser/issue1293
Browse files Browse the repository at this point in the history
propagate_positions: Bugfix + Better documentation
  • Loading branch information
erezsh committed Jul 3, 2023
2 parents 623f04d + c1af476 commit 679ba4a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lark/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class LarkOptions(Serialize):
transformer
Applies the transformer to every parse tree (equivalent to applying it after the parse, but faster)
propagate_positions
Propagates (line, column, end_line, end_column) attributes into all tree branches.
Propagates positional attributes into the 'meta' attribute of all tree branches.
Sets attributes: (line, column, end_line, end_column, start_pos, end_pos,
container_line, container_column, container_end_line, container_end_column)
Accepts ``False``, ``True``, or a callable, which will filter which nodes to ignore when propagating.
maybe_placeholders
When ``True``, the ``[]`` operator returns ``None`` when not matched.
Expand Down
2 changes: 2 additions & 0 deletions lark/parse_tree_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __call__(self, children):

res_meta.container_line = getattr(first_meta, 'container_line', first_meta.line)
res_meta.container_column = getattr(first_meta, 'container_column', first_meta.column)
res_meta.container_start_pos = getattr(first_meta, 'container_start_pos', first_meta.start_pos)

last_meta = self._pp_get_meta(reversed(children))
if last_meta is not None:
Expand All @@ -61,6 +62,7 @@ def __call__(self, children):

res_meta.container_end_line = getattr(last_meta, 'container_end_line', last_meta.end_line)
res_meta.container_end_column = getattr(last_meta, 'container_end_column', last_meta.end_column)
res_meta.container_end_pos = getattr(first_meta, 'container_end_pos', last_meta.end_pos)

return res

Expand Down
7 changes: 6 additions & 1 deletion lark/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ class Tree(Generic[_Leaf_T]):
data: The name of the rule or alias
children: List of matched sub-rules and terminals
meta: Line & Column numbers (if ``propagate_positions`` is enabled).
meta attributes: line, column, start_pos, end_line, end_column, end_pos
meta attributes: (line, column, end_line, end_column, start_pos, end_pos,
container_line, container_column, container_end_line, container_end_column)
container_* attributes consider all symbols, including those that have been inlined in the tree.
For example, in the rule 'a: _A B _C', the regular attributes will mark the start and end of B,
but the container_* attributes will also include _A and _C in the range. However, rules that
contain 'a' will consider it in full, including _A and _C for all attributes.
"""

data: str
Expand Down

0 comments on commit 679ba4a

Please sign in to comment.