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.

53 lines
956 B

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