#! /usr/bin/python

"""
Parsing eo sources.

Bytes:
>>> eo = EoParser("65 6F 20 6C 61 6E 67")
>>> eo.parse()
'eo lang'
"""

from StringIO import StringIO
import argparse
import os


class Byte(object):

	def __init__(self, char, infile):
		self.value = char + infile.read(1)

	def __radd__(self, string):
		return string + chr(int(self.value, 16))


class EoParser(object):

	def __init__(self, infile):
		if type(infile) == str:
			self.infile = StringIO(infile)
		else:
			self.infile = infile
		self.infile.seek(0, os.SEEK_END)
		self.length = self.infile.tell()
		self.infile.seek(0)

	def parse(self):
		result = str()
		while self.infile.tell() < self.length:
			char = self.infile.read(1)
			if char in "0123456789ABCDEF":
				byte = Byte(char, self.infile)
				result += byte
		return result


if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))
	args = parser.parse_args()

@@ -111,16 +111,104 @@
 eo lang'
+%0A%0AStrings:%0A%3E%3E%3E eo = EoParser(' %22string in eo lang%22 ')%0A%3E%3E%3E eo.parse()%0A'string in eo lang'
 %0A%22%22%22%0A%0Afr
@@ -431,16 +431,242 @@
  16))%0A%0A%0A
+class String(object):%0A%0A%09def __init__(self, infile):%0A%09%09self.value = str()%0A%09%09char = infile.read(1)%0A%09%09while char != '%22':%0A%09%09%09self.value += char%0A%09%09%09char = infile.read(1)%0A%0A%09def __radd__(self, string):%0A%09%09return string + self.value%0A%0A%0A
 class Eo
@@ -1072,16 +1072,16 @@
 infile)%0A
-
 %09%09%09%09resu
@@ -1091,16 +1091,91 @@
 += byte%0A
+%09%09%09elif char == '%22':%0A%09%09%09%09string = String(self.infile)%0A%09%09%09%09result += string%0A
 %09%09return

Create string data type.