Skip to content

Commit

Permalink
Parse for-range statements
Browse files Browse the repository at this point in the history
  • Loading branch information
5nord committed Feb 2, 2024
1 parent 53068c5 commit b7400aa
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 30 deletions.
13 changes: 13 additions & 0 deletions ttcn3/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ func (p *printer) print(values ...interface{}) {
p.print(n.RParen)
p.print(n.Body)

case *syntax.ForRangeStmt:
if n == nil {
return
}
p.print(n.Tok)
p.print(n.LParen)
p.print(n.VarTok)
p.print(n.Var)
p.print(n.InTok)
p.print(n.Range)
p.print(n.RParen)
p.print(n.Body)

case *syntax.WhileStmt:
if n == nil {
return
Expand Down
5 changes: 5 additions & 0 deletions ttcn3/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func NewScope(n syntax.Node, tree *Tree) *Scope {
case *syntax.ForStmt:
scp.add(n.Init)

case *syntax.ForRangeStmt:
if (n.VarTok != nil) && (n.Var != nil) {
scp.Insert(n.Var, n.Var)
}

case *syntax.IfStmt:
scp.add(n.Then)
scp.add(n.Else)
Expand Down
45 changes: 23 additions & 22 deletions ttcn3/syntax/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,14 @@ type (
}

ForRangeStmt struct {
Tok Token
LParen Token
VarTok Token
Iterator *Ident
InTok Token
Range Expr
RParen Token
Body *BlockStmt
Tok Token
LParen Token
VarTok Token
Var *Ident
InTok Token
Range Expr
RParen Token
Body *BlockStmt
}

// A WhilStmt represents a "while" statement.
Expand Down Expand Up @@ -477,20 +477,21 @@ type (
}
)

func (x *BlockStmt) stmtNode() {}
func (x *DeclStmt) stmtNode() {}
func (x *ExprStmt) stmtNode() {}
func (x *BranchStmt) stmtNode() {}
func (x *ReturnStmt) stmtNode() {}
func (x *CallStmt) stmtNode() {}
func (x *AltStmt) stmtNode() {}
func (x *ForStmt) stmtNode() {}
func (x *WhileStmt) stmtNode() {}
func (x *DoWhileStmt) stmtNode() {}
func (x *IfStmt) stmtNode() {}
func (x *SelectStmt) stmtNode() {}
func (x *CaseClause) stmtNode() {}
func (x *CommClause) stmtNode() {}
func (x *BlockStmt) stmtNode() {}
func (x *DeclStmt) stmtNode() {}
func (x *ExprStmt) stmtNode() {}
func (x *BranchStmt) stmtNode() {}
func (x *ReturnStmt) stmtNode() {}
func (x *CallStmt) stmtNode() {}
func (x *AltStmt) stmtNode() {}
func (x *ForStmt) stmtNode() {}
func (x *ForRangeStmt) stmtNode() {}
func (x *WhileStmt) stmtNode() {}
func (x *DoWhileStmt) stmtNode() {}
func (x *IfStmt) stmtNode() {}
func (x *SelectStmt) stmtNode() {}
func (x *CaseClause) stmtNode() {}
func (x *CommClause) stmtNode() {}

// All nested types implement TypeSpec interface.
type TypeSpec interface {
Expand Down
14 changes: 7 additions & 7 deletions ttcn3/syntax/nodes_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion ttcn3/syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,9 @@ func (p *parser) parseStmt() Stmt {
case LBRACK:
return p.parseAltGuard()
case FOR:
if p.peek(4).Kind() == IN || p.peek(5).Kind() == IN {
return p.parseForRangeLoop()
}
return p.parseForLoop()
case WHILE:
return p.parseWhileLoop()
Expand Down Expand Up @@ -2468,7 +2471,24 @@ func (p *parser) parseForLoop() *ForStmt {
x.Cond = p.parseExpr()
x.CondSemi = p.expect(SEMICOLON)
x.Post = p.parseSimpleStmt()
x.LParen = p.expect(RPAREN)
x.RParen = p.expect(RPAREN)
x.Body = p.parseBlockStmt()
return x
}

func (p *parser) parseForRangeLoop() *ForRangeStmt {
x := new(ForRangeStmt)
x.Tok = p.consume()
x.LParen = p.expect(LPAREN)
if p.tok == VAR {
x.VarTok = p.consume()
x.Var = p.parseName()
} else {
x.Var = p.parseIdent()
}
x.InTok = p.expect(IN)
x.Range = p.parseExpr()
x.RParen = p.expect(RPAREN)
x.Body = p.parseBlockStmt()
return x
}
Expand Down
2 changes: 2 additions & 0 deletions ttcn3/syntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ func TestStmts(t *testing.T) {
{pass, `goto L2;`},
{pass, `for (var int i := 0; i<23; i := i+1) {}`},
{pass, `for (i:=x; i<23; i:=i+1) {}`},
{pass, `for (x in {1,2,3}) {}`},
{pass, `for (var x in a) {}`},
{pass, `while (23) {}`},
{pass, `do {} while (23);`},
{pass, `if (1) {}`},
Expand Down

0 comments on commit b7400aa

Please sign in to comment.