Skip to content

Commit

Permalink
extract constants
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Feb 15, 2012
1 parent c3b1b70 commit 94de2db
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions clj.py
Expand Up @@ -49,13 +49,14 @@ def number(v):
else: else:
return int(v) return int(v)


_STOP_CHARS = [" ", ",", "\n", "\r"]
_COLL_OPEN_CHARS = ["#", "[", "{"]
_EXTRA_NUM_CHARS = ["-", "+", ".", "e", "E"]

class CljDecoder(object): class CljDecoder(object):
def __init__(self, fd): def __init__(self, fd):
self.fd = fd self.fd = fd
self.value_stack = [] self.value_stack = []
self.stop_chars = [" ", ",", "\n", "\r"]
self.coll_open_chars = ["#", "[", "{"]
self.extra_num_chars = ["-", "+", ".", "e", "E"]
self.terminator = None ## for collection type self.terminator = None ## for collection type


def decode(self): def decode(self):
Expand Down Expand Up @@ -98,7 +99,7 @@ def __read_token(self):
c = fd.read(1) c = fd.read(1)


## skip all stop chars if necessary ## skip all stop chars if necessary
while c in self.stop_chars: while c in _STOP_CHARS:
c = fd.read(1) c = fd.read(1)


## raise exception when unexpected EOF found ## raise exception when unexpected EOF found
Expand Down Expand Up @@ -140,7 +141,7 @@ def __read_token(self):


elif t == "char": elif t == "char":
buf = [] buf = []
while c is not self.terminator and c is not "" and c not in self.stop_chars: while c is not self.terminator and c is not "" and c not in _STOP_CHARS:
c = fd.read(1) c = fd.read(1)
buf.append(c) buf.append(c)


Expand All @@ -153,7 +154,7 @@ def __read_token(self):


elif t == "number": elif t == "number":
buf = [] buf = []
while c.isdigit() or (c in self.extra_num_chars): while c.isdigit() or (c in _EXTRA_NUM_CHARS):
buf.append(c) buf.append(c)
c = fd.read(1) c = fd.read(1)
e = c e = c
Expand All @@ -163,12 +164,12 @@ def __read_token(self):
## special case for ## special case for
## [23[12]] ## [23[12]]
## this is a valid clojure form ## this is a valid clojure form
if e in self.coll_open_chars: if e in _COLL_OPEN_CHARS:
fd.seek(-1, os.SEEK_CUR) fd.seek(-1, os.SEEK_CUR)


elif t == "keyword": elif t == "keyword":
buf = [] ##skip the leading ":" buf = [] ##skip the leading ":"
while c is not self.terminator and c is not "" and c not in self.stop_chars: while c is not self.terminator and c is not "" and c not in _STOP_CHARS:
c = fd.read(1) c = fd.read(1)
buf.append(c) buf.append(c)


Expand Down

0 comments on commit 94de2db

Please sign in to comment.