Skip to content

Commit 20de3e3

Browse files
author
webdev778
committed
Merge pull request #749 from interscript/interscript-errors
2 parents 9e2fad4 + 1857e83 commit 20de3e3

14 files changed

Lines changed: 62 additions & 55 deletions

File tree

‎lib/interscript.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
require "yaml"
33

44
module Interscript
5+
# An error caused by a lack of some map
56
class MapNotFoundError < StandardError; end
7+
# An error caused by a missing dependency
8+
class ExternalUtilError < StandardError; end
9+
# An error caused by a particular compiler
10+
class SystemConversionError < StandardError; end
11+
# An error caused by an incorrect map implementation
12+
class MapLogicError < StandardError; end
613

714
class << self
815
def load_path
@@ -121,8 +128,8 @@ def rababa_provision(model_name, model_uri)
121128
write_path = path unless write_path
122129
rescue
123130
end
124-
125-
raise StandardError, "Can't find a writable path for Rababa. Consider setting a RABABA_DATA environment variable" unless write_path
131+
132+
raise ExternalUtilError, "Can't find a writable path for Rababa. Consider setting a RABABA_DATA environment variable" unless write_path
126133

127134
model_path = "#{write_path}/model-#{model_name}.onnx"
128135

‎lib/interscript/compiler/javascript.rb‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ def compile_rule(r, map = @map, wrapper = false)
7070
# Try to build a tree
7171
a = []
7272
r.children.each do |i|
73-
raise ArgumentError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
74-
raise ArgumentError, "Can't parallelize rules with :before" if i.before
75-
raise ArgumentError, "Can't parallelize rules with :after" if i.after
76-
raise ArgumentError, "Can't parallelize rules with :not_before" if i.not_before
77-
raise ArgumentError, "Can't parallelize rules with :not_after" if i.not_after
73+
raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
74+
raise Interscript::SystemConversionError, "Can't parallelize rules with :before" if i.before
75+
raise Interscript::SystemConversionError, "Can't parallelize rules with :after" if i.after
76+
raise Interscript::SystemConversionError, "Can't parallelize rules with :not_before" if i.not_before
77+
raise Interscript::SystemConversionError, "Can't parallelize rules with :not_after" if i.not_after
7878

7979
next if i.reverse_run == true
8080
a << [compile_item(i.from, map, :par), compile_item(i.to, map, :parstr)]
@@ -89,7 +89,7 @@ def compile_rule(r, map = @map, wrapper = false)
8989
# Otherwise let's build a megaregexp
9090
a = []
9191
Interscript::Stdlib.deterministic_sort_by_max_length(r.children).each do |i|
92-
raise ArgumentError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
92+
raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
9393

9494
next if i.reverse_run == true
9595
a << [build_regexp(i, map), compile_item(i.to, map, :parstr)]
@@ -122,7 +122,7 @@ def compile_rule(r, map = @map, wrapper = false)
122122
end
123123
c += "s = Interscript.transliterate(#{stage.doc_name.to_json}, s, #{stage.name.to_json});\n"
124124
else
125-
raise ArgumentError, "Can't compile unhandled #{r.class}"
125+
raise Interscript::SystemConversionError, "Can't compile unhandled #{r.class}"
126126
end
127127
c
128128
end
@@ -157,17 +157,17 @@ def compile_item i, doc=@map, target=nil
157157
astr = if i.map
158158
d = doc.dep_aliases[i.map].document
159159
a = d.imported_aliases[i.name]
160-
raise ArgumentError, "Alias #{i.name} of #{i.stage.map} not found" unless a
160+
raise Interscript::SystemConversionError, "Alias #{i.name} of #{i.stage.map} not found" unless a
161161
"Interscript.get_alias_ALIASTYPE(#{a.doc_name.to_json}, #{a.name.to_json})"
162162
elsif Interscript::Stdlib::ALIASES.include?(i.name)
163163
if target != :re && Interscript::Stdlib.re_only_alias?(i.name)
164-
raise ArgumentError, "Can't use #{i.name} in a #{target} context"
164+
raise Interscript::SystemConversionError, "Can't use #{i.name} in a #{target} context"
165165
end
166166
stdlib_alias = true
167167
"Interscript.aliases.#{i.name}"
168168
else
169169
a = doc.imported_aliases[i.name]
170-
raise ArgumentError, "Alias #{i.name} not found" unless a
170+
raise Interscript::SystemConversionError, "Alias #{i.name} not found" unless a
171171

