Class: RubyLexer::KeepWsTokenPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylexer/tokenprinter.rb

Overview


Constant Summary collapse

ACCUMSIZE =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sep = '', line = 1, showzw = 0) ⇒ KeepWsTokenPrinter

Returns a new instance of KeepWsTokenPrinter.



107
108
109
110
111
112
113
114
115
# File 'lib/rubylexer/tokenprinter.rb', line 107

def initialize(sep='',line=1,showzw=0)
   @sep=sep
   @inws=false
   @lasttok=''
   #@latestline=line
   @lastprintline=0
   @accum=[]
   @showzw=showzw
end

Instance Attribute Details

#inwsObject

,:latestline



102
103
104
# File 'lib/rubylexer/tokenprinter.rb', line 102

def inws
  @inws
end

#lastfalObject

,:latestline



102
103
104
# File 'lib/rubylexer/tokenprinter.rb', line 102

def lastfal
  @lastfal
end

#lasttokObject

,:latestline



102
103
104
# File 'lib/rubylexer/tokenprinter.rb', line 102

def lasttok
  @lasttok
end

#showzwObject (readonly)

Returns the value of attribute showzw.



103
104
105
# File 'lib/rubylexer/tokenprinter.rb', line 103

def showzw
  @showzw
end

Instance Method Details

#aprint(tok) ⇒ Object Also known as: sprint



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
# File 'lib/rubylexer/tokenprinter.rb', line 125

def aprint(tok)
   if StringToken===tok or 
       HereBodyToken===tok
#         (HerePlaceholderToken===tok and 
#          tok.bodyclass!=OutlinedHereBodyToken
#         )
         str_needs_escnls=(tok.line-@lastfal.line).nonzero?
   end if false
   result=tok.ws_munge(self) and return result


   #insert extra ws unless an ambiguous op immediately follows
   #id or num, in which case ws would change the meaning
   result=tok
   result=
   case tok
   when ZwToken,EoiToken,NoWsToken, HereBodyToken, NewlineToken,
        ImplicitParamListStartToken,ImplicitParamListEndToken;
     tok
   else
     @sep.dup<<tok.to_s
   end unless NoWsToken===lasttok
   
   if str_needs_escnls
     result=result.to_s
     result.gsub!(/(["`\/])$/){ "\\\n"*str_needs_escnls+$1 }
   end

   @lasttok=tok
   @inws=false

   return result
end

#lastfal_every_10_linesObject



161
162
163
164
165
166
# File 'lib/rubylexer/tokenprinter.rb', line 161

def lastfal_every_10_lines
   if(@lastprintline+=1) > 10
      @lastprintline=0
      %Q[ #@lastfal]
   end
end

#pprint(tok, output = $stdout) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/rubylexer/tokenprinter.rb', line 117

def pprint(tok,output=$stdout)
   @accum<<aprint(tok).to_s
   if (@accum.size>ACCUMSIZE and NewlineToken===tok) or EoiToken===tok
      output.print(@accum.join)
      @accum=[]
   end
end

#ws_would_change_meaning?(tok, lasttok) ⇒ Boolean

insert extra ws unless an ambiguous op immediately follows id or num, in which case ws would change the meaning

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
# File 'lib/rubylexer/tokenprinter.rb', line 171

def ws_would_change_meaning?(tok,lasttok)   #yukk -- is it used?
   tok.kind_of?(String) and
   /^[%(]$/===tok and #is ambiguous operator?
       lasttok and
       (lasttok.kind_of?(Numeric) or
        (lasttok.kind_of?(String) and
         /^[$@a-zA-Z_]/===@lasttok)) #lasttok is id or num?
end