This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

47 Zeilen
1.2 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, Transformer
  15. parser = Lark(r"""
  16. start: _nl? section+
  17. section: "[" name "]" _nl item+
  18. item: name "=" value _nl
  19. name: /[a-zA-Z_]/ /\w/*
  20. value: /./+
  21. _nl: (_CR? _LF)+
  22. _CR : /\r/
  23. _LF : /\n/
  24. """, parser="earley_nolex")
  25. class RestoreTokens(Transformer):
  26. value = ''.join
  27. name = ''.join
  28. def test():
  29. sample_conf = """
  30. [bla]
  31. a=Hello
  32. this="that",4
  33. """
  34. r = parser.parse(sample_conf)
  35. print(RestoreTokens().transform(r).pretty())
  36. if __name__ == '__main__':
  37. test()