Class: Livetext::Expansion
Constant Summary collapse
- Ident =
"[[:alpha:]]([[:alnum:]]|_)*"- Dotted =
"#{Ident}(\\.#{Ident})*"- Func =
"\\$\\$"- Var =
"\\$"- Lbrack =
"\\["- Colon =
":"
Instance Method Summary collapse
- #expand_function_calls(str) ⇒ Object
- #expand_variables(str) ⇒ Object
- #format(line) ⇒ Object
- #funcall(name, param) ⇒ Object
-
#initialize(instance) ⇒ Expansion
constructor
A new instance of Expansion.
Constructor Details
#initialize(instance) ⇒ Expansion
Returns a new instance of Expansion.
11 12 13 |
# File 'lib/livetext/expansion.rb', line 11 def initialize(instance) @live = instance end |
Instance Method Details
#expand_function_calls(str) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/livetext/expansion.rb', line 62 def (str) # Assume variables already resolved pat1 = "(?<result>" + Func + Dotted + ")" colon = ":" lbrack = "\\[" rbrack = "\\]" space_eol = "( |$)" prx1 = "(?<param>[^ ]+)" prx2 = "(?<param>.+)" pat2 = "(?<full_param>#{colon}#{prx1})" pat3 = "(?<full_param>#{lbrack}#{prx2}#{rbrack})" rx = Regexp.compile("#{pat1}(#{pat2}|#{pat3})?") buffer = "" loop do |i| case # Var or Func or false alarm when str.nil? return buffer when str.empty? # end of string break when str.slice(0..1) == "$$" # Func? fmatch = rx.match(str) fname = fmatch["result"] # includes $$ param = fmatch["param"] # may be nil full = fmatch["full_param"] fsym = fname[2..-1] # no $$ #STDERR.puts "rx = #{rx.inspect}" #STDERR.puts "fmatch = #{fmatch.inspect}" #STDERR.puts "fname = #{fname.inspect}" #STDERR.puts "param = #{param.inspect}" #STDERR.puts "full = #{full.inspect}" #STDERR.puts "fsym = #{fsym.inspect}" str.sub!(fname, "") str.sub!(full, "") if full retval = funcall(fsym, param) # puts "retval = #{retval.inspect}" buffer << retval else # other char = str.slice!(0) buffer << char end end # STDERR.puts "buffer = #{buffer.inspect}" buffer end |
#expand_variables(str) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/livetext/expansion.rb', line 22 def (str) rx = Regexp.compile("(?<result>" + Var + Dotted + ")") buffer = "" loop do |i| case # var or func or false alarm when str.empty? # end of string break when str.slice(0..1) == "\\$" # escaped, ignore it str.slice!(0..1) buffer << "$" when str.slice(0..1) == "$$" # func? buffer << str.slice!(0..1) when str.slice(0) == "$" # var? vmatch = rx.match(str) if vmatch.nil? buffer << str.slice!(0) next end vname = vmatch["result"] str.sub!(vname, "") vsym = vname[1..-1].to_sym vars = @live.vars buffer << vars.get(vsym) else # other char = str.slice!(0) buffer << char end end buffer end |
#format(line) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/livetext/expansion.rb', line 15 def format(line) return "" if line == "\n" || line.nil? with_vars = (line) with_func = (with_vars) formatted = Formatter.format(with_func) end |
#funcall(name, param) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/livetext/expansion.rb', line 53 def funcall(name, param) err = "[Error evaluating $$#{name}(#{param})]" name = name.gsub(/\./, "__") return if self.send?(name, param) fobj = ::Livetext::Functions.new result = fobj.send(name, param) rescue err result.to_s end |