-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrl_json_parser.y
115 lines (94 loc) · 3.33 KB
/
grl_json_parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
%{
//
// grl_json_parser.y
// GRLJSON
//
// Created by Måns Severin on 2010-03-09.
// Copyright 2010 Graphiclife. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#include <stdio.h>
#include <stdlib.h>
#include "grl_json_parser.h"
int grl_json_lex ( void *lvalp, void *grl_scanner );
void grl_json_error ( struct grl_json_parse_context *context, const char *error );
#define grl_json_scanner context->scanner
%}
%pure-parser
%name-prefix="grl_json_"
%defines
%parse-param { struct grl_json_parse_context *context }
%lex-param { void *grl_json_scanner }
%union
{
long long integer_value;
double float_value;
unsigned char boolean_value;
struct grl_json_string *string_value;
struct grl_json_value *json_value;
struct grl_json_pair *pair_value;
struct grl_json_value_list *value_list_value;
struct grl_json_pair_list *pair_list_value;
}
%token NULLX COMMA COLON
%token L_BRACE R_BRACE L_BRACKET R_BRACKET
%token <integer_value> INTEGER
%token <float_value> FLOAT
%token <string_value> STRING
%token <boolean_value> BOOLEAN
%type <json_value> value array object
%type <pair_value> pair
%type <value_list_value> elements
%type <pair_list_value> members
%%
start: value { context->result = $1; }
;
object:
L_BRACE R_BRACE { $$ = grl_json_object_value( context, NULL ); }
| L_BRACE members R_BRACE { $$ = grl_json_object_value( context, grl_json_pair_list_reverse( $2 ) ); }
;
array:
L_BRACKET R_BRACKET { $$ = grl_json_array_value( context, NULL ); }
| L_BRACKET elements R_BRACKET { $$ = grl_json_array_value( context, grl_json_value_list_reverse( $2 ) ); }
;
members:
pair { $$ = grl_json_pair_list( context, $1, NULL ); }
| members COMMA pair { $$ = grl_json_pair_list( context, $3, $1 ); }
;
pair:
STRING COLON value { $$ = grl_json_pair( context, $1, $3 ); }
;
elements:
value { $$ = grl_json_value_list( context, $1, NULL ); }
| elements COMMA value { $$ = grl_json_value_list( context, $3, $1 ); }
;
value:
INTEGER { $$ = grl_json_integer_value( context, $1 ); }
| FLOAT { $$ = grl_json_float_value( context, $1 ); }
| STRING { $$ = grl_json_string_value( context, $1 ); }
| BOOLEAN { $$ = grl_json_boolean_value( context, $1 ); }
| NULLX { $$ = grl_json_null_value( context ); }
| object { $$ = $1; }
| array { $$ = $1; }
;
%%