if (reader == NULL)
m_currentToken->m_tokenType = TOKEN_END_OF_SOURCE;
else {
- if (waitingPosition == NULL){
- m_waitingPosition2 = m_currentPosition;
- m_currentPosition = m_source->newPosition(m_currentCol);
- } else {
- m_currentPosition = m_waitingPosition1;
- m_waitingPosition1 = m_waitingPosition2;
- m_waitingPosition2 = NULL;
- ((RplSourcePosition*)waitingPosition)->setSourceUnit(
- m_source->currentReader()->currentSourceUnit());
- ((RplSourcePosition*)waitingPosition)->setColumn(m_currentCol);
- waitingPosition = NULL;
- }
+ m_waitingPosition2 = m_waitingPosition1;
+ m_waitingPosition1 = m_currentPosition;
+ m_currentPosition = m_source->newPosition(m_currentCol);
if (! fillInput()){
m_currentToken->m_tokenType = TOKEN_END_OF_SOURCE;
} else {
*/
void RplLexer::undoLastToken2()
{
- m_waitingToken = m_currentToken;
- m_waitingToken2 = m_currentToken == &m_token1 ? &m_token2 : &m_token1;
+ m_waitingToken2 = m_currentToken;
+ m_waitingToken = m_currentToken == &m_token1 ? &m_token2 : &m_token1;
m_waitingPosition2 = m_waitingPosition1;
m_waitingPosition1 = m_currentPosition;
}
*/
void RplLexer::saveLastToken()
{
- m_currentToken = m_currentToken == &m_token1 ? &m_token2 : &m_token1;
+ if (m_waitingToken == NULL)
+ m_currentToken = m_currentToken == &m_token1 ? &m_token2 : &m_token1;
}
/**
RplToken* token2;
while(again){
token = m_lexer.nextNonSpaceToken();
- if (token->isOperator(O_LBRACKET))
+ if (token->isOperator(O_RBRACKET))
again = false;
else{
variant = NULL;
break;
}
}
- if (variant == NULL){
+ if (variant == NULL && ! token->isOperator(O_RBRACKET)){
m_lexer.undoLastToken2();
RplASExprStatement* expr = dynamic_cast<RplASExprStatement*>
(parseExprStatement());
+ if (expr != NULL){
// chaining per m_successor is for freeing in destruction:
expr->setSuccessor(rc->child());
rc->setChild(expr);
+ // freed in the destructor of varList (~RplASVariant()):
+ variant = new RplASVariant();
variant->setObject(expr, &RplASFormula::m_instance);
token = m_lexer.currentToken();
if (token->isOperator(O_RBRACKET))
again = false;
- else if (token->isOperator(O_COMMA))
- m_lexer.undoLastToken();
- else
+ else if (! token->isOperator(O_COMMA))
syntaxError(L_PARSE_LIST_NO_COMMA, "',' or ']' expected");
+ }
}
- list->append(variant);
+ if (variant != NULL)
+ list->append(variant);
}
}
return rc;
}
break;
}
+ case TOKEN_END_OF_SOURCE:
+ break;
default:
// this call never comes back (exception!)
syntaxError(L_PARSE_OPERAND_WRONG,
RplASItem* RplMFParser::parseExpr(int depth){
RplToken* token;
RplASItem* top = parseOperand(depth);
- int lastPrio = INT_MAX;
- bool again = true;
- do {
- token = m_lexer.nextNonSpaceToken();
- RplTokenType tokenType = token->tokenType();
- switch(tokenType){
- case TOKEN_OPERATOR:
- {
- Operator opId = (Operator) token->id();
- if (IS_BINARY_OP(opId)){
- RplASBinaryOp* op = new RplASBinaryOp();
- op->setPosition(m_lexer.currentPosition());
- op->setOperator(opId);
+ if (top != NULL){
+ int lastPrio = INT_MAX;
+ bool again = true;
+ do {
+ token = m_lexer.nextNonSpaceToken();
+ RplTokenType tokenType = token->tokenType();
+ switch(tokenType){
+ case TOKEN_OPERATOR:
+ {
+ Operator opId = (Operator) token->id();
+ if (IS_BINARY_OP(opId)){
+ RplASBinaryOp* op = new RplASBinaryOp();
+ op->setPosition(m_lexer.currentPosition());
+ op->setOperator(opId);
- int prio = m_lexer.prioOfOp(token->id());
- if (prio < lastPrio
- || (prio == lastPrio
- && ! m_lexer.isRightAssociative(opId))){
- op->setChild(top);
- top = op;
- } else {
- // right assoc or higher priority:
- RplASBinaryOp* top2 = dynamic_cast<RplASBinaryOp*>(top);
- op->setChild(top2->child2());
- top2->setChild2(op);
- }
- lastPrio = prio;
- op->setChild2(parseOperand(depth));
- } else
+ int prio = m_lexer.prioOfOp(token->id());
+ if (prio < lastPrio
+ || (prio == lastPrio
+ && ! m_lexer.isRightAssociative(opId))){
+ op->setChild(top);
+ top = op;
+ } else {
+ // right assoc or higher priority:
+ RplASBinaryOp* top2 = dynamic_cast<RplASBinaryOp*>(top);
+ op->setChild(top2->child2());
+ top2->setChild2(op);
+ }
+ lastPrio = prio;
+ op->setChild2(parseOperand(depth));
+ } else
+ again = false;
+ break;
+ }
+ case TOKEN_STRING:
+ syntaxError(L_TERM_WRONG_STRING, "Operator expected, not a string");
+ break;
+ case TOKEN_NUMBER:
+ case TOKEN_REAL:
+ syntaxError(L_TERM_WRONG_NUMBER, "Operator expected, not a number");
+ break;
+ case TOKEN_KEYWORD:
+ case TOKEN_ID:
+ case TOKEN_END_OF_SOURCE:
+ default:
again = false;
- break;
- }
- case TOKEN_STRING:
- syntaxError(L_TERM_WRONG_STRING, "Operator expected, not a string");
- break;
- case TOKEN_NUMBER:
- case TOKEN_REAL:
- syntaxError(L_TERM_WRONG_NUMBER, "Operator expected, not a number");
- break;
- case TOKEN_KEYWORD:
- case TOKEN_ID:
- case TOKEN_END_OF_SOURCE:
- default:
- again = false;
- break;
- }
- } while(again);
+ break;
+ }
+ } while(again);
+ }
return top;
}
RplASItem* RplMFParser::parseExprStatement()
{
RplASItem* item = parseExpr(0);
- RplASExprStatement* statement = new RplASExprStatement();
- statement->setPosition(item->position());
- statement->setChild(item);
+ RplASExprStatement* statement = NULL;
+ if (item != NULL){
+ statement = new RplASExprStatement();
+ statement->setPosition(item->position());
+ statement->setChild(item);
+ }
return statement;
}
checkAST("forIt1.txt", __LINE__);
}
void listTest(){
- setSource("List a = [2, 3.14, 1+9, 'hi', a]; List b = [];");
+ setSource("List b = [];");
RplMFParser parser(m_source, m_tree);
parser.parse();
- checkAST("forIt1.txt", __LINE__);
+ checkAST("list1.txt", __LINE__);
+ setSource("List a = [2+3, 3.14, 7, 'hi', a]; List b = [];");
+ parser.parse();
+ checkAST("list2.txt", __LINE__);
}
virtual void doIt(void) {