diff --git a/hy/lex/machine.py b/hy/lex/machine.py index 119ca97..aebdd92 100644 --- a/hy/lex/machine.py +++ b/hy/lex/machine.py @@ -19,6 +19,7 @@ # DEALINGS IN THE SOFTWARE. from hy.lex.states import Idle, LexException +from hy.models.quote import HyQuote class Machine(object): @@ -28,12 +29,17 @@ class Machine(object): """ __slots__ = ("submachine", "nodes", "state", "line", "column", - "start_line", "start_column") + "start_line", "start_column", "modifier") + + modifiers = { + "`": HyQuote + } def __init__(self, state, line, column): self.nodes = [] self.line = line self.column = column + self.modifier = None self.submachine = None self.state = None self.set_state(state) @@ -71,6 +77,10 @@ class Machine(object): result.start_line, result.end_line = (self.start_line, self.line) result.start_column, result.end_column = (self.start_column, self.column) + if self.modifier is not None: + result = self.modifier(result) + self.modifier = None + self.nodes.append(result) def process(self, buf): @@ -95,6 +105,10 @@ class Machine(object): self.state.nodes.append(nodes[0]) continue + if char in self.modifiers: + self.modifier = self.modifiers[char] + continue + new = self.state.process(char) if new: self.set_state(new) diff --git a/hy/models/quote.py b/hy/models/quote.py new file mode 100644 index 0000000..2612b93 --- /dev/null +++ b/hy/models/quote.py @@ -0,0 +1,26 @@ +# Copyright (c) 2012 Paul Tagliamonte +# +# 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 HyQuote(HyObject): + def __init__(self, obj): + self.obj = obj diff --git a/tests/lex/test_lex.py b/tests/lex/test_lex.py index b20ef22..772a775 100644 --- a/tests/lex/test_lex.py +++ b/tests/lex/test_lex.py @@ -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.quote import HyQuote from hy.models.dict import HyDict from hy.lex.states import LexException @@ -143,3 +144,9 @@ def test_nospace(): assert entry.end_line == 1 assert entry.end_column == 13 + + +def test_lex_quoted_form(): + objs = tokenize("`(foo 2)") + assert type(objs[0]) == HyQuote + assert type(objs[0].obj) == HyExpression