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.

56 righe
1.0 KiB

  1. """
  2. Parsing Indentation
  3. ===================
  4. A demonstration of parsing indentation (“whitespace significant” language)
  5. and the usage of the Indenter class.
  6. Since indentation is context-sensitive, a postlex stage is introduced to
  7. manufacture INDENT/DEDENT tokens.
  8. It is crucial for the indenter that the NL_type matches
  9. the spaces (and tabs) after the newline.
  10. """
  11. from lark import Lark
  12. from lark.indenter import Indenter
  13. tree_grammar = r"""
  14. ?start: _NL* tree
  15. tree: NAME _NL [_INDENT tree+ _DEDENT]
  16. %import common.CNAME -> NAME
  17. %import common.WS_INLINE
  18. %declare _INDENT _DEDENT
  19. %ignore WS_INLINE
  20. _NL: /(\r?\n[\t ]*)+/
  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()