Skip to content

Commit

Permalink
Implement basic scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Apr 26, 2024
1 parent 3bf865d commit c879681
Show file tree
Hide file tree
Showing 17 changed files with 1,660 additions and 939 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ docs/
*.jsonl
*txt
/test_parsing
/test/parser_test.go
/test/parser_test.go
*.jsonte
38 changes: 27 additions & 11 deletions grammar/JsonTemplate.g4
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Spread: '...';
As: 'as';
Comma: ',';
Arrow: '=>';
Colon: ':';
Semicolon: ';';
Dot: '.';

LeftBrace: '{';
RightBrace: '}';
Expand All @@ -41,17 +44,30 @@ Null: 'null';
False: 'false';
True: 'true';

Return: 'return';
For: 'for';
In: 'in';
If: 'if';
Else: 'else';
While: 'while';
Do: 'do';
Break: 'break';
Continue: 'continue';

script
: statement*
;

statement
: field ';'
| 'return' field ';'
| 'for' (name (Comma name)*)? 'in' field statements
| 'if' field statements ('else' 'if' field statements)* ('else' statements)?
| 'while' field statements
| 'do' statements 'while' field ';'
: statements
| field Semicolon
| Return field? Semicolon
| Break Semicolon
| Continue Semicolon
| For (name (Comma name)?) In field statements
| If field statements (Else If field statements)* (Else statements)?
| While field statements
| Do statements While field Semicolon
;

statements
Expand Down Expand Up @@ -85,7 +101,7 @@ field
| array
| object
| name
| field (Question? '.' name)
| field (Question? Dot name)
| field (Question? LeftBracket index RightBracket)
| field LeftParen (function_param (Comma function_param)*)? RightParen
| Subtract field
Expand All @@ -102,7 +118,7 @@ field
| field NotEqual field
| field And field
| field Or field
| field Question field (':' field)?
| field Question field (Colon field)?
| field Literal field
;

Expand All @@ -115,12 +131,12 @@ spread_field
;

object
: '{' (object_field (Comma object_field)*)? '}'
: LeftBrace (object_field (Comma object_field)*)? RightBrace
;

object_field
: name ':' field
| ESCAPED_STRING ':' field
: name Colon field
| ESCAPED_STRING Colon field
| Spread? field
;

Expand Down
4 changes: 1 addition & 3 deletions jsonte/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func EvalScript(text string, scope deque.Deque[*types.JsonObject], path string)
p.RemoveErrorListeners()
p.AddErrorListener(&listener)
p.BuildParseTrees = true
tree := p.Expression()
tree := p.Script()
if listener.Error != nil {
return Result{}, burrito.WrapErrorf(listener.Error, "Failed to parse expression \"%s\"", text)
}
Expand All @@ -99,8 +99,6 @@ func EvalScript(text string, scope deque.Deque[*types.JsonObject], path string)
return Result{
Value: r,
Action: visitor.action,
Name: *visitor.name,
IndexName: *visitor.indexName,
VariableScope: visitor.variableScope,
Scope: scope,
}, err
Expand Down
Loading

0 comments on commit c879681

Please sign in to comment.