#! /usr/bin/python

"""
Parsing eo sources.

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

Strings:
>>> eo = EoParser(' "string in eo lang" ')
>>> eo.parse()
'string in 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 String(object):

	def __init__(self, infile):
		self.value = str()
		char = infile.read(1)
		while char != '"':
			self.value += char
			char = infile.read(1)

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


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
			elif char == '"':
				string = String(self.infile)
				result += string
		return result


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

@@ -11,16 +11,40 @@
 n/python
+%0A# -*- coding: utf-8 -*-
 %0A%0A%22%22%22%0APa
@@ -223,16 +223,135 @@
 eo lang'
+%0A%0AFunctions:%0A%3E%3E%3E eo = EoParser(' @last_name %7B %22Nuffer%22 %7D %22%C3%82ngelo %22 last_name ')%0A%3E%3E%3E eo.parse()%0A'%5C%5Cxc3%5C%5Cx82ngelo Nuffer'
 %0A%22%22%22%0A%0Afr
@@ -404,16 +404,26 @@
 port os%0A
+import re%0A
 %0A%0Aclass 
@@ -806,24 +806,392 @@
 elf.value%0A%0A%0A
+class Function(object):%0A%0A%09def __init__(self, infile):%0A%09%09self.name = str()%0A%09%09char = infile.read(1)%0A%09%09while char not in %22 %7B%22:%0A%09%09%09self.name += char%0A%09%09%09char = infile.read(1)%0A%09%09while char != %22%7B%22:%0A%09%09%09char = infile.read(1)%0A%09%09char = infile.read(1)%0A%09%09content = str()%0A%09%09while char != %22%7D%22:%0A%09%09%09content += char%0A%09%09%09char = infile.read(1)%0A%09%09self.content = EoParser(content).parse()%0A%0A%0A
 class EoPars
@@ -1195,32 +1195,56 @@
 arser(object):%0A%0A
+%09VAR_TOKEN = r%22%5Ba-z_%5D%22%0A%0A
 %09def __init__(se
@@ -1439,16 +1439,42 @@
 .seek(0)
+%0A%09%09self.functions = list()
 %0A%0A%09def p
@@ -1708,32 +1708,32 @@
 ng(self.infile)%0A
-
 %09%09%09%09result += st
@@ -1737,16 +1737,367 @@
  string%0A
+%09%09%09elif char == %22@%22:%0A%09%09%09%09function = Function(self.infile)%0A%09%09%09%09self.functions.append(function)%0A%09%09%09elif re.match(EoParser.VAR_TOKEN, char):%0A%09%09%09%09name = str()%0A%09%09%09%09while re.match(EoParser.VAR_TOKEN, char):%0A%09%09%09%09%09name += char%0A%09%09%09%09%09char = self.infile.read(1)%0A%09%09%09%09for function in self.functions:%0A%09%09%09%09%09if name == function.name:%0A%09%09%09%09%09%09result += function.content%0A
 %09%09return

Create function.