Class: Kwartz::EperlHandler
- Defined in:
- lib/kwartz/binding/eperl.rb
Overview
directive handler for ePerl
Constant Summary collapse
- PERL_DIRECTIVE_PATTERN =
/\A(\w+)(?:\s*\(\s*(.*)\))?\z/
- PERL_MAPPING_PATTERN =
/\A'([-:\w]+)',\s*(.*)\z/
- PERL_DIRECTIVE_FORMAT =
'%s(%s)'
Instance Attribute Summary
Attributes inherited from Handler
#converter, #even, #filename, #odd
Instance Method Summary collapse
- #directive_format ⇒ Object
- #directive_pattern ⇒ Object
- #handle(stmt_list, handler_arg) ⇒ Object
- #mapping_pattern ⇒ Object
Methods inherited from Handler
#_elem_info_table, #_import_element_info_from_handler, #extract, get_class, #get_element_info, #get_element_ruleset, #initialize, register_class
Methods included from ElementExpander
#expand_element_info, #expand_statement, #get_element_info, #get_element_ruleset
Methods included from Assertion
Methods included from StatementHelper
#add_foreach_stmts, #add_native_code, #add_native_expr_with_default, #build_print_args, #build_print_expr_stmt, #build_print_stmt, #create_text_print_stmt, #etag_stmt, #stag_stmt, #wrap_content_with_native_stmt, #wrap_element_with_native_stmt
Methods included from ConverterHelper
#_last_stmt_kind, #convert_error, #error_if_empty_tag, #error_when_last_stmt_is_not_if, #include_properties
Constructor Details
This class inherits a constructor from Kwartz::Handler
Instance Method Details
#directive_format ⇒ Object
39 40 41 |
# File 'lib/kwartz/binding/eperl.rb', line 39 def directive_format return PERL_DIRECTIVE_FORMAT end |
#directive_pattern ⇒ Object
25 26 27 |
# File 'lib/kwartz/binding/eperl.rb', line 25 def directive_pattern return PERL_DIRECTIVE_PATTERN end |
#handle(stmt_list, handler_arg) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/kwartz/binding/eperl.rb', line 44 def handle(stmt_list, handler_arg) ret = super return ret if ret arg = handler_arg d_name = arg.directive_name d_arg = arg.directive_arg d_str = arg.directive_str case d_name when :foreach, :Foreach, :FOREACH, :list, :List, :LIST is_foreach = d_name == :foreach || d_name == :Foreach || d_name == :FOREACH unless d_arg =~ /\A(\$\w+)(?:,\s*(\$\w+))?\s+in\s+(.*)\z/ raise convert_error("'#{d_str}': invalid argument.", stag_info.linenum) end loopvar = $1 ; loopval = $2 ; looplist = $3 counter = d_name == :foreach || d_name == :list ? nil : "#{loopvar}_ctr" toggle = d_name != :FOREACH && d_name != :LIST ? nil : "#{loopvar}_tgl" code = [] code << "my #{counter} = 0;" if counter if loopval code << "foreach my #{loopvar} (keys #{looplist}) {" code << " my #{loopval} = #{looplist.sub(/\A%/,'$')}{#{loopvar}};" else code << "foreach my #{loopvar} (#{looplist}) {" end code << " #{counter}++;" if counter code << " my #{toggle} = #{counter}%2==0 ? #{self.even} : #{self.odd};" if toggle if is_foreach wrap_element_with_native_stmt(stmt_list, arg, code, "}", :foreach) else wrap_content_with_native_stmt(stmt_list, arg, code, "}", :foreach) end #stmt_list << stag_stmt(arg) if !is_foreach #stmt_list << NativeStatement.new("my #{counter} = 0;") if counter #if loopval # stmt_list << NativeStatement.new("foreach my #{loopvar} (keys #{looplist}) {", :foreach) # stmt_list << NativeStatement.new(" my #{loopval} = #{looplist.sub(/\A%/,'$')}{#{loopvar}};") #else # stmt_list << NativeStatement.new("foreach my #{loopvar} (#{looplist}) {", :foreach) #end #stmt_list << NativeStatement.new(" #{counter}++;") if counter #stmt_list << NativeStatement.new(" my #{toggle} = #{counter}%2==0 ? #{self.even} : #{self.odd};") if toggle #stmt_list << stag_stmt(arg) if is_foreach #stmt_list.concat(arg.cont_stmts) #stmt_list << etag_stmt(arg) if is_foreach #stmt_list << NativeStatement.new("}", :foreach) #stmt_list << etag_stmt(arg) if !is_foreach when :while wrap_element_with_native_stmt(stmt_list, arg, "while (#{d_arg}) {", "}", :while) #stmt_list << NativeStatement.new("while (#{d_arg}) {", :while) #stmt_list << stag_stmt(arg) #stmt_list.concat(cont_stmts) #stmt_list << etag_stmt(arg) #stmt_list << NativeStatement.new("}", :while) when :loop error_if_empty_tag(arg) wrap_content_with_native_stmt(stmt_list, arg, "while (#{d_arg}) {", "}", :while) #stmt_list << stag_stmt #stmt_list << NativeStatement.new("while (#{d_arg}) {", :while) #stmt_list.concat(cont_stmts) #stmt_list << NativeStatement.new("}", :while) #stmt_list << etag_stmt when :set wrap_element_with_native_stmt(stmt_list, arg, "#{d_arg};", nil, :set) #stmt_list << NativeStatement.new("#{d_arg};", :set) #stmt_list << stag_stmt #stmt_list.concat(cont_stmts) #stmt_list << etag_stmt when :if wrap_element_with_native_stmt(stmt_list, arg, "if (#{d_arg}) {", "}", :if) #stmt_list << NativeStatement.new("if (#{d_arg}) {", :if) #stmt_list << stag_stmt #stmt_list.concat(cont_stmts) #stmt_list << etag_stmt #stmt_list << NativeStatement.new("}", :if) when :elsif, :else error_when_last_stmt_is_not_if(stmt_list, arg) stmt_list.pop # delete '}' kind = d_name == :else ? :else : :elseif code = d_name == :else ? "} else {" : "} elsif (#{d_arg}) {" wrap_element_with_native_stmt(stmt_list, arg, code, "}", kind) #stmt_list << NativeStatement.new(code, kind) #stmt_list << stag_stmt #stmt_list.concat(cont_stmts) #stmt_list << etag_stmt #stmt_list << NativeStatement.new("}", kind) when :default, :Default, :DEFAULT error_if_empty_tag(arg) expr_code = d_arg flag_escape = d_name == :default ? nil : (d_name == :Default) add_native_expr_with_default(stmt_list, arg, expr_code, flag_escape, "if (#{d_arg}) {", "} else {", "}") #stmt_list << stag_stmt(arg) #stmt_list << NativeStatement.new_without_newline("if (#{d_arg}) {", :if) #flag_escape = d_name == :default ? nil : (d_name == :Default) #stmt_list << PrintStatement.new([ NativeExpression.new(d_arg, flag_escape) ]) #stmt_list << NativeStatement.new_without_newline("} else {", :else) #stmt_list.concat(arg.cont_stmts) #stmt_list << NativeStatement.new_without_newline("}", :else) #stmt_list << etag_stmt(arg) else return false end #case return true end |
#mapping_pattern ⇒ Object
32 33 34 |
# File 'lib/kwartz/binding/eperl.rb', line 32 def mapping_pattern return PERL_MAPPING_PATTERN end |