def _parse(tokenIter): """ _parse() recursively extract the expression from the given token iterator //param tokenIter is the token iterator that represents the stream of tokens //return a pair, where the first parameter reports the error code occurs while parsing (OK indicates no error happens); the second parameter is the extracted expression, which will be None if error occurs. """ try: token = tokenIter.next() except StopIteration: return ParseError.TOKEN_NORMALLY_ENDS, None # Token type is ERROR if token.tokenType == Tokens.ERROR: return ParseError.TOKENIZE_ERROR, token # All tokens, except the special characters, are considered. special char are: ( ) ' if not token.tokenType in Tokens.specialCharacters: return ParseError.OK, Value.makeFromToken(token) # check if this is the end of an expression if token.tokenType == Tokens.RPAREN: return ParseError.CE_ENDS, token """ # If the token either indicates the expression element # (operator, parameters)nor the end of expression, then it # must be the "beginning" of a expression. if token.tokenType != Tokens.LPAREN: return ParseError.EXPECT_LPAREN, token """ if token.tokenType == Tokens.QUOTE: error, result = _parse(tokenIter) return ParseError.OK, Value.makeLiteral(result) sList = [] # Read parameters, operator is placed together with parameter as the first element of the list while True: error, param = _parse(tokenIter) if error != ParseError.OK: # reach the end the this expression? if error == ParseError.CE_ENDS: break elif error == ParseError.TOKEN_NORMALLY_ENDS: return ParseError.TOKEN_UNEXPECTED_ENDS, None #position: the end of the expression # or real error occurs else: return error, param sList.append(param) return ParseError.OK, Value.makeList(sList)