From 2ed019b670ee12dcb88a5b699baae4e4c5e3da60 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 1 Jun 2017 13:39:51 +1000 Subject: [PATCH] Add "while:label" and "break:label" syntax. --- lang/ast/ast.go | 16 +++++++++++----- lang/check/check_test.go | 12 +++++++----- lang/parse/parse.go | 20 ++++++++++++++++++-- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 49b0268f..9afeef48 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -319,20 +319,23 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field { } } -// While is "while LHS, List0 { List1 }": +// While is "while:ID1 LHS, List0 { List1 }": +// - ID1: <0|label> // - LHS: // - List0: asserts // - List1: loop body type While Node func (n *While) Node() *Node { return (*Node)(n) } +func (n *While) Label() t.ID { return n.id1 } func (n *While) Condition() *Expr { return n.lhs.Expr() } func (n *While) Asserts() []*Node { return n.list0 } func (n *While) Body() []*Node { return n.list1 } -func NewWhile(condition *Expr, asserts []*Node, body []*Node) *While { +func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While { return &While{ kind: KWhile, + id1: label, lhs: condition.Node(), list0: asserts, list1: body, @@ -376,17 +379,20 @@ func NewReturn(value *Expr) *Return { } } -// Jump is "break" or "continue": -// TODO: label? +// Jump is "break" or "continue", with an optional label, "break:label": +// - ID0: +// - ID1: <0|label> type Jump Node func (n *Jump) Node() *Node { return (*Node)(n) } func (n *Jump) Keyword() t.ID { return n.id0 } +func (n *Jump) Label() t.ID { return n.id1 } -func NewJump(keyword t.ID) *Jump { +func NewJump(keyword t.ID, label t.ID) *Jump { return &Jump{ kind: KJump, id0: keyword, + id1: label, } } diff --git a/lang/check/check_test.go b/lang/check/check_test.go index 8b52c029..7c526d0f 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -18,9 +18,9 @@ import ( "github.com/google/puffs/lang/token" ) -func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, src string) error { +func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, comments []string, src string) error { buf := &bytes.Buffer{} - if err := render.Render(buf, idMap, tokens, nil); err != nil { + if err := render.Render(buf, idMap, tokens, comments); err != nil { return err } got := strings.Split(buf.String(), "\n") @@ -70,18 +70,20 @@ func TestCheck(t *testing.T) { assert true - while p == q, + while:label p == q, pre true, inv true, post p != q, { + // Redundant, but shows the labeled jump syntax. + continue:label } } `) + "\n" idMap := &token.IDMap{} - tokens, _, err := token.Tokenize(idMap, filename, []byte(src)) + tokens, comments, err := token.Tokenize(idMap, filename, []byte(src)) if err != nil { t.Fatalf("Tokenize: %v", err) } @@ -91,7 +93,7 @@ func TestCheck(t *testing.T) { t.Fatalf("Parse: %v", err) } - if err := compareToPuffsfmt(idMap, tokens, src); err != nil { + if err := compareToPuffsfmt(idMap, tokens, comments, src); err != nil { t.Fatalf("compareToPuffsfmt: %v", err) } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index b551faa9..a9f99c02 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -461,6 +461,14 @@ func (p *parser) parseStatement() (*a.Node, error) { return n, err } +func (p *parser) parseLabel() (t.ID, error) { + if p.peek1().Key() == t.KeyColon { + p.src = p.src[1:] + return p.parseIdent() + } + return 0, nil +} + func (p *parser) parseStatement1() (*a.Node, error) { switch x := p.peek1(); x.Key() { case t.KeyAssert, t.KeyPre, t.KeyPost: @@ -468,10 +476,18 @@ func (p *parser) parseStatement1() (*a.Node, error) { case t.KeyBreak, t.KeyContinue: p.src = p.src[1:] - return a.NewJump(x).Node(), nil + label, err := p.parseLabel() + if err != nil { + return nil, err + } + return a.NewJump(x, label).Node(), nil case t.KeyWhile: p.src = p.src[1:] + label, err := p.parseLabel() + if err != nil { + return nil, err + } condition, err := p.parseExpr() if err != nil { return nil, err @@ -491,7 +507,7 @@ func (p *parser) parseStatement1() (*a.Node, error) { if err != nil { return nil, err } - return a.NewWhile(condition, asserts, body).Node(), nil + return a.NewWhile(label, condition, asserts, body).Node(), nil case t.KeyIf: o, err := p.parseIf()