From d4811441189e2422568486c00a153c4dc469415d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 5 May 2017 10:48:36 +1000 Subject: [PATCH] Parse "a[i:j]". --- lang/ast/ast.go | 15 ++++++++----- lang/parse/parse.go | 51 ++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index e4e2c3f4..f75935f6 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -19,6 +19,7 @@ const ( // - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDDot> // - ID1: <0|identifier name|literal> // - LHS: + // - MHS: // - RHS: // - List0: function call arguments // - FlagsSuspendible is "f(x)" vs "f?(x)" @@ -31,11 +32,13 @@ const ( // operands. The LHS may be null for the "-" and "+" operators, which are // also unary operators. // - // For function calls, like "lhs(list0)", ID0 is IDOpenParen. + // For function calls, like "LHS(List0)", ID0 is IDOpenParen. // - // For indexes, like "lhs[rhs]", ID0 is IDOpenBracket. + // For indexes, like "LHS[RHS]", ID0 is IDOpenBracket. // - // For selectors, like "lhs.id1", ID0 is IDDot. + // For slices, like "LHS[MHS:RHS]", ID0 is IDColon. + // + // For selectors, like "LHS.ID1", ID0 is IDDot. KExpr // KAssert is "assert RHS": @@ -81,6 +84,7 @@ const ( // - ID0: <0|package name|IDPtr> // - ID1: <0|type name> // - LHS: + // - MHS: // - RHS: // // An IDPtr ID0 means "ptr RHS". RHS is a KType. @@ -89,8 +93,8 @@ const ( // // Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" // or "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be - // refined as "pkg.foo[LHS:RHS]". LHS and RHS are KExpr's, possibly nil. - // For example, the LHS for "u32[:4096]" is nil. + // refined as "pkg.foo[MHS:RHS]". MHS and RHS are KExpr's, possibly nil. + // For example, the MHS for "u32[:4096]" is nil. KType // KFunc is "func ID0.ID1(List0) (List1) { List2 }": @@ -142,6 +146,7 @@ type Node struct { ID0 token.ID ID1 token.ID LHS *Node // Left Hand Side. + MHS *Node // Middle Hand Side. RHS *Node // Right Hand Side. List0 []*Node List1 []*Node diff --git a/lang/parse/parse.go b/lang/parse/parse.go index b53fe527..c76ceb9a 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -207,9 +207,9 @@ func (p *parser) parseType() (*a.Node, error) { return nil, err } - lhs, rhs := (*a.Node)(nil), (*a.Node)(nil) + mhs, rhs := (*a.Node)(nil), (*a.Node)(nil) if x := p.peekID(); x == t.IDOpenBracket { - lhs, rhs, err = p.parseRange() + _, mhs, rhs, err = p.parseRangeOrIndex(false) if err != nil { return nil, err } @@ -219,45 +219,59 @@ func (p *parser) parseType() (*a.Node, error) { Kind: a.KType, ID0: id0, ID1: id1, - LHS: lhs, + MHS: mhs, RHS: rhs, }, nil } -func (p *parser) parseRange() (lhs *a.Node, rhs *a.Node, err error) { +// parseRangeOrIndex parses "[i:j]", "[i:]", "[:j]" and "[:]". If allowIndex, +// it also parses "[x]". The returned op is t.IDColon for a range and +// t.IDOpenBracket for an index. +func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, mhs *a.Node, rhs *a.Node, err error) { if x := p.peekID(); x != t.IDOpenBracket { got := p.m.ByKey(x.Key()) - return nil, nil, fmt.Errorf("parse: expected \"[\", got %q for range at %s:%d", got, p.filename, p.line()) + return 0, nil, nil, fmt.Errorf("parse: expected \"[\", got %q at %s:%d", got, p.filename, p.line()) } p.src = p.src[1:] if p.peekID() != t.IDColon { - lhs, err = p.parseExpr() + mhs, err = p.parseExpr() if err != nil { - return nil, nil, err + return 0, nil, nil, err } } - if x := p.peekID(); x != t.IDColon { + switch x := p.peekID(); { + case x == t.IDColon: + p.src = p.src[1:] + + case x == t.IDCloseBracket && allowIndex: + p.src = p.src[1:] + return t.IDOpenBracket, nil, mhs, nil + + default: + expected := `":"` + if allowIndex { + expected = `":" or "]"` + } got := p.m.ByKey(x.Key()) - return nil, nil, fmt.Errorf("parse: expected \":\", got %q for range at %s:%d", got, p.filename, p.line()) + return 0, nil, nil, fmt.Errorf("parse: expected %s, got %q at %s:%d", expected, got, p.filename, p.line()) } - p.src = p.src[1:] if p.peekID() != t.IDCloseBracket { rhs, err = p.parseExpr() if err != nil { - return nil, nil, err + return 0, nil, nil, err } } if x := p.peekID(); x != t.IDCloseBracket { got := p.m.ByKey(x.Key()) - return nil, nil, fmt.Errorf("parse: expected \"]\", got %q for range at %s:%d", got, p.filename, p.line()) + return 0, nil, nil, fmt.Errorf("parse: expected \"]\", got %q at %s:%d", got, p.filename, p.line()) } p.src = p.src[1:] - return lhs, rhs, nil + return t.IDColon, mhs, rhs, nil } func (p *parser) parseBlock() ([]*a.Node, error) { @@ -502,20 +516,15 @@ func (p *parser) parseOperand() (*a.Node, error) { } case t.IDOpenBracket: - p.src = p.src[1:] - rhs, err := p.parseExpr() + id0, mhs, rhs, err := p.parseRangeOrIndex(true) if err != nil { return nil, err } - if x := p.peekID(); x != t.IDCloseBracket { - got := p.m.ByKey(x.Key()) - return nil, fmt.Errorf("parse: expected \"]\", got %q at %s:%d", got, p.filename, p.line()) - } - p.src = p.src[1:] lhs = &a.Node{ Kind: a.KExpr, - ID0: t.IDOpenBracket, + ID0: id0, LHS: lhs, + MHS: mhs, RHS: rhs, }