This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

47 righe
905 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 = """
  8. ?start: _NL* tree
  9. tree: /\w+/ _NL [_INDENT tree+ _DEDENT]
  10. NAME: /\w+/
  11. WS.ignore: /\s+/
  12. _NL.newline: /(\r?\n[\t ]*)+/
  13. """
  14. class TreeIndenter(Indenter):
  15. NL_type = '_NL'
  16. OPEN_PAREN_types = []
  17. CLOSE_PAREN_types = []
  18. INDENT_type = '_INDENT'
  19. DEDENT_type = '_DEDENT'
  20. tab_len = 8
  21. parser = Lark(tree_grammar, parser='lalr', postlex=TreeIndenter())
  22. test_tree = """
  23. a
  24. b
  25. c
  26. d
  27. e
  28. f
  29. g
  30. """
  31. def test():
  32. print parser.parse(test_tree).pretty()
  33. if __name__ == '__main__':
  34. test()