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.

53 lines
1.4 KiB

  1. #
  2. # This example demonstrates an experimental feature: Text reconstruction
  3. # The Reconstructor takes a parse tree (already filtered from punctuation, of course),
  4. # and reconstructs it into correct text, that can be parsed correctly.
  5. # 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.
  6. #
  7. import json
  8. from lark import Lark
  9. from lark.reconstruct import Reconstructor
  10. from .json_parser import json_grammar
  11. test_json = '''
  12. {
  13. "empty_object" : {},
  14. "empty_array" : [],
  15. "booleans" : { "YES" : true, "NO" : false },
  16. "numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
  17. "strings" : [ "This", [ "And" , "That", "And a \\"b" ] ],
  18. "nothing" : null
  19. }
  20. '''
  21. def test_scanless():
  22. json_parser = Lark(json_grammar)
  23. tree = json_parser.parse(test_json)
  24. # print ('@@', tree.pretty())
  25. # for x in tree.find_data('true'):
  26. # x.data = 'false'
  27. # # x.children[0].value = '"HAHA"'
  28. new_json = Reconstructor(json_parser).reconstruct(tree)
  29. print (new_json)
  30. print (json.loads(new_json) == json.loads(test_json))
  31. def test_lalr():
  32. json_parser = Lark(json_grammar, parser='lalr')
  33. tree = json_parser.parse(test_json)
  34. new_json = Reconstructor(json_parser).reconstruct(tree)
  35. print (new_json)
  36. print (json.loads(new_json) == json.loads(test_json))
  37. test_scanless()
  38. test_lalr()