[UNSTABLE] Use duck typing for ASTLeaf/ASTNode checks. NEXT: Get `enum` and friends working

pull/1125/head
Rick Carlino 2019-02-21 14:27:35 -06:00
parent 01b035b66a
commit c360e1a0ea
4 changed files with 26 additions and 26 deletions

View File

@ -5,9 +5,26 @@
# to do that. -RC
module CeleryScript
class AstLeaf < AstBase
FRIENDLY_ERRORS = CeleryScript::Checker::FRIENDLY_ERRORS
BAD_LEAF = CeleryScript::Checker::BAD_LEAF
attr_reader :kind, :value, :parent
def initialize(parent, value, kind)
@parent, @value, @kind = parent, value, kind
end
def cross_check(corpus)
allowed = corpus.fetchArg(kind).allowed_values
unless allowed.any? { |spec| spec.valid?(self, corpus) }
message = (FRIENDLY_ERRORS.dig(kind, parent.kind) || BAD_LEAF) % {
kind: kind,
parent_kind: parent.kind,
allowed: "[#{allowed.map(&:name).join(", ")}]",
actual: value.class
}
raise TypeCheckError, message
end
end
end
end

View File

@ -8,13 +8,10 @@ module CeleryScript
"no leaves here."
LEAVES_NEED_KEYS = "Tried to initialize a leaf without a key."
NEVER = :__NEVER__
FRIENDLY_ERRORS = CeleryScript::Checker::FRIENDLY_ERRORS
BAD_LEAF = CeleryScript::Checker::BAD_LEAF
def initialize(parent = nil,
args:,
body: nil,
comment: "",
kind:,
uuid: nil)
def initialize(parent = nil, args:, body: nil, comment: "", kind:, uuid: nil)
@comment, @kind, @parent = comment, kind, parent
@args = args.map do |key, value|
@ -46,5 +43,9 @@ module CeleryScript
(hash[:args].is_a?(Hash)) &&
(hash[:kind].is_a?(String))
end
def cross_check(corpus)
print "🔥"
end
end
end

View File

@ -133,25 +133,7 @@ module CeleryScript
# IDEA: Add a refinement to string class to allow it to quack like other
# special classes.
def needs_new_name(node, arg_key = nil)
case node
when CeleryScript::AstNode
print "🔥"
when CeleryScript::AstLeaf
allowed = corpus.fetchArg(node.kind).allowed_values
unless allowed.any? { |spec| spec.valid?(node, corpus) }
actual = node.value.class
kind = node.kind
parent_kind = node.parent.kind
message = (FRIENDLY_ERRORS.dig(kind, parent_kind) || BAD_LEAF) % {
kind: kind,
parent_kind: parent_kind,
allowed: "[#{allowed.map(&:name).join(", ")}]",
actual: actual
}
raise TypeCheckError, message
end
end
node.cross_check(corpus)
end
def validate_leaf_pairing(key, value)

View File

@ -25,7 +25,7 @@ module CeleryScript
end
def valid?(node, corpus)
return corpus
return corpus # TODO: Clean this up to actually use encapsulation.
.instance_variable_get(:@value_def_list)
.fetch(value)
.values