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.

29 lines
534 B

  1. """
  2. Templates
  3. =========
  4. This example shows how to use Lark's templates to achieve cleaner grammars
  5. """"
  6. from lark import Lark
  7. grammar = r"""
  8. start: list | dict
  9. list: "[" _seperated{atom, ","} "]"
  10. dict: "{" _seperated{key_value, ","} "}"
  11. key_value: atom ":" atom
  12. _seperated{x, sep}: x (sep x)* // Define a sequence of 'x sep x sep x ...'
  13. atom: NUMBER | ESCAPED_STRING
  14. %import common (NUMBER, ESCAPED_STRING, WS)
  15. %ignore WS
  16. """
  17. parser = Lark(grammar)
  18. print(parser.parse('[1, "a", 2]'))
  19. print(parser.parse('{"a": 2, "b": 6}'))