Class: Interscript::Interpreter::Stage
- Inherits:
-
Object
- Object
- Interscript::Interpreter::Stage
- Defined in:
- lib/interscript/interpreter.rb
Instance Method Summary collapse
- #build_item(i, target = nil, doc = @map) ⇒ Object
- #build_regexp(r) ⇒ Object
- #execute_rule(r) ⇒ Object
-
#initialize(map, str) ⇒ Stage
constructor
A new instance of Stage.
Constructor Details
#initialize(map, str) ⇒ Stage
Returns a new instance of Stage.
73 74 75 76 |
# File 'lib/interscript/interpreter.rb', line 73 def initialize(map, str) @str = str @map = map end |
Instance Method Details
#build_item(i, target = nil, doc = @map) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/interscript/interpreter.rb', line 171 def build_item i, target=nil, doc=@map i = i.nth_string if %i[str parstr].include? target i = Interscript::Node::Item.try_convert(i) target = :par if target == :parstr out = case i when Interscript::Node::Item::Alias if i.map d = doc.dep_aliases[i.map].document a = d.imported_aliases[i.name] raise Interscript::SystemConversionError, "Alias #{i.name} of #{i.stage.map} not found" unless a build_item(a.data, target, d) elsif Interscript::Stdlib::ALIASES.include?(i.name) if target != :re && Interscript::Stdlib.re_only_alias?(i.name) raise Interscript::SystemConversionError, "Can't use #{i.name} in a #{target} context" end Interscript::Stdlib::ALIASES[i.name] else a = doc.imported_aliases[i.name] raise Interscript::SystemConversionError, "Alias #{i.name} not found" unless a build_item(a.data, target, doc) end when Interscript::Node::Item::String if [:str, :par].include? target i.data else#if target == :re Regexp.escape(i.data) end when Interscript::Node::Item::Group if target == :par i.children.map do |j| build_item(j, target, doc) end.reduce([""]) do |j,k| Array(j).product(Array(k)).map(&:join) end else i.children.map { |j| build_item(j, target, doc) }.join end when Interscript::Node::Item::CaptureGroup if target == :par raise Interscript::SystemConversionError, "Can't use a CaptureGroup in a #{target} context" end "(" + build_item(i.data, target, doc) + ")" when Interscript::Node::Item::Maybe, Interscript::Node::Item::MaybeSome, Interscript::Node::Item::Some resuffix = { Interscript::Node::Item::Maybe => "?" , Interscript::Node::Item::Some => "+" , Interscript::Node::Item::MaybeSome => "*" }[i.class] if target == :par raise Interscript::SystemConversionError, "Can't use a MaybeSome in a #{target} context" end if Interscript::Node::Item::String === i.data && i.data.data.length != 1 "(?:" + build_item(i.data, target, doc) + ")" + resuffix else build_item(i.data, target, doc) + resuffix end when Interscript::Node::Item::CaptureRef if target == :par raise Interscript::SystemConversionError, "Can't use CaptureRef in parallel mode" end "\\#{i.id}" when Interscript::Node::Item::Any if target == :str # We may never reach this point raise Interscript::SystemConversionError, "Can't use Any in a string context" elsif target == :par i.data.map(&:data) elsif target == :re case i.value when Array data = i.data.map { |j| build_item(j, target, doc) } "(?:"+data.join("|")+")" when String "[#{Regexp.escape(i.value)}]" when Range "[#{Regexp.escape(i.value.first)}-#{Regexp.escape(i.value.last)}]" end end end end |
#build_regexp(r) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/interscript/interpreter.rb', line 155 def build_regexp(r) from = build_item(r.from, :re) before = build_item(r.before, :re) if r.before after = build_item(r.after, :re) if r.after not_before = build_item(r.not_before, :re) if r.not_before not_after = build_item(r.not_after, :re) if r.not_after re = "" re += "(?<=#{before})" if before re += "(?<!#{not_before})" if not_before re += from re += "(?!#{not_after})" if not_after re += "(?=#{after})" if after re end |
#execute_rule(r) ⇒ Object
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/interscript/interpreter.rb', line 78 def execute_rule r return if r.reverse_run == true case r when Interscript::Node::Group::Parallel if r.cached_tree @str = Interscript::Stdlib.parallel_replace_tree(@str, r.cached_tree) elsif r.subs_regexp && r.subs_replacements if $DEBUG_RE @str = Interscript::Stdlib.parallel_regexp_gsub_debug(@str, r.subs_regexp, r.subs_replacements) else @str = Interscript::Stdlib.parallel_regexp_gsub(@str, r.subs_regexp, r.subs_replacements) end else begin # Try to build a tree subs_array = [] r.children.each do |i| raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i raise Interscript::SystemConversionError, "Can't parallelize rules with :before" if i.before raise Interscript::SystemConversionError, "Can't parallelize rules with :after" if i.after raise Interscript::SystemConversionError, "Can't parallelize rules with :not_before" if i.not_before raise Interscript::SystemConversionError, "Can't parallelize rules with :not_after" if i.not_after next if i.reverse_run == true subs_array << [build_item(i.from, :par), build_item(i.to, :parstr)] end tree = Interscript::Stdlib.parallel_replace_compile_tree(subs_array) #.sort_by{|k,v| -k.length}) @str = Interscript::Stdlib.parallel_replace_tree(@str, tree) r.cached_tree = tree # $using_tree = true rescue # $using_tree = false # Otherwise let's build a megaregexp subs_array = [] Interscript::Stdlib.deterministic_sort_by_max_length(r.children).each do |i| # rule.from.max_length gives somewhat better test results, why is that raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i next if i.reverse_run == true subs_array << [build_regexp(i), build_item(i.to, :parstr)] end r.subs_regexp = Interscript::Stdlib.parallel_regexp_compile(subs_array) r.subs_replacements = subs_array.map(&:last) if $DEBUG_RE # puts subs_array.inspect $subs_array = subs_array @str = Interscript::Stdlib.parallel_regexp_gsub_debug(@str, r.subs_regexp, r.subs_replacements) else @str = Interscript::Stdlib.parallel_regexp_gsub(@str, r.subs_regexp, r.subs_replacements) end end end when Interscript::Node::Group r.children.each do |t| execute_rule(t) end when Interscript::Node::Rule::Sub if r.to == :upcase @str = @str.gsub(Regexp.new(build_regexp(r)), &:upcase) elsif r.to == :downcase @str = @str.gsub(Regexp.new(build_regexp(r)), &:downcase) else @str = @str.gsub(Regexp.new(build_regexp(r)), build_item(r.to, :str)) end when Interscript::Node::Rule::Funcall @str = Interscript::Stdlib::Functions.public_send(r.name, @str, **r.kwargs) when Interscript::Node::Rule::Run if r.stage.map doc = @map.dep_aliases[r.stage.map].document stage = doc.imported_stages[r.stage.name] @str = Stage.new(doc, @str).execute_rule(stage) else stage = @map.imported_stages[r.stage.name] @str = Stage.new(@map, @str).execute_rule(stage) end end @str end |