Hoist some methods from token.ID to token.Token.

This commit is contained in:
Nigel Tao committed 2017-04-30 13:42:48 +10:00
1 parent a48bbf6d2e
commit 5c934e5599
3 files changed
+19 -15

No files matched your search

+2 -2
View File
@@ -124,8 +124,8 @@ func (p *parser) parseIdent() (t.ID, error) {
return 0, fmt.Errorf("parse: expected identifier at %s:%d", p.filename, p.line()) return 0, fmt.Errorf("parse: expected identifier at %s:%d", p.filename, p.line())
} }
x := p.src[0] x := p.src[0]
if !x.ID.IsIdent() { if !x.IsIdent() {
name := p.m.ByKey(x.ID.Key()) name := p.m.ByKey(x.Key())
return 0, fmt.Errorf("parse: expected identifier, got %q at %s:%d", name, p.filename, p.line()) return 0, fmt.Errorf("parse: expected identifier, got %q at %s:%d", name, p.filename, p.line())
} }
p.src = p.src[1:] p.src = p.src[1:]
+16 -7
View File
@@ -39,13 +39,22 @@ type ID uint32
func (x ID) Key() Key { return Key(x >> idShift) } func (x ID) Key() Key { return Key(x >> idShift) }
func (x ID) Flags() Flags { return Flags(x & idMask) } func (x ID) Flags() Flags { return Flags(x & idMask) }
func (x ID) IsUnaryOp() bool { return Flags(x)&FlagsUnaryOp != 0 } // Token combines an ID and the line number it was seen.
func (x ID) IsBinaryOp() bool { return Flags(x)&FlagsBinaryOp != 0 } type Token struct {
func (x ID) IsAssociative() bool { return Flags(x)&FlagsAssociative != 0 } ID ID
func (x ID) IsAssign() bool { return Flags(x)&FlagsAssign != 0 } Line uint32
func (x ID) IsLiteral() bool { return Flags(x)&FlagsLiteral != 0 } }
func (x ID) IsIdent() bool { return Flags(x)&FlagsIdent != 0 }
func (x ID) IsImplicitSemicolon() bool { return Flags(x)&FlagsImplicitSemicolon != 0 } func (t Token) Key() Key { return Key(t.ID >> idShift) }
func (t Token) Flags() Flags { return Flags(t.ID & idMask) }
func (t Token) IsUnaryOp() bool { return Flags(t.ID)&FlagsUnaryOp != 0 }
func (t Token) IsBinaryOp() bool { return Flags(t.ID)&FlagsBinaryOp != 0 }
func (t Token) IsAssociative() bool { return Flags(t.ID)&FlagsAssociative != 0 }
func (t Token) IsAssign() bool { return Flags(t.ID)&FlagsAssign != 0 }
func (t Token) IsLiteral() bool { return Flags(t.ID)&FlagsLiteral != 0 }
func (t Token) IsIdent() bool { return Flags(t.ID)&FlagsIdent != 0 }
func (t Token) IsImplicitSemicolon() bool { return Flags(t.ID)&FlagsImplicitSemicolon != 0 }
// nBuiltInKeys is the number of built-in Keys. The packing is: // nBuiltInKeys is the number of built-in Keys. The packing is:
// - Zero is invalid. // - Zero is invalid.
+1 -6
View File
@@ -61,11 +61,6 @@ func (m *IDMap) ByKey(k Key) string {
return "" return ""
} }
type Token struct {
ID ID
Line uint32
}
func alpha(c byte) bool { func alpha(c byte) bool {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || (c == '_') return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || (c == '_')
} }
@@ -102,7 +97,7 @@ loop:
if c <= ' ' { if c <= ' ' {
if c == '\n' { if c == '\n' {
if len(tokens) > 0 && tokens[len(tokens)-1].ID.IsImplicitSemicolon() { if len(tokens) > 0 && tokens[len(tokens)-1].IsImplicitSemicolon() {
tokens = append(tokens, Token{IDSemicolon, line}) tokens = append(tokens, Token{IDSemicolon, line})
} }
if line == 1<<32-1 { if line == 1<<32-1 {