Skip to content

Commit

Permalink
repl: implement %parse (#933)
Browse files Browse the repository at this point in the history
* repl: implement %parse

Signed-off-by: Justin King <jcking@google.com>

* Fix help test

Signed-off-by: Justin King <jcking@google.com>

---------

Signed-off-by: Justin King <jcking@google.com>
  • Loading branch information
jcking committed Apr 25, 2024
1 parent 2133a6d commit 65f4686
Show file tree
Hide file tree
Showing 14 changed files with 1,890 additions and 2,304 deletions.
18 changes: 18 additions & 0 deletions repl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (
compileUsage = `Compile emits a textproto representation of the compiled expression.
%compile <expr>`

parseUsage = `Parse emits a textproto representation of the parsed expression.
%parse <expr>`

declareUsage = `Declare introduces a variable or function for type checking, but
doesn't define a value for it:
%declare <identifier> : <type>
Expand Down Expand Up @@ -86,6 +89,10 @@ type compileCmd struct {
expr string
}

type parseCmd struct {
expr string
}

type evalCmd struct {
expr string
}
Expand Down Expand Up @@ -124,6 +131,10 @@ func (c *compileCmd) Cmd() string {
return "compile"
}

func (c *parseCmd) Cmd() string {
return "parse"
}

func (c *evalCmd) Cmd() string {
return "eval"
}
Expand Down Expand Up @@ -186,6 +197,7 @@ func Parse(line string) (Cmder, error) {
if listener.cmd.Cmd() == "help" {
return nil, errors.New(strings.Join([]string{
compileUsage,
parseUsage,
declareUsage,
deleteUsage,
letUsage,
Expand Down Expand Up @@ -281,6 +293,10 @@ func (c *commandParseListener) EnterCompile(ctx *parser.CompileContext) {
c.cmd = &compileCmd{}
}

func (c *commandParseListener) EnterParse(ctx *parser.ParseContext) {
c.cmd = &parseCmd{}
}

func (c *commandParseListener) EnterExprCmd(ctx *parser.ExprCmdContext) {
c.cmd = &evalCmd{}
}
Expand Down Expand Up @@ -366,6 +382,8 @@ func (c *commandParseListener) ExitExpr(ctx *parser.ExprContext) {
switch cmd := c.cmd.(type) {
case *compileCmd:
cmd.expr = expr
case *parseCmd:
cmd.expr = expr
case *evalCmd:
cmd.expr = expr
case *letFnCmd:
Expand Down
3 changes: 3 additions & 0 deletions repl/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func TestParse(t *testing.T) {
commandLine: `%help`,
wantErr: errors.New(`Compile emits a textproto representation of the compiled expression.
%compile <expr>
Parse emits a textproto representation of the parsed expression.
%parse <expr>
Declare introduces a variable or function for type checking, but
doesn't define a value for it:
Expand Down
23 changes: 23 additions & 0 deletions repl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,16 @@ func (e *Evaluator) Process(cmd Cmder) (string, bool, error) {
return "", false, fmt.Errorf("compile failed:\n%v", err)
}
return prototext.Format(cAST), false, nil
case *parseCmd:
ast, err := e.Parse(cmd.expr)
if err != nil {
return "", false, fmt.Errorf("parse failed:\n%v", err)
}
pAST, err := cel.AstToParsedExpr(ast)
if err != nil {
return "", false, fmt.Errorf("parse failed:\n%v", err)
}
return prototext.Format(pAST), false, nil
case *evalCmd:
val, resultT, err := e.Evaluate(cmd.expr)
if err != nil {
Expand Down Expand Up @@ -1051,3 +1061,16 @@ func (e *Evaluator) Compile(expr string) (*cel.Ast, error) {
}
return ast, nil
}

// Parse parses the input expression using the current REPL context.
func (e *Evaluator) Parse(expr string) (*cel.Ast, error) {
env, _, err := e.applyContext()
if err != nil {
return nil, err
}
ast, iss := env.Parse(expr)
if iss.Err() != nil {
return nil, iss.Err()
}
return ast, nil
}
4 changes: 3 additions & 1 deletion repl/parser/Commands.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ command: help |
delete |
simple |
compile |
parse |
exprCmd |
empty;

Expand All @@ -48,6 +49,8 @@ empty: ;

compile: '%compile' e=expr;

parse: '%parse' e=expr;

exprCmd: '%eval'? e=expr;

qualId: leadingDot='.'? rid=IDENTIFIER ('.' qualifiers+=IDENTIFIER)*;
Expand All @@ -69,4 +72,3 @@ COMMAND: '%' IDENTIFIER;
FLAG: '-'? '-' IDENTIFIER;
ARROW: '->';
EQUAL_ASSIGN: '=';

5 changes: 4 additions & 1 deletion repl/parser/Commands.interp

Large diffs are not rendered by default.

144 changes: 73 additions & 71 deletions repl/parser/Commands.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,82 @@ T__3=4
T__4=5
T__5=6
T__6=7
COMMAND=8
FLAG=9
ARROW=10
EQUAL_ASSIGN=11
EQUALS=12
NOT_EQUALS=13
IN=14
LESS=15
LESS_EQUALS=16
GREATER_EQUALS=17
GREATER=18
LOGICAL_AND=19
LOGICAL_OR=20
LBRACKET=21
RPRACKET=22
LBRACE=23
RBRACE=24
LPAREN=25
RPAREN=26
DOT=27
COMMA=28
MINUS=29
EXCLAM=30
QUESTIONMARK=31
COLON=32
PLUS=33
STAR=34
SLASH=35
PERCENT=36
CEL_TRUE=37
CEL_FALSE=38
NUL=39
WHITESPACE=40
COMMENT=41
NUM_FLOAT=42
NUM_INT=43
NUM_UINT=44
STRING=45
BYTES=46
IDENTIFIER=47
T__7=8
COMMAND=9
FLAG=10
ARROW=11
EQUAL_ASSIGN=12
EQUALS=13
NOT_EQUALS=14
IN=15
LESS=16
LESS_EQUALS=17
GREATER_EQUALS=18
GREATER=19
LOGICAL_AND=20
LOGICAL_OR=21
LBRACKET=22
RPRACKET=23
LBRACE=24
RBRACE=25
LPAREN=26
RPAREN=27
DOT=28
COMMA=29
MINUS=30
EXCLAM=31
QUESTIONMARK=32
COLON=33
PLUS=34
STAR=35
SLASH=36
PERCENT=37
CEL_TRUE=38
CEL_FALSE=39
NUL=40
WHITESPACE=41
COMMENT=42
NUM_FLOAT=43
NUM_INT=44
NUM_UINT=45
STRING=46
BYTES=47
IDENTIFIER=48
'%help'=1
'%?'=2
'%let'=3
'%declare'=4
'%delete'=5
'%compile'=6
'%eval'=7
'->'=10
'='=11
'=='=12
'!='=13
'in'=14
'<'=15
'<='=16
'>='=17
'>'=18
'&&'=19
'||'=20
'['=21
']'=22
'{'=23
'}'=24
'('=25
')'=26
'.'=27
','=28
'-'=29
'!'=30
'?'=31
':'=32
'+'=33
'*'=34
'/'=35
'%'=36
'true'=37
'false'=38
'null'=39
'%parse'=7
'%eval'=8
'->'=11
'='=12
'=='=13
'!='=14
'in'=15
'<'=16
'<='=17
'>='=18
'>'=19
'&&'=20
'||'=21
'['=22
']'=23
'{'=24
'}'=25
'('=26
')'=27
'.'=28
','=29
'-'=30
'!'=31
'?'=32
':'=33
'+'=34
'*'=35
'/'=36
'%'=37
'true'=38
'false'=39
'null'=40
5 changes: 4 additions & 1 deletion repl/parser/CommandsLexer.interp

Large diffs are not rendered by default.

0 comments on commit 65f4686

Please sign in to comment.