Class: Bison::GrammarFile

Inherits:
Object
  • Object
show all
Defined in:
lib/bison/grammar_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, rules, code) ⇒ GrammarFile

Returns a new instance of GrammarFile.



9
10
11
# File 'lib/bison/grammar_file.rb', line 9

def initialize(tokens, rules, code)
  @tokens, @rules, @code = tokens, rules, code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/bison/grammar_file.rb', line 7

def code
  @code
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/bison/grammar_file.rb', line 6

def name
  @name
end

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/bison/grammar_file.rb', line 7

def rules
  @rules
end

#tokensObject (readonly)

Returns the value of attribute tokens.



7
8
9
# File 'lib/bison/grammar_file.rb', line 7

def tokens
  @tokens
end

Instance Method Details



40
41
42
43
# File 'lib/bison/grammar_file.rb', line 40

def print_actions_module(out=$stdout)
  template = File.expand_path('../../../templates/actions.rb.erb', __FILE__)
  out.puts(ERB.new(File.read(template), nil, '-').result(binding))
end


35
36
37
38
# File 'lib/bison/grammar_file.rb', line 35

def print_base_module(out=$stdout)
  template = File.expand_path('../../../templates/base.rb.erb', __FILE__)
  out.puts(ERB.new(File.read(template), nil, '-').result(binding))
end


45
46
47
48
# File 'lib/bison/grammar_file.rb', line 45

def print_bison(out=$stdout)
  template = File.expand_path('../../../templates/parser.y.erb', __FILE__)
  out.puts(ERB.new(File.read(template), nil, '-').result(binding))
end


30
31
32
33
# File 'lib/bison/grammar_file.rb', line 30

def print_class(out=$stdout)
  template = File.expand_path('../../../templates/class.rb.erb', __FILE__)
  out.puts(ERB.new(File.read(template), nil, '-').result(binding))
end


50
51
52
53
# File 'lib/bison/grammar_file.rb', line 50

def print_extconf(out=$stdout)
  template = File.expand_path('../../../templates/extconf.rb.erb', __FILE__)
  out.puts(ERB.new(File.read(template), nil, '-').result(binding))
end

#unameObject



55
56
57
# File 'lib/bison/grammar_file.rb', line 55

def uname
  name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

#validateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bison/grammar_file.rb', line 13

def validate
  errors = []
  symbols = tokens.map(&:names).flatten + rules.map(&:name)
  rules.map(&:components).flatten.map(&:elements).flatten.each do |el|
    unless !(Bison::Nonterminal === el) || symbols.include?(el.name)
      errors << "#{el.location.join('.')}: #{el.name} is not defined"
    end
  end
  rules.map(&:components).flatten.each do |seq|
    seq.elements.grep(Bison::Action).each do |action|
      err = action.errors
      errors << err unless err.nil?
    end
  end
  abort(errors.join("\n")) unless errors.empty?
end