Parse the "!" in "foo!()", meaning impure.

This commit is contained in:
Nigel Tao committed 2017-05-27 10:38:19 +10:00
1 parent 0819a42ab8
commit 849f885993
3 files changed
+28 -11

No files matched your search

+9 -4
View File
@@ -68,8 +68,9 @@ var kindStrings = [...]string{
type Flags uint32
const (
FlagsSuspendible = Flags(0x00000001)
FlagsTypeChecked = Flags(0x00000002)
FlagsImpure = Flags(0x00000001)
FlagsSuspendible = Flags(0x00000002)
FlagsTypeChecked = Flags(0x00000004)
)
type Node struct {
@@ -160,7 +161,8 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
const MaxExprDepth = 255
// Expr is an expression, such as "i", "+j" or "k + l[m(n, o)].p":
// - FlagsSuspendible is "f(x)" vs "f?(x)"
// - FlagsImpure is "f(x)" vs "f!(x)"
// - FlagsSuspendible is "f(x)" vs "f?(x)", it implies FlagsImpure
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot>
// - ID1: <0|ident|literal>
// - LHS: <nil|Expr>
@@ -189,6 +191,7 @@ const MaxExprDepth = 255
type Expr Node
func (n *Expr) Node() *Node { return (*Node)(n) }
func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 }
func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
func (n *Expr) ConstValue() *big.Int { return n.constValue }
func (n *Expr) MType() *TypeExpr { return n.mType }
@@ -437,7 +440,8 @@ func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inne
}
// Func is "func ID0.ID1(LHS)(RHS) { List1 }":
// - FlagsSuspendible is "ID1" vs "ID1?"
// - FlagsImpure is "ID1" vs "ID1!"
// - FlagsSuspendible is "ID1" vs "ID1?", it implies FlagsImpure
// - ID0: <0|receiver>
// - ID1: name
// - LHS: <Struct> in-parameters
@@ -447,6 +451,7 @@ func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inne
type Func Node
func (n *Func) Node() *Node { return (*Node)(n) }
func (n *Func) Impure() bool { return n.flags&FlagsImpure != 0 }
func (n *Func) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
func (n *Func) Filename() string { return n.filename }
func (n *Func) Line() uint32 { return n.line }
+12 -4
View File
@@ -87,8 +87,12 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
return nil, fmt.Errorf(`parse: built-in %q used for func name at %s:%d`,
p.m.ByID(id1), p.filename, p.line())
}
if p.peek1().Key() == t.KeyQuestion {
flags |= a.FlagsSuspendible
switch p.peek1().Key() {
case t.KeyExclam:
flags |= a.FlagsImpure
p.src = p.src[1:]
case t.KeyQuestion:
flags |= a.FlagsImpure | a.FlagsSuspendible
p.src = p.src[1:]
}
inFields, err := p.parseList(t.KeyCloseParen, (*parser).parseFieldNode)
@@ -667,9 +671,13 @@ func (p *parser) parseOperand() (*a.Expr, error) {
default:
return lhs, nil
case t.KeyQuestion:
case t.KeyExclam, t.KeyQuestion:
if p.src[0].Key() == t.KeyExclam {
flags |= a.FlagsImpure
} else {
flags |= a.FlagsImpure | a.FlagsSuspendible
}
p.src = p.src[1:]
flags |= a.FlagsSuspendible
fallthrough
case t.KeyOpenParen:
+7 -3
View File
@@ -139,6 +139,7 @@ const (
KeyDot = Key(IDDot >> KeyShift)
KeyDotDot = Key(IDDotDot >> KeyShift)
KeyComma = Key(IDComma >> KeyShift)
KeyExclam = Key(IDExclam >> KeyShift)
KeyQuestion = Key(IDQuestion >> KeyShift)
KeyColon = Key(IDColon >> KeyShift)
KeySemicolon = Key(IDSemicolon >> KeyShift)
@@ -264,9 +265,10 @@ const (
IDDot = ID(0x20<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDDotDot = ID(0x21<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDComma = ID(0x22<<KeyShift | FlagsTightLeft)
IDQuestion = ID(0x23<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDColon = ID(0x24<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDSemicolon = ID(0x25<<KeyShift | FlagsTightLeft)
IDExclam = ID(0x23<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDQuestion = ID(0x24<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDColon = ID(0x25<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDSemicolon = ID(0x26<<KeyShift | FlagsTightLeft)
IDEq = ID(0x30<<KeyShift | FlagsAssign)
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
@@ -397,6 +399,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyDot: {".", IDDot},
KeyDotDot: {"..", IDDotDot},
KeyComma: {",", IDComma},
KeyExclam: {"!", IDExclam},
KeyQuestion: {"?", IDQuestion},
KeyColon: {":", IDColon},
KeySemicolon: {";", IDSemicolon},
@@ -517,6 +520,7 @@ var lexers = [256][]suffixLexer{
},
'!': {
{"=", IDNotEq},
{"", IDExclam},
},
'&': {
{"^=", IDAmpHatEq},