diff --git a/examples/lang/history1.mcl b/examples/lang/history1.mcl index da68ba25..c953501f 100644 --- a/examples/lang/history1.mcl +++ b/examples/lang/history1.mcl @@ -2,7 +2,7 @@ import "datetime" $dt = datetime.now() -$hystvalues = {"ix0" => $dt, "ix1" => $dt{1}, "ix2" => $dt{2}, "ix3" => $dt{3},} +$hystvalues = {"ix0" => $dt, "ix1" => history($dt, 1), "ix2" => history($dt, 2), "ix3" => history($dt, 3),} file "/tmp/mgmt/history" { state => $const.res.file.state.exists, diff --git a/examples/lang/hysteresis1.mcl b/examples/lang/hysteresis1.mcl index 2eee093c..5fc96e67 100644 --- a/examples/lang/hysteresis1.mcl +++ b/examples/lang/hysteresis1.mcl @@ -12,8 +12,8 @@ $threshold = 1.5 # change me if you like # simple hysteresis implementation $h1 = $theload > $threshold -$h2 = $theload{1} > $threshold -$h3 = $theload{2} > $threshold +$h2 = history($theload, 1) > $threshold +$h3 = history($theload, 2) > $threshold $unload = $h1 or $h2 or $h3 virt "mgmt1" { diff --git a/lang/funcs/history_func.go b/lang/funcs/history_func.go index 9614601a..3c3cd283 100644 --- a/lang/funcs/history_func.go +++ b/lang/funcs/history_func.go @@ -27,9 +27,9 @@ import ( ) const ( - // HistoryFuncName is the name this function is registered as. This - // starts with an underscore so that it cannot be used from the lexer. - HistoryFuncName = "_history" + // HistoryFuncName is the name this function is registered as. + // TODO: move this into a separate package + HistoryFuncName = "history" // arg names... historyArgNameValue = "value" diff --git a/lang/parser/lexer.nex b/lang/parser/lexer.nex index 5d165c39..411c52f6 100644 --- a/lang/parser/lexer.nex +++ b/lang/parser/lexer.nex @@ -295,40 +295,6 @@ panic(fmt.Sprintf("error lexing FLOAT, got: %v", err)) } } -/\$[a-z]+([a-z0-9_]*[a-z0-9]+)?{[0-9]+}/ - { - // we have this as a single token, because otherwise the - // parser can get confused by the curly brackets :/ - yylex.pos(lval) // our pos - s := yylex.Text() - s = s[1:len(s)] // remove the leading $ - s = s[0:len(s)-1] // remove the trailing close curly - // XXX: nex has a bug that it gets confused by the - // following single curly brace. Please see: - // https://github.com/blynn/nex/issues/48 - a := strings.Split(s, "{") // XXX: close match here: } - if len(a) != 2 { - panic(fmt.Sprintf("error lexing VAR_IDENTIFIER_HX: %v", a)) - } - lval.str = a[0] - var err error - lval.int, err = strconv.ParseInt(a[1], 10, 64) // int64 - if err == nil { - return VAR_IDENTIFIER_HX - } else if e := err.(*strconv.NumError); e.Err == strconv.ErrRange { - // this catches range errors for very large ints - lp := yylex.cast() - lp.lexerErr = &LexParseErr{ - Err: ErrLexerIntegerOverflow, - Str: a[1], - Row: yylex.Line(), - Col: yylex.Column(), - } - return ERROR - } else { - panic(fmt.Sprintf("error lexing VAR_IDENTIFIER_HX, got: %v", err)) - } - } /\$[a-z]([a-z0-9_]*[a-z0-9]+)?/ { // an alternate pattern: /\$[a-z](|[a-z0-9_]*[a-z0-9])/ diff --git a/lang/parser/parser.y b/lang/parser/parser.y index 13e887f1..df1730fe 100644 --- a/lang/parser/parser.y +++ b/lang/parser/parser.y @@ -86,7 +86,6 @@ func init() { %token ELVIS ROCKET ARROW DOT %token BOOL_IDENTIFIER STR_IDENTIFIER INT_IDENTIFIER FLOAT_IDENTIFIER %token MAP_IDENTIFIER STRUCT_IDENTIFIER VARIANT_IDENTIFIER VAR_IDENTIFIER -%token VAR_IDENTIFIER_HX %token RES_IDENTIFIER %token IDENTIFIER CAPITALIZED_IDENTIFIER %token FUNC_IDENTIFIER @@ -703,40 +702,6 @@ call: }, } } -| VAR_IDENTIFIER_HX - // get the N-th historical value, eg: $foo{3} is equivalent to: history($foo, 3) - { - posLast(yylex, yyDollar) // our pos - $$.expr = &ast.ExprCall{ - Name: funcs.HistoryFuncName, - Args: []interfaces.Expr{ - &ast.ExprVar{ - Name: $1.str, - }, - &ast.ExprInt{ - V: $1.int, - }, - }, - } - } -// TODO: fix conflicts with this method, and replace the above VAR_IDENTIFIER_HX -// TODO: allow $pkg.ns.foo{4}, instead of $foo = $pkg.ns.foo ; $foo{4} -// TODO: use this: dotted_var_identifier OPEN_BRACK INTEGER CLOSE_BRACK instead? -//| dotted_var_identifier OPEN_CURLY INTEGER CLOSE_CURLY -// { -// posLast(yylex, yyDollar) // our pos -// $$.expr = &ast.ExprCall{ -// Name: funcs.HistoryFuncName, -// Args: []interfaces.Expr{ -// &ast.ExprVar{ -// Name: $1.str, -// }, -// &ast.ExprInt{ -// V: $3.int, -// }, -// }, -// } -// } | expr IN expr { posLast(yylex, yyDollar) // our pos