Class: Antelope::Generator::CSource

Inherits:
CHeader show all
Defined in:
lib/antelope/generator/c_source.rb

Constant Summary

Constants inherited from Base

Base::Boolean

Instance Attribute Summary

Attributes inherited from Base

#file, #grammar, #mods

Instance Method Summary collapse

Methods inherited from CHeader

#define_stype?, #guard_name, #lex_params, #params, #parse_params, #prefix, #push?, #stype, #symbols, #terminal_type, #token_prefix, #union_body, #upper_prefix

Methods inherited from Base

directive, directives, inherited, #initialize, register_as, source_root, #template

Methods included from Base::Extra

#productions, #table

Methods included from Base::Coerce

#coerce_directive_class, #coerce_directive_value, #coerce_nested_hash, #directives

Constructor Details

This class inherits a constructor from Antelope::Generator::Base

Instance Method Details

#action_for(state) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/antelope/generator/c_source.rb', line 5

def action_for(state)
  out = ""

  grammar.terminals.each do |terminal|
    action = state[terminal.name]

    if action.size == 2 && action[0] == :state
      out << "#{action[1] + 1}, "
    elsif action.size == 2 &&
      [:reduce, :accept].include?(action[0])
      if $DEBUG
        out << "#{prefix.upcase}STATES + #{action[1] + 1}, "
      else
        out << "#{table.size + action[1] + 1}, "
      end
    else
      out << "0, "
    end

  end

  out.chomp(", ")
end

#cify_block(block) ⇒ Object



29
30
31
32
# File 'lib/antelope/generator/c_source.rb', line 29

def cify_block(block)
  block.gsub(/\$([0-9]+)/, "#{prefix}vals[\\1]")
       .gsub(/\$\$/, "#{prefix}out")
end

#generateObject



34
35
36
# File 'lib/antelope/generator/c_source.rb', line 34

def generate
  template "c_source", "#{file}.c"
end