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 lines
932 B

  1. #
  2. # This example demonstrates lex-less 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. from lark import Lark, Transformer
  11. parser = Lark(r"""
  12. start: _nl? section+
  13. section: "[" name "]" _nl item+
  14. item: name "=" value _nl
  15. name: /[a-zA-Z_]/ /\w/*
  16. value: /./+
  17. _nl: (_CR? _LF)+
  18. _CR : /\r/
  19. _LF : /\n/
  20. """, parser="earley_nolex")
  21. class RestoreTokens(Transformer):
  22. value = ''.join
  23. name = ''.join
  24. def test():
  25. sample_conf = """
  26. [bla]
  27. a=Hello
  28. this="that",4
  29. """
  30. r = parser.parse(sample_conf)
  31. print(RestoreTokens().transform(r).pretty())
  32. if __name__ == '__main__':
  33. test()