hy/hy/lex/states.py

316 lines
6.8 KiB
Python
Raw Normal View History

2013-03-03 02:24:32 +01:00
# 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.
2013-03-01 04:37:23 +01:00
from hy.models.expression import HyExpression
2013-03-03 01:28:10 +01:00
from hy.models.integer import HyInteger
2013-03-01 04:37:23 +01:00
from hy.models.symbol import HySymbol
2013-03-03 00:40:00 +01:00
from hy.models.string import HyString
2013-03-07 02:59:45 +01:00
from hy.models.dict import HyDict
2013-03-06 02:28:09 +01:00
from hy.models.list import HyList
2013-03-03 01:28:10 +01:00
2013-03-01 04:27:20 +01:00
from hy.errors import HyError
2013-03-03 05:36:57 +01:00
from abc import ABCMeta, abstractmethod
2013-03-01 04:37:23 +01:00
2013-03-01 04:27:20 +01:00
WHITESPACE = [" ", "\t", "\n", "\r"]
class LexException(HyError):
2013-03-03 02:38:18 +01:00
"""
Error during the Lexing of a Hython expression.
"""
2013-03-01 04:27:20 +01:00
pass
2013-03-01 04:37:23 +01:00
def _resolve_atom(obj):
2013-03-03 02:38:18 +01:00
"""
Resolve a bare atom into one of the following (in order):
- Integer
- Symbol
"""
2013-03-03 01:28:10 +01:00
try:
return HyInteger(obj)
except ValueError:
pass
2013-03-09 05:07:21 +01:00
table = {
"true": "True",
"false": "False",
"null": "None",
}
2013-03-10 01:46:32 +01:00
2013-03-09 05:07:21 +01:00
if obj in table:
return HySymbol(table[obj])
2013-03-12 20:46:20 +01:00
if obj.startswith("*") and obj.endswith("*") and obj != "*":
obj = obj[1:-1].upper()
2013-03-10 00:58:47 +01:00
if "-" in obj and obj != "-":
obj = obj.replace("-", "_")
2013-03-01 04:37:23 +01:00
return HySymbol(obj)
2013-03-01 04:27:20 +01:00
class State(object):
2013-03-03 02:38:18 +01:00
"""
Generic State model.
"""
2013-03-03 00:03:59 +01:00
__slots__ = ("nodes", "machine")
2013-03-03 05:36:57 +01:00
__metaclass__ = ABCMeta
2013-03-01 04:27:20 +01:00
def __init__(self, machine):
self.machine = machine
2013-03-03 00:03:59 +01:00
def _enter(self):
2013-03-03 02:38:18 +01:00
""" Internal shim for running global ``enter`` code """
2013-03-03 00:03:59 +01:00
self.result = None
self.nodes = []
self.enter()
def _exit(self):
2013-03-03 02:38:18 +01:00
""" Internal shim for running global ``exit`` code """
2013-03-03 00:03:59 +01:00
self.exit()
2013-03-01 04:27:20 +01:00
def enter(self):
2013-03-03 02:38:18 +01:00
"""
Overridable ``enter`` routines. Subclasses may implement this.
"""
2013-03-03 05:36:57 +01:00
pass
2013-03-01 04:27:20 +01:00
def exit(self):
2013-03-03 02:38:18 +01:00
"""
Overridable ``exit`` routines. Subclasses may implement this.
"""
2013-03-03 05:36:57 +01:00
pass
2013-03-03 00:03:59 +01:00
2013-03-03 05:36:57 +01:00
@abstractmethod
2013-03-03 00:03:59 +01:00
def process(self, char):
2013-03-03 02:38:18 +01:00
"""
Overridable ``process`` routines. Subclasses must implement this to be
useful.
"""
2013-03-03 00:03:59 +01:00
pass # ABC
2013-03-01 04:27:20 +01:00
2013-03-07 00:57:21 +01:00
class ListeyThing(State):
2013-03-03 00:03:59 +01:00
2013-03-01 04:27:20 +01:00
def enter(self):
self.buf = ""
def commit(self):
2013-03-03 00:40:00 +01:00
if self.buf != "":
2013-03-03 20:03:59 +01:00
ret = _resolve_atom(self.buf)
ret.start_line = self._start_line
ret.start_column = self._start_column
ret.end_line = self.machine.line
ret.end_column = (self.machine.column - 1)
self.nodes.append(ret)
2013-03-01 04:27:20 +01:00
self.buf = ""
2013-03-06 02:28:09 +01:00
def exit(self):
self.commit()
2013-03-07 00:57:21 +01:00
self.result = self.result_type(self.nodes)
2013-03-06 02:28:09 +01:00
def process(self, char):
if char == "(":
2013-03-08 01:23:11 +01:00
self.commit()
2013-03-06 02:28:09 +01:00
self.machine.sub(Expression)
return
2013-03-07 02:59:45 +01:00
if char == "{":
2013-03-08 01:23:11 +01:00
self.commit()
2013-03-07 02:59:45 +01:00
self.machine.sub(Dict)
return
2013-03-06 02:28:09 +01:00
if char == "[":
2013-03-08 01:23:11 +01:00
self.commit()
2013-03-06 02:28:09 +01:00
self.machine.sub(List)
return
if char == "\"":
2013-03-08 01:23:11 +01:00
self.commit()
2013-03-06 02:28:09 +01:00
self.machine.sub(String)
return
2013-03-14 02:30:17 +01:00
if char == ";":
self.commit()
self.machine.sub(Comment)
return
2013-03-07 00:57:21 +01:00
if char == self.end_char:
2013-03-06 02:28:09 +01:00
return Idle
if char in WHITESPACE:
self.commit()
return
if self.buf == "":
self._start_line = self.machine.line
self._start_column = self.machine.column
self.buf += char
2013-03-07 00:57:21 +01:00
class List(ListeyThing):
2013-03-06 02:28:09 +01:00
"""
2013-03-07 00:57:21 +01:00
This state parses a Hy list (like a Clojure vector) for use in native
Python interop.
2013-03-06 02:28:09 +01:00
2013-03-07 00:57:21 +01:00
[foo 1 2 3 4] is a good example.
2013-03-06 02:28:09 +01:00
"""
2013-03-07 00:57:21 +01:00
result_type = HyList
end_char = "]"
2013-03-01 04:27:20 +01:00
2013-03-03 02:38:18 +01:00
2013-03-07 00:57:21 +01:00
class Expression(ListeyThing):
"""
This state parses a Hy expression (statement, to be evaluated at runtime)
for running things & stuff.
"""
2013-03-03 20:03:59 +01:00
2013-03-07 00:57:21 +01:00
result_type = HyExpression
end_char = ")"
2013-03-01 04:27:20 +01:00
2013-03-07 02:59:45 +01:00
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 = "}"
2013-03-03 00:40:00 +01:00
class String(State):
2013-03-03 02:38:18 +01:00
"""
String state. This will handle stuff like:
(println "foobar")
^^^^^^^^ -- String
"""
2013-03-12 01:33:06 +01:00
def enter(self):
self.escaped = False
2013-03-03 00:40:00 +01:00
def exit(self):
self.result = HyString("".join(self.nodes))
def process(self, char):
2013-03-03 02:38:18 +01:00
"""
State transitions:
- " - Idle
"""
2013-03-12 01:33:06 +01:00
if self.escaped:
self.escaped = False
if char == "n":
self.nodes.append("\n")
return
raise LexException("Unknown modifier")
2013-03-03 00:40:00 +01:00
if char == "\"":
return Idle
2013-03-12 01:33:06 +01:00
if char == "\\":
self.escaped = True
return
2013-03-03 00:40:00 +01:00
self.nodes.append(char)
2013-03-01 04:27:20 +01:00
class Idle(State):
2013-03-03 02:38:18 +01:00
"""
Idle state. This is the first (and last) thing that we should
be in.
"""
2013-03-01 04:27:20 +01:00
def process(self, char):
2013-03-03 02:38:18 +01:00
"""
State transitions:
- ( - Expression
- (default) - Error
"""
2013-03-03 00:03:59 +01:00
if char == "(":
return Expression
2013-03-01 04:27:20 +01:00
2013-03-15 00:44:33 +01:00
if char == "[":
return List
2013-03-07 02:59:45 +01:00
if char == "{":
return Dict
2013-03-04 01:40:46 +01:00
if char == ";":
return Comment
2013-03-15 02:03:33 +01:00
if char == "#":
return Hash
if char in WHITESPACE:
return
2013-03-03 00:03:59 +01:00
raise LexException("Unknown char (Idle state): `%s`" % (char))
2013-03-04 01:40:46 +01:00
class Comment(State):
"""
Comment state.
"""
def process(self, char):
"""
State transitions:
- \n - Idle
- (default) - disregard.
"""
if char == "\n":
return Idle
2013-03-15 02:03:33 +01:00
class Hash(State):
"""
Hash state
"""
def process(self, char):
"""
State transitions:
- ! - Comment
"""
if char == "!":
return Comment
raise LexException("Unknown char (Hash state): `%s`" % (char))