Class: FormatLine

Inherits:
StringParser show all
Defined in:
lib/formatline.rb

Overview

Class FormatLine handles the parsing of comments, dot commands, and simple formatting characters.

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

Attributes inherited from StringParser

#eos, #i, #len, #line

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StringParser

#eos?, #grab, #next!, #peek, #prev, #skip_spaces, #ungrab

Constructor Details

#initialize(line) ⇒ FormatLine

Returns a new instance of FormatLine.



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

def initialize(line)
  super
  @token = Null.dup
  @tokenlist = []
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



27
28
29
# File 'lib/formatline.rb', line 27

def out
  @out
end

#tokenlistObject (readonly)

Returns the value of attribute tokenlist.



28
29
30
# File 'lib/formatline.rb', line 28

def tokenlist
  @tokenlist
end

Class Method Details

.parse!(line) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/formatline.rb', line 36

def self.parse!(line)
  return nil if line.nil?
  x = self.new(line.chomp)
  t = x.tokenize
# TTY.puts "tokens = \n#{t.inspect}\n "
  x.evaluate
end

.var_func_parse(str) ⇒ Object



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

def self.var_func_parse(str)
  return nil if str.nil?
  x = self.new(str.chomp)
  char = x.peek
  loop do
    char = x.grab
    break if char == LF || char == nil
    x.handle_escaping if char == Escape
    x.dollar if char == "$"
    x.add char
  end
  x.add_token(:str)
  result = x.evaluate
  result
end

Instance Method Details

#add(str) ⇒ Object



176
177
178
# File 'lib/formatline.rb', line 176

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

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



180
181
182
183
184
# File 'lib/formatline.rb', line 180

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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/formatline.rb', line 330

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 peek.nil?
        return str
      when peek == Escape
        str << escaped
        next
      when terminate?(terminators, peek)
        break 
    else
      str << peek    # 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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/formatline.rb', line 298

def collect_bracketed(sym, terminators)
  str = Null.dup   # next is not " ","*","["
  grab   # ZZZ
  loop do
    if peek == Escape
      grab
      str << grab
      next
    end
    if terminate?(terminators, peek)
      break 
    end
    str << peek    # not a terminator
    grab
  end

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

#dollarObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/formatline.rb', line 210

def dollar
  grab
  case peek
    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 = peek + grab_alpha_dot
      add_token(:var, var)
  else 
    add "$" + peek
    add_token(:string)
  end
end

#double_dollarObject



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

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, "$$" + peek; return
  end
end

#double_marker(char) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/formatline.rb', line 276

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



96
97
98
99
# File 'lib/formatline.rb', line 96

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

#embedded?Boolean

Returns:

  • (Boolean)


374
375
376
# File 'lib/formatline.rb', line 374

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

#escapedObject



324
325
326
327
328
# File 'lib/formatline.rb', line 324

def escaped
  grab
  ch = grab
  ch
end

#evaluate(tokens = @tokenlist) ⇒ Object



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

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 = Livetext.interpolate(param)
        end
        @out << funcall(val, param)
      when :b, :i, :t, :s
        val = Livetext.interpolate(val)
        @out << embed(sym, val)
    else
      add_token :str
    end
    token = gen.next
  end
  @out
end

#funcall(name, param) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
# File 'lib/formatline.rb', line 357

def funcall(name, param)
  err = "[Error evaluating $$#{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) rescue err
    end
  result
end

#grab_alphaObject



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/formatline.rb', line 186

def grab_alpha
  str = Null.dup
  grab
  loop do
    break if eos?
    str << peek
    break if terminate?(NoAlpha, next!)
    grab
  end
  str
end

#grab_alpha_dotObject



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/formatline.rb', line 198

def grab_alpha_dot
  str = Null.dup
  grab
  loop do
    break if peek.nil?   # eos?
    str << peek
    break if terminate?(NoAlphaDot, next!)
    grab
  end
  str
end

#grab_colon_paramObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/formatline.rb', line 134

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/formatline.rb', line 154

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 peek
  grab
  param = nil if param.empty?
  param
end

#handle_escapingObject



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

def handle_escaping
  grab
  add grab
end

#long_marker(char) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/formatline.rb', line 290

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 end



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/formatline.rb', line 249

def marker(char)
  add_token :str
  sym = Syms[char]
  if embedded?
#     add char    # ??? add_token "*", :string
    return 
  end

  grab
  case peek
    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 = peek + collect!(sym, Blank)
    add str
    add_token sym, str
    grab
  end
end

#terminate?(terminators, ch) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/formatline.rb', line 67

def terminate?(terminators, ch)
  if terminators.is_a? Regexp
    terminators === ch
  else
    terminators.include?(ch)
  end
end

#tokenizeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/formatline.rb', line 44

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

#varsub(name) ⇒ Object



369
370
371
372
# File 'lib/formatline.rb', line 369

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