Class: Livetext::Expansion

Inherits:
Object show all
Defined in:
lib/livetext/expansion.rb

Constant Summary collapse

Ident =
"[[:alpha:]]([[:alnum:]]|_)*"
Dotted =
"#{Ident}(\\.#{Ident})*"
Func =
"\\$\\$"
Var =
"\\$"
Lbrack =
"\\["
Colon =
":"

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Expansion

Livetext::Expansion



11
12
13
# File 'lib/livetext/expansion.rb', line 11

def initialize(instance)   # Livetext::Expansion
  @live = instance
end

Instance Method Details

#expand_function_calls(str) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/livetext/expansion.rb', line 82

def expand_function_calls(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 expand_variables(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 = expand_variables(line)
  with_func = expand_function_calls(with_vars)
  formatted = @live.formatter.format(with_func)
end

#funcall(name, param) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/livetext/expansion.rb', line 53

def funcall(name, param)
  # Use the unified function registry
  name = name.gsub(/\./, "__")
  result = @live.function_registry.call(name, param)
  
  # If not found in registry, fall back to old system for backward compatibility
  if result.start_with?("[Error evaluating $$#{name}(")
    # Try old Livetext::Functions system
    fobj = ::Livetext::Functions.new
    # Set api on Livetext::Functions so functions can access it
    Livetext::Functions.api = @live.api
    old_result = fobj.send(name, param) rescue nil
    return old_result.to_s if old_result
    
            # Try Livetext instance (for mixin functions)
    if @live.respond_to?(name)
      method = @live.method(name)
      if method.parameters.empty?
        old_result = @live.send(name) rescue nil
      else
        old_result = @live.send(name, param) rescue nil
      end
      return old_result.to_s if old_result
    end
  end
  
  result
end