From 3e098c1d1f063c4a2499d9d90009fe623001621b Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Fri, 2 Mar 2012 10:40:46 -0500 Subject: [PATCH] Implemented new list syntax. Found a bug in btparser from parse-utils. Exceptions are being created but not thrown. *slaps face* --- example.dl | 14 ++++---- source/dlparser/dlparser.cpp | 68 ++++++++++++++++++++++++++---------- source/dlparser/dlparser.h | 28 ++++++++------- 3 files changed, 71 insertions(+), 39 deletions(-) diff --git a/example.dl b/example.dl index 27cc0a3..94fe6ab 100644 --- a/example.dl +++ b/example.dl @@ -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 diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index 223b62d..cc4b695 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -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; } diff --git a/source/dlparser/dlparser.h b/source/dlparser/dlparser.h index 0ab48b2..51ab63c 100644 --- a/source/dlparser/dlparser.h +++ b/source/dlparser/dlparser.h @@ -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); -- 2.52.0