172172
"Interscript.get_alias_ALIASTYPE(#{a.doc_name.to_json}, #{a.name.to_json})"
173173
end
@@ -205,7 +205,7 @@ def compile_item i, doc=@map, target=nil
205205
end
206206
when Interscript::Node::Item::CaptureGroup
207207
if target != :re
208-
raise ArgumentError, "Can't use a CaptureGroup in a #{target} context"
208+
raise Interscript::SystemConversionError, "Can't use a CaptureGroup in a #{target} context"
209209
end
210210
"(" + compile_item(i.data, doc, target) + ")"
211211
when Interscript::Node::Item::Maybe,
@@ -217,7 +217,7 @@ def compile_item i, doc=@map, target=nil
217217
Interscript::Node::Item::MaybeSome => "*" }[i.class]
218218

219219
if target == :par
220-
raise ArgumentError, "Can't use a MaybeSome in a #{target} context"
220+
raise Interscript::SystemConversionError, "Can't use a MaybeSome in a #{target} context"
221221
end
222222
if Interscript::Node::Item::String === i.data && i.data.data.length != 1
223223
"(?:" + compile_item(i.data, doc, target) + ")" + resuffix
@@ -226,15 +226,15 @@ def compile_item i, doc=@map, target=nil
226226
end
227227
when Interscript::Node::Item::CaptureRef
228228
if target == :par
229-
raise ArgumentError, "Can't use CaptureRef in parallel mode"
229+
raise Interscript::SystemConversionError, "Can't use CaptureRef in parallel mode"
230230
elsif target == :re
231231
"\\\\#{i.id}"
232232
elsif target == :str
233233
"\"$#{i.id}\""
234234
end
235235
when Interscript::Node::Item::Any
236236
if target == :str
237-
raise ArgumentError, "Can't use Any in a string context" # A linter could find this!
237+
raise Interscript::SystemConversionError, "Can't use Any in a string context" # A linter could find this!
238238
elsif target == :par
239239
i.data.map(&:data)
240240
elsif target == :re

‎lib/interscript/compiler/ruby.rb‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def compile_rule(r, map = @map, wrapper = false)
6060
# Try to build a tree
6161
a = []
6262
r.children.each do |i|
63-
raise ArgumentError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
64-
raise ArgumentError, "Can't parallelize rules with :before" if i.before
65-
raise ArgumentError, "Can't parallelize rules with :after" if i.after
66-
raise ArgumentError, "Can't parallelize rules with :not_before" if i.not_before
67-
raise ArgumentError, "Can't parallelize rules with :not_after" if i.not_after
63+
raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
64+
raise Interscript::SystemConversionError, "Can't parallelize rules with :before" if i.before
65+
raise Interscript::SystemConversionError, "Can't parallelize rules with :after" if i.after
66+
raise Interscript::SystemConversionError, "Can't parallelize rules with :not_before" if i.not_before
67+
raise Interscript::SystemConversionError, "Can't parallelize rules with :not_after" if i.not_after
6868

6969
next if i.reverse_run == true
7070
a << [compile_item(i.from, map, :par), compile_item(i.to, map, :parstr)]
@@ -79,7 +79,7 @@ def compile_rule(r, map = @map, wrapper = false)
7979
# Otherwise let's build a megaregexp
8080
a = []
8181
Interscript::Stdlib.deterministic_sort_by_max_length(r.children).each do |i|
82-
raise ArgumentError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
82+
raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
8383

