testing lib with json
This commit is contained in:
		
							
								
								
									
										3
									
								
								other/json/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								other/json/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					# Json parser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Ce projet est basé sur le tutoriel de lark et me permet de prendre la main avec cet outil
 | 
				
			||||||
							
								
								
									
										19
									
								
								other/json/json.lark
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								other/json/json.lark
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					?value: dict
 | 
				
			||||||
 | 
						 | list
 | 
				
			||||||
 | 
						 | string
 | 
				
			||||||
 | 
						 | SIGNED_NUMBER	-> number
 | 
				
			||||||
 | 
						 | "true"			-> true
 | 
				
			||||||
 | 
						 | "false" 			-> false
 | 
				
			||||||
 | 
						 | "null"			-> null 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					list: "[" [value ("," value)*] "]"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					dict: "{" [pair ("," pair)*] "}"
 | 
				
			||||||
 | 
					pair: string ":" value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					string: ESCAPED_STRING
 | 
				
			||||||
 | 
					%import common.ESCAPED_STRING
 | 
				
			||||||
 | 
					%import common.SIGNED_NUMBER
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%import common.WS
 | 
				
			||||||
 | 
					%ignore WS
 | 
				
			||||||
							
								
								
									
										29
									
								
								other/json/json.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								other/json/json.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					import lark
 | 
				
			||||||
 | 
					import pprint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class JsonTransformer(lark.Transformer):
 | 
				
			||||||
 | 
					    def string(self, el):
 | 
				
			||||||
 | 
					        (el,) = el
 | 
				
			||||||
 | 
					        return el[1:-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def number(self, el):
 | 
				
			||||||
 | 
					        (el, ) = el
 | 
				
			||||||
 | 
					        return float(el)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    list = list
 | 
				
			||||||
 | 
					    dict = dict
 | 
				
			||||||
 | 
					    pair = tuple
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    null = lambda self, _: None
 | 
				
			||||||
 | 
					    true = lambda self, _: True
 | 
				
			||||||
 | 
					    false = lambda self, _: False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					json = '{"key": ["item0", "item1", 3.14, true]}'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					with open("json.lark") as f:
 | 
				
			||||||
 | 
					    json_parser = lark.Lark(f, start="value", parser="lalr", transformer=JsonTransformer())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					t = json_parser.parse(json)
 | 
				
			||||||
 | 
					print(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Reference in New Issue
	
	Block a user