From: Gunnar Beutner Date: Tue, 3 Nov 2015 15:34:34 +0000 (+0100) Subject: Implement the current_filename and current_line keywords X-Git-Tag: v2.4.0~98 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=51f473d08a06deef8da745658c0b6435559c3374;p=icinga2 Implement the current_filename and current_line keywords fixes #10243 --- diff --git a/doc/18-language-reference.md b/doc/18-language-reference.md index fbdaa5ffd..4100948a9 100644 --- a/doc/18-language-reference.md +++ b/doc/18-language-reference.md @@ -859,6 +859,15 @@ supports: Additional documentation on type methods is available in the [library reference](19-library-reference.md#library-reference). +## Location Information + +The location of the currently executing script can be obtained using the +`current_filename` and `current_line` keywords. + +Example: + + log("Hello from '" + current_filename + "' in line " + current_line) + ## Reserved Keywords These keywords are reserved and must not be used as constants or custom attributes. @@ -867,6 +876,7 @@ These keywords are reserved and must not be used as constants or custom attribut template include include_recursive + ignore_on_error library null true @@ -887,6 +897,8 @@ These keywords are reserved and must not be used as constants or custom attribut if else in + current_filename + current_line You can escape reserved keywords using the `@` character. The following example tries to set `vars.include` which references a reserved keyword and generates diff --git a/lib/base/configwriter.cpp b/lib/base/configwriter.cpp index 1f4eecd88..bb160a97a 100644 --- a/lib/base/configwriter.cpp +++ b/lib/base/configwriter.cpp @@ -232,6 +232,8 @@ const std::vector& ConfigWriter::GetKeywords(void) keywords.push_back("locals"); keywords.push_back("use"); keywords.push_back("ignore_on_error"); + keywords.push_back("current_filename"); + keywords.push_back("current_line"); keywords.push_back("apply"); keywords.push_back("to"); keywords.push_back("where"); diff --git a/lib/config/config_lexer.ll b/lib/config/config_lexer.ll index 7827a183d..7343a7635 100644 --- a/lib/config/config_lexer.ll +++ b/lib/config/config_lexer.ll @@ -196,6 +196,8 @@ else return T_ELSE; while return T_WHILE; throw return T_THROW; ignore_on_error return T_IGNORE_ON_ERROR; +current_filename return T_CURRENT_FILENAME; +current_line return T_CURRENT_LINE; =\> return T_FOLLOWS; \<\< return T_SHIFT_LEFT; \>\> return T_SHIFT_RIGHT; diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy index a0f83b663..271a22830 100644 --- a/lib/config/config_parser.yy +++ b/lib/config/config_parser.yy @@ -144,6 +144,8 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %token T_LOCALS "locals (T_LOCALS)" %token T_CONST "const (T_CONST)" %token T_IGNORE_ON_ERROR "ignore_on_error (T_IGNORE_ON_ERROR)" +%token T_CURRENT_FILENAME "current_filename (T_CURRENT_FILENAME)" +%token T_CURRENT_LINE "current_line (T_CURRENT_LINE)" %token T_USE "use (T_USE)" %token T_OBJECT "object (T_OBJECT)" %token T_TEMPLATE "template (T_TEMPLATE)" @@ -799,6 +801,14 @@ rterm_no_side_effect: T_STRING { $$ = new GetScopeExpression(ScopeLocal); } + | T_CURRENT_FILENAME + { + $$ = MakeLiteral(@$.Path); + } + | T_CURRENT_LINE + { + $$ = MakeLiteral(@$.FirstLine); + } | rterm_array | rterm_scope_require_side_effect {