]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Made the syntax more permissive of commas in lists, vectors, maps, and macros. This...
authorMike D. Lowis <mike@mdlowis.com>
Mon, 26 Mar 2012 17:59:09 +0000 (13:59 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Mon, 26 Mar 2012 17:59:09 +0000 (13:59 -0400)
example.dl
source/dlparser/dlparser.cpp

index 4480afaa4976ee874e258af89fe098b98a4c6e03..8c248ea6ac7d9fd66b34df2d6e8e892e485e81a3 100644 (file)
@@ -20,7 +20,7 @@ foo = $some_symbol
 foo = {
     $foo : 1 + 1,
     "stuff" : 2 + 2,
-    $bar : 3 + 3
+    $bar : 3 + 3,
 }
 
 print( foo[$bar] )
@@ -36,6 +36,7 @@ foo = [1,2,3,4,5][2]
 
 # List
 foo = ()
+foo = (1,)
 foo = (1,2,3)
 foo = foo[1]
 foo = (1,2,3,4,5)[2]
@@ -53,7 +54,7 @@ foo = ({|a,b| a + b })(1,2)
 
 @ if [
     (E B B) : exec_if($1, $2, $3),
-    (E B)   : exec_if($1, $2)
+    (E B)   : exec_if($1, $2),
 ]
 
 if (1 < 2)
index 2c49bc995999e8d2b68944e3e0fb6e08cb00c0ee..1c4ab10f3f45d680bfb41bee471fbe694f6da424 100644 (file)
@@ -421,29 +421,30 @@ AST* DLParser::Literal(void)
 // MapLiteral = '{' (Literal ':' LogicalExpr)* '}'
 AST* DLParser::MapLiteral(void)
 {
-    AST* ret = NULL;
+    AST* ret = _new AST(MAP);
     AST* child = NULL;
     try
     {
         match(LBRACE);
         do
         {
-            if( lookaheadType(1) == COMMA ) consume();
-
             child = Literal();
             match(SEP);
             child = _new AST(SEP, 2, child, LogicalExpr());
-
-            ret = ((ret == NULL) ? _new AST(MAP) : ret);
             ret->addChild(child);
+
+            if( lookaheadType(1) == COMMA ) consume();
         }
-        while( lookaheadType(1) == COMMA );
+        while( lookaheadType(1) != RBRACE );
         match(RBRACE);
     }
     catch(Exception e)
     {
-        if(ret != NULL) delete ret;
+        // Cleanup our mess so we dont leak memory
+        delete ret;
         if(child != NULL) delete child;
+
+        // Re throw the exception so higher-ups can handle it
         throw e;
     }
     return ret;
@@ -516,10 +517,16 @@ std::list<Pattern> DLParser::MacroPatternList(void)
     std::list<Pattern> patterns;
 
     patterns.push_back( MacroPattern() );
-    while(lookaheadType(1) == COMMA)
+    while (lookaheadType(1) != RBRACK)
     {
-        match(COMMA);
-        patterns.push_back( MacroPattern() );
+        if ( lookaheadType(1) == COMMA )
+        {
+            consume();
+            if (lookaheadType(1) != RBRACK)
+            {
+                patterns.push_back( MacroPattern() );
+            }
+        }
     }
 
     return patterns;
@@ -560,10 +567,16 @@ AST* DLParser::ExpList(TokenType_T node_type, TokenType_T terminator)
     if(lookaheadType(1) != terminator)
     {
         node->addChild( Expression() );
-        while(lookaheadType(1) == COMMA)
+        while(lookaheadType(1) != terminator)
         {
-            match(COMMA);
-            node->addChild( Expression() );
+            if ( lookaheadType(1) == COMMA )
+            {
+                consume();
+                if (lookaheadType(1) != terminator)
+                {
+                    node->addChild( Expression() );
+                }
+            }
         }
     }
     return node;