Module: GenMachine::Helpers::Ruby

Included in:
Generators::RubyGenerator
Defined in:
lib/genmachine/generators/ruby/helper.rb

Constant Summary collapse

INPUT =
'__i'
STATE =
'__state'

Instance Method Summary collapse

Instance Method Details

#rb_acc_commands(acc_phrase) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/genmachine/generators/ruby/helper.rb', line 102

def rb_acc_commands(acc_phrase)
  case
  when (acc_phrase.nil? or acc_phrase == '')
    return ['@fwd=true']
  when acc_phrase.strip == '>>'
    return []
  when acc_phrase =~ /^\s*([a-zA-Z_][a-zA-Z0-9_.]*)\s*>>\s*$/
    return ["#{rb_vars($1)}.reset!"]
  when acc_phrase =~ /^\s*(["'a-zA-Z_][^>]*)\s*>>\s*([a-zA-Z_][a-zA-Z0-9_.]*)\s*$/
    return ["#{rb_vars($1)}.into(#{rb_vars($2)})"]
  when acc_phrase =~ /^\s*(["'a-zA-Z_][^>]*)\s*>>>\s*([a-zA-Z_][a-zA-Z0-9_.]*)\s*$/
    return ["#{rb_vars($1)}.into!(#{rb_vars($2)})"]
  else raise("Can't figure out your accumulator statement: #{acc_phrase}")
  end
end

#rb_charset_cond(input, has_eof_state, sep = '||') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/genmachine/generators/ruby/helper.rb', line 19

def rb_charset_cond(input,has_eof_state,sep='||')
  return 'true' if input.nil?
  outs = []
  sep = '||' if input[:kind] == :exclude
  input[:ranges].each do |range|
    if input[:kind] == :include
      if range.is_a? Array
        outs << "nl?" if (range[0] <= 0x0a) && (range[1] >= 0x0a)
        outs << "space?" if (range[0] <= 0x20) && (range[1] >= 0x20)
        outs << "(#{INPUT}>#{range[0]-1}&&#{INPUT}<#{range[1]+1})"
      else
        outs << case range
                when 0x0a; 'nl?'
                when 0x20; 'space?'
                when :any; has_eof_state ? 'true' : '!eof?'
                else "#{INPUT}==#{range}" end
      end
    end
  end
  out = outs.join(sep)
  out = '!('+out+')' if input[:kind] == :exclude
  return out
end

#rb_commands(clause, currstate) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/genmachine/generators/ruby/helper.rb', line 43

def rb_commands(clause,currstate)
  cmds = []
  cmds += rb_simple_acc_commands(clause[:acc])
  clause[:exprs].each do |expr|
    if expr.include? '>>'
      cmds += rb_acc_commands(expr)
    else
      cmds << rb_vars(expr.strip)
    end
  end
  cmds += rb_transition_commands(clause[:next],currstate)
  return cmds.join('; ')
end

#rb_conditional(clause, states, clauses) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/genmachine/generators/ruby/helper.rb', line 7

def rb_conditional(clause,states,clauses)
  has_eof_state = eof_state?(states) || eof_clause?(clauses)
  out = ''
  if clause[:cond].size > 0
    out += '('+clause[:cond].join(' || ')+')'
    out += "&& (#{rb_charset_cond(clause[:input],has_eof_state)})" unless clause[:input].nil?
  else
    out += rb_charset_cond(clause[:input],has_eof_state,',')
  end
  return rb_vars(out)
end

#rb_simple_acc_commands(acc_phrase) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/genmachine/generators/ruby/helper.rb', line 92

def rb_simple_acc_commands(acc_phrase)
  case
  when (acc_phrase.nil? or acc_phrase == ''); return ['@fwd=true']
  when acc_phrase.strip == '>>'; return []
  when acc_phrase =~ /^\s*>>\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*$/
    return ["#{INPUT}.into(#{rb_vars($1)})"]
  else raise("Can't figure out your accumulator statement: #{acc_phrase}")
  end
end

#rb_transition_commands(st, currstate) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/genmachine/generators/ruby/helper.rb', line 57

def rb_transition_commands(st,currstate)
  st = st.strip.split(';').map(&:strip)
  out = []
  add_next = false
  st.each do |s|
    case
    when s =~ /^([^\(\[]+)(?:\[([^\]]*)\])?\(([^\)]*)\)$/   # Call another group
      funname = $1
      rename = $2
      params = $3.split(',').map{|p|rb_vars(p)}
      params << 's'
      params << "'#{rename}'" unless (rename.nil? or rename.strip=='')
      out << "#{STATE}=#{funname}(#{params.join(',')})"
      add_next = true
    when s =~ /^<done>$/
      out << "return(s)"
      add_next = false
    when s =~ /^<([^>]+)>$/
      out << "return(#{rb_vars($1)})"
      add_next = false
    when s =~ /^(:[a-zA-Z0-9_:-]+)$/
      out << "#{STATE}='#{$1}'" unless currstate == $1
      add_next = true
    else
      out << s
    end
  end
  out << 'next' if add_next
  return out
end

#rb_vars(str) ⇒ Object



88
89
90
# File 'lib/genmachine/generators/ruby/helper.rb', line 88

def rb_vars(str)
  str.tr('$','@').gsub /(:[a-zA-Z0-9_:-]+)/, '\'\1\''
end