lang: parser, funcs: Take out the built-in history function

It's a normally named function for now, until we think of a common
package to move it into.

Hopefully this makes improving the lexer and parser easier for now. We
can consider bringing it back if needed.
This commit is contained in:
James Shubin
2023-10-09 17:14:29 -04:00
parent 05d570d250
commit b8f8ec8aa3
5 changed files with 6 additions and 75 deletions

View File

@@ -2,7 +2,7 @@ import "datetime"
$dt = datetime.now() $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" { file "/tmp/mgmt/history" {
state => $const.res.file.state.exists, state => $const.res.file.state.exists,

View File

@@ -12,8 +12,8 @@ $threshold = 1.5 # change me if you like
# simple hysteresis implementation # simple hysteresis implementation
$h1 = $theload > $threshold $h1 = $theload > $threshold
$h2 = $theload{1} > $threshold $h2 = history($theload, 1) > $threshold
$h3 = $theload{2} > $threshold $h3 = history($theload, 2) > $threshold
$unload = $h1 or $h2 or $h3 $unload = $h1 or $h2 or $h3
virt "mgmt1" { virt "mgmt1" {

View File

@@ -27,9 +27,9 @@ import (
) )
const ( const (
// HistoryFuncName is the name this function is registered as. This // HistoryFuncName is the name this function is registered as.
// starts with an underscore so that it cannot be used from the lexer. // TODO: move this into a separate package
HistoryFuncName = "_history" HistoryFuncName = "history"
// arg names... // arg names...
historyArgNameValue = "value" historyArgNameValue = "value"

View File

@@ -295,40 +295,6 @@
panic(fmt.Sprintf("error lexing FLOAT, got: %v", err)) 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]+)?/ /\$[a-z]([a-z0-9_]*[a-z0-9]+)?/
{ {
// an alternate pattern: /\$[a-z](|[a-z0-9_]*[a-z0-9])/ // an alternate pattern: /\$[a-z](|[a-z0-9_]*[a-z0-9])/

View File

@@ -86,7 +86,6 @@ func init() {
%token ELVIS ROCKET ARROW DOT %token ELVIS ROCKET ARROW DOT
%token BOOL_IDENTIFIER STR_IDENTIFIER INT_IDENTIFIER FLOAT_IDENTIFIER %token BOOL_IDENTIFIER STR_IDENTIFIER INT_IDENTIFIER FLOAT_IDENTIFIER
%token MAP_IDENTIFIER STRUCT_IDENTIFIER VARIANT_IDENTIFIER VAR_IDENTIFIER %token MAP_IDENTIFIER STRUCT_IDENTIFIER VARIANT_IDENTIFIER VAR_IDENTIFIER
%token VAR_IDENTIFIER_HX
%token RES_IDENTIFIER %token RES_IDENTIFIER
%token IDENTIFIER CAPITALIZED_IDENTIFIER %token IDENTIFIER CAPITALIZED_IDENTIFIER
%token FUNC_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 | expr IN expr
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos