1 # vim: set fileencoding=utf8
5 # Created by Antonio on 2/10/08.
6 # Trinary Research Project: Digital logic simulator
7 # Update (02.17.2008) : Tokenizer will now identify integers.
13 from Identifier
import *
20 def removeWhiteSpace(infile
):
21 '''removeWhiteSpace: remove preceding white space in the buffer
22 infile: file containing the chars to read
23 return: False if no more
24 valid chars are in the buffer or True if there are still valid
27 value
= infile
.read(1)
28 while value
and value
.isspace():
29 value
= infile
.read(1)
36 def isKeyword(infile
, value
):
37 '''isKeyword: identifies token as keyword or symbol
39 value: string to identify
40 return: keyword or identifier
42 infile
.seek(infile
.tell() - 1)
43 if value
in keywords
: #string is a keyword
45 else: #string is an identifier
46 return Identifier(value
)
48 def tokenizeVector(infile
, value
):
49 '''tokenizeVector: find the next trit vector in the file
51 value: current value of trit vector
52 return: Trit object containing the vector
57 raise "EOF file before end of vector."
60 return tokenizeVector(infile
, value
)
64 raise "Invalid symbol detected: |%s|" % (next
, )
66 def tokenizeTrit(infile
):
67 '''tokenizeTrit: find the next trit or trit vector in the file
69 return: Trit object containing the trit
72 assert next
in trit_char
79 def tokenizeString(infile
, value
):
80 '''tokenizeString: find the next keyword or identifier in the file
82 value: current value of the keyword/identifier
83 return: string containing the keyword/identifier
88 return tokenizeString(infile
, value
)
90 return isKeyword(infile
, value
)
92 def tokenizeNumber(infile
, value
):
93 '''tokenizeNumber: identify the next integer in the file
98 return tokenizeNumber(infile
, value
)
100 infile
.seek(infile
.tell() - 1)
101 return Literal(str(value
))
103 def nextToken(infile
):
104 '''nextToken: read the next token from the given file
105 infile: reference to file
106 return: next token in the file: False if no more tokens, else True.
108 value
= removeWhiteSpace(infile
)
110 if value
is None or len(value
) == 0: # None if no more tokens
113 return tokenizeTrit(infile
) # returns a Trit
115 return tokenizeVector(infile
, "") # returns a Trit vector
116 elif value
.isalpha():
117 return tokenizeString(infile
, value
) # returns an Identifier
118 elif value
.isdigit():
119 return tokenizeNumber(infile
, value
) # returns a Literal
120 elif value
in symbols
:
122 else: #invalid symbol detected
123 raise "Invalid symbol detected: |%s|" % (value
, )
125 if __name__
== "__main__":
126 f
= file("testParser", "r")#sys.stdin