Class: Antelope::Generator::CHeader

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

Direct Known Subclasses

CSource

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

#define_stype?Boolean

Returns:



20
21
22
# File 'lib/antelope/generator/c_header.rb', line 20

def define_stype?
  !!directives.union[0] && !directives.api.value.type
end

#generateObject



100
101
102
# File 'lib/antelope/generator/c_header.rb', line 100

def generate
  template "c_header", "#{file}.h"
end

#guard_nameObject



96
97
98
# File 'lib/antelope/generator/c_header.rb', line 96

def guard_name
  "#{prefix.upcase}#{file.gsub(/[\W]/, "_").upcase}"
end

#lex_paramsObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/antelope/generator/c_header.rb', line 24

def lex_params
  params = [directives.lex_param, directives.param].compact.
    flatten

  if params.any?
    ", " << params.join(", ")
  else
    ""
  end
end

#paramsObject



40
41
42
43
44
45
46
# File 'lib/antelope/generator/c_header.rb', line 40

def params
  if directives.param
    directives.param.join(", ")
  else
    ""
  end
end

#parse_paramsObject



35
36
37
38
# File 'lib/antelope/generator/c_header.rb', line 35

def parse_params
  [directives.parse_param, directives.param].compact.flatten.
    join(", ")
end

#prefixObject



76
77
78
79
80
81
82
# File 'lib/antelope/generator/c_header.rb', line 76

def prefix
  if directives.api.prefix
    directives.api.prefix
  else
    "yy"
  end
end

#push?Boolean

Returns:



16
17
18
# File 'lib/antelope/generator/c_header.rb', line 16

def push?
  directives.api.push_pull == "push"
end

#stypeObject



48
49
50
51
52
53
54
55
56
# File 'lib/antelope/generator/c_header.rb', line 48

def stype
  prefix.upcase << if directives.api.value.type
    directives.api.value.type
  elsif directives.union.size > 1
    directives.union[0]
  else
    "STYPE"
  end
end

#symbolsObject



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

def symbols
  @_symbols ||= begin
    sym  = grammar.terminals.map(&:name) + grammar.nonterminals
    nums = sym.each_with_index.map { |v, i| [v, i + 257] }
    Hash[nums]
  end
end

#terminal_typeObject



62
63
64
# File 'lib/antelope/generator/c_header.rb', line 62

def terminal_type
  "int" # for now
end

#token_prefixObject



66
67
68
69
70
71
72
73
74
# File 'lib/antelope/generator/c_header.rb', line 66

def token_prefix
  if directives.api.token.prefix
    directives.api.token.prefix
  elsif directives.api.prefix
    prefix.upcase
  else
    ""
  end
end

#union_bodyObject



58
59
60
# File 'lib/antelope/generator/c_header.rb', line 58

def union_body
  directives.union.last
end

#upper_prefixObject



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

def upper_prefix
  prefix.upcase
end