8484
next if i.reverse_run == true
8585
a << [build_regexp(i, map), compile_item(i.to, map, :parstr)]
@@ -112,7 +112,7 @@ def compile_rule(r, map = @map, wrapper = false)
112112
end
113113
c += "s = Interscript::Maps.transliterate(#{stage.doc_name.inspect}, s, #{stage.name.inspect})\n"
114114
else
115-
raise ArgumentError, "Can't compile unhandled #{r.class}"
115+
raise Interscript::SystemConversionError, "Can't compile unhandled #{r.class}"
116116
end
117117
c
118118
end
@@ -146,17 +146,17 @@ def compile_item i, doc=@map, target=nil
146146
astr = if i.map
147147
d = doc.dep_aliases[i.map].document
148148
a = d.imported_aliases[i.name]
149-
raise ArgumentError, "Alias #{i.name} of #{i.stage.map} not found" unless a
149+
raise Interscript::SystemConversionError, "Alias #{i.name} of #{i.stage.map} not found" unless a
150150
"Interscript::Maps.get_alias_ALIASTYPE(#{a.doc_name.inspect}, #{a.name.inspect})"
151151
elsif Interscript::Stdlib::ALIASES.include?(i.name)
152152
if target != :re && Interscript::Stdlib.re_only_alias?(i.name)
153-
raise ArgumentError, "Can't use #{i.name} in a #{target} context"
153+
raise Interscript::SystemConversionError, "Can't use #{i.name} in a #{target} context"
154154
end
155155
stdlib_alias = true
156156
"Interscript::Stdlib::ALIASES[#{i.name.inspect}]"
157157
else
158158
a = doc.imported_aliases[i.name]
159-
raise ArgumentError, "Alias #{i.name} not found" unless a
159+
raise Interscript::SystemConversionError, "Alias #{i.name} not found" unless a
160160

161161
"Interscript::Maps.get_alias_ALIASTYPE(#{a.doc_name.inspect}, #{a.name.inspect})"
162162
end
@@ -194,7 +194,7 @@ def compile_item i, doc=@map, target=nil
194194
end
195195
when Interscript::Node::Item::CaptureGroup
196196
if target != :re
197-
raise ArgumentError, "Can't use a CaptureGroup in a #{target} context"
197+
raise Interscript::SystemConversionError, "Can't use a CaptureGroup in a #{target} context"
198198
end
199199
"(" + compile_item(i.data, doc, target) + ")"
200200
when Interscript::Node::Item::Maybe,
@@ -206,7 +206,7 @@ def compile_item i, doc=@map, target=nil
206206
Interscript::Node::Item::MaybeSome => "*" }[i.class]
207207

208208
if target == :par
209-
raise ArgumentError, "Can't use a Maybe in a #{target} context"
209+
raise Interscript::SystemConversionError, "Can't use a Maybe in a #{target} context"
210210
end
211211
if Interscript::Node::Item::String === i.data && i.data.data.length != 1
212212
"(?:" + compile_item(i.data, doc, target) + ")" + resuffix
@@ -215,15 +215,15 @@ def compile_item i, doc=@map, target=nil
215215
end
216216
when Interscript::Node::Item::CaptureRef
217217
if target == :par
218-
raise ArgumentError, "Can't use CaptureRef in parallel mode"
218+
raise Interscript::SystemConversionError, "Can't use CaptureRef in parallel mode"
219219
elsif target == :re
220220
"\\#{i.id}"
221221
elsif target == :str
222222
"\"\\\\#{i.id}\""
223223
end
224224
when Interscript::Node::Item::Any
225225
if target == :str
226-
raise ArgumentError, "Can't use Any in a string context" # A linter could find this!
226+
raise Interscript::SystemConversionError, "Can't use Any in a string context" # A linter could find this!
227227
elsif target == :par
228228
i.data.map(&:data)
229229
elsif target == :re

