Debian Codegraph @

lib/tomlrb/parser.y

raw
1 class Tomlrb::GeneratedParser
2 token IDENTIFIER STRING_MULTI STRING_BASIC STRING_LITERAL_MULTI STRING_LITERAL DATETIME LOCAL_TIME INTEGER NON_DEC_INTEGER FLOAT FLOAT_KEYWORD BOOLEAN NEWLINE EOS
3 rule
4 expressions
5 | expressions expression
6 | expressions EOS
7 ;
8 expression
9 : table
10 | assignment
11 | inline_table
12 | NEWLINE
13 ;
14 table
15 : table_start table_identifier table_end newlines
16 | table_start table_identifier table_end EOS
17 | table_start table_end newlines
18 | table_start table_end EOS
19 ;
20 table_start
21 : '[' '[' { @handler.start_(:array_of_tables) }
22 | '[' { @handler.start_(:table) }
23 ;
24 ;
25 table_end
26 : ']' ']' { array = @handler.end_(:array_of_tables); @handler.set_context(array, is_array_of_tables: true) }
27 | ']' { array = @handler.end_(:table); @handler.set_context(array) }
28 ;
29 table_identifier
30 : table_identifier '.' table_identifier_component { @handler.push(val[2]) }
31 | table_identifier '.' FLOAT { val[2].split('.').each { |k| @handler.push(k) } }
32 | FLOAT {
33 keys = val[0].split('.')
34 @handler.start_(:table)
35 keys.each { |key| @handler.push(key) }
36 }
37 | table_identifier_component { @handler.push(val[0]) }
38 ;
39 table_identifier_component
40 : IDENTIFIER
41 | STRING_BASIC { result = StringUtils.replace_escaped_chars(val[0]) }
42 | STRING_LITERAL
43 | INTEGER
44 | NON_DEC_INTEGER
45 | FLOAT_KEYWORD
46 | BOOLEAN
47 | DATETIME { result = val[0][0] }
48 | LOCAL_TIME { result = val[0][0] }
49 ;
50 inline_table
51 : inline_table_start inline_table_end
52 | inline_table_start inline_continued inline_table_end
53 ;
54 inline_table_start
55 : '{' { @handler.start_(:inline) }
56 ;
57 inline_table_end
58 : '}' {
59 array = @handler.end_(:inline)
60 @handler.push_inline(array)
61 }
62 ;
63 inline_continued
64 : inline_assignment
65 | inline_assignment inline_next
66 ;
67 inline_next
68 : ',' inline_continued
69 ;
70 inline_assignment
71 : inline_assignment_key '=' value {
72 keys = @handler.end_(:inline_keys)
73 @handler.push(keys)
74 }
75 ;
76 inline_assignment_key
77 : inline_assignment_key '.' assignment_key_component {
78 @handler.push(val[2])
79 }
80 | inline_assignment_key '.' FLOAT { val[2].split('.').each { |k| @handler.push(k) } }
81 | FLOAT {
82 keys = val[0].split('.')
83 @handler.start_(:inline_keys)
84 keys.each { |key| @handler.push(key) }
85 }
86 | assignment_key_component {
87 @handler.start_(:inline_keys)
88 @handler.push(val[0])
89 }
90 ;
91 assignment
92 : assignment_key '=' value EOS {
93 keys = @handler.end_(:keys)
94 value = keys.pop
95 @handler.validate_value(value)
96 @handler.push(value)
97 @handler.assign(keys)
98 }
99 | assignment_key '=' value NEWLINE {
100 keys = @handler.end_(:keys)
101 value = keys.pop
102 @handler.validate_value(value)
103 @handler.push(value)
104 @handler.assign(keys)
105 }
106 ;
107 assignment_key
108 : assignment_key '.' assignment_key_component { @handler.push(val[2]) }
109 | assignment_key '.' FLOAT { val[2].split('.').each { |k| @handler.push(k) } }
110 | FLOAT {
111 keys = val[0].split('.')
112 @handler.start_(:keys)
113 keys.each { |key| @handler.push(key) }
114 }
115 | assignment_key_component { @handler.start_(:keys); @handler.push(val[0]) }
116 ;
117 assignment_key_component
118 : IDENTIFIER
119 | STRING_BASIC { result = StringUtils.replace_escaped_chars(val[0]) }
120 | STRING_LITERAL
121 | INTEGER
122 | NON_DEC_INTEGER
123 | FLOAT_KEYWORD
124 | BOOLEAN
125 | DATETIME { result = val[0][0] }
126 | LOCAL_TIME { result = val[0][0] }
127 ;
128 array
129 : start_array array_first_value array_values comma end_array
130 | start_array array_first_value array_values end_array
131 | start_array array_first_value comma end_array
132 | start_array array_first_value end_array
133 | start_array end_array
134 ;
135 array_first_value
136 : newlines non_nil_value
137 | non_nil_value
138 ;
139 array_values
140 : array_values array_value
141 | array_value
142 ;
143 array_value
144 : comma newlines non_nil_value
145 | comma non_nil_value
146 ;
147 start_array
148 : '[' { @handler.start_(:array) }
149 ;
150 end_array
151 : newlines ']' { array = @handler.end_(:array); @handler.push(array.compact) }
152 | ']' { array = @handler.end_(:array); @handler.push(array.compact) }
153 ;
154 comma
155 : newlines ','
156 | ','
157 ;
158 newlines
159 : newlines NEWLINE
160 | NEWLINE
161 ;
162 value
163 : scalar { @handler.push(val[0]) }
164 | array
165 | inline_table
166 ;
167 non_nil_value
168 : non_nil_scalar { @handler.push(val[0]) }
169 | array
170 | inline_table
171 ;
172 scalar
173 : string
174 | literal
175 ;
176 non_nil_scalar
177 : string
178 | non_nil_literal
179 ;
180 literal
181 | non_nil_literal
182 ;
183 non_nil_literal
184 : FLOAT { result = val[0].to_f }
185 | FLOAT_KEYWORD {
186 v = val[0]
187 result = if v.end_with?('nan')
188 Float::NAN
189 else
190 (v[0] == '-' ? -1 : 1) * Float::INFINITY
191 end
192 }
193 | INTEGER { result = val[0].to_i }
194 | NON_DEC_INTEGER {
195 base = case val[0][1]
196 when 'x' then 16
197 when 'o' then 8
198 when 'b' then 2
199 end
200 result = val[0].to_i(base)
201 }
202 | BOOLEAN { result = val[0] == 'true' ? true : false }
203 | DATETIME {
204 _str, year, month, day, hour, min, sec, offset = val[0]
205 result = if offset.nil?
206 if hour.nil?
207 LocalDate.new(year, month, day)
208 else
209 LocalDateTime.new(year, month, day, hour, min || 0, sec.to_f)
210 end
211 else
212 # Patch for 24:00:00 which Ruby parses
213 if hour.to_i == 24 && min.to_i == 0 && sec.to_i == 0
214 hour = (hour.to_i + 1).to_s
215 end
216
217 time = Time.new(year, month, day, hour || 0, min || 0, sec.to_f, offset)
218 # Should be out of parser.y?
219 raise ArgumentError, "Invalid Offset Date-Time: #{year}-#{month}-#{day}T#{hour}:#{min}:#{sec}#{offset}" unless min.to_i == time.min && hour.to_i == time.hour && day.to_i == time.day && month.to_i == time.month && year.to_i == time.year
220 time
221 end
222 }
223 | LOCAL_TIME { result = LocalTime.new(*val[0][1..-1]) }
224 ;
225 string
226 : STRING_MULTI { result = StringUtils.replace_escaped_chars(StringUtils.multiline_replacements(val[0])) }
227 | STRING_BASIC { result = StringUtils.replace_escaped_chars(val[0]) }
228 | STRING_LITERAL_MULTI { result = StringUtils.strip_spaces(val[0]) }
229 | STRING_LITERAL { result = val[0] }
230 ;