Class: Kwartz::BaseTranslator

Inherits:
Translator show all
Defined in:
lib/kwartz/translator.rb

Overview

concrete class for visitor pattern

see ErbTranslator, PhpTranslator, JstlTranslator, and so on for detail.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Translator

get_class, register_class

Constructor Details

#initialize(marks = [], properties = {}) ⇒ BaseTranslator

Returns a new instance of BaseTranslator.



83
84
85
86
87
88
89
90
# File 'lib/kwartz/translator.rb', line 83

def initialize(marks=[], properties={})
  @stmt_l, @stmt_r, @expr_l, @expr_r, @escape_l, @escape_r = marks
  @nl = properties[:nl] || "\n"
  @escape = properties[:escape]
  @escape = Config::PROPERTY_ESCAPE if @escape == nil
  @header = properties[:header]
  @footer = properties[:footer]
end

Instance Attribute Details

#escapeObject

Returns the value of attribute escape.



93
94
95
# File 'lib/kwartz/translator.rb', line 93

def escape
  @escape
end

Returns the value of attribute footer.



93
94
95
# File 'lib/kwartz/translator.rb', line 93

def footer
  @footer
end

#headerObject

Returns the value of attribute header.



93
94
95
# File 'lib/kwartz/translator.rb', line 93

def header
  @header
end

Instance Method Details

#translate(stmt_list) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/kwartz/translator.rb', line 96

def translate(stmt_list)
  @sb = ''
  @sb << @header if @header
  stmt_list.each do |stmt|
    stmt.accept(self)
  end
  @sb << @footer if @footer
  return @sb
end

#translate_native_expr(expr) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/kwartz/translator.rb', line 128

def translate_native_expr(expr)
  assert unless expr.is_a?(NativeExpression)
  flag_escape = expr.escape?
  flag_escape = @escape if flag_escape == nil
  if flag_escape
    add_escaped_expr(expr.code)
  else
    add_plain_expr(expr.code)
  end
end

#translate_native_stmt(stmt) ⇒ Object



107
108
109
110
# File 'lib/kwartz/translator.rb', line 107

def translate_native_stmt(stmt)
  @sb << @stmt_l << stmt.code << @stmt_r   # ex. <% stmt.code %>
  @sb << @nl unless stmt.no_newline
end

#translate_print_stmt(stmt) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kwartz/translator.rb', line 113

def translate_print_stmt(stmt)
  stmt.args.each do |arg|
    #arg.accept(self)
    if arg.is_a?(String)
      #translate_string(arg)
      parse_embedded_expr(arg)
    elsif arg.is_a?(NativeExpression)
      translate_native_expr(arg)
    else
      assert
    end
  end
end

#translate_string(str) ⇒ Object



140
141
142
# File 'lib/kwartz/translator.rb', line 140

def translate_string(str)
  @sb << str
end