‎lib/interscript/dsl.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def self.parse(map_name, reverse: true)
6767
ruby << l
6868
end
6969
end
70-
raise ArgumentError, "metadata stage isn't terminated" if md_reading
70+
raise Interscript::MapLogicError, "metadata stage isn't terminated" if md_reading
7171
ruby, yaml = ruby.join("\n"), yaml.join("\n")
7272

7373
obj = Interscript::DSL::Document.new(map_name)

‎lib/interscript/dsl/aliases.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def def_alias(name, value)
1414
end
1515

1616
unless Symbol === name
17-
raise TypeError, "Alias name must be a Symbol, given #{name.class}"
17+
raise Interscript::SystemConversionError, "Alias name must be a Symbol, given #{name.class}"
1818
end
1919

2020
puts "def_alias(#{name.inspect}, #{thing.inspect})" if $DEBUG

‎lib/interscript/dsl/group.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(&block)
1010

1111
def run(stage, **kwargs)
1212
if stage.class != Interscript::Node::Item::Stage
13-
raise TypeError, "I::Node::Item::Stage expected, got #{stage.class}"
13+
raise Interscript::MapLogicError, "I::Node::Item::Stage expected, got #{stage.class}"
1414
end
1515
@node.children << Interscript::Node::Rule::Run.new(stage, **kwargs)
1616
end

‎lib/interscript/dsl/items.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module Maps
5555
class << self
5656
# Select a remote map
5757
def [] map
58-
Symbol === map or raise TypeError, "A map name must be a Symbol, not #{alias_name.class}"
58+
Symbol === map or raise Interscript::MapLogicError, "A map name must be a Symbol, not #{alias_name.class}"
5959
Map.new(map)
6060
end
6161
alias method_missing []
@@ -68,7 +68,7 @@ def initialize name; @name = name; end
6868

6969
# Implementation of `map.x.aliasname`
7070
def [] alias_name
71-
Symbol === alias_name or raise TypeError, "An alias name must be a Symbol, not #{alias_name.class}"
71+
Symbol === alias_name or raise Interscript::MapLogicError, "An alias name must be a Symbol, not #{alias_name.class}"
7272
Interscript::Node::Item::Alias.new(alias_name, map: @name)
7373
end
7474
alias method_missing []

‎lib/interscript/dsl/metadata.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Interscript::DSL::Metadata
44
attr_accessor :node
55

66
def initialize(yaml: false, map_name: "", library: true, &block)
7-
raise ArgumentError, "Can't evaluate metadata from Ruby context" unless yaml
7+
raise Interscript::MapLogicError, "Can't evaluate metadata from Ruby context" unless yaml
88
@map_name = map_name
99
@node = Interscript::Node::MetaData.new
1010
self.instance_exec(&block)

