This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

43 строки
1.1 KiB

  1. #
  2. # This example demonstrates scanless parsing using the earley_nolex frontend
  3. #
  4. # Using a lexer for configuration files is tricky, because values don't
  5. # have to be surrounded by delimiters.
  6. # In this example with skip lexing and let the Earley parser resolve the ambiguity.
  7. #
  8. # Future versions of lark will make it easier to write these kinds of grammars.
  9. #
  10. # Another approach is to use the contextual lexer. It is less powerful than the scanless approach,
  11. # but it can handle some ambiguity in lexing and it's much faster since it uses LALR(1).
  12. # See examples/conf.py for an example of that approach.
  13. #
  14. from lark import Lark
  15. parser = Lark(r"""
  16. start: _NL? section+
  17. section: "[" NAME "]" _NL item+
  18. item: NAME "=" VALUE _NL
  19. VALUE: /./*
  20. %import common.CNAME -> NAME
  21. %import common.NEWLINE -> _NL
  22. %import common.WS_INLINE
  23. %ignore WS_INLINE
  24. """, lexer=None)
  25. def test():
  26. sample_conf = """
  27. [bla]
  28. a=Hello
  29. this="that",4
  30. """
  31. r = parser.parse(sample_conf)
  32. print (r.pretty())
  33. if __name__ == '__main__':
  34. test()