Class: Hivemind::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/hivemind/syntax.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar) ⇒ Syntax

Returns a new instance of Syntax.



109
110
111
# File 'lib/hivemind/syntax.rb', line 109

def initialize(grammar)
  @grammar_source = grammar
end

Class Method Details

.generate_syntax(bidirectional_grammar) ⇒ Object



105
106
107
# File 'lib/hivemind/syntax.rb', line 105

def self.generate_syntax(bidirectional_grammar)
  new(bidirectional_grammar).generate
end

.load_rules(grammar) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/hivemind/syntax.rb', line 230

def self.load_rules(grammar)
  lines = grammar.split("\n")
  rules = {}
  current_rule = nil
  rule_body = []
  lines.each do |line|
    if line.start_with? '#'
      if not current_rule.nil?
        rule_body << "\n" if rule_body.length > 1
        rules[current_rule.to_sym] = rule_body.join("\n")
        rule_body = []
      end
      current_rule = line[1..-1].strip
    elsif line.strip != ''
      rule_body << line
    end
  end
  rule_body << "\n" if rule_body.length > 1
  rules[current_rule.to_sym] = rule_body.join("\n")
  rules
end

Instance Method Details

#generateObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hivemind/syntax.rb', line 113

def generate
  # parse grammar
  # combine into base grammar
  rules = self.class.load_rules(@grammar_source)
  refs = {}

  rules.each do |name, rule|
    refs[:"_#{name}"] = parse_rule rule, TYPES[name]
  end

  [REFS[:image], REFS.merge(refs)]
end

#parse_rule(rule, types) ⇒ Object



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
# File 'lib/hivemind/syntax.rb', line 126

def parse_rule(rule, types)
  parsers = []
  tokens = []
  token = ''
  i = 0
  while i < rule.length
    z = rule[i]
    if '<>'.include?(z)
      tokens << token unless token.empty?
      token = ''
      if z == '>'
        if rule[i + 1] == '>'
          tokens << '>>'
          i += 1
        else
          tokens << '>'
        end
      elsif z == '<'
        if rule[i + 1] == '<'
          tokens << '<<'
          i += 1
        else
          tokens << '<'
        end
      end
    elsif z == "'"
      tokens << token unless token.empty?
      token = ''
      j = i
      i += 1
      while rule[i] != "'"
        i += 1
      end
      tokens << rule[j..i]
    elsif z == ' '
      tokens << token unless token.empty?
      token = ''
      j = i
      while rule[i] == ' '
        i += 1
      end
      tokens << rule[j..i - 1]
      i -= 1
    elsif z == "\n"
      tokens << token unless token.empty?
      tokens << "\n"
    elsif z.match /[a-zA-Z_0-9]/
      token += z
    else
      tokens << token unless token.empty?
      token = ''
      tokens << z
    end
    i += 1
  end
  tokens << token unless token.empty?

  r = 0
  in_var = false

  tokens.each_with_index do |token, i|
    if token == '>>'
      if tokens[i - 2] == ':'
        parsers << Join.new(
            Ref.new(types[tokens[i - 3].to_sym]),
            tokens[i - 1][1..-2], as: tokens[i - 3])
      else
        parsers << Join.new(Ref.new(types[tokens[i - 1].to_sym]), "\n#{'    ' * r}", as: tokens[i - 1])
        #Many.new(Ref.new(types[tokens[i - 1].to_sym]), as: tokens[i - 1])
      end
      in_var = false
    elsif token == '>'
      parsers << Ref.new(types[tokens[i - 1].to_sym])
      in_var = false
    elsif token == "\n"
      parsers << Ref.new(:nl)
      if tokens[i + 1] == "\n"
        e = 2
      elsif tokens[i + 1]
        match = tokens[i + 1].match /([    ]+)/
        if match.nil? || match.captures.empty?
          indent = 0
        else
          indent = match.captures.first.size / 4
        end
        if indent > r
          parsers << Ref.new(:indent)
        elsif indent < r
          parsers << Ref.new(:dedent)
        end
        r = indent
      end 
    elsif token.match(/\A +\z/) 
      parsers << Ref.new(:ws)
    elsif (token == '<<' || token == '<') && tokens[i + 1] >= 'a' && tokens[i + 1] <= 'z'
      in_var = true
    elsif !in_var 
      parsers << Lit.new(token)
    end
  end
  # parsers.map { |pa| puts pa.inspect }
  parsers.reduce(:&)
end