Created
January 19, 2013 14:39
-
-
Save v2e4lisp/4573003 to your computer and use it in GitHub Desktop.
basic-parser-or-nothing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BasicParser: | |
def __init__(self, doc): | |
self.pos = 0 | |
self.doc = doc | |
def next (self): | |
self.pos += 1 | |
return self | |
def chr(self): | |
return self.doc[self.pos] | |
def string (self): | |
if self.blank().chr() not in ["'", '"']: return False | |
start = self.chr() | |
content = '' | |
while (self.next().chr() != start): | |
content += self.chr() | |
self.next() | |
return content | |
def symbol (self): | |
if not self.blank().chr().isalpha(): return False | |
content = self.chr() | |
while (self.next.chr().isalpha()): | |
content += self.chr() | |
return content | |
def number (self): | |
def isdigit(c): | |
return c >'9' or c < '0' | |
content = self.blank().chr() | |
while (isdigit (self.next().chr())): | |
content += isdigit | |
return content | |
def blank (self): | |
blanks = ' \t\n\r' | |
while (self.chr() in blanks): | |
self.next() | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment