]> gitweb.hamatoma.de Git - reqt/commitdiff
day's work
authorhama <hama@siduction.net>
Fri, 11 Jul 2014 23:44:31 +0000 (01:44 +0200)
committerhama <hama@siduction.net>
Fri, 11 Jul 2014 23:44:31 +0000 (01:44 +0200)
rplexpr/rplasclasses.cpp
rplexpr/rpllexer.cpp
rplexpr/rplmfparser.cpp
test/rplmfparser/list1.txt [new file with mode: 0644]
test/rplmfparser/list2.txt [new file with mode: 0644]
unittests/rplmfparser_test.cpp

index 9362ce561012b9956680c0ea5644b9720dbc880f..e780bd0f872b3210c7176b76c2aa07e417862358 100644 (file)
@@ -788,7 +788,7 @@ RplASFormula::RplASFormula() :
  * @param formula    ignored
  * @return
  */
-void*RplASFormula::newValueInstance(void* expr) const
+void* RplASFormula::newValueInstance(void* expr) const
 {
     return expr;
 }
@@ -821,7 +821,11 @@ bool RplASFormula::boolValueOf(void*) const
  * @param maxLength ignored
  * @return          the empty string
  */
-QString RplASFormula::toString(void*, int) const
+QString RplASFormula::toString(void* object, int) const
 {
-    return QString("");
+    RplASExprStatement* expr = static_cast<RplASExprStatement*>(object);
+
+    QString rc;
+    rc.sprintf("<formula %d>", expr->id());
+    return rc;
 }
index ef5c7c93a87323f6617635af43d922a6800dcd54..414e39437920d6297464bdc7f1f51d9e56e1925b 100644 (file)
@@ -869,18 +869,9 @@ RplToken* RplLexer::nextToken()
         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 {
@@ -983,8 +974,8 @@ void RplLexer::undoLastToken()
  */
 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;
 }
@@ -1002,7 +993,8 @@ void RplLexer::undoLastToken2()
  */
 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;
 }
 
 /**
index ca93d7ca974402a5eec156b72b23c0c965f3fb86..202dffa6b5839ab81f36c72d5999a4f6599fc2cb 100644 (file)
@@ -255,7 +255,7 @@ RplASItem* RplMFParser::parseList()
     RplToken* token2;
     while(again){
         token = m_lexer.nextNonSpaceToken();
-        if (token->isOperator(O_LBRACKET))
+        if (token->isOperator(O_RBRACKET))
             again = false;
         else{
             variant = NULL;
@@ -298,23 +298,26 @@ RplASItem* RplMFParser::parseList()
                     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;
@@ -437,6 +440,8 @@ RplASItem* RplMFParser::parseOperand(int level)
         }
         break;
     }
+    case TOKEN_END_OF_SOURCE:
+        break;
     default:
         // this call never comes back (exception!)
         syntaxError(L_PARSE_OPERAND_WRONG,
@@ -466,53 +471,55 @@ RplASItem* RplMFParser::parseOperand(int level)
 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;
 }
 
@@ -528,9 +535,12 @@ RplASItem* RplMFParser::parseExpr(int depth){
 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;
 }
 
diff --git a/test/rplmfparser/list1.txt b/test/rplmfparser/list1.txt
new file mode 100644 (file)
index 0000000..2b6cc66
--- /dev/null
@@ -0,0 +1,8 @@
+List b = [];
+= <test> (module) parent: global
+== Classes:
+== Variables:
+== Body:
+varDef b (List) id: 1 succ: 0 attr: 0x0 <test>:1:5
+       listConst id: 2 <test>:1:9
+               []
diff --git a/test/rplmfparser/list2.txt b/test/rplmfparser/list2.txt
new file mode 100644 (file)
index 0000000..cc1885d
--- /dev/null
@@ -0,0 +1,11 @@
+List a = [2+3, 3.14, 7, 'hi', a]; List b = [];
+= <test> (module) parent: global
+== Classes:
+== Variables:
+== Body:
+varDef a (List) id: 1 succ: 9 attr: 0x0 <test>:1:5
+       listConst id: 2 <test>:1:9
+               [<formula 6>,3.140000,7,'hi',<formula 8>]
+varDef b (List) id: 9 succ: 0 attr: 0x0 <test>:1:39
+       listConst id: 10 <test>:1:43
+               []
index f2ad3b77fe86c5e0b488542b1993adbfe40feca0..533b3698e9dbee313b1fcf0194d780702475e8bb 100644 (file)
@@ -114,10 +114,13 @@ public:
         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) {