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

@@ -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])/

View File

@@ -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