Class: FormatLine
- Inherits:
-
Object
- Object
- FormatLine
- Defined in:
- lib/formatline.rb
Constant Summary collapse
- SimpleFormats =
{}
- Null =
""- Space =
" "- Alpha =
/[A-Za-z]/- AlNum =
/[A-Za-z0-9_]/- LF =
"\n"- LBrack =
"["- Blank =
[" ", nil, "\n"]
- Punc =
[")", ",", ".", " ", "\n"]
- NoAlpha =
/[^A-Za-z0-9_]/- NoAlphaDot =
/[^.A-Za-z0-9_]/- Param =
["]", "\n", nil]
- Escape =
not an ESC char
"\\"- Syms =
{ "*" => :b, "_" => :i, "`" => :t, "~" => :s }
Instance Attribute Summary collapse
-
#out ⇒ Object
readonly
Returns the value of attribute out.
-
#tokenlist ⇒ Object
readonly
Returns the value of attribute tokenlist.
Class Method Summary collapse
Instance Method Summary collapse
- #add(str) ⇒ Object
- #add_token(kind, token = @token) ⇒ Object
- #collect!(sym, terminators, param = false) ⇒ Object
- #curr ⇒ Object
- #dollar ⇒ Object
- #double_dollar ⇒ Object
- #double_marker(char) ⇒ Object
- #embed(sym, str) ⇒ Object
- #embedded? ⇒ Boolean
- #evaluate(tokens = @tokenlist) ⇒ Object
-
#funcall(name, param) ⇒ Object
From FormatLine:.
- #grab ⇒ Object
- #grab_alpha ⇒ Object
- #grab_alpha_dot ⇒ Object
- #grab_colon_param ⇒ Object
- #grab_func_param ⇒ Object
-
#initialize(line) ⇒ FormatLine
constructor
A new instance of FormatLine.
- #long_marker(char) ⇒ Object
-
#marker(char) ⇒ Object
def dollar_dot add_token :ddot, @line end.
- #next! ⇒ Object
- #prev ⇒ Object
- #showme(tag) ⇒ Object
- #terminate?(terminators, ch) ⇒ Boolean
- #tokenize(line) ⇒ Object
- #varsub(name) ⇒ Object
Constructor Details
#initialize(line) ⇒ FormatLine
35 36 37 38 39 40 |
# File 'lib/formatline.rb', line 35 def initialize(line) @line = line @i = -1 @token = Null.dup @tokenlist = [] end |
Instance Attribute Details
#out ⇒ Object (readonly)
Returns the value of attribute out.
32 33 34 |
# File 'lib/formatline.rb', line 32 def out @out end |
#tokenlist ⇒ Object (readonly)
Returns the value of attribute tokenlist.
33 34 35 |
# File 'lib/formatline.rb', line 33 def tokenlist @tokenlist end |
Class Method Details
.parse!(line) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/formatline.rb', line 42 def self.parse!(line) return nil if line.nil? x = self.new(line.chomp) t = x.tokenize(line) x.evaluate end |
.var_func_parse(str) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/formatline.rb', line 74 def self.var_func_parse(str) return nil if str.nil? x = self.new(str.chomp) x.grab loop do case x.curr when Escape; x.grab; x.add x.curr; x.grab when "$" x.dollar when LF, nil break else x.add x.curr end x.grab end x.add_token(:str) x.evaluate end |
Instance Method Details
#add(str) ⇒ Object
191 192 193 |
# File 'lib/formatline.rb', line 191 def add(str) @token << str unless str.nil? end |
#add_token(kind, token = @token) ⇒ Object
195 196 197 198 |
# File 'lib/formatline.rb', line 195 def add_token(kind, token = @token) @tokenlist << [kind, token] unless token.empty? @token = Null.dup end |
#collect!(sym, terminators, param = false) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/formatline.rb', line 313 def collect!(sym, terminators, param=false) str = Null.dup # next is not " ","*","[" grab loop do if curr == Escape str << grab # ch = escaped char grab next end break if terminate?(terminators, curr) str << curr # not a terminator grab end grab if param && curr == "]" # skip right bracket add str end |
#curr ⇒ Object
133 134 135 |
# File 'lib/formatline.rb', line 133 def curr @line[@i] end |
#dollar ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/formatline.rb', line 224 def dollar grab case curr when LF; add "$"; add_token :str when " "; add "$ "; add_token :str when nil; add "$"; add_token :str when "$"; double_dollar # when "."; dollar_dot when /[A-Za-z]/ add_token :str var = curr + grab_alpha_dot add_token(:var, var) else add "$" + curr add_token(:string) end end |
#double_dollar ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/formatline.rb', line 242 def double_dollar case next! when Space; add_token :string, "$$ "; grab; return when LF, nil; add "$$"; add_token :str when Alpha add_token(:str, @token) func = grab_alpha add_token(:func, func) case next! when ":"; param = grab_colon_param; add_token(:colon, param) when "["; param = grab_func_param; add_token(:brackets, param) else # do nothing end else grab; add_token :str, "$$" + curr; return end end |
#double_marker(char) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/formatline.rb', line 290 def double_marker(char) sym = Syms[char] grab kind = sym # "string_#{char}".to_sym case next! # first char after ** when Space, LF, nil pre, post = SimpleFormats[sym] add_token kind else str = collect!(sym, Punc) grab unless next!.nil? add_token kind, str end end |
#embed(sym, str) ⇒ Object
94 95 96 97 |
# File 'lib/formatline.rb', line 94 def (sym, str) pre, post = SimpleFormats[sym] pre + str + post end |
#embedded? ⇒ Boolean
357 358 359 |
# File 'lib/formatline.rb', line 357 def ! (['"', "'", " ", nil].include? prev) end |
#evaluate(tokens = @tokenlist) ⇒ Object
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 |
# File 'lib/formatline.rb', line 99 def evaluate(tokens = @tokenlist) @out = "" return "" if tokens.empty? gen = tokens.each token = gen.next loop do break if token.nil? sym, val = *token case sym when :str @out << val unless val == "\n" # BUG when :var # STDERR.puts "=== lt: sym = #{sym} val = #{val} sub = #{varsub(val).inspect} #{Livetext::Vars[sym].inspect}" @out << varsub(val) when :func param = nil arg = gen.peek if [:colon, :brackets].include? arg[0] arg = gen.next # for real param = arg[1] param = FormatLine.var_func_parse(param) end @out << funcall(val, param) when :b, :i, :t, :s val = FormatLine.var_func_parse(val) @out << (sym, val) else add_token :str end token = gen.next end @out end |
#funcall(name, param) ⇒ Object
From FormatLine:
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/formatline.rb', line 334 def funcall(name, param) result = if self.respond_to?("func_" + name.to_s) self.send("func_" + name.to_s, param) else fobj = ::Livetext::Functions.new fobj.send(name, param) end result end |
#grab ⇒ Object
145 146 147 |
# File 'lib/formatline.rb', line 145 def grab @line[@i+=1] end |
#grab_alpha ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/formatline.rb', line 200 def grab_alpha str = Null.dup grab loop do break if curr.nil? str << curr break if terminate?(NoAlpha, next!) grab end str end |
#grab_alpha_dot ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/formatline.rb', line 212 def grab_alpha_dot str = Null.dup grab loop do break if curr.nil? str << curr break if terminate?(NoAlphaDot, next!) grab end str end |
#grab_colon_param ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/formatline.rb', line 149 def grab_colon_param grab # grab : param = "" loop do case next! when Escape grab param << next! grab when Space, LF, nil; break else param << next! grab end end param = nil if param.empty? param end |
#grab_func_param ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/formatline.rb', line 169 def grab_func_param grab # [ param = "" loop do case next! when Escape grab param << next! grab when "]", LF, nil; break else param << next! grab end end add curr grab param = nil if param.empty? param end |
#long_marker(char) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/formatline.rb', line 305 def long_marker(char) sym = Syms[char] # grab # skip left bracket kind = sym # "param_#{sym}".to_sym arg = collect!(sym, Param, true) add_token kind, arg end |
#marker(char) ⇒ Object
def dollar_dot
add_token :ddot, @line[@i..-1]
end
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/formatline.rb', line 264 def marker(char) add_token :str sym = Syms[char] if # add char # ??? add_token "*", :string return end grab case curr when Space add char + " " add_token :str grab when LF, nil add char add_token :str when char; double_marker(char) when LBrack; long_marker(char) else add curr str = collect!(sym, Blank) add_token sym, str add curr # next char onto next token... end end |
#next! ⇒ Object
141 142 143 |
# File 'lib/formatline.rb', line 141 def next! @line[@i+1] end |
#prev ⇒ Object
137 138 139 |
# File 'lib/formatline.rb', line 137 def prev @line[@i-1] end |
#showme(tag) ⇒ Object
352 353 354 355 |
# File 'lib/formatline.rb', line 352 def showme(tag) char = @line[@cc] puts "--- #{tag}: ch=#{@ch.inspect} next=#{@next.inspect} (cc=#@cc:#{char.inspect}) out=#{@out.inspect}" end |
#terminate?(terminators, ch) ⇒ Boolean
24 25 26 27 28 29 30 |
# File 'lib/formatline.rb', line 24 def terminate?(terminators, ch) if terminators.is_a? Regexp terminators === ch else terminators.include?(ch) end end |
#tokenize(line) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/formatline.rb', line 49 def tokenize(line) grab loop do case curr when Escape; grab; add curr; grab; add curr # puts "Found #{curr.inspect}" when "$" dollar when "*", "_", "`", "~" marker curr add curr # grab when LF break if @i >= line.size - 1 when nil break else add curr end grab end add_token(:str) @tokenlist end |