[pypy-svn] r66669 - in pypy/branch/io-lang/pypy/lang/io: . test
david at codespeak.net
david at codespeak.net
Wed Jul 29 17:53:41 CEST 2009
Author: david
Date: Wed Jul 29 17:53:38 2009
New Revision: 66669
Added:
pypy/branch/io-lang/pypy/lang/io/test/test_message_parser.py
Modified:
pypy/branch/io-lang/pypy/lang/io/parserhack.io
pypy/branch/io-lang/pypy/lang/io/parserhack.py
Log:
Switched to custom parser for results produced by parserhack.io
Modified: pypy/branch/io-lang/pypy/lang/io/parserhack.io
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/parserhack.io (original)
+++ pypy/branch/io-lang/pypy/lang/io/parserhack.io Wed Jul 29 17:53:38 2009
@@ -2,7 +2,7 @@
nil pythonize := nil
Message pythonize := method(
- "W_Message(space," print
+ "(" print
addArguments
next pythonize
")" print
@@ -12,9 +12,9 @@
"\"" print
name asMutable escape print
"\"" print
- ", [" print
- arguments foreach(i, argument, argument pythonize; ", " print)
- "]," print
+ "[" print
+ arguments foreach(i, argument, argument pythonize; "," print)
+ "]" print
)
in := File standardInput
Modified: pypy/branch/io-lang/pypy/lang/io/parserhack.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/parserhack.py (original)
+++ pypy/branch/io-lang/pypy/lang/io/parserhack.py Wed Jul 29 17:53:38 2009
@@ -6,6 +6,59 @@
from pypy.lang.io.model import W_Number, parse_literal, W_Message
from pypy.lang.io.objspace import ObjSpace
+class MessageParser(object):
+ def __init__(self, space, input,):
+ self.space = space
+ self.input = input
+ self.position = 0
+ def parse(self):
+ while not self._char() == ')':
+ self.next()
+ name = self._read_name()
+
+ arguments = self.parse_arguments()
+ if self._char() == '(':
+ nmsg = self.parse()
+ self.next()
+ else:
+ nmsg = None
+ return W_Message(self.space, name, arguments, nmsg)
+ def _char(self):
+ return self.input[self.position]
+ def _prev_char(self):
+ return self.input[self.position-1]
+ def _next_char(self):
+ return self.input[self.position+1]
+ def _read_name(self):
+ name = []
+ assert self._char() == '"'
+ self.next()
+ while not self._char() == '"':
+ if self._char() == '\\' and self._next_char() == '"':
+ self.next()
+
+ name.append(self._char())
+ self.next()
+ self.next()
+ return ''.join(name)
+
+ def next(self):
+ self.position += 1
+
+ def parse_arguments(self):
+ arguments = []
+ assert self._char() == '['
+ self.next()
+ while not self._char() == ']':
+ arguments.append(self.parse())
+ self.next()
+ assert self._char() == ','
+ self.next()
+
+ assert self._char() == ']'
+ self.next()
+ return arguments
+
io_file = py.magic.autopath().dirpath().join("parserhack.io")
def parse(input, space=None):
@@ -13,8 +66,8 @@
child_in.write(input)
child_in.close()
s = child_out_err.read().strip()
- print s
- return eval(s)
+ # print s
+ return MessageParser(space, s).parse()
def interpret(code):
space = ObjSpace()
@@ -27,7 +80,10 @@
code = f.read()
f.close()
return parse(code, space)
+
+def extract_name(input):
+ re.match(input, '\"(\\"|[^"])+\"')
def load_io_files(space):
files = glob.glob('io/*.io')
for f in files:
@@ -37,4 +93,7 @@
if __name__ == '__main__':
import sys
space = ObjSpace()
- parse(py.path.local(sys.argv[1]).read(), space)
\ No newline at end of file
+ # print parse(py.path.local(sys.argv[1]).read(), space)
+ print parse(sys.argv[1], space)
+
+
Added: pypy/branch/io-lang/pypy/lang/io/test/test_message_parser.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_message_parser.py Wed Jul 29 17:53:38 2009
@@ -0,0 +1,28 @@
+from pypy.lang.io.model import W_Message, W_ImmutableSequence
+from pypy.lang.io.parserhack import parse, MessageParser
+from pypy.lang.io.objspace import ObjSpace
+
+
+def test_parse_simple():
+ space = ObjSpace()
+ input = '("a"[])'
+ ast = MessageParser(space, input).parse()
+ assert ast == W_Message(space, "a", [])
+
+def test_parse_simple_next():
+ input = '("a"[]("b"[]))'
+ space = ObjSpace()
+ ast = MessageParser(space, input).parse()
+ assert ast == W_Message(space, "a", [], W_Message(space, 'b', []))
+
+def test_parse_args():
+ input = '("a"[]("+"[("b"[]),]))'
+ space = ObjSpace()
+ ast = MessageParser(space, input).parse()
+ assert ast == W_Message(space, "a", [], W_Message(space, '+', [W_Message(space, 'b', [])]))
+
+def test_parse_quoted_strings():
+ input = '("setSlot"[("\\"a\\""[]),("b"[]),])'
+ space = ObjSpace()
+ ast = MessageParser(space, input).parse()
+ assert ast == W_Message(space, "setSlot", [W_Message(space, '"a"', []), W_Message(space, 'b', [])])
\ No newline at end of file
More information about the Pypy-commit
mailing list