Add 'optional' matcher
This commit is contained in:
@@ -12,6 +12,9 @@ class Signature:
|
|||||||
|
|
||||||
def varargSignature(varargMatcher, *basicSignature, wrapVarargInValue=False):
|
def varargSignature(varargMatcher, *basicSignature, wrapVarargInValue=False):
|
||||||
def check(args):
|
def check(args):
|
||||||
|
if any([ matcher.optional for matcher in [ varargMatcher, *basicSignature ]]):
|
||||||
|
raise RuntimeError("Vararg signature can't have optional arguments")
|
||||||
|
|
||||||
if len(basicSignature) > len(args):
|
if len(basicSignature) > len(args):
|
||||||
return doesNotMatchVararg(basicSignature)
|
return doesNotMatchVararg(basicSignature)
|
||||||
|
|
||||||
@@ -38,7 +41,7 @@ def doesNotMatchVararg(basicSignature):
|
|||||||
|
|
||||||
def signature(*signature):
|
def signature(*signature):
|
||||||
def check(args):
|
def check(args):
|
||||||
if len(signature) != len(args):
|
if len(args) not in [len(signature), len([ matcher for matcher in signature if not matcher.optional ])]:
|
||||||
return doesNotMatch(signature)
|
return doesNotMatch(signature)
|
||||||
|
|
||||||
for s, a in zip(signature, args):
|
for s, a in zip(signature, args):
|
||||||
@@ -52,6 +55,12 @@ def signature(*signature):
|
|||||||
return Signature(check, string, signature)
|
return Signature(check, string, signature)
|
||||||
|
|
||||||
|
|
||||||
|
def optional(matcher):
|
||||||
|
matcher.optional = True
|
||||||
|
matcher.string += "?"
|
||||||
|
return matcher
|
||||||
|
|
||||||
|
|
||||||
def doesNotMatch(sign):
|
def doesNotMatch(sign):
|
||||||
return (False, *[None for n in sign])
|
return (False, *[None for n in sign])
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ class Matcher:
|
|||||||
self.type = objectType
|
self.type = objectType
|
||||||
self.matcher = matcher
|
self.matcher = matcher
|
||||||
self.string = string
|
self.string = string
|
||||||
|
self.optional = False
|
||||||
|
|
||||||
def match(self, value):
|
def match(self, value):
|
||||||
if self.type is not None and self.type != value.type:
|
if self.type is not None and self.type != value.type:
|
||||||
|
|||||||
Reference in New Issue
Block a user