Class: FormatLine

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ FormatLine

Returns a new instance of 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

#outObject (readonly)

Returns the value of attribute out.



32
33
34
# File 'lib/formatline.rb', line 32

def out
  @out
end

#tokenlistObject (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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/formatline.rb', line 72

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



193
194
195
# File 'lib/formatline.rb', line 193

def add(str)
  @token << str unless str.nil?
end

#add_token(kind, token = @token) ⇒ Object



197
198
199
200
201
# File 'lib/formatline.rb', line 197

def add_token(kind, token = @token)
  return if token.nil?
  @tokenlist << [kind, token] unless token.empty?
  @token = Null.dup
end

#collect!(sym, terminators, bracketed = nil) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/formatline.rb', line 349

def collect!(sym, terminators, bracketed=nil)
  return collect_bracketed(sym, terminators) if bracketed

  str = Null.dup   # next is not " ","*","["
  grab   # ZZZ
  loop do
    case
      when curr.nil?
        return str
      when curr == Escape
        str << escaped
        next
      when terminate?(terminators, curr)
        break 
    else
      str << curr    # not a terminator
    end
    grab
  end
  ungrab
  add str
  str
rescue => err
  STDERR.puts "ERR = #{err}\n#{err.backtrace}"
  STDERR.puts "=== str = #{str.inspect}"
end

#collect_bracketed(sym, terminators) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/formatline.rb', line 315

def collect_bracketed(sym, terminators)
  str = Null.dup   # next is not " ","*","["
  grab   # ZZZ
  loop do
    if curr == Escape
      str << grab # ch = escaped char
      grab
      next
    end
    if terminate?(terminators, curr)
      break 
    end
                # STDERR.puts "#{curr.inspect} is not a terminator"
    str << curr    # not a terminator
    grab
                # STDERR.puts "After grab, curr is #{curr.inspect}"
  end

  if curr == "]" # skip right bracket
    grab 
  end
  add str
  str
rescue => err
  STDERR.puts "ERR = #{err}\n#{err.backtrace}"
  STDERR.puts "=== str = #{str.inspect}"
end

#currObject



130
131
132
# File 'lib/formatline.rb', line 130

def curr
  @line[@i]
end

#dollarObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/formatline.rb', line 227

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_dollarObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/formatline.rb', line 245

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)
      end
    else
      grab; add_token :str, "$$" + curr; return
  end
end

#double_marker(char) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/formatline.rb', line 293

def double_marker(char)
  sym = Syms[char]
  kind = sym
  case next!   # first char after **
    when Space, LF, nil
      pre, post = SimpleFormats[sym]
      add_token kind
    else
      str = collect!(sym, Punc)
      add_token kind, str
      grab 
  end
end

#embed(sym, str) ⇒ Object



92
93
94
95
# File 'lib/formatline.rb', line 92

def embed(sym, str)
  pre, post = SimpleFormats[sym]
  pre + str + post
end

#embedded?Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/formatline.rb', line 403

def embedded?
  ! (['"', "'", " ", nil].include? prev)
end

#escapedObject



343
344
345
346
347
# File 'lib/formatline.rb', line 343

def escaped
  ch = grab
  grab
  ch
end

#evaluate(tokens = @tokenlist) ⇒ Object



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
# File 'lib/formatline.rb', line 97

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
        @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 << embed(sym, val)
    else
      add_token :str
    end
    token = gen.next
  end
  @out
end

#funcall(name, param) ⇒ Object

From FormatLine:



380
381
382
383
384
385
386
387
388
389
# File 'lib/formatline.rb', line 380

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

#grabObject



143
144
145
# File 'lib/formatline.rb', line 143

def grab
  @line[@i+=1]
end

#grab_alphaObject



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/formatline.rb', line 203

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_dotObject



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/formatline.rb', line 215

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_paramObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/formatline.rb', line 151

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_paramObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/formatline.rb', line 171

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



307
308
309
310
311
312
313
# File 'lib/formatline.rb', line 307

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/formatline.rb', line 266

def marker(char)
  add_token :str
  sym = Syms[char]
  if embedded?
#     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
    str = curr + collect!(sym, Blank)
    add str
    add_token sym, str
    grab
  end
end

#next!Object



139
140
141
# File 'lib/formatline.rb', line 139

def next!
  @line[@i+1]
end

#prevObject



134
135
136
137
# File 'lib/formatline.rb', line 134

def prev
  return nil if @i <= 0
  @line[@i-1]
end

#showme(tag) ⇒ Object



398
399
400
401
# File 'lib/formatline.rb', line 398

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

Returns:

  • (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
# File 'lib/formatline.rb', line 49

def tokenize(line)
  grab
  loop do 
    case curr
      when Escape; grab; add curr; grab; add curr
      when "$"
        dollar
      when "*", "_", "`", "~"
        marker curr
        add curr
      when LF
        break if @i >= line.size - 1
      when nil
        break
      else
        add curr
    end
    grab
  end
  add_token(:str)
  @tokenlist
end

#ungrabObject



147
148
149
# File 'lib/formatline.rb', line 147

def ungrab
  @line[@i-=1]
end

#varsub(name) ⇒ Object



391
392
393
394
# File 'lib/formatline.rb', line 391

def varsub(name)
  result = Livetext::Vars[name] || "[#{name} is undefined]"
  result
end