This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1 KiB

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