From 1717d5befd2edfc55da1e9a50b0c027c3b4eb828 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 15 Apr 2018 15:22:09 +1000 Subject: [PATCH] Remove token ID/Key distinction, part 8 of N --- lang/check/type.go | 4 ++-- lang/parse/parse.go | 14 +++++++------- lang/token/list.go | 12 ++++++------ lang/token/token.go | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lang/check/type.go b/lang/check/type.go index 34bfa71c..06cd2c25 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -375,7 +375,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { switch n.Operator().Key() { case 0: id1 := n.Ident() - if id1.IsNumLiteral() { + if id1.IsNumLiteral(q.tm) { z := big.NewInt(0) s := id1.Str(q.tm) if _, ok := z.SetString(s, 0); !ok { @@ -385,7 +385,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetMType(typeExprIdeal) return nil - } else if id1.IsIdent() { + } else if id1.IsIdent(q.tm) { if q.localVars != nil { if typ, ok := q.localVars[id1]; ok { n.SetMType(typ) diff --git a/lang/parse/parse.go b/lang/parse/parse.go index ce752998..16d720a2 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -105,7 +105,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { case t.KeyPackageID, t.KeyUse: p.src = p.src[1:] path := p.peek1() - if !path.IsStrLiteral() { + if !path.IsStrLiteral(p.tm) { got := p.tm.ByID(path) return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line()) } @@ -239,7 +239,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { keyword := p.src[0].ID p.src = p.src[1:] message := p.peek1() - if !message.IsStrLiteral() { + if !message.IsStrLiteral(p.tm) { got := p.tm.ByID(message) return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line()) } @@ -309,7 +309,7 @@ func (p *parser) parseIdent() (t.ID, error) { return 0, fmt.Errorf(`parse: expected identifier at %s:%d`, p.filename, p.line()) } x := p.src[0] - if !x.ID.IsIdent() { + if !x.ID.IsIdent(p.tm) { got := p.tm.ByToken(x) return 0, fmt.Errorf(`parse: expected identifier, got %q at %s:%d`, got, p.filename, p.line()) } @@ -561,7 +561,7 @@ func (p *parser) parseAssertNode() (*a.Node, error) { if p.peek1().Key() == t.KeyVia { p.src = p.src[1:] reason = p.peek1() - if !reason.IsStrLiteral() { + if !reason.IsStrLiteral(p.tm) { got := p.tm.ByID(reason) return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line()) } @@ -633,7 +633,7 @@ func (p *parser) parseStatement1() (*a.Node, error) { // TODO: allow expressions, not just literals, as unroll counts? unrollID := p.peek1() unrollStr := p.tm.ByKey(unrollID.Key()) - if !unrollID.IsLiteral() { + if !unrollID.IsLiteral(p.tm) { return nil, fmt.Errorf(`parse: expected literal unroll count, got %q at %s:%d`, unrollStr, p.filename, p.line()) } @@ -930,7 +930,7 @@ func (p *parser) parseOperand() (*a.Expr, error) { } return a.NewExpr(0, op, 0, 0, nil, nil, rhs.Node(), nil), nil - case x.IsLiteral(): + case x.IsLiteral(p.tm): p.src = p.src[1:] return a.NewExpr(0, 0, 0, x, nil, nil, nil, nil), nil @@ -955,7 +955,7 @@ func (p *parser) parseOperand() (*a.Expr, error) { message := p.peek1() // TODO: parse the "pkg" in `error pkg."foo"`. statusPkg := t.ID(0) - if !message.IsStrLiteral() { + if !message.IsStrLiteral(p.tm) { got := p.tm.ByID(message) return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line()) } diff --git a/lang/token/list.go b/lang/token/list.go index 0e0f49e7..c37447c3 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -73,10 +73,10 @@ func (x ID) IsAssociativeOp() bool { return minOpKey <= x.Key() && x.Key() <= maxOpKey && x.AssociativeForm() != 0 } -func (x ID) IsLiteral() bool { return Flags(x)&FlagsLiteral != 0 } -func (x ID) IsNumLiteral() bool { return Flags(x)&FlagsNumLiteral != 0 } -func (x ID) IsStrLiteral() bool { return Flags(x)&FlagsStrLiteral != 0 } -func (x ID) IsIdent() bool { return Flags(x)&FlagsIdent != 0 } +func (x ID) IsLiteral(m *Map) bool { return Flags(x)&FlagsLiteral != 0 } +func (x ID) IsNumLiteral(m *Map) bool { return Flags(x)&FlagsNumLiteral != 0 } +func (x ID) IsStrLiteral(m *Map) bool { return Flags(x)&FlagsStrLiteral != 0 } +func (x ID) IsIdent(m *Map) bool { return Flags(x)&FlagsIdent != 0 } func (x ID) IsOpen() bool { return x.Key() < Key(len(isOpen)) && isOpen[x.Key()] } func (x ID) IsClose() bool { return x.Key() < Key(len(isClose)) && isClose[x.Key()] } @@ -86,8 +86,8 @@ func (x ID) IsTightRight() bool { return x.Key() < Key(len(isTightRight)) && isT func (x ID) IsAssign() bool { return minAssignKey <= x.Key() && x.Key() <= maxAssignKey } func (x ID) IsNumType() bool { return minNumTypeKey <= x.Key() && x.Key() <= maxNumTypeKey } -func (x ID) IsImplicitSemicolon() bool { - return x.IsLiteral() || x.IsIdent() || +func (x ID) IsImplicitSemicolon(m *Map) bool { + return x.IsLiteral(m) || x.IsIdent(m) || (x.Key() < Key(len(isImplicitSemicolon)) && isImplicitSemicolon[x.Key()]) } diff --git a/lang/token/token.go b/lang/token/token.go index af0e5f6b..1c25613e 100644 --- a/lang/token/token.go +++ b/lang/token/token.go @@ -132,7 +132,7 @@ loop: if c <= ' ' { if c == '\n' { - if len(tokens) > 0 && tokens[len(tokens)-1].ID.IsImplicitSemicolon() { + if len(tokens) > 0 && tokens[len(tokens)-1].ID.IsImplicitSemicolon(m) { tokens = append(tokens, Token{IDSemicolon, line}) } if line == maxLine {