Add in dicts.

This commit is contained in:
Paul R. Tagliamonte 2013-03-06 20:59:45 -05:00
parent 3571ac0118
commit 935ef5424e
4 changed files with 62 additions and 2 deletions

View File

@ -13,7 +13,7 @@ all:
@echo "" @echo ""
dev: test flake tox dev: test flake
test: test:
nosetests -sv nosetests -sv
@ -32,4 +32,4 @@ d: clear dev
diff: diff:
git diff --color | less -r git diff --color | less -r
r: d diff r: d tox diff

View File

@ -22,6 +22,7 @@ from hy.models.expression import HyExpression
from hy.models.integer import HyInteger from hy.models.integer import HyInteger
from hy.models.symbol import HySymbol from hy.models.symbol import HySymbol
from hy.models.string import HyString from hy.models.string import HyString
from hy.models.dict import HyDict
from hy.models.list import HyList from hy.models.list import HyList
from hy.errors import HyError from hy.errors import HyError
@ -121,6 +122,10 @@ class ListeyThing(State):
self.machine.sub(Expression) self.machine.sub(Expression)
return return
if char == "{":
self.machine.sub(Dict)
return
if char == "[": if char == "[":
self.machine.sub(List) self.machine.sub(List)
return return
@ -165,6 +170,20 @@ class Expression(ListeyThing):
end_char = ")" 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): class String(State):
""" """
String state. This will handle stuff like: String state. This will handle stuff like:
@ -205,6 +224,9 @@ class Idle(State):
if char == "(": if char == "(":
return Expression return Expression
if char == "{":
return Dict
if char == ";": if char == ";":
return Comment return Comment

28
hy/models/dict.py Normal file
View 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

View File

@ -22,6 +22,7 @@ from hy.models.expression import HyExpression
from hy.models.integer import HyInteger from hy.models.integer import HyInteger
from hy.models.symbol import HySymbol from hy.models.symbol import HySymbol
from hy.models.string import HyString from hy.models.string import HyString
from hy.models.dict import HyDict
from hy.lex.states import LexException 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_line == 2
assert inner.start_column == 5 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"
})]