This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

92 líneas
2.5 KiB

  1. """
  2. Simple JSON Parser
  3. ==================
  4. The code is short and clear, and outperforms every other parser (that's written in Python).
  5. For an explanation, check out the JSON parser tutorial at /docs/json_tutorial.md
  6. """
  7. import sys
  8. from lark import Lark, Transformer, v_args
  9. json_grammar = r"""
  10. ?start: value
  11. ?value: object
  12. | array
  13. | string
  14. | SIGNED_NUMBER -> number
  15. | "true" -> true
  16. | "false" -> false
  17. | "null" -> null
  18. array : "[" [value ("," value)*] "]"
  19. object : "{" [pair ("," pair)*] "}"
  20. pair : string ":" value
  21. string : ESCAPED_STRING
  22. %import common.ESCAPED_STRING
  23. %import common.SIGNED_NUMBER
  24. %import common.WS
  25. %ignore WS
  26. """
  27. class TreeToJson(Transformer):
  28. @v_args(inline=True)
  29. def string(self, s):
  30. return s[1:-1].replace('\\"', '"')
  31. array = list
  32. pair = tuple
  33. object = dict
  34. number = v_args(inline=True)(float)
  35. null = lambda self, _: None
  36. true = lambda self, _: True
  37. false = lambda self, _: False
  38. ### Create the JSON parser with Lark, using the Earley algorithm
  39. # json_parser = Lark(json_grammar, parser='earley', lexer='standard')
  40. # def parse(x):
  41. # return TreeToJson().transform(json_parser.parse(x))
  42. ### Create the JSON parser with Lark, using the LALR algorithm
  43. json_parser = Lark(json_grammar, parser='lalr',
  44. # Using the standard lexer isn't required, and isn't usually recommended.
  45. # But, it's good enough for JSON, and it's slightly faster.
  46. lexer='standard',
  47. # Disabling propagate_positions and placeholders slightly improves speed
  48. propagate_positions=False,
  49. maybe_placeholders=False,
  50. # Using an internal transformer is faster and more memory efficient
  51. transformer=TreeToJson())
  52. parse = json_parser.parse
  53. def test():
  54. test_json = '''
  55. {
  56. "empty_object" : {},
  57. "empty_array" : [],
  58. "booleans" : { "YES" : true, "NO" : false },
  59. "numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
  60. "strings" : [ "This", [ "And" , "That", "And a \\"b" ] ],
  61. "nothing" : null
  62. }
  63. '''
  64. j = parse(test_json)
  65. print(j)
  66. import json
  67. assert j == json.loads(test_json)
  68. if __name__ == '__main__':
  69. # test()
  70. with open(sys.argv[1]) as f:
  71. print(parse(f.read()))