adding in some silly lexing voodoo
This commit is contained in:
parent
e536d22a88
commit
68cf93e6d0
@ -1,4 +1,5 @@
|
|||||||
from hy.lang.expression import HYExpression
|
from hy.lang.expression import HYExpression
|
||||||
|
from hy.lex.errors import LexException
|
||||||
from hy.lex.machine import Machine
|
from hy.lex.machine import Machine
|
||||||
|
|
||||||
|
|
||||||
@ -69,10 +70,44 @@ class Expression(State):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if x == "\"":
|
if x == "\"":
|
||||||
return String
|
self.sub_machine = Machine(String)
|
||||||
|
return
|
||||||
|
|
||||||
if x == "(":
|
if x == "(":
|
||||||
self.sub_machine = Machine(Expression)
|
self.sub_machine = Machine(Expression)
|
||||||
return
|
return
|
||||||
|
|
||||||
self.bulk += x
|
self.bulk += x
|
||||||
|
|
||||||
|
|
||||||
|
class String(State):
|
||||||
|
magic = {
|
||||||
|
"n": "\n",
|
||||||
|
"t": "\t",
|
||||||
|
"\\": "\\",
|
||||||
|
"\"": "\""
|
||||||
|
}
|
||||||
|
|
||||||
|
def enter(self):
|
||||||
|
self.buf = ""
|
||||||
|
self.esc = False
|
||||||
|
|
||||||
|
def exit(self):
|
||||||
|
self.machine.nodes.append(self.buf)
|
||||||
|
|
||||||
|
def p(self, x):
|
||||||
|
if x == "\\":
|
||||||
|
self.esc = True
|
||||||
|
return
|
||||||
|
|
||||||
|
if x == "\"" and not self.esc:
|
||||||
|
return Idle
|
||||||
|
|
||||||
|
if self.esc and x not in self.magic:
|
||||||
|
raise LexException("Unknown escape: \\%s" % (x))
|
||||||
|
elif self.esc:
|
||||||
|
x = self.magic[x]
|
||||||
|
|
||||||
|
self.esc = False
|
||||||
|
|
||||||
|
self.buf += x
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
from hy.lex.machine import Machine
|
from hy.lex.machine import Machine
|
||||||
from hy.lex.states import Idle
|
from hy.lex.states import Idle
|
||||||
|
from hy.lex.errors import LexException
|
||||||
|
|
||||||
def tokenize(buff):
|
def tokenize(buff):
|
||||||
m = Machine(Idle)
|
m = Machine(Idle)
|
||||||
m.process(buff)
|
m.process(buff)
|
||||||
|
if type(m.state) != Idle:
|
||||||
|
raise LexException("End of file.")
|
||||||
return m.nodes
|
return m.nodes
|
||||||
|
Loading…
Reference in New Issue
Block a user