summaryrefslogtreecommitdiff
path: root/tool/lrama/lib/lrama/grammar/symbols/resolver.rb
blob: 1788ed63fabbc582e86c94bc684a55088fd10f63 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
module Lrama
  class Grammar
    class Symbols
      class Resolver
        attr_reader :terms, :nterms

        def initialize
          @terms = []
          @nterms = []
        end

        def symbols
          @symbols ||= (@terms + @nterms)
        end

        def sort_by_number!
          symbols.sort_by!(&:number)
        end

        def add_term(id:, alias_name: nil, tag: nil, token_id: nil, replace: false)
          if token_id && (sym = find_symbol_by_token_id(token_id))
            if replace
              sym.id = id
              sym.alias_name = alias_name
              sym.tag = tag
            end

            return sym
          end

          if (sym = find_symbol_by_id(id))
            return sym
          end

          @symbols = nil
          term = Symbol.new(
            id: id, alias_name: alias_name, number: nil, tag: tag,
            term: true, token_id: token_id, nullable: false
          )
          @terms << term
          term
        end

        def add_nterm(id:, alias_name: nil, tag: nil)
          return if find_symbol_by_id(id)

          @symbols = nil
          nterm = Symbol.new(
            id: id, alias_name: alias_name, number: nil, tag: tag,
            term: false, token_id: nil, nullable: nil,
          )
          @nterms << nterm
          nterm
        end

        def find_symbol_by_s_value(s_value)
          symbols.find { |s| s.id.s_value == s_value }
        end

        def find_symbol_by_s_value!(s_value)
          find_symbol_by_s_value(s_value) || (raise "Symbol not found. value: `#{s_value}`")
        end

        def find_symbol_by_id(id)
          symbols.find do |s|
            s.id == id || s.alias_name == id.s_value
          end
        end

        def find_symbol_by_id!(id)
          find_symbol_by_id(id) || (raise "Symbol not found. #{id}")
        end

        def find_symbol_by_token_id(token_id)
          symbols.find {|s| s.token_id == token_id }
        end

        def find_symbol_by_number!(number)
          sym = symbols[number]

          raise "Symbol not found. number: `#{number}`" unless sym
          raise "[BUG] Symbol number mismatch. #{number}, #{sym}" if sym.number != number

          sym
        end

        def fill_symbol_number
          # YYEMPTY = -2
          # YYEOF   =  0
          # YYerror =  1
          # YYUNDEF =  2
          @number = 3
          fill_terms_number
          fill_nterms_number
        end

        def fill_nterm_type(types)
          types.each do |type|
            nterm = find_nterm_by_id!(type.id)
            nterm.tag = type.tag
          end
        end

        def fill_printer(printers)
          symbols.each do |sym|
            printers.each do |printer|
              printer.ident_or_tags.each do |ident_or_tag|
                case ident_or_tag
                when Lrama::Lexer::Token::Ident
                  sym.printer = printer if sym.id == ident_or_tag
                when Lrama::Lexer::Token::Tag
                  sym.printer = printer if sym.tag == ident_or_tag
                else
                  raise "Unknown token type. #{printer}"
                end
              end
            end
          end
        end

        def fill_destructor(destructors)
          symbols.each do |sym|
            destructors.each do |destructor|
              destructor.ident_or_tags.each do |ident_or_tag|
                case ident_or_tag
                when Lrama::Lexer::Token::Ident
                  sym.destructor = destructor if sym.id == ident_or_tag
                when Lrama::Lexer::Token::Tag
                  sym.destructor = destructor if sym.tag == ident_or_tag
                else
                  raise "Unknown token type. #{destructor}"
                end
              end
            end
          end
        end

        def fill_error_token(error_tokens)
          symbols.each do |sym|
            error_tokens.each do |token|
              token.ident_or_tags.each do |ident_or_tag|
                case ident_or_tag
                when Lrama::Lexer::Token::Ident
                  sym.error_token = token if sym.id == ident_or_tag
                when Lrama::Lexer::Token::Tag
                  sym.error_token = token if sym.tag == ident_or_tag
                else
                  raise "Unknown token type. #{token}"
                end
              end
            end
          end
        end

        def token_to_symbol(token)
          case token
          when Lrama::Lexer::Token
            find_symbol_by_id!(token)
          else
            raise "Unknown class: #{token}"
          end
        end

        def validate!
          validate_number_uniqueness!
          validate_alias_name_uniqueness!
        end

        private

        def find_nterm_by_id!(id)
          @nterms.find do |s|
            s.id == id
          end || (raise "Symbol not found. #{id}")
        end

        def fill_terms_number
          # Character literal in grammar file has
          # token id corresponding to ASCII code by default,
          # so start token_id from 256.
          token_id = 256

          @terms.each do |sym|
            while used_numbers[@number] do
              @number += 1
            end

            if sym.number.nil?
              sym.number = @number
              used_numbers[@number] = true
              @number += 1
            end

            # If id is Token::Char, it uses ASCII code
            if sym.token_id.nil?
              if sym.id.is_a?(Lrama::Lexer::Token::Char)
                # Ignore ' on the both sides
                case sym.id.s_value[1..-2]
                when "\\b"
                  sym.token_id = 8
                when "\\f"
                  sym.token_id = 12
                when "\\n"
                  sym.token_id = 10
                when "\\r"
                  sym.token_id = 13
                when "\\t"
                  sym.token_id = 9
                when "\\v"
                  sym.token_id = 11
                when "\""
                  sym.token_id = 34
                when "'"
                  sym.token_id = 39
                when "\\\\"
                  sym.token_id = 92
                when /\A\\(\d+)\z/
                  unless (id = Integer($1, 8)).nil?
                    sym.token_id = id
                  else
                    raise "Unknown Char s_value #{sym}"
                  end
                when /\A(.)\z/
                  unless (id = $1&.bytes&.first).nil?
                    sym.token_id = id
                  else
                    raise "Unknown Char s_value #{sym}"
                  end
                else
                  raise "Unknown Char s_value #{sym}"
                end
              else
                sym.token_id = token_id
                token_id += 1
              end
            end
          end
        end

        def fill_nterms_number
          token_id = 0

          @nterms.each do |sym|
            while used_numbers[@number] do
              @number += 1
            end

            if sym.number.nil?
              sym.number = @number
              used_numbers[@number] = true
              @number += 1
            end

            if sym.token_id.nil?
              sym.token_id = token_id
              token_id += 1
            end
          end
        end

        def used_numbers
          return @used_numbers if defined?(@used_numbers)

          @used_numbers = {}
          symbols.map(&:number).each do |n|
            @used_numbers[n] = true
          end
          @used_numbers
        end

        def validate_number_uniqueness!
          invalid = symbols.group_by(&:number).select do |number, syms|
            syms.count > 1
          end

          return if invalid.empty?

          raise "Symbol number is duplicated. #{invalid}"
        end

        def validate_alias_name_uniqueness!
          invalid = symbols.select(&:alias_name).group_by(&:alias_name).select do |alias_name, syms|
            syms.count > 1
          end

          return if invalid.empty?

          raise "Symbol alias name is duplicated. #{invalid}"
        end
      end
    end
  end
end