Class: Antelope::Generator::Ruby

Inherits:
Base
  • Object
show all
Defined in:
lib/antelope/generator/ruby.rb

Overview

Generates a ruby parser.

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 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

#basic_indentObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/antelope/generator/ruby.rb', line 36

def basic_indent
  @_indent ||= case indent_type
               when 'space'
                 ' '
               when 'tab'
                 "\t"
               else
                 indent_type
               end * indent_size
end

#define_own_handler?Boolean

Returns:



83
84
85
86
# File 'lib/antelope/generator/ruby.rb', line 83

def define_own_handler?
  directives.ruby.error_class? ||
    panic_mode?
end

#error_classObject



94
95
96
# File 'lib/antelope/generator/ruby.rb', line 94

def error_class
  directives.ruby.error_class
end

#generatevoid

This method returns an undefined value.

Actually performs the generation. Takes the template from ruby.ant and outputs it to <file>.rb.



102
103
104
105
106
107
108
109
# File 'lib/antelope/generator/ruby.rb', line 102

def generate
  template 'ruby', "#{file}.rb" do |body|
    body
      .gsub!("\n", "\n#{indent}")
      .gsub!(/^[ \t]*\n/, "\n")
    format(grammar.compiler.body, write: body)
  end
end

#generate_action_tableString

Creates an action table for the parser.

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/antelope/generator/ruby.rb', line 18

def generate_action_table
  parts = []
  table.each do |state|
    out = ''
    state.each do |token, action|
      inspect = %(:"#{token}" =>)
      out << "#{basic_indent}#{inspect} #{action.inspect},\n"
    end
    parts << "{\n#{out.chomp(",\n")}\n}"
  end

  "[#{parts.join(', ')}]"
end

#generate_productions_listString

Outputs an array of all of the productions.

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/antelope/generator/ruby.rb', line 64

def generate_productions_list
  out = "[\n"

  productions.each do |(label, size, block)|
    out << '[' << label.name.inspect << ', ' <<
      size.inspect << ', '

    block = if block.empty?
              'DEFAULT_PROC'
            else
              "proc { |match| #{block[1..-2]} }"
            end

    out << block << "],\n"
  end
  out.chomp!(",\n")
  out << ']'
end

#indentObject



32
33
34
# File 'lib/antelope/generator/ruby.rb', line 32

def indent
  basic_indent * (directives.ruby.indent || 2)
end

#indent_sizeObject



51
52
53
54
55
56
57
58
59
# File 'lib/antelope/generator/ruby.rb', line 51

def indent_size
  directives.ruby.indent_size ||
    case indent_type
    when 'tab'
      1
    else
      2
    end
end

#indent_typeObject



47
48
49
# File 'lib/antelope/generator/ruby.rb', line 47

def indent_type
  directives.ruby.indent_type || 'space'
end

#panic_mode?Boolean

Returns:



88
89
90
91
92
# File 'lib/antelope/generator/ruby.rb', line 88

def panic_mode?
  directives.panic_mode &&
    directives.ruby.error_class? &&
    grammar.contains_error_token?
end