Skip to content

Commit

Permalink
Simplify simple_space_serializer using tokens() method (#263)
Browse files Browse the repository at this point in the history
The new `tokens()` method of rule nodes makes it easier to access
their leaf node sequences. This update leverages the `tokens()`
method to simplify the implementation of `simple_space_serializer`.
  • Loading branch information
renatahodovan authored Dec 20, 2024
1 parent 6086a01 commit d6caa88
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions grammarinator/runtime/serializer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Copyright (c) 2017-2023 Renata Hodovan, Akos Kiss.
# Copyright (c) 2017-2024 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
# This file may not be copied, modified, or distributed except
# according to those terms.

from .rule import ParentRule


def simple_space_serializer(root):
"""
Expand All @@ -17,14 +15,4 @@ def simple_space_serializer(root):
:return: The serialized tree as string.
:rtype: str
"""

def _tokens():
stack = [root]
while stack:
node = stack.pop()
if isinstance(node, ParentRule):
stack.extend(reversed(node.children))
else:
yield node.src

return ' '.join(token for token in _tokens())
return ' '.join(root.tokens())

0 comments on commit d6caa88

Please sign in to comment.