class Object
Public Instance Methods
expect(kind)
click to toggle source
# File lib/template_parser.rb, line 37 def expect kind if (actual=showNext.kind)!=kind abort "ERROR at #{showNext.pos}. Expecting #{kind}. Got #{actual}" else return acceptIt() end end
init(str)
click to toggle source
# File lib/template_lexer.rb, line 59 def init str @ss=StringScanner.new(str) @line=0 end
next_token()
click to toggle source
next token can detect spaces length
# File lib/template_lexer.rb, line 74 def next_token if @ss.bol? @line+=1 @old_pos=@ss.pos end position=[@line,@ss.pos-@old_pos+1] return :eos if @ss.eos? case when text = @ss.scan(NEWLINE) next_token() when text = @ss.scan(SPACE) next_token() when text = @ss.scan(LPAREN) return Token.new [:lparen,text,position] when text = @ss.scan(COMMENT) return Token.new [:m3e_comment,text,position] when text = @ss.scan(RPAREN) return Token.new [:rparen,text,position] when text = @ss.scan(FLOAT) return Token.new [:float_lit,text,position] when text = @ss.scan(FRAC) return Token.new [:frac_lit,text,position] when text = @ss.scan(RANGE) return Token.new [:range_lit,text,position] when text = @ss.scan(INTEGER) return Token.new [:integer_lit,text,position] when text = @ss.scan(STRING) return Token.new [:string_lit,text,position] when text = @ss.scan(FRAC) return Token.new [:frac_lit,text,position] when text = @ss.scan(IDENT) case <%=apply_regexp%> when value = text.match(NIL) return Token.new [:nil,text,position] when value = text.match(TRUE) return Token.new [:true,text,position] when value = text.match(FALSE) return Token.new [:false,text,position] else return Token.new [:identifier,text,position] end else x = @ss.getch return Token.new [x, x,position] end end
nil_maybe?()
click to toggle source
# File lib/template_parser.rb, line 45 def nil_maybe? if showNext.is_a?(:nil) return acceptIt else return nil end end
parse(filename)
click to toggle source
# File lib/template_parser.rb, line 18 def parse filename str=::IO.read(filename) @tokens=lexer.tokenize(str) last_pos = @tokens.last.pos @tokens << Token.new([:eos,'',last_pos]) pp @tokens if @verbose parse<%=mm.classes.first.name%> end def acceptIt tok=tokens.shift say "consuming #{tok.val} (#{tok.kind})" tok end
parse_comments()
click to toggle source
# File lib/template_parser.rb, line 53 def parse_comments ret=nil while showNext.is_a? :m3e_comment str||="" str << acceptIt.val end if str ret=Token.new([:comment,str,[0,0]]) end ret end
print_rec(ary,indent=0)
click to toggle source
# File lib/template_pretty_printer.rb, line 21 def print_rec ary,indent=0 str="" case e=ary when Array str << "\n" if e.first.to_s.start_with? "#" str << " "*indent+ary.shift+"\n" end str << " "*indent+"(" ary.each do |e| str << print_rec(e,indent+2) end if ary.last.is_a? Array str << "\n"+" "*indent+")" else str<< "\b)" end else str << e.to_s+ " " end return str end
process(node,level=0)
click to toggle source
# File lib/template_ast_printer.rb, line 35 def process node,level=0 id=node.object_id if node.is_a? Token sink="#{id}" val=node.val nodes_decl << "#{sink} [label=\"#{val}\",color=\"red\"]" else kname=node.class.name.split("::")[1] id=node.object_id nodes_decl << "#{id} [label=\"#{kname}\"]" node.instance_variables.each{|vname| ivar=node.instance_variable_get(vname) vname=vname.to_s[1..-1] case ivar when Array ivar.each_with_index{|e,idx| sink=process(e,level+2) @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}[#{idx}]\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink } when Token val=ivar.val sink="#{ivar.object_id}" nodes_decl << "#{sink} [label=\"#{val}\",color=\"red\"]" @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink when nil else sink=process(ivar,level+2) @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink end } end return id end
prpr(ary)
click to toggle source
# File lib/template_pretty_printer.rb, line 17 def prpr ary print_rec(ary).lstrip end
showNext(n=0)
click to toggle source
# File lib/template_parser.rb, line 33 def showNext n=0 tokens[n] end
tokenize(str)
click to toggle source
# File lib/template_lexer.rb, line 64 def tokenize str @tokens=[] init(str) until @ss.eos? @tokens << next_token() end return @tokens[0..-2] end