This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

53 wiersze
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()