This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
946 B

  1. """This example demonstrates usage of the Indenter class.
  2. Since indentation is context-sensitive, a postlex stage is introduced to manufacture INDENT/DEDENT tokens.
  3. It is crucial for the indenter that the NL_type matches the spaces (and tabs) after the newline.
  4. """
  5. from lark.lark import Lark
  6. from lark.indenter import Indenter
  7. tree_grammar = r"""
  8. ?start: _NL* tree
  9. tree: NAME _NL [_INDENT tree+ _DEDENT]
  10. NAME: /\w+/
  11. WS.ignore: /\s+/
  12. _NL: /(\r?\n[\t ]*)+/
  13. _INDENT: "<INDENT>"
  14. _DEDENT: "<DEDENT>"
  15. """
  16. class TreeIndenter(Indenter):
  17. NL_type = '_NL'
  18. OPEN_PAREN_types = []
  19. CLOSE_PAREN_types = []
  20. INDENT_type = '_INDENT'
  21. DEDENT_type = '_DEDENT'
  22. tab_len = 8
  23. parser = Lark(tree_grammar, parser='lalr', postlex=TreeIndenter())
  24. test_tree = """
  25. a
  26. b
  27. c
  28. d
  29. e
  30. f
  31. g
  32. """
  33. def test():
  34. print(parser.parse(test_tree).pretty())
  35. if __name__ == '__main__':
  36. test()