Add in dicts.
This commit is contained in:
parent
3571ac0118
commit
935ef5424e
4
Makefile
4
Makefile
@ -13,7 +13,7 @@ all:
|
||||
@echo ""
|
||||
|
||||
|
||||
dev: test flake tox
|
||||
dev: test flake
|
||||
|
||||
test:
|
||||
nosetests -sv
|
||||
@ -32,4 +32,4 @@ d: clear dev
|
||||
diff:
|
||||
git diff --color | less -r
|
||||
|
||||
r: d diff
|
||||
r: d tox diff
|
||||
|
@ -22,6 +22,7 @@ from hy.models.expression import HyExpression
|
||||
from hy.models.integer import HyInteger
|
||||
from hy.models.symbol import HySymbol
|
||||
from hy.models.string import HyString
|
||||
from hy.models.dict import HyDict
|
||||
from hy.models.list import HyList
|
||||
|
||||
from hy.errors import HyError
|
||||
@ -121,6 +122,10 @@ class ListeyThing(State):
|
||||
self.machine.sub(Expression)
|
||||
return
|
||||
|
||||
if char == "{":
|
||||
self.machine.sub(Dict)
|
||||
return
|
||||
|
||||
if char == "[":
|
||||
self.machine.sub(List)
|
||||
return
|
||||
@ -165,6 +170,20 @@ class Expression(ListeyThing):
|
||||
end_char = ")"
|
||||
|
||||
|
||||
class Dict(ListeyThing):
|
||||
"""
|
||||
This state parses a Hy dict for things.
|
||||
"""
|
||||
|
||||
def exit(self):
|
||||
self.commit()
|
||||
it = iter(self.nodes)
|
||||
result = dict(zip(it, it))
|
||||
self.result = HyDict(result)
|
||||
|
||||
end_char = "}"
|
||||
|
||||
|
||||
class String(State):
|
||||
"""
|
||||
String state. This will handle stuff like:
|
||||
@ -205,6 +224,9 @@ class Idle(State):
|
||||
if char == "(":
|
||||
return Expression
|
||||
|
||||
if char == "{":
|
||||
return Dict
|
||||
|
||||
if char == ";":
|
||||
return Comment
|
||||
|
||||
|
28
hy/models/dict.py
Normal file
28
hy/models/dict.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Copyright (c) 2012 Paul Tagliamonte <paultag@debian.org>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||
# copy of this software and associated documentation files (the "Software"),
|
||||
# to deal in the Software without restriction, including without limitation
|
||||
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
# and/or sell copies of the Software, and to permit persons to whom the
|
||||
# Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
# DEALINGS IN THE SOFTWARE.
|
||||
|
||||
from hy.models import HyObject
|
||||
|
||||
|
||||
class HyDict(HyObject, dict):
|
||||
"""
|
||||
HyDict (just a dict)
|
||||
"""
|
||||
pass
|
@ -22,6 +22,7 @@ from hy.models.expression import HyExpression
|
||||
from hy.models.integer import HyInteger
|
||||
from hy.models.symbol import HySymbol
|
||||
from hy.models.string import HyString
|
||||
from hy.models.dict import HyDict
|
||||
|
||||
from hy.lex.states import LexException
|
||||
|
||||
@ -115,3 +116,12 @@ def test_lex_line_counting_multi_inner():
|
||||
|
||||
assert inner.start_line == 2
|
||||
assert inner.start_column == 5
|
||||
|
||||
|
||||
def tgest_dicts():
|
||||
""" Ensure that we can tokenize a dict. """
|
||||
objs = tokenize("{foo bar bar baz}")
|
||||
assert objs == [HyDict({
|
||||
"foo": "bar",
|
||||
"bar": "baz"
|
||||
})]
|
||||
|
Loading…
Reference in New Issue
Block a user