File: parser.y

package info (click to toggle)
ruby-tomlrb 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136 kB
  • sloc: ruby: 1,090; yacc: 229; makefile: 4
file content (230 lines) | stat: -rw-r--r-- 6,682 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
class Tomlrb::GeneratedParser
token IDENTIFIER STRING_MULTI STRING_BASIC STRING_LITERAL_MULTI STRING_LITERAL DATETIME LOCAL_TIME INTEGER NON_DEC_INTEGER FLOAT FLOAT_KEYWORD BOOLEAN NEWLINE EOS
rule
  expressions
    | expressions expression
    | expressions EOS
    ;
  expression
    : table
    | assignment
    | inline_table
    | NEWLINE
    ;
  table
    : table_start table_identifier table_end newlines
    | table_start table_identifier table_end EOS
    | table_start table_end newlines
    | table_start table_end EOS
    ;
  table_start
    : '[' '[' { @handler.start_(:array_of_tables) }
    | '[' { @handler.start_(:table) }
    ;
    ;
  table_end
    : ']' ']' { array = @handler.end_(:array_of_tables); @handler.set_context(array, is_array_of_tables: true) }
    | ']' { array = @handler.end_(:table); @handler.set_context(array) }
    ;
  table_identifier
    : table_identifier '.' table_identifier_component { @handler.push(val[2]) }
    | table_identifier '.' FLOAT { val[2].split('.').each { |k| @handler.push(k) } }
    | FLOAT {
      keys = val[0].split('.')
      @handler.start_(:table)
      keys.each { |key| @handler.push(key) }
    }
    | table_identifier_component { @handler.push(val[0]) }
    ;
  table_identifier_component
    : IDENTIFIER
    | STRING_BASIC { result = StringUtils.replace_escaped_chars(val[0]) }
    | STRING_LITERAL
    | INTEGER
    | NON_DEC_INTEGER
    | FLOAT_KEYWORD
    | BOOLEAN
    | DATETIME { result = val[0][0] }
    | LOCAL_TIME { result = val[0][0] }
    ;
  inline_table
    : inline_table_start inline_table_end
    | inline_table_start inline_continued inline_table_end
    ;
  inline_table_start
    : '{' { @handler.start_(:inline) }
    ;
  inline_table_end
    : '}' {
      array = @handler.end_(:inline)
      @handler.push_inline(array)
    }
    ;
  inline_continued
    : inline_assignment
    | inline_assignment inline_next
    ;
  inline_next
    : ',' inline_continued
    ;
  inline_assignment
    : inline_assignment_key '=' value {
      keys = @handler.end_(:inline_keys)
      @handler.push(keys)
    }
    ;
  inline_assignment_key
    : inline_assignment_key '.' assignment_key_component { 
      @handler.push(val[2]) 
    }
    | inline_assignment_key '.' FLOAT { val[2].split('.').each { |k| @handler.push(k) } }
    | FLOAT {
      keys = val[0].split('.')
      @handler.start_(:inline_keys)
      keys.each { |key| @handler.push(key) }
    }
    | assignment_key_component { 
      @handler.start_(:inline_keys) 
      @handler.push(val[0]) 
    }
    ;
  assignment
    : assignment_key '=' value EOS {
      keys = @handler.end_(:keys)
      value = keys.pop
      @handler.validate_value(value)
      @handler.push(value)
      @handler.assign(keys)
    }
    | assignment_key '=' value NEWLINE {
      keys = @handler.end_(:keys)
      value = keys.pop
      @handler.validate_value(value)
      @handler.push(value)
      @handler.assign(keys)
    }
    ;
  assignment_key
    : assignment_key '.' assignment_key_component { @handler.push(val[2]) }
    | assignment_key '.' FLOAT { val[2].split('.').each { |k| @handler.push(k) } }
    | FLOAT {
      keys = val[0].split('.')
      @handler.start_(:keys)
      keys.each { |key| @handler.push(key) }
    }
    | assignment_key_component { @handler.start_(:keys); @handler.push(val[0]) }
    ;
  assignment_key_component
    : IDENTIFIER
    | STRING_BASIC { result = StringUtils.replace_escaped_chars(val[0]) }
    | STRING_LITERAL
    | INTEGER
    | NON_DEC_INTEGER
    | FLOAT_KEYWORD
    | BOOLEAN
    | DATETIME { result = val[0][0] }
    | LOCAL_TIME { result = val[0][0] }
    ;
  array
    : start_array array_first_value array_values comma end_array
    | start_array array_first_value array_values end_array
    | start_array array_first_value comma end_array
    | start_array array_first_value end_array
    | start_array end_array
    ;
  array_first_value
    : newlines non_nil_value
    | non_nil_value
    ;
  array_values
    : array_values array_value
    | array_value
    ;
  array_value
    : comma newlines non_nil_value
    | comma non_nil_value
    ;
  start_array
    : '[' { @handler.start_(:array) }
    ;
  end_array
    : newlines ']' { array = @handler.end_(:array); @handler.push(array.compact) }
    | ']' { array = @handler.end_(:array); @handler.push(array.compact) }
    ;
  comma
    : newlines ','
    | ','
    ;
  newlines
    : newlines NEWLINE
    | NEWLINE
    ;
  value
    : scalar { @handler.push(val[0]) }
    | array
    | inline_table
    ;
  non_nil_value
    : non_nil_scalar { @handler.push(val[0]) }
    | array
    | inline_table
    ;
  scalar
    : string
    | literal
    ;
  non_nil_scalar
    : string
    | non_nil_literal
    ;
  literal
    | non_nil_literal
    ;
  non_nil_literal
    : FLOAT { result = val[0].to_f }
    | FLOAT_KEYWORD {
      v = val[0]
      result = if v.end_with?('nan')
                 Float::NAN
               else
                 (v[0] == '-' ? -1 : 1) * Float::INFINITY
               end
    }
    | INTEGER { result = val[0].to_i }
    | NON_DEC_INTEGER {
      base = case val[0][1]
             when 'x' then 16
             when 'o' then 8
             when 'b' then 2
             end
      result = val[0].to_i(base)
    }
    | BOOLEAN { result = val[0] == 'true' ? true : false }
    | DATETIME {
      _str, year, month, day, hour, min, sec, offset = val[0]
      result = if offset.nil?
                 if hour.nil?
                   LocalDate.new(year, month, day)
                 else
                   LocalDateTime.new(year, month, day, hour, min || 0, sec.to_f)
                 end
               else
                 # Patch for 24:00:00 which Ruby parses
                 if hour.to_i == 24 && min.to_i == 0 && sec.to_i == 0
                   hour = (hour.to_i + 1).to_s
                 end

                 time = Time.new(year, month, day, hour || 0, min || 0, sec.to_f, offset)
                 # Should be out of parser.y?
                 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
                 time
               end
    }
    | LOCAL_TIME { result = LocalTime.new(*val[0][1..-1]) }
    ;
  string
    : STRING_MULTI { result = StringUtils.replace_escaped_chars(StringUtils.multiline_replacements(val[0])) }
    | STRING_BASIC { result = StringUtils.replace_escaped_chars(val[0]) }
    | STRING_LITERAL_MULTI { result = StringUtils.strip_spaces(val[0]) }
    | STRING_LITERAL { result = val[0] }
    ;