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_]/
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.



34
35
36
37
38
39
# File 'lib/formatline.rb', line 34

def initialize(line)
  @line = line
  @i = -1
  @token = Null.dup
  @tokenlist = []
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



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

def out
  @out
end

#tokenlistObject (readonly)

Returns the value of attribute tokenlist.



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

def tokenlist
  @tokenlist
end

Class Method Details

.parse!(line) ⇒ Object



41
42
43
44
45
46
# File 'lib/formatline.rb', line 41

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.go; 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



188
189
190
# File 'lib/formatline.rb', line 188

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

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



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

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

#collect!(sym, terminators, param = false) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/formatline.rb', line 298

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

#currObject



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

def curr
  @line[@i]
end

#dollarObject



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

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
      add_token(:var, var)
  else 
    add "$" + curr
    add_token(:string)
  end
end

#double_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 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



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

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



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)


342
343
344
# File 'lib/formatline.rb', line 342

def embedded?
  ! (['"', "'", " ", nil].include? prev)
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:



319
320
321
322
323
324
325
326
327
328
# File 'lib/formatline.rb', line 319

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



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

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

#grab_alphaObject



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

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_colon_paramObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/formatline.rb', line 146

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/formatline.rb', line 166

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



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[@i..-1]

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



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

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

#prevObject



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

def prev
  @line[@i-1]
end

#showme(tag) ⇒ Object



337
338
339
340
# File 'lib/formatline.rb', line 337

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)


23
24
25
26
27
28
29
# File 'lib/formatline.rb', line 23

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

#tokenize(line) ⇒ Object



48
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 48

def tokenize(line)
  grab
  loop do 
    case curr
      when Escape; go; add curr; grab
      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

#varsub(name) ⇒ Object



330
331
332
333
# File 'lib/formatline.rb', line 330

def varsub(name)
  result = Livetext::Vars[name]
  result
end