Module: Packrat::GrammarBuild

Included in:
Grammar
Defined in:
lib/packrat/grammar.rb,
lib/packrat/grammar.rb,
lib/packrat/grammar.rb,
lib/packrat/grammar.rb,
lib/packrat/grammar.rb,
lib/packrat/grammar.rb,
lib/packrat/grammar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



147
148
149
# File 'lib/packrat/grammar.rb', line 147

def start
  @start
end

Instance Method Details

#[](name) ⇒ Object



158
# File 'lib/packrat/grammar.rb', line 158

def [](name); @rules[name]; end

#any(*subs) ⇒ Object

any() can be implemented in many ways but if all the sub-elements are strings we simply create a regexp matching any of them. If they are not all strings we add an internal rule with the alternatives as productions.



289
290
291
292
293
294
295
296
297
298
# File 'lib/packrat/grammar.rb', line 289

def any(*subs)
  if subs.all? {|e| String === e}
    re_string = subs.map {|s| "(" + Regexp.escape(s) + ")"}.join("|")
    Packrat::RegexpLiteral.new(Regexp.new(re_string))
  else
    name = internal_rule_name() 
    rule(name, *subs.map {|s| [s, lift(0)]})
    Packrat::RuleRef.new(name)
  end
end

#ast(name, options = {}) ⇒ Object



343
344
345
# File 'lib/packrat/grammar.rb', line 343

def ast(name, options = {})
  Packrat::ASTBuilder.new(name, options)
end

#ast_class(name, prod, nameMap) ⇒ Object

Return the ast class with the given <nodeName> for the given <production>. If not previously created we create it and add it to the Tree module.



349
350
351
352
353
354
355
356
# File 'lib/packrat/grammar.rb', line 349

def ast_class(name, prod, nameMap)
  acn = ast_class_name(name)
  begin
    const_get("ASTs").const_get(acn)
  rescue
    const_get("ASTs").const_set(acn, make_ast_class(acn, prod, nameMap))
  end
end

#ast_class_name(name) ⇒ Object



358
359
360
361
# File 'lib/packrat/grammar.rb', line 358

def ast_class_name(name)
  s = name.to_s
  s[0,1].upcase + s[1..-1]
end

#each_prodObject



176
177
178
# File 'lib/packrat/grammar.rb', line 176

def each_prod
  each_rule {|r| r.prods.each {|p| yield(p)}}
end

#each_ruleObject



173
174
175
# File 'lib/packrat/grammar.rb', line 173

def each_rule
  rules.values.each {|r| yield(r)}
end

#eosObject



318
# File 'lib/packrat/grammar.rb', line 318

def eos(); hidden(Packrat::EOS.new); end

#finalize!Object

Finalize the building of the grammar by conducting postprocessing.



166
167
168
169
# File 'lib/packrat/grammar.rb', line 166

def finalize!
  postprocess_set_grammar_on_rules
  each_prod {|p| p.finalize!}
end

#hidden(elem) ⇒ Object



160
161
162
163
164
# File 'lib/packrat/grammar.rb', line 160

def hidden(elem)
  e = elem.to_packrat_grammar_element
  e.hidden = true
  e
end

#internal_rule_nameObject



305
306
307
# File 'lib/packrat/grammar.rb', line 305

def internal_rule_name()
  ("_r_" + next_internal_rule_num.to_s).intern
end

#lift(index, &b) ⇒ Object



282
# File 'lib/packrat/grammar.rb', line 282

def lift(index, &b); Packrat::LiftOneResultProducer.new(index, &b); end

#make_ast_class(klassName, production, nameMap) ⇒ Object



363
364
365
# File 'lib/packrat/grammar.rb', line 363

def make_ast_class(klassName, production, nameMap)
  Packrat::AST.new_subclass(klassName, production, nameMap)
end

#maybe(element) ⇒ Object



237
# File 'lib/packrat/grammar.rb', line 237

def maybe(element); Packrat::Maybe.new(element); end

#mult(element) ⇒ Object



219
# File 'lib/packrat/grammar.rb', line 219

def mult(element); Packrat::Repeat.new(element, 0, false); end

#next_internal_rule_numObject



300
301
302
303
# File 'lib/packrat/grammar.rb', line 300

def next_internal_rule_num
  @internal_rule_counter ||= 0
  @internal_rule_counter += 1
end

#plus(element) ⇒ Object



218
# File 'lib/packrat/grammar.rb', line 218

def plus(element); Packrat::Repeat.new(element, 1, false); end

#postprocess_set_grammar_on_rulesObject



170
171
172
# File 'lib/packrat/grammar.rb', line 170

def postprocess_set_grammar_on_rules
  each_prod {|r| r.grammar = self}
end

#prod(name, rhs) ⇒ Object



153
154
155
156
157
# File 'lib/packrat/grammar.rb', line 153

def prod(name, rhs)
  pr = Packrat::Production.new(name, rhs)
  pr = Packrat::ErrorReporter.new(pr) if $DEBUG
  rules[name] << pr
end

#rep(min, max, element) ⇒ Object



220
# File 'lib/packrat/grammar.rb', line 220

def rep(min, max, element); Packrat::Repeat.new(element, min, max); end

#rule(name, *rhss) ⇒ Object



150
151
152
# File 'lib/packrat/grammar.rb', line 150

def rule(name, *rhss)
  rhss.each {|rhs| prod(name, rhs)}
end

#rulesObject



149
# File 'lib/packrat/grammar.rb', line 149

def rules; @rules ||= (Hash.new {|h,k| h[k] = Packrat::Rule.new(k)}); end

#sexpr(name) ⇒ Object



281
# File 'lib/packrat/grammar.rb', line 281

def sexpr(name); Packrat::SexprProducer.new(name); end

#start_ruleObject



159
# File 'lib/packrat/grammar.rb', line 159

def start_rule; self[self.start]; end

#start_symbol(name) ⇒ Object



148
# File 'lib/packrat/grammar.rb', line 148

def start_symbol(name); @start = name; end