]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Implemented new list syntax. Found a bug in btparser from parse-utils. Exceptions...
authorMike D. Lowis <mike@mdlowis.com>
Fri, 2 Mar 2012 15:40:46 +0000 (10:40 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Fri, 2 Mar 2012 15:40:46 +0000 (10:40 -0500)
example.dl
source/dlparser/dlparser.cpp
source/dlparser/dlparser.h

index 27cc0a37291b157288eb49ab0edc4c9a41324bc0..94fe6ab8c17c3fe9a6db510163f1e37207b14fea 100644 (file)
@@ -1,12 +1,10 @@
 foo = ()
-foo = (1,2,3)
+foo = (1)
+foo = (1,2)
 foo = []
-foo = [1,2,3]
+foo = [1]
+foo = [1,2]
 foo = ""
-foo = "123"
-foo = 'a'
-foo = 1.0
-foo = $foo
-foo[0]
+foo = "1"
+foo = "12"
 
-foo = 1 + 1 * 1 - 1
index 223b62d24ae8e74bd2e6a68397c2f14d4048f799..cc4b6956faca8251ddc6ee4c45605063a487994d 100644 (file)
@@ -48,11 +48,38 @@ AST* DLParser::parseMacroParam(Param* param)
             oss << "Expected macro parameter type. Expected " << param->type() << ", received " << tok.type() << ".";
             Exception ex( tok.line(), tok.column() );
             ex.setMessage(oss.str());
+            throw ex;
             break;
     }
     return ret;
 }
 
+bool DLParser::speculate_GroupExpr(void)
+{
+    AST* throw_away = NULL;
+    bool success = true;
+
+    mark();
+    try
+    {
+        match(LPAR);
+        throw_away = LogicalExpr();
+        match(RPAR);
+    }
+    catch (Exception e)
+    {
+        success = false;
+    }
+    release();
+
+    if (throw_away != NULL)
+    {
+        delete throw_away;
+    }
+
+    return success;
+}
+
 AST* DLParser::Program(void)
 {
     AST* node = _new AST( PROGRAM );
@@ -156,28 +183,31 @@ AST* DLParser::UnaryExpr(void)
 AST* DLParser::GroupExpr(void)
 {
     AST* ret = NULL;
-    //if(lookaheadType(1) == LPAR)
-    //{
-    //    match(LPAR);
-    //    ret = LogicalExpr();
-    //    match(RPAR);
-    //}
-    //else
+
+    if(speculate_GroupExpr())
+    {
+        match(LPAR);
+        ret = LogicalExpr();
+        match(RPAR);
+    }
+    else
     {
         ret = Literal();
-        if( lookaheadType(1) == LPAR )
-        {
-            match(LPAR);
-            ret = _new AST( FN_CALL, 2, ret, ExpList( LIST, RPAR ) );
-            match(RPAR);
-        }
-        else if( lookaheadType(1) == LBRACK )
-        {
-            match(LBRACK);
-            ret = _new AST( ARRY_IDX, 2, ret, LogicalExpr() );
-            match(RBRACK);
-        }
     }
+
+    if( lookaheadType(1) == LPAR )
+    {
+        match(LPAR);
+        ret = _new AST( FN_CALL, 2, ret, ExpList( LIST, RPAR ) );
+        match(RPAR);
+    }
+    else if( lookaheadType(1) == LBRACK )
+    {
+        match(LBRACK);
+        ret = _new AST( ARRY_IDX, 2, ret, LogicalExpr() );
+        match(RBRACK);
+    }
+
     return ret;
 }
 
index 0ab48b25f53bb378e4f7a28d929634074fe1b2e8..51ab63ca1311e297bbfe41b280ea42b61b7479f4 100644 (file)
@@ -16,6 +16,7 @@ class DLParser : public BTParser
         AST* parse(void);
         bool isMacro(Token& token);
         AST* parseMacroParam(Param* param);
+        bool speculate_GroupExpr(void);
 
         /**********************************************************************
          * EBNF Syntax Grammar
@@ -39,19 +40,22 @@ class DLParser : public BTParser
         //           | GroupExpr
         //
         // GroupExpr = '(' LogicalExpr ')'
-        //           | Literal '(' ExpList ')'
+        //           | '(' LogicalExpr ')' '(' ExpList ')'
+        //           | '(' LogicalExpr ')' '[' LogicalExpr ']'
         //           | Literal
+        //           | Literal '(' ExpList ')'
+        //           | Literal '[' LogicalExpr ']'
         //
-        // Literal    = VectorLiteral
-        //            | ListLiteral
-        //            | FuncLiteral
-        //            | ID
-        //            | NUM
-        //            | CHAR
-        //            | STRING
-        //            | SYMBOL
+        // Literal = VectorLiteral
+        //         | ListLiteral
+        //         | FuncLiteral
+        //         | ID
+        //         | NUM
+        //         | CHAR
+        //         | STRING
+        //         | SYMBOL
         //
-        // VectorLiteral =  '[' ExpList ']'
+        // VectorLiteral = '[' ExpList ']'
         //
         // ListLiteral = '(' ExpList ')'
         //
@@ -64,9 +68,9 @@ class DLParser : public BTParser
         //
         // MacroParam = ID (':' ID)?
         //
-        // ExpList    = (GroupExpr (',' GroupExpr)*)?
+        // ExpList = (GroupExpr (',' GroupExpr)*)?
         //
-        // ExpBlock   = Expression*
+        // ExpBlock = Expression*
     private:
         AST* Program(void);
         AST* Expression(void);