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.

43 rivejä
1.0 KiB

  1. #
  2. # This example demonstrates 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. # Another approach is to use the contextual lexer with LALR. It is less powerful than Earley,
  10. # but it can handle some ambiguity when lexing and it's much faster.
  11. # See examples/conf_lalr.py for an example of that approach.
  12. #
  13. from lark import Lark
  14. parser = Lark(r"""
  15. start: _NL? section+
  16. section: "[" NAME "]" _NL item+
  17. item: NAME "=" VALUE? _NL
  18. VALUE: /./+
  19. %import common.CNAME -> NAME
  20. %import common.NEWLINE -> _NL
  21. %import common.WS_INLINE
  22. %ignore WS_INLINE
  23. """, parser="earley")
  24. def test():
  25. sample_conf = """
  26. [bla]
  27. a=Hello
  28. this="that",4
  29. empty=
  30. """
  31. r = parser.parse(sample_conf)
  32. print (r.pretty())
  33. if __name__ == '__main__':
  34. test()