‎lib/interscript/interpreter.rb‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ def execute_rule r
9292
# Try to build a tree
9393
subs_array = []
9494
r.children.each do |i|
95-
raise ArgumentError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
96-
raise ArgumentError, "Can't parallelize rules with :before" if i.before
97-
raise ArgumentError, "Can't parallelize rules with :after" if i.after
98-
raise ArgumentError, "Can't parallelize rules with :not_before" if i.not_before
99-
raise ArgumentError, "Can't parallelize rules with :not_after" if i.not_after
95+
raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
96+
raise Interscript::SystemConversionError, "Can't parallelize rules with :before" if i.before
97+
raise Interscript::SystemConversionError, "Can't parallelize rules with :after" if i.after
98+
raise Interscript::SystemConversionError, "Can't parallelize rules with :not_before" if i.not_before
99+
raise Interscript::SystemConversionError, "Can't parallelize rules with :not_after" if i.not_after
100100
next if i.reverse_run == true
101101
subs_array << [build_item(i.from, :par), build_item(i.to, :parstr)]
102102
end
@@ -109,7 +109,7 @@ def execute_rule r
109109
# Otherwise let's build a megaregexp
110110
subs_array = []
111111
Interscript::Stdlib.deterministic_sort_by_max_length(r.children).each do |i| # rule.from.max_length gives somewhat better test results, why is that
112-
raise ArgumentError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
112+
raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i
113113
next if i.reverse_run == true
114114
subs_array << [build_regexp(i), build_item(i.to, :parstr)]
115115
end
@@ -178,16 +178,16 @@ def build_item i, target=nil, doc=@map
178178
if i.map
179179
d = doc.dep_aliases[i.map].document
180180
a = d.imported_aliases[i.name]
181-
raise ArgumentError, "Alias #{i.name} of #{i.stage.map} not found" unless a
181+
raise Interscript::SystemConversionError, "Alias #{i.name} of #{i.stage.map} not found" unless a
182182
build_item(a.data, target, d)
183183
elsif Interscript::Stdlib::ALIASES.include?(i.name)
184184
if target != :re && Interscript::Stdlib.re_only_alias?(i.name)
185-
raise ArgumentError, "Can't use #{i.name} in a #{target} context"
185+
raise Interscript::SystemConversionError, "Can't use #{i.name} in a #{target} context"
186186
end
187187
Interscript::Stdlib::ALIASES[i.name]
188188
else
189189
a = doc.imported_aliases[i.name]
190-
raise ArgumentError, "Alias #{i.name} not found" unless a
190+
raise Interscript::SystemConversionError, "Alias #{i.name} not found" unless a
191191
build_item(a.data, target, doc)
192192
end
193193
when Interscript::Node::Item::String
@@ -208,7 +208,7 @@ def build_item i, target=nil, doc=@map
208208
end
209209
when Interscript::Node::Item::CaptureGroup
210210
if target == :par
211-
raise ArgumentError, "Can't use a CaptureGroup in a #{target} context"
211+
raise Interscript::SystemConversionError, "Can't use a CaptureGroup in a #{target} context"
212212
end
213213
"(" + build_item(i.data, target, doc) + ")"
214214
when Interscript::Node::Item::Maybe,
@@ -220,7 +220,7 @@ def build_item i, target=nil, doc=@map
220220
Interscript::Node::Item::MaybeSome => "*" }[i.class]
221221

222222
if target == :par
223-
raise ArgumentError, "Can't use a MaybeSome in a #{target} context"
223+
raise Interscript::SystemConversionError, "Can't use a MaybeSome in a #{target} context"
224224
end
225225
if Interscript::Node::Item::String === i.data && i.data.data.length != 1
226226
"(?:" + build_item(i.data, target, doc) + ")" + resuffix
@@ -229,13 +229,13 @@ def build_item i, target=nil, doc=@map
229229
end
230230
when Interscript::Node::Item::CaptureRef
231231
if target == :par
232-
raise ArgumentError, "Can't use CaptureRef in parallel mode"
232+
raise Interscript::SystemConversionError, "Can't use CaptureRef in parallel mode"
233233
end
234234
"\\#{i.id}"
235235
when Interscript::Node::Item::Any
236236
if target == :str
237237
# We may never reach this point
238-
raise ArgumentError, "Can't use Any in a string context"
238+
raise Interscript::SystemConversionError, "Can't use Any in a string context"
239239
elsif target == :par
240240
i.data.map(&:data)
241241
elsif target == :re

‎lib/interscript/node/item.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def ==(other)
4242

4343
def self.try_convert(i)
4444
i = Interscript::Node::Item::String.new(i) if i.class == ::String
45-
raise TypeError, "Wrong type #{i.class}, expected I::Node::Item" unless Interscript::Node::Item === i
45+
raise Interscript::MapLogicError, "Wrong type #{i.class}, expected I::Node::Item" unless Interscript::Node::Item === i
4646
i
4747
end
4848
end

0 commit comments

Comments
 (0)