-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
toml.h
194 lines (164 loc) · 7.54 KB
/
toml.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Peppa PEG - Ultra lightweight PEG Parser in ANSI C.
*
* MIT License
*
* Copyright (c) 2021 Ju
*
* 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.
*
* Example: Write a TOML Parser using Peppa PEG.
*
* https://toml.io/en/v1.0.0
*
*/
# ifndef P4_LANG_TOML_H
# define P4_LANG_TOML_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "../peppapeg.h"
P4_Grammar* P4_CreateTomlGrammar() {
return P4_LoadGrammar(
"toml = expression (newline expression)*;\n"
"expression = ws (keyval / table)? ws comment?;\n"
/* Key-Value pairs */
"keyval = key keyval_sep val;\n"
"@lifted keyval_sep = ws \"=\" ws;\n"
/* Key */
"@lifted key = dotted_key / simple_key;\n"
"@lifted simple_key = quoted_key / unquoted_key;\n"
"@squashed unquoted_key = (ALPHA / DIGIT / \"-\" / \"_\"){1,};\n"
"@squashed quoted_key = basic_string / literal_string;\n"
"dotted_key = simple_key (dot_sep simple_key)+;\n"
"@lifted dot_sep = ws \".\" ws;\n"
/* Value */
"@lifted val = boolean / datetime / array / inline_table / string / float / integer;\n"
"@lifted string = ml_literal_string / ml_basic_string / basic_string / literal_string;\n"
/* Basic String */
"basic_string = \"\\\"\" basic_char* \"\\\"\";\n"
"@lifted basic_char = basic_unescaped / escaped;\n"
"@lifted basic_unescaped = wschar / \"!\" / [\\u0023-\\u005B] / [\\u005D-\\u007E] / non_ascii;\n"
"@scoped @squashed escaped = \"\\\\\" (\"\\\"\" / \"\\\\\" / \"b\" / \"f\" / \"n\" / \"r\" / \"t\" / \"u\" HEXDIG{4} / \"U\" HEXDIG{8});\n"
/* Multi-line Basic String */
"@squashed ml_basic_string = \"\\x22\\x22\\x22\" newline? ml_basic_body \"\\x22\\x22\\x22\";\n"
"ml_basic_body = mlb_content* (mlb_quotes mlb_content+)*;\n"
"mlb_content = mlb_char / newline / mlb_escaped_nl;\n"
"mlb_char = mlb_unescaped / escaped;\n"
"mlb_quotes = \"\\x22\";\n"
"mlb_unescaped = wschar / \"\\x21\" / [\\x23-\\x5B] / [\\x5D-\\x7E] / non_ascii;\n"
"mlb_escaped_nl = escaped ws newline *( wschar / newline );\n"
/* Literal String */
"literal_string = \"'\" literal_char* \"'\";\n"
"@lifted literal_char = \"\\t\" / [\\u0020-\\u0026] / [\\u0028-\\u007E] / non_ascii;\n"
/* Multi-line literal String */
"@squashed ml_literal_string = \"'''\" newline? ml_literal_body \"'''\";\n"
"ml_literal_body = mll_content* (mll_quotes mll_content+)*;\n"
"mll_content = mll_char / newline;\n"
"mll_char = \"\\x09\" / [\\x20-\\x26] / [\\x28-\\x7e] / non_ascii;\n"
"mll_quotes = \"''\" / \"'\";\n"
/* Boolean */
"boolean = \"true\" / \"false\";\n"
/* Integer */
"minus = \"-\";\n"
"plus = \"+\";\n"
"underscore = \"_\";\n"
"one_nine = [1-9];\n"
"zero_seven = [0-7];\n"
"zero_one = [0-1];\n"
"hex_prefix = i\"0x\";\n"
"oct_prefix = i\"0o\";\n"
"bin_prefix = i\"0b\";\n"
"dec_int = (minus / plus)? unsigned_dec_int;\n"
"unsigned_dec_int = \"0\" / one_nine (DIGIT / underscore DIGIT)*;\n"
"hex_int = hex_prefix HEXDIG (HEXDIG / underscore HEXDIG)*;\n"
"oct_int = oct_prefix zero_seven (zero_seven / underscore zero_seven)*;\n"
"bin_int = bin_prefix zero_one (zero_one / underscore zero_one)*;\n"
"@squashed integer = hex_int / oct_int / bin_int / dec_int;\n"
/* Float */
"@squashed float = special_float / dec_int (exp / frac exp?);"
"zero_prefixed_int = DIGIT (underscore DIGIT / DIGIT)*;\n"
"frac = \".\" zero_prefixed_int;\n"
"exp = i\"e\" (minus / plus)? zero_prefixed_int;\n"
"special_float = (minus / plus)? (inf / nan);\n"
"inf = \"inf\";\n"
"nan = \"nan\";\n"
/* Date and Time */
"@squashed date_fullyear = DIGIT{4};\n"
"@squashed date_month = \"0\" [1-9] / \"1\" [0-2];\n"
"@squashed date_mday = DIGIT{2};\n"
"time_delim = i\"t\" / \" \";\n"
"@squashed time_hour = DIGIT{2};\n"
"@squashed time_minute = DIGIT{2};\n"
"@squashed time_second = DIGIT{2};\n"
"@squashed time_secfrac = DIGIT+;\n"
"time_numoffset = (\"+\" / \"-\") time_hour \":\" time_minute;\n"
"time_offset = i\"z\" / time_numoffset;\n"
"partial_time = time_hour \":\" time_minute \":\" time_second (\".\" time_secfrac)?;\n"
"full_date = date_fullyear \"-\" date_month \"-\" date_mday;\n"
"full_time = partial_time time_offset;\n"
"offset_date_time = full_date time_delim full_time;\n"
"local_date_time = full_date time_delim partial_time;\n"
"local_date = full_date;\n"
"local_time = partial_time;\n"
"datetime = offset_date_time / local_date_time / local_date / local_time;\n"
/* Array */
"array = array_open array_ws array_values? array_ws array_close;\n"
"@lifted array_values = val (array_ws array_sep array_ws val)*;\n"
"@lifted array_open = \"[\";\n"
"@lifted array_close = \"]\";\n"
"@lifted array_sep = \",\";\n"
"@lifted @squashed array_ws = (wschar / comment? newline)*;\n"
/* Inline Table */
"inline_table = inline_table_open ws inline_table_values? ws inline_table_close;\n"
"@lifted inline_table_values = keyval (ws inline_table_sep ws keyval)*;\n"
"@lifted inline_table_open = \"{\";\n"
"@lifted inline_table_close = \"}\";\n"
"@lifted inline_table_sep = \",\";\n"
/* Table */
"@lifted table = array_table / std_table;\n"
/* Standard Table */
"std_table = std_table_open ws key ws std_table_close;\n"
"@lifted std_table_open = \"[\";\n"
"@lifted std_table_close = \"]\";\n"
/* Array Table */
"array_table = array_table_open ws key ws array_table_close;\n"
"@lifted array_table_open = \"[[\";\n"
"@lifted array_table_close = \"]]\";\n"
/* Comment */
"comment = comment_start comment_body;\n"
"comment_start = \"#\";\n"
"@squashed comment_body = non_eol*;\n"
"non_eol = \"\\t\" / [\\u0020-\\u007F] / non_ascii;\n"
"non_ascii = [\\u0080-\\uD7FF] / [\\uE000-\\U0010FFFF];\n"
/* Newline */
"newline = \"\\n\" / \"\\r\\n\";\n"
/* Whitespace */
"@lifted @squashed ws = wschar*;\n"
"wschar = \" \" / \"\\t\";\n"
"ALPHA = [a-z] / [A-Z];\n"
"DIGIT = [0-9];\n"
"HEXDIG = [a-f] / [A-F] / [0-9];\n"
);
}
#ifdef __cplusplus
}
#endif
# endif