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.

54 lines
1003 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. %import common.CNAME -> NAME
  16. %import common.WS_INLINE
  17. %ignore WS_INLINE
  18. _NL: /(\r?\n[\t ]*)+/
  19. _INDENT: "<INDENT>"
  20. _DEDENT: "<DEDENT>"
  21. """
  22. class TreeIndenter(Indenter):
  23. NL_type = '_NL'
  24. OPEN_PAREN_types = []
  25. CLOSE_PAREN_types = []
  26. INDENT_type = '_INDENT'
  27. DEDENT_type = '_DEDENT'
  28. tab_len = 8
  29. parser = Lark(tree_grammar, parser='lalr', postlex=TreeIndenter())
  30. test_tree = """
  31. a
  32. b
  33. c
  34. d
  35. e
  36. f
  37. g
  38. """
  39. def test():
  40. print(parser.parse(test_tree).pretty())
  41. if __name__ == '__main__':
  42. test()