This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

53 Zeilen
979 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 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. %declare _INDENT _DEDENT
  18. %ignore WS_INLINE
  19. _NL: /(\r?\n[\t ]*)+/
  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()