Make HyKeyword constructor consistent
The colon is now only part of its string representation and no longer accepted as part of the constructor: :foobar == HyKeyword("foobar")
This commit is contained in:
parent
1b7dfd2839
commit
291847af15
@ -747,7 +747,7 @@ class HyASTCompiler(object):
|
|||||||
elif isinstance(form, HyString):
|
elif isinstance(form, HyString):
|
||||||
x = [HySymbol(name), form]
|
x = [HySymbol(name), form]
|
||||||
if form.brackets is not None:
|
if form.brackets is not None:
|
||||||
x.extend([HyKeyword(":brackets"), form.brackets])
|
x.extend([HyKeyword("brackets"), form.brackets])
|
||||||
return imports, HyExpression(x).replace(form), False
|
return imports, HyExpression(x).replace(form), False
|
||||||
|
|
||||||
return imports, HyExpression([HySymbol(name),
|
return imports, HyExpression([HySymbol(name),
|
||||||
@ -811,7 +811,7 @@ class HyASTCompiler(object):
|
|||||||
ret += self.compile(expr.pop(0))
|
ret += self.compile(expr.pop(0))
|
||||||
|
|
||||||
cause = None
|
cause = None
|
||||||
if len(expr) == 2 and expr[0] == HyKeyword(":from"):
|
if len(expr) == 2 and expr[0] == HyKeyword("from"):
|
||||||
if not PY3:
|
if not PY3:
|
||||||
raise HyCompileError(
|
raise HyCompileError(
|
||||||
"raise from only supported in python 3")
|
"raise from only supported in python 3")
|
||||||
@ -1170,7 +1170,7 @@ class HyASTCompiler(object):
|
|||||||
if isinstance(iexpr, HyList) and iexpr:
|
if isinstance(iexpr, HyList) and iexpr:
|
||||||
module = iexpr.pop(0)
|
module = iexpr.pop(0)
|
||||||
entry = iexpr[0]
|
entry = iexpr[0]
|
||||||
if entry == HyKeyword(":as"):
|
if entry == HyKeyword("as"):
|
||||||
if not len(iexpr) == 2:
|
if not len(iexpr) == 2:
|
||||||
raise HyTypeError(iexpr,
|
raise HyTypeError(iexpr,
|
||||||
"garbage after aliased import")
|
"garbage after aliased import")
|
||||||
@ -1464,7 +1464,7 @@ class HyASTCompiler(object):
|
|||||||
else:
|
else:
|
||||||
assignments = {}
|
assignments = {}
|
||||||
while names:
|
while names:
|
||||||
if len(names) > 1 and names[1] == HyKeyword(":as"):
|
if len(names) > 1 and names[1] == HyKeyword("as"):
|
||||||
k, _, v = names[:3]
|
k, _, v = names[:3]
|
||||||
del names[:3]
|
del names[:3]
|
||||||
assignments[k] = v
|
assignments[k] = v
|
||||||
@ -1473,7 +1473,7 @@ class HyASTCompiler(object):
|
|||||||
assignments[symbol] = symbol
|
assignments[symbol] = symbol
|
||||||
require(module, self.module_name, assignments=assignments)
|
require(module, self.module_name, assignments=assignments)
|
||||||
elif (isinstance(entry, HyList) and len(entry) == 3
|
elif (isinstance(entry, HyList) and len(entry) == 3
|
||||||
and entry[1] == HyKeyword(":as")):
|
and entry[1] == HyKeyword("as")):
|
||||||
# e.g., (require [foo :as bar])
|
# e.g., (require [foo :as bar])
|
||||||
module, _, prefix = entry
|
module, _, prefix = entry
|
||||||
__import__(module)
|
__import__(module)
|
||||||
@ -2190,12 +2190,12 @@ class HyASTCompiler(object):
|
|||||||
return asty.Name(symbol, id=ast_str(symbol), ctx=ast.Load())
|
return asty.Name(symbol, id=ast_str(symbol), ctx=ast.Load())
|
||||||
|
|
||||||
@builds(HyKeyword)
|
@builds(HyKeyword)
|
||||||
def compile_keyword(self, string):
|
def compile_keyword(self, obj):
|
||||||
ret = Result()
|
ret = Result()
|
||||||
ret += asty.Call(
|
ret += asty.Call(
|
||||||
string,
|
obj,
|
||||||
func=asty.Name(string, id="HyKeyword", ctx=ast.Load()),
|
func=asty.Name(obj, id="HyKeyword", ctx=ast.Load()),
|
||||||
args=[asty.Str(string, s=str_type(string))],
|
args=[asty.Str(obj, s=obj.name)],
|
||||||
keywords=[])
|
keywords=[])
|
||||||
ret.add_imports("hy", {"HyKeyword"})
|
ret.add_imports("hy", {"HyKeyword"})
|
||||||
return ret
|
return ret
|
||||||
|
@ -460,7 +460,7 @@ as EOF (defaults to an empty string)."
|
|||||||
|
|
||||||
Strings numbers and even objects with the __name__ magic will work."
|
Strings numbers and even objects with the __name__ magic will work."
|
||||||
(if (keyword? value)
|
(if (keyword? value)
|
||||||
(HyKeyword (unmangle value))
|
(HyKeyword (unmangle value.name))
|
||||||
(if (string? value)
|
(if (string? value)
|
||||||
(HyKeyword (unmangle value))
|
(HyKeyword (unmangle value))
|
||||||
(try
|
(try
|
||||||
|
@ -380,7 +380,7 @@ def symbol_like(obj):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if obj.startswith(":") and "." not in obj:
|
if obj.startswith(":") and "." not in obj:
|
||||||
return HyKeyword(obj)
|
return HyKeyword(obj[1:])
|
||||||
|
|
||||||
|
|
||||||
@pg.error
|
@pg.error
|
||||||
|
18
hy/models.py
18
hy/models.py
@ -124,34 +124,32 @@ _wrappers[type(None)] = lambda foo: HySymbol("None")
|
|||||||
class HyKeyword(HyObject):
|
class HyKeyword(HyObject):
|
||||||
"""Generic Hy Keyword object."""
|
"""Generic Hy Keyword object."""
|
||||||
|
|
||||||
__slots__ = ['_value']
|
__slots__ = ['name']
|
||||||
|
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
if value[0] != ':':
|
self.name = value
|
||||||
value = ':' + value
|
|
||||||
self._value = value
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "%s(%r)" % (self.__class__.__name__, self._value)
|
return "%s(%r)" % (self.__class__.__name__, self.name)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self._value
|
return ":%s" % self.name
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self._value)
|
return hash(self.name)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, HyKeyword):
|
if not isinstance(other, HyKeyword):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self._value == other._value
|
return self.name == other.name
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
if not isinstance(other, HyKeyword):
|
if not isinstance(other, HyKeyword):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self._value != other._value
|
return self.name != other.name
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return bool(self._value[1:])
|
return bool(self.name)
|
||||||
|
|
||||||
|
|
||||||
def strip_digit_separators(number):
|
def strip_digit_separators(number):
|
||||||
|
@ -434,9 +434,9 @@ def test_discard():
|
|||||||
assert tokenize("(#_foo)") == [HyExpression()]
|
assert tokenize("(#_foo)") == [HyExpression()]
|
||||||
assert tokenize("(#_foo bar)") == [HyExpression([HySymbol("bar")])]
|
assert tokenize("(#_foo bar)") == [HyExpression([HySymbol("bar")])]
|
||||||
assert tokenize("(foo #_bar)") == [HyExpression([HySymbol("foo")])]
|
assert tokenize("(foo #_bar)") == [HyExpression([HySymbol("foo")])]
|
||||||
assert tokenize("(foo :bar 1)") == [HyExpression([HySymbol("foo"), HyKeyword(":bar"), HyInteger(1)])]
|
assert tokenize("(foo :bar 1)") == [HyExpression([HySymbol("foo"), HyKeyword("bar"), HyInteger(1)])]
|
||||||
assert tokenize("(foo #_:bar 1)") == [HyExpression([HySymbol("foo"), HyInteger(1)])]
|
assert tokenize("(foo #_:bar 1)") == [HyExpression([HySymbol("foo"), HyInteger(1)])]
|
||||||
assert tokenize("(foo :bar #_1)") == [HyExpression([HySymbol("foo"), HyKeyword(":bar")])]
|
assert tokenize("(foo :bar #_1)") == [HyExpression([HySymbol("foo"), HyKeyword("bar")])]
|
||||||
# discard term with nesting
|
# discard term with nesting
|
||||||
assert tokenize("[1 2 #_[a b c [d e [f g] h]] 3 4]") == [
|
assert tokenize("[1 2 #_[a b c [d e [f g] h]] 3 4]") == [
|
||||||
HyList([HyInteger(1), HyInteger(2), HyInteger(3), HyInteger(4)])
|
HyList([HyInteger(1), HyInteger(2), HyInteger(3), HyInteger(4)])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user