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.

35 lines
969 B

  1. #
  2. # This example demonstrates error handling using a parsing puppet in LALR
  3. #
  4. # When the parser encounters an UnexpectedToken exception, it creates a
  5. # parsing puppet with the current parse-state, and lets you control how
  6. # to proceed step-by-step. When you've achieved the correct parse-state,
  7. # you can resume the run by returning True.
  8. #
  9. from lark import UnexpectedToken, Token
  10. from .json_parser import json_parser
  11. def ignore_errors(e):
  12. if e.token.type == 'COMMA':
  13. # Skip comma
  14. return True
  15. elif e.token.type == 'SIGNED_NUMBER':
  16. # Try to feed a comma and retry the number
  17. e.puppet.feed_token(Token('COMMA', ','))
  18. e.puppet.feed_token(e.token)
  19. return True
  20. # Unhandled error. Will stop parse and raise exception
  21. return False
  22. def main():
  23. s = "[0 1, 2,, 3,,, 4, 5 6 ]"
  24. res = json_parser.parse(s, on_error=ignore_errors)
  25. print(res) # prints [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
  26. main()