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
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
#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
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
189 190 191 |
# File 'lib/formatline.rb', line 189 def add(str) @token << str unless str.nil? end |
#add_token(kind, token = @token) ⇒ Object
193 194 195 196 |
# File 'lib/formatline.rb', line 193 def add_token(kind, token = @token) @tokenlist << [kind, token] unless token.empty? @token = Null.dup end |
#collect!(sym, terminators, param = false) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/formatline.rb', line 311 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 rescue => err STDERR.puts "ERR = #{err}\n#{err.backtrace}" STDERR.puts "=== str = #{str.inspect}" end |
#curr ⇒ Object
131 132 133 |
# File 'lib/formatline.rb', line 131 def curr @line[@i] end |
#dollar ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/formatline.rb', line 222 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
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/formatline.rb', line 240 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
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/formatline.rb', line 288 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 (sym, str) pre, post = SimpleFormats[sym] pre + str + post end |
#embedded? ⇒ Boolean
358 359 360 |
# File 'lib/formatline.rb', line 358 def ! (['"', "'", " ", 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 129 |
# 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 # 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:
335 336 337 338 339 340 341 342 343 344 |
# File 'lib/formatline.rb', line 335 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
143 144 145 |
# File 'lib/formatline.rb', line 143 def grab @line[@i+=1] end |
#grab_alpha ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/formatline.rb', line 198 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
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/formatline.rb', line 210 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
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/formatline.rb', line 147 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/formatline.rb', line 167 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
303 304 305 306 307 308 309 |
# File 'lib/formatline.rb', line 303 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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/formatline.rb', line 262 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
139 140 141 |
# File 'lib/formatline.rb', line 139 def next! @line[@i+1] end |
#prev ⇒ Object
135 136 137 |
# File 'lib/formatline.rb', line 135 def prev @line[@i-1] end |
#showme(tag) ⇒ Object
353 354 355 356 |
# File 'lib/formatline.rb', line 353 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 |
# 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 |