]> granicus.if.org Git - icinga2/commitdiff
Implement another syntax for nullary lambdas
authorGunnar Beutner <gunnar@beutner.name>
Fri, 30 Jan 2015 08:49:57 +0000 (09:49 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 30 Jan 2015 08:49:57 +0000 (09:49 +0100)
refs #7564

doc/15-language-reference.md
lib/config/config_lexer.ll
lib/config/config_parser.yy

index ea4112cccea8d37d85d5918bff0b97ef1d1cda87..17fab5fd8ace2554784c8e908fb17b09aaae2133 100644 (file)
@@ -574,6 +574,16 @@ For lambdas which take exactly one argument the braces around the arguments can
 
     f = x => x * x
 
+## <a id="nullary-lambdas"></a> Abbreviated Lambda Syntax
+
+Lambdas which take no arguments can also be written using the abbreviated lambda syntax.
+
+Example:
+
+    f = {{ 3 }}
+
+This creates a new function which returns the value 3.
+
 ## <a id="variable-scopes"></a> Variable Scopes
 
 When setting a variable Icinga checks the following scopes in this order whether the variable
index 169fd7dba830c9466d9333b1f5bd4a33def2c03b..5885e3de19a9de895c5282796b066d464bd50a66 100644 (file)
@@ -217,6 +217,8 @@ while                               return T_WHILE;
 in                             return T_IN;
 &&                             return T_LOGICAL_AND;
 \|\|                           return T_LOGICAL_OR;
+\{\{                           return T_NULLARY_LAMBDA_BEGIN;
+\}\}                           return T_NULLARY_LAMBDA_END;
 [a-zA-Z_][a-zA-Z0-9\_]*                { yylval->text = strdup(yytext); return T_IDENTIFIER; }
 @[a-zA-Z_][a-zA-Z0-9\_]*       { yylval->text = strdup(yytext + 1); return T_IDENTIFIER; }
 \<[^ \>]*\>                    { yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING_ANGLE; }
index acef893fa94c177edb2f777b98555acdc97cb05d..83a2e55149094c87f8293729f944c659409c3c60 100644 (file)
@@ -177,6 +177,8 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
 %token T_ELSE "else (T_ELSE)"
 %token T_WHILE "while (T_WHILE)"
 %token T_FOLLOWS "=> (T_FOLLOWS)"
+%token T_NULLARY_LAMBDA_BEGIN "{{ (T_NULLARY_LAMBDA_BEGIN)"
+%token T_NULLARY_LAMBDA_END "}} (T_NULLARY_LAMBDA_END)"
 
 %type <text> identifier
 %type <elist> rterm_items
@@ -871,6 +873,23 @@ rterm_no_side_effect: T_STRING
                $$ = new FunctionExpression(*$3, $5, aexpr, @$);
                delete $3;
        }
+       | T_NULLARY_LAMBDA_BEGIN statements T_NULLARY_LAMBDA_END
+       {
+               std::vector<Expression *> dlist;
+               typedef std::pair<Expression *, EItemInfo> EListItem;
+               int num = 0;
+               BOOST_FOREACH(const EListItem& litem, *$2) {
+                       if (!litem.second.SideEffect && num != $2->size() - 1)
+                               yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used.");
+                       dlist.push_back(litem.first);
+                       num++;
+               }
+               delete $2;
+               DictExpression *aexpr = new DictExpression(dlist, @$);
+               aexpr->MakeInline();
+
+               $$ = new FunctionExpression(std::vector<String>(), NULL, aexpr, @$);
+       }
        ;
 
 rterm: rterm_side_effect