Symbol table that provides for one-pass replacement of all symbols
surrounded by delimiters with the symbol values
:delims |
[R] |
|
:var_restr |
[R] |
|
- delims
- pre- and post-name delimiters for symbol dereferencing, separated by
whitespace (e.g., "$ $"). A symbol not bracketed by
these will not be substituted.
# File sytab.rb, line 49
def initialize(delims)
darr = delims.split
@mpos = darr[0].size
@mend = -(darr[1].size)
@delims = darr.map {
|s| Regexp.escape(s)
}
@regex = nil
@var_restr = [ @delims[0], "[a-zA-Z_][a-zA-Z_0-9]*", @delims[1] ].join
@var_regex = Regexp.new(@var_restr)
end
Call Hash#delete and invalidate match expression.
# File sytab.rb, line 65
def delete(k)
super(k)
@regex = nil
end
Call Array#[]= and invalidate match expression.
# File sytab.rb, line 72
def []=(k,v)
super(k,v)
@regex = nil
end
Call Array#store and invalidate match expression.
# File sytab.rb, line 79
def store(k,v)
super(k,v)
@regex = nil
end
Build a new match expression consisting of each key of the hash surrounded
by delimiters, then grouped with parens, and each group separated by
"|".
# File sytab.rb, line 88
def compile!()
@regex = Regexp.new(keys.map {
|k| "(" + @delims[0] + k + @delims[1] + ")"
}.join("|"))
end
Return a string with each occurrence of a key surrounded by delimiters
replaced with the lookup value for the key.
# File sytab.rb, line 97
def replace(s)
if keys.size < 1
s
else
compile! unless @regex
s.gsub!(@regex) {
|match| fetch(match[@mpos...@mend])
}
s.gsub(@var_regex, "*UNDEFINED*")
end
end
Shorthand for replace(s).
# File sytab.rb, line 111
def <<(s)
replace(s)
end