Changed the token.py to ptoken.py (for Placeholder Token) to avoid naming conflicts with jsonpickle.
This commit is contained in:
+18
-18
@@ -1,4 +1,4 @@
|
||||
import token
|
||||
import ptoken as token
|
||||
|
||||
def get_tokens_from_expression_string(expression_string):
|
||||
|
||||
@@ -18,31 +18,31 @@ def get_tokens_from_expression_string(expression_string):
|
||||
if len(tokens) > 1:
|
||||
lookBehind = tokens[-1]
|
||||
if lookBehind.type == token.TokenType.exp_end:
|
||||
tokens.append(token.CToken("^", token.TokenType.power))
|
||||
tokens.append(token.Token("^", token.TokenType.power))
|
||||
if (i + 1) == len(expression_string):
|
||||
tokens.append(token.CToken(tmp + c, token.TokenType.constant))
|
||||
tokens.append(token.Token(tmp + c, token.TokenType.constant))
|
||||
parsing_number = True
|
||||
tmp = tmp + c
|
||||
continue
|
||||
elif c == '(':
|
||||
if parsing_number:
|
||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
||||
tokens.append(token.CToken("*", token.TokenType.multiply))
|
||||
tokens.append(token.CToken("(", token.TokenType.exp_start))
|
||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||
tokens.append(token.Token("*", token.TokenType.multiply))
|
||||
tokens.append(token.Token("(", token.TokenType.exp_start))
|
||||
parsing_number = False
|
||||
tmp = ""
|
||||
continue
|
||||
elif c == ')':
|
||||
if parsing_number:
|
||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
||||
tokens.append(token.CToken(")", token.TokenType.exp_end))
|
||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||
tokens.append(token.Token(")", token.TokenType.exp_end))
|
||||
parsing_number = False
|
||||
tmp = ""
|
||||
continue
|
||||
elif token.TokenType.get_operator(c) != token.TokenType.unknown:
|
||||
if parsing_number:
|
||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
||||
tokens.append(token.CToken(c, token.TokenType.get_operator(c)))
|
||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||
tokens.append(token.Token(c, token.TokenType.get_operator(c)))
|
||||
parsing_number = False
|
||||
tmp = ""
|
||||
continue
|
||||
@@ -50,23 +50,23 @@ def get_tokens_from_expression_string(expression_string):
|
||||
if len(tokens) > 1:
|
||||
lookBehind = tokens[-1]
|
||||
if lookBehind.type == token.TokenType.exp_end:
|
||||
tokens.append(token.CToken("^", token.TokenType.power))
|
||||
tokens.append(token.Token("^", token.TokenType.power))
|
||||
if (i + 1) < len(expression_string):
|
||||
lookAHead = expression_string[i + 1]
|
||||
if lookAHead == '(':
|
||||
tokens.append(token.CToken(c, token.TokenType.variable))
|
||||
tokens.append(token.CToken("*", token.TokenType.multiply))
|
||||
tokens.append(token.Token(c, token.TokenType.variable))
|
||||
tokens.append(token.Token("*", token.TokenType.multiply))
|
||||
tmp = ""
|
||||
parsing_number = False
|
||||
continue
|
||||
if parsing_number:
|
||||
tokens.append(token.CToken(tmp, token.TokenType.constant))
|
||||
tokens.append(token.CToken("*", token.TokenType.multiply))
|
||||
tokens.append(token.Token(tmp, token.TokenType.constant))
|
||||
tokens.append(token.Token("*", token.TokenType.multiply))
|
||||
if c in symbols_dic.keys():
|
||||
symbols_dic[c] += 1
|
||||
else:
|
||||
symbols_dic[c] = 1
|
||||
tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable))
|
||||
tokens.append(token.Token(c + str(symbols_dic[c]), token.TokenType.variable))
|
||||
parsing_number = False
|
||||
tmp = ""
|
||||
|
||||
@@ -77,8 +77,8 @@ def replace_variables(tokens, new_constants):
|
||||
newTokenList = []
|
||||
for t in tokens:
|
||||
if t.type == token.TokenType.variable:
|
||||
newTokenList.append(token.CToken(new_constants.pop(0), token.TokenType.constant))
|
||||
newTokenList.append(token.Token(new_constants.pop(0), token.TokenType.constant))
|
||||
else:
|
||||
newTokenList.append(token.CToken(t.value, t.type))
|
||||
newTokenList.append(token.Token(t.value, t.type))
|
||||
|
||||
return newTokenList
|
||||
|
||||
Reference in New Issue
Block a user