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.
 
 

51 line
1.4 KiB

  1. """
  2. Reconstruct a JSON
  3. ==================
  4. Demonstrates the experimental text-reconstruction feature
  5. The Reconstructor takes a parse tree (already filtered from punctuation, of course),
  6. and reconstructs it into correct text, that can be parsed correctly.
  7. It can be useful for creating "hooks" to alter data before handing it to other parsers. You can also use it to generate samples from scratch.
  8. """
  9. import json
  10. from lark import Lark
  11. from lark.reconstruct import Reconstructor
  12. from _json_parser import json_grammar
  13. test_json = '''
  14. {
  15. "empty_object" : {},
  16. "empty_array" : [],
  17. "booleans" : { "YES" : true, "NO" : false },
  18. "numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
  19. "strings" : [ "This", [ "And" , "That", "And a \\"b" ] ],
  20. "nothing" : null
  21. }
  22. '''
  23. def test_earley():
  24. json_parser = Lark(json_grammar, maybe_placeholders=False)
  25. tree = json_parser.parse(test_json)
  26. new_json = Reconstructor(json_parser).reconstruct(tree)
  27. print (new_json)
  28. print (json.loads(new_json) == json.loads(test_json))
  29. def test_lalr():
  30. json_parser = Lark(json_grammar, parser='lalr', maybe_placeholders=False)
  31. tree = json_parser.parse(test_json)
  32. new_json = Reconstructor(json_parser).reconstruct(tree)
  33. print (new_json)
  34. print (json.loads(new_json) == json.loads(test_json))
  35. test_earley()
  36. test_lalr()