This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

44 righe
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()