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-03 00:03:59 +01:00
|
|
|
from hy.lex.states import Idle, LexException
|
|
|
|
|
2013-03-01 04:27:20 +01:00
|
|
|
|
|
|
|
class Machine(object):
|
2013-03-03 02:38:18 +01:00
|
|
|
"""
|
|
|
|
Hy State Machine. This controls all the state hopping we need to do
|
|
|
|
to properly parse Hy source.
|
|
|
|
"""
|
|
|
|
|
2013-03-01 04:27:20 +01:00
|
|
|
__slots__ = ("submachine", "nodes", "state", "line", "column",
|
|
|
|
"start_line", "start_column")
|
|
|
|
|
|
|
|
def __init__(self, state, line, column):
|
|
|
|
self.nodes = []
|
|
|
|
self.line = line
|
|
|
|
self.column = column
|
|
|
|
self.submachine = None
|
|
|
|
self.state = None
|
|
|
|
self.set_state(state)
|
|
|
|
|
|
|
|
def set_state(self, state):
|
2013-03-03 02:38:18 +01:00
|
|
|
"""
|
|
|
|
Set the new internal machine state. This helps keep line annotations
|
|
|
|
correct, and make sure that we properly call enter and exit.
|
|
|
|
"""
|
|
|
|
|
2013-03-01 04:27:20 +01:00
|
|
|
if self.state:
|
2013-03-03 00:03:59 +01:00
|
|
|
self.state._exit()
|
|
|
|
|
2013-03-03 00:40:00 +01:00
|
|
|
self.accept_result(self.state)
|
2013-03-01 04:27:20 +01:00
|
|
|
|
|
|
|
self.state = state(self)
|
2013-03-03 00:03:59 +01:00
|
|
|
self.state._enter()
|
2013-03-01 04:27:20 +01:00
|
|
|
|
|
|
|
self.start_line = self.line
|
|
|
|
self.start_column = self.column
|
|
|
|
|
|
|
|
def sub(self, state):
|
2013-03-03 02:38:18 +01:00
|
|
|
"""
|
|
|
|
Set up a submachine for this machine.
|
|
|
|
"""
|
2013-03-01 04:27:20 +01:00
|
|
|
self.submachine = Machine(state, self.line, self.column)
|
|
|
|
|
2013-03-03 00:40:00 +01:00
|
|
|
def accept_result(self, state):
|
2013-03-03 02:38:18 +01:00
|
|
|
"""
|
|
|
|
Accept and annotate the result.
|
|
|
|
"""
|
2013-03-03 00:40:00 +01:00
|
|
|
if state and state.result:
|
2013-03-03 01:41:55 +01:00
|
|
|
result = state.result
|
|
|
|
|
|
|
|
result.start_line, result.end_line = (self.start_line, self.line)
|
|
|
|
result.start_column, result.end_column = (self.start_column,
|
|
|
|
self.column)
|
|
|
|
self.nodes.append(result)
|
2013-03-03 00:03:59 +01:00
|
|
|
|
2013-03-01 04:27:20 +01:00
|
|
|
def process(self, buf):
|
2013-03-03 02:38:18 +01:00
|
|
|
"""
|
|
|
|
process an iterable of chars into Hy internal models of the Source.
|
|
|
|
"""
|
2013-03-01 04:27:20 +01:00
|
|
|
for char in buf:
|
2013-03-03 01:41:55 +01:00
|
|
|
|
|
|
|
self.column += 1
|
|
|
|
if char == "\n":
|
|
|
|
self.line += 1
|
|
|
|
self.column = 0
|
|
|
|
|
2013-03-01 04:27:20 +01:00
|
|
|
if self.submachine:
|
|
|
|
self.submachine.process([char])
|
2013-03-03 00:03:59 +01:00
|
|
|
if type(self.submachine.state) == Idle:
|
2013-03-03 00:40:00 +01:00
|
|
|
if len(self.submachine.nodes) != 1:
|
|
|
|
raise LexException("Funky Submachine stuff")
|
|
|
|
result = self.submachine.nodes[0]
|
2013-03-01 04:27:20 +01:00
|
|
|
self.submachine = None
|
2013-03-03 00:40:00 +01:00
|
|
|
if result:
|
|
|
|
self.state.nodes.append(result)
|
2013-03-03 00:03:59 +01:00
|
|
|
continue
|
2013-03-01 04:27:20 +01:00
|
|
|
|
2013-03-03 00:03:59 +01:00
|
|
|
new = self.state.process(char)
|
|
|
|
if new:
|
|
|
|
self.set_state(new)
|