Class Kwartz::BaseTranslator
In: kwartz/translator.rb
Parent: Translator

concrete class for visitor pattern

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

Methods

Attributes

escape  [RW] 
footer  [RW] 
header  [RW] 

Public Class methods

[Source]

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

Public Instance methods

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

# File kwartz/translator.rb, line 140
    def translate_string(str)
      @sb << str
    end

Protected Instance methods

[Source]

# File kwartz/translator.rb, line 190
    def add_debug_expr(expr_code)
      not_implemented
    end

[Source]

# File kwartz/translator.rb, line 185
    def add_escaped_expr(expr_code)
      @sb << @escape_l << expr_code << @escape_r   # ex. <%=h expr_code %>
    end

[Source]

# File kwartz/translator.rb, line 180
    def add_plain_expr(expr_code)
      @sb << @expr_l << expr_code << @expr_r       # ex. <%= expr_code %>
    end

concat several print statements into a statement

[Source]

# File kwartz/translator.rb, line 196
    def optimize_print_stmts(stmt_list)
      stmt_list2 = []
      args = []
      stmt_list.each do |stmt|
        if stmt.is_a?(PrintStatement)
          args += stmt.args
        else
          if !args.empty?
            args = _compact_args(args)
            stmt_list2 << PrintStatement.new(args)
            args = []
          end
          stmt_list2 << stmt
        end
      end
      if !args.empty?
        args = _compact_args(args)
        stmt_list2 << PrintStatement.new(args)
      end
      return stmt_list2
    end

def translate_text_expr(expr)

  @sb << expr.text

end

[Source]

# File kwartz/translator.rb, line 153
    def parse_embedded_expr(text)
      pos = 0
      text.scan(/@(!*)\{(.*?)\}@/) do |indicator, expr_str|
        m = Regexp.last_match
        s = text[pos, m.begin(0) - pos]
        pos = m.end(0)
        translate_string(s) unless s.empty?
        expr_str = parse_expr_str!(expr_str)
        case indicator
        when nil, ''  ;  add_escaped_expr(expr_str)
        when '!'      ;  add_plain_expr(expr_str)
        when '!!'     ;  add_debug_expr(expr_str)
        else          ;  # do nothing
        end
      end
      rest = pos == 0 ? text : $'
      translate_string(rest) unless rest.empty?
    end

(abstract) convert expression to native expression string. ex. ‘item.name’ => ’$item->name’, ’$item->name’ => ’$item->name‘

[Source]

# File kwartz/translator.rb, line 175
    def parse_expr_str!(expr_str)
      not_implemented
    end

[Validate]