rparser.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. """
  2. MIT License
  3. Copyright (c) 2022 Texas Tech University
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. """
  21. This file is part of MonSter.
  22. Author:
  23. Jie Li, jie.li@ttu.edu
  24. """
  25. JSON_COMMA = ','
  26. JSON_COLON = ':'
  27. JSON_LEFTBRACKET = '['
  28. JSON_RIGHTBRACKET = ']'
  29. JSON_LEFTBRACE = '{'
  30. JSON_RIGHTBRACE = '}'
  31. JSON_QUOTE = '"'
  32. JSON_QUOTE = '"'
  33. JSON_WHITESPACE = [' ', '\t', '\b', '\n', '\r']
  34. JSON_SYNTAX = [JSON_COMMA, JSON_COLON, JSON_LEFTBRACKET, JSON_RIGHTBRACKET,
  35. JSON_LEFTBRACE, JSON_RIGHTBRACE]
  36. FALSE_LEN = len('false')
  37. TRUE_LEN = len('true')
  38. NULL_LEN = len('null')
  39. def lex_string(string):
  40. json_string = ''
  41. if string[0] == JSON_QUOTE:
  42. string = string[1:]
  43. else:
  44. return None, string
  45. for c in string:
  46. if c == JSON_QUOTE:
  47. return json_string, string[len(json_string)+1:]
  48. else:
  49. json_string += c
  50. # raise Exception('Expected end-of-string quote')
  51. return json_string, ''
  52. def lex_number(string):
  53. json_number = ''
  54. number_characters = [str(d) for d in range(0, 10)] + ['-', 'e', '.']
  55. for c in string:
  56. if c in number_characters:
  57. json_number += c
  58. else:
  59. break
  60. rest = string[len(json_number):]
  61. if not len(json_number):
  62. return None, string
  63. if '.' in json_number:
  64. return float(json_number), rest
  65. return int(json_number), rest
  66. def lex_bool(string):
  67. string_len = len(string)
  68. if string_len >= TRUE_LEN and \
  69. string[:TRUE_LEN] == 'true':
  70. return True, string[TRUE_LEN:]
  71. elif string_len >= FALSE_LEN and \
  72. string[:FALSE_LEN] == 'false':
  73. return False, string[FALSE_LEN:]
  74. return None, string
  75. def lex_null(string):
  76. string_len = len(string)
  77. if string_len >= NULL_LEN and \
  78. string[:NULL_LEN] == 'null':
  79. return True, string[NULL_LEN]
  80. return None, string
  81. def lex(string):
  82. tokens = []
  83. while len(string):
  84. json_string, string = lex_string(string)
  85. if json_string is not None:
  86. tokens.append(json_string)
  87. continue
  88. json_number, string = lex_number(string)
  89. if json_number is not None:
  90. tokens.append(json_number)
  91. continue
  92. json_bool, string = lex_bool(string)
  93. if json_bool is not None:
  94. tokens.append(json_bool)
  95. continue
  96. json_null, string = lex_null(string)
  97. if json_null is not None:
  98. tokens.append(None)
  99. continue
  100. if string[0] in JSON_WHITESPACE:
  101. string = string[1:]
  102. elif string[0] in JSON_SYNTAX:
  103. tokens.append(string[0])
  104. string = string[1:]
  105. else:
  106. raise Exception('Unexpected character: {}'.format(string[0]))
  107. # print(tokens)
  108. return tokens
  109. def parse_array(tokens):
  110. # print("PARSE ARRAY: ")
  111. # print(tokens)
  112. json_array = []
  113. if tokens:
  114. t = tokens[0]
  115. if t == JSON_RIGHTBRACKET:
  116. return json_array, tokens[1:]
  117. while True:
  118. json, tokens = parse(tokens)
  119. json_array.append(json)
  120. # print(f'Json array: {json_array}')
  121. if tokens:
  122. t = tokens[0]
  123. if t == JSON_RIGHTBRACKET:
  124. return json_array, tokens[1:]
  125. elif t != JSON_COMMA:
  126. raise Exception('Expected comma after object in array')
  127. else:
  128. tokens = tokens[1:]
  129. else:
  130. return json_array, None
  131. return None, None
  132. # raise Exception('Expected end-of-array bracket')
  133. def parse_object(tokens):
  134. # print("PARSE OBJECT: ")
  135. # print(tokens)
  136. json_object = {}
  137. if tokens:
  138. t = tokens[0]
  139. if t == JSON_RIGHTBRACE:
  140. return json_object, tokens[1:]
  141. while True:
  142. if tokens:
  143. json_key = tokens[0]
  144. # print(f'Json key: {json_key}')
  145. if type(json_key) is str:
  146. tokens = tokens[1:]
  147. else:
  148. raise Exception('Expected string key, got: {}'.format(json_key))
  149. if tokens:
  150. if tokens[0] != JSON_COLON:
  151. raise Exception('Expected colon after key in object, got: {}'.format(t))
  152. json_value, tokens = parse(tokens[1:])
  153. # print(f'Json value: {json_value}')
  154. json_object[json_key] = json_value
  155. # print(f'Json object: {json_object}')
  156. if tokens:
  157. t = tokens[0]
  158. if t == JSON_RIGHTBRACE:
  159. return json_object, tokens[1:]
  160. elif t != JSON_COMMA:
  161. raise Exception('Expected comma after pair in object, got: {}'.format(t))
  162. tokens = tokens[1:]
  163. else:
  164. return json_object, None
  165. return None, None
  166. # raise Exception('Expected end-of-object brace')
  167. def parse(tokens):
  168. if tokens:
  169. t = tokens[0]
  170. # print("PARSE: ")
  171. # print(tokens)
  172. if t == JSON_LEFTBRACKET:
  173. return parse_array(tokens[1:])
  174. elif t == JSON_LEFTBRACE:
  175. return parse_object(tokens[1:])
  176. else:
  177. return t, tokens[1:]
  178. else:
  179. return None, None
  180. def report_parser(string):
  181. tokens = lex(string)
  182. return parse(tokens)[0]