Add a packageid keyword.

This commit is contained in:
Nigel Tao committed 2017-07-06 11:17:15 +10:00
1 parent 6d5e2f0193
commit 17b518376f
8 files changed
+197 -108

No files matched your search

+2 -1
View File
@@ -288,11 +288,12 @@ be enclosed by curly `{}`s. There is no 'dangling else' ambiguity.
## Keywords ## Keywords
Puffs is a simple language, with only 22 keywords. 5 of these introduce Puffs is a simple language, with only 23 keywords. 6 of these introduce
top-level concepts: top-level concepts:
- `error` - `error`
- `func` - `func`
- `packageid`
- `status` - `status`
- `struct` - `struct`
- `use` - `use`
+4 -4
View File
@@ -370,11 +370,11 @@ func (g *gen) cName(name string) string {
} }
func (g *gen) gatherStatuses(n *a.Status) error { func (g *gen) gatherStatuses(n *a.Status) error {
msg := n.Message().String(g.tm) raw := n.Message().String(g.tm)
if len(msg) < 2 || msg[0] != '"' || msg[len(msg)-1] != '"' { msg, ok := t.Unescape(raw)
return fmt.Errorf("bad status message %q", msg) if !ok {
return fmt.Errorf("bad status message %q", raw)
} }
msg = msg[1 : len(msg)-1]
prefix := "status " prefix := "status "
isError := n.Keyword().Key() == t.KeyError isError := n.Keyword().Key() == t.KeyError
if isError { if isError {
+55 -34
View File
@@ -30,6 +30,7 @@ const (
KFunc KFunc
KIf KIf
KJump KJump
KPackageID
KReturn KReturn
KStatus KStatus
KStruct KStruct
@@ -49,22 +50,23 @@ func (k Kind) String() string {
var kindStrings = [...]string{ var kindStrings = [...]string{
KInvalid: "KInvalid", KInvalid: "KInvalid",
KArg: "KArg", KArg: "KArg",
KAssert: "KAssert", KAssert: "KAssert",
KAssign: "KAssign", KAssign: "KAssign",
KExpr: "KExpr", KExpr: "KExpr",
KField: "KField", KField: "KField",
KFile: "KFile", KFile: "KFile",
KFunc: "KFunc", KFunc: "KFunc",
KIf: "KIf", KIf: "KIf",
KJump: "KJump", KJump: "KJump",
KReturn: "KReturn", KPackageID: "KPackageID",
KStatus: "KStatus", KReturn: "KReturn",
KStruct: "KStruct", KStatus: "KStatus",
KTypeExpr: "KTypeExpr", KStruct: "KStruct",
KUse: "KUse", KTypeExpr: "KTypeExpr",
KVar: "KVar", KUse: "KUse",
KWhile: "KWhile", KVar: "KVar",
KWhile: "KWhile",
} }
type Flags uint32 type Flags uint32
@@ -104,23 +106,24 @@ func (n *Node) Kind() Kind { return n.kind }
func (n *Node) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 } func (n *Node) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 }
func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked } func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked }
func (n *Node) Arg() *Arg { return (*Arg)(n) } func (n *Node) Arg() *Arg { return (*Arg)(n) }
func (n *Node) Assert() *Assert { return (*Assert)(n) } func (n *Node) Assert() *Assert { return (*Assert)(n) }
func (n *Node) Assign() *Assign { return (*Assign)(n) } func (n *Node) Assign() *Assign { return (*Assign)(n) }
func (n *Node) Expr() *Expr { return (*Expr)(n) } func (n *Node) Expr() *Expr { return (*Expr)(n) }
func (n *Node) Field() *Field { return (*Field)(n) } func (n *Node) Field() *Field { return (*Field)(n) }
func (n *Node) File() *File { return (*File)(n) } func (n *Node) File() *File { return (*File)(n) }
func (n *Node) Func() *Func { return (*Func)(n) } func (n *Node) Func() *Func { return (*Func)(n) }
func (n *Node) If() *If { return (*If)(n) } func (n *Node) If() *If { return (*If)(n) }
func (n *Node) Jump() *Jump { return (*Jump)(n) } func (n *Node) Jump() *Jump { return (*Jump)(n) }
func (n *Node) Raw() *Raw { return (*Raw)(n) } func (n *Node) PackageID() *PackageID { return (*PackageID)(n) }
func (n *Node) Return() *Return { return (*Return)(n) } func (n *Node) Raw() *Raw { return (*Raw)(n) }
func (n *Node) Status() *Status { return (*Status)(n) } func (n *Node) Return() *Return { return (*Return)(n) }
func (n *Node) Struct() *Struct { return (*Struct)(n) } func (n *Node) Status() *Status { return (*Status)(n) }
func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) } func (n *Node) Struct() *Struct { return (*Struct)(n) }
func (n *Node) Use() *Use { return (*Use)(n) } func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) }
func (n *Node) Var() *Var { return (*Var)(n) } func (n *Node) Use() *Use { return (*Use)(n) }
func (n *Node) While() *While { return (*While)(n) } func (n *Node) Var() *Var { return (*Var)(n) }
func (n *Node) While() *While { return (*While)(n) }
func (n *Node) Walk(f func(*Node) error) error { func (n *Node) Walk(f func(*Node) error) error {
if n != nil { if n != nil {
@@ -624,6 +627,24 @@ func NewStruct(flags Flags, filename string, line uint32, name t.ID, fields []*N
} }
} }
// PackageID is "packageid ID1":
// - ID1: <string literal> package ID
type PackageID Node
func (n *PackageID) Node() *Node { return (*Node)(n) }
func (n *PackageID) Filename() string { return n.filename }
func (n *PackageID) Line() uint32 { return n.line }
func (n *PackageID) ID() t.ID { return n.id1 }
func NewPackageID(filename string, line uint32, id t.ID) *PackageID {
return &PackageID{
kind: KPackageID,
filename: filename,
line: line,
id1: id,
}
}
// Use is "use ID1": // Use is "use ID1":
// - ID1: <string literal> package path // - ID1: <string literal> package path
type Use Node type Use Node
@@ -643,7 +664,7 @@ func NewUse(filename string, line uint32, path t.ID) *Use {
} }
// File is a file of source code: // File is a file of source code:
// - List0: <Func|Status|Struct|Use> top-level declarations // - List0: <Func|PackageID|Status|Struct|Use> top-level declarations
type File Node type File Node
func (n *File) Node() *Node { return (*Node)(n) } func (n *File) Node() *Node { return (*Node)(n) }
+41
View File
@@ -7,6 +7,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/google/puffs/lang/base38"
a "github.com/google/puffs/lang/ast" a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token" t "github.com/google/puffs/lang/token"
) )
@@ -104,6 +106,7 @@ func Check(tm *t.Map, files ...*a.File) (*Checker, error) {
c := &Checker{ c := &Checker{
tm: tm, tm: tm,
reasonMap: rMap, reasonMap: rMap,
packageID: base38.Max + 1,
funcs: map[t.QID]Func{}, funcs: map[t.QID]Func{},
statuses: map[t.ID]Status{}, statuses: map[t.ID]Status{},
structs: map[t.ID]Struct{}, structs: map[t.ID]Struct{},
@@ -135,6 +138,7 @@ var phases = [...]struct {
kind a.Kind kind a.Kind
check func(*Checker, *a.Node) error check func(*Checker, *a.Node) error
}{ }{
{a.KPackageID, (*Checker).checkPackageID},
{a.KUse, (*Checker).checkUse}, {a.KUse, (*Checker).checkUse},
{a.KStatus, (*Checker).checkStatus}, {a.KStatus, (*Checker).checkStatus},
{a.KStruct, (*Checker).checkStructDecl}, {a.KStruct, (*Checker).checkStructDecl},
@@ -150,6 +154,9 @@ type Checker struct {
tm *t.Map tm *t.Map
reasonMap reasonMap reasonMap reasonMap
packageID uint32
otherPackageID *a.PackageID
funcs map[t.QID]Func funcs map[t.QID]Func
statuses map[t.ID]Status statuses map[t.ID]Status
structs map[t.ID]Struct structs map[t.ID]Struct
@@ -157,10 +164,44 @@ type Checker struct {
unsortedStructs []*a.Struct unsortedStructs []*a.Struct
} }
func (c *Checker) PackageID() uint32 { return c.packageID }
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
func (c *Checker) Statuses() map[t.ID]Status { return c.statuses } func (c *Checker) Statuses() map[t.ID]Status { return c.statuses }
func (c *Checker) Structs() map[t.ID]Struct { return c.structs } func (c *Checker) Structs() map[t.ID]Struct { return c.structs }
func (c *Checker) checkPackageID(node *a.Node) error {
n := node.PackageID()
if c.otherPackageID != nil {
return &Error{
Err: fmt.Errorf("check: multiple packageid declarations"),
Filename: n.Filename(),
Line: n.Line(),
OtherFilename: c.otherPackageID.Filename(),
OtherLine: c.otherPackageID.Line(),
}
}
raw := n.ID().String(c.tm)
s, ok := t.Unescape(raw)
if !ok {
return &Error{
Err: fmt.Errorf("check: %q is not a valid packageid", raw),
Filename: n.Filename(),
Line: n.Line(),
}
}
u, ok := base38.Encode(s)
if !ok || u == 0 {
return &Error{
Err: fmt.Errorf("check: %q is not a valid packageid", s),
Filename: n.Filename(),
Line: n.Line(),
}
}
c.packageID = u
c.otherPackageID = n
return nil
}
func (c *Checker) checkUse(node *a.Node) error { func (c *Checker) checkUse(node *a.Node) error {
// TODO. // TODO.
return nil return nil
+17 -3
View File
@@ -8,6 +8,8 @@ package parse
import ( import (
"fmt" "fmt"
"github.com/google/puffs/lang/base38"
a "github.com/google/puffs/lang/ast" a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token" t "github.com/google/puffs/lang/token"
) )
@@ -72,8 +74,8 @@ func (p *parser) parseFile() (*a.File, error) {
func (p *parser) parseTopLevelDecl() (*a.Node, error) { func (p *parser) parseTopLevelDecl() (*a.Node, error) {
flags := a.Flags(0) flags := a.Flags(0)
line := p.src[0].Line line := p.src[0].Line
switch p.peek1().Key() { switch k := p.peek1().Key(); k {
case t.KeyUse: case t.KeyPackageID, t.KeyUse:
p.src = p.src[1:] p.src = p.src[1:]
path := p.peek1() path := p.peek1()
if !path.IsStrLiteral() { if !path.IsStrLiteral() {
@@ -86,7 +88,19 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line()) return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
} }
p.src = p.src[1:] p.src = p.src[1:]
return a.NewUse(p.filename, line, path).Node(), nil if k == t.KeyPackageID {
raw := path.String(p.tm)
s, ok := t.Unescape(raw)
if !ok {
return nil, fmt.Errorf(`parse: %q is not a valid packageid`, raw)
}
if u, ok := base38.Encode(s); !ok || u == 0 {
return nil, fmt.Errorf(`parse: %q is not a valid packageid`, s)
}
return a.NewPackageID(p.filename, line, path).Node(), nil
} else {
return a.NewUse(p.filename, line, path).Node(), nil
}
case t.KeyPub: case t.KeyPub:
flags |= a.FlagsPublic flags |= a.FlagsPublic
+69 -66
View File
@@ -188,28 +188,29 @@ const (
KeyAs = Key(IDAs >> KeyShift) KeyAs = Key(IDAs >> KeyShift)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
KeyFunc = Key(IDFunc >> KeyShift) KeyFunc = Key(IDFunc >> KeyShift)
KeyPtr = Key(IDPtr >> KeyShift) KeyPtr = Key(IDPtr >> KeyShift)
KeyAssert = Key(IDAssert >> KeyShift) KeyAssert = Key(IDAssert >> KeyShift)
KeyWhile = Key(IDWhile >> KeyShift) KeyWhile = Key(IDWhile >> KeyShift)
KeyIf = Key(IDIf >> KeyShift) KeyIf = Key(IDIf >> KeyShift)
KeyElse = Key(IDElse >> KeyShift) KeyElse = Key(IDElse >> KeyShift)
KeyReturn = Key(IDReturn >> KeyShift) KeyReturn = Key(IDReturn >> KeyShift)
KeyBreak = Key(IDBreak >> KeyShift) KeyBreak = Key(IDBreak >> KeyShift)
KeyContinue = Key(IDContinue >> KeyShift) KeyContinue = Key(IDContinue >> KeyShift)
KeyStruct = Key(IDStruct >> KeyShift) KeyStruct = Key(IDStruct >> KeyShift)
KeyUse = Key(IDUse >> KeyShift) KeyUse = Key(IDUse >> KeyShift)
KeyVar = Key(IDVar >> KeyShift) KeyVar = Key(IDVar >> KeyShift)
KeyNptr = Key(IDNptr >> KeyShift) KeyNptr = Key(IDNptr >> KeyShift)
KeyPre = Key(IDPre >> KeyShift) KeyPre = Key(IDPre >> KeyShift)
KeyInv = Key(IDInv >> KeyShift) KeyInv = Key(IDInv >> KeyShift)
KeyPost = Key(IDPost >> KeyShift) KeyPost = Key(IDPost >> KeyShift)
KeyVia = Key(IDVia >> KeyShift) KeyVia = Key(IDVia >> KeyShift)
KeyPub = Key(IDPub >> KeyShift) KeyPub = Key(IDPub >> KeyShift)
KeyPri = Key(IDPri >> KeyShift) KeyPri = Key(IDPri >> KeyShift)
KeyError = Key(IDError >> KeyShift) KeyError = Key(IDError >> KeyShift)
KeyStatus = Key(IDStatus >> KeyShift) KeyStatus = Key(IDStatus >> KeyShift)
KeyLimit = Key(IDLimit >> KeyShift) KeyLimit = Key(IDLimit >> KeyShift)
KeyPackageID = Key(IDPackageID >> KeyShift)
KeyFalse = Key(IDFalse >> KeyShift) KeyFalse = Key(IDFalse >> KeyShift)
KeyTrue = Key(IDTrue >> KeyShift) KeyTrue = Key(IDTrue >> KeyShift)
@@ -346,28 +347,29 @@ const (
IDAs = ID(0x5B<<KeyShift | FlagsBinaryOp) IDAs = ID(0x5B<<KeyShift | FlagsBinaryOp)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
IDFunc = ID(0x60<<KeyShift | FlagsOther) IDFunc = ID(0x60<<KeyShift | FlagsOther)
IDPtr = ID(0x61<<KeyShift | FlagsOther) IDPtr = ID(0x61<<KeyShift | FlagsOther)
IDAssert = ID(0x62<<KeyShift | FlagsOther) IDAssert = ID(0x62<<KeyShift | FlagsOther)
IDWhile = ID(0x63<<KeyShift | FlagsOther) IDWhile = ID(0x63<<KeyShift | FlagsOther)
IDIf = ID(0x64<<KeyShift | FlagsOther) IDIf = ID(0x64<<KeyShift | FlagsOther)
IDElse = ID(0x65<<KeyShift | FlagsOther) IDElse = ID(0x65<<KeyShift | FlagsOther)
IDReturn = ID(0x66<<KeyShift | FlagsOther | FlagsImplicitSemicolon) IDReturn = ID(0x66<<KeyShift | FlagsOther | FlagsImplicitSemicolon)
IDBreak = ID(0x67<<KeyShift | FlagsOther | FlagsImplicitSemicolon) IDBreak = ID(0x67<<KeyShift | FlagsOther | FlagsImplicitSemicolon)
IDContinue = ID(0x68<<KeyShift | FlagsOther | FlagsImplicitSemicolon) IDContinue = ID(0x68<<KeyShift | FlagsOther | FlagsImplicitSemicolon)
IDStruct = ID(0x69<<KeyShift | FlagsOther) IDStruct = ID(0x69<<KeyShift | FlagsOther)
IDUse = ID(0x6A<<KeyShift | FlagsOther) IDUse = ID(0x6A<<KeyShift | FlagsOther)
IDVar = ID(0x6B<<KeyShift | FlagsOther) IDVar = ID(0x6B<<KeyShift | FlagsOther)
IDNptr = ID(0x6C<<KeyShift | FlagsOther) IDNptr = ID(0x6C<<KeyShift | FlagsOther)
IDPre = ID(0x6D<<KeyShift | FlagsOther) IDPre = ID(0x6D<<KeyShift | FlagsOther)
IDInv = ID(0x6E<<KeyShift | FlagsOther) IDInv = ID(0x6E<<KeyShift | FlagsOther)
IDPost = ID(0x6F<<KeyShift | FlagsOther) IDPost = ID(0x6F<<KeyShift | FlagsOther)
IDVia = ID(0x70<<KeyShift | FlagsOther) IDVia = ID(0x70<<KeyShift | FlagsOther)
IDPub = ID(0x71<<KeyShift | FlagsOther) IDPub = ID(0x71<<KeyShift | FlagsOther)
IDPri = ID(0x72<<KeyShift | FlagsOther) IDPri = ID(0x72<<KeyShift | FlagsOther)
IDError = ID(0x73<<KeyShift | FlagsOther) IDError = ID(0x73<<KeyShift | FlagsOther)
IDStatus = ID(0x74<<KeyShift | FlagsOther) IDStatus = ID(0x74<<KeyShift | FlagsOther)
IDLimit = ID(0x75<<KeyShift | FlagsOther) IDLimit = ID(0x75<<KeyShift | FlagsOther)
IDPackageID = ID(0x76<<KeyShift | FlagsOther)
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon) IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon) IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -524,28 +526,29 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyAs: {"as", IDAs}, KeyAs: {"as", IDAs},
// TODO: do we need a "~as" form of the as operator, similar to "~+". // TODO: do we need a "~as" form of the as operator, similar to "~+".
KeyFunc: {"func", IDFunc}, KeyFunc: {"func", IDFunc},
KeyPtr: {"ptr", IDPtr}, KeyPtr: {"ptr", IDPtr},
KeyAssert: {"assert", IDAssert}, KeyAssert: {"assert", IDAssert},
KeyWhile: {"while", IDWhile}, KeyWhile: {"while", IDWhile},
KeyIf: {"if", IDIf}, KeyIf: {"if", IDIf},
KeyElse: {"else", IDElse}, KeyElse: {"else", IDElse},
KeyReturn: {"return", IDReturn}, KeyReturn: {"return", IDReturn},
KeyBreak: {"break", IDBreak}, KeyBreak: {"break", IDBreak},
KeyContinue: {"continue", IDContinue}, KeyContinue: {"continue", IDContinue},
KeyStruct: {"struct", IDStruct}, KeyStruct: {"struct", IDStruct},
KeyUse: {"use", IDUse}, KeyUse: {"use", IDUse},
KeyVar: {"var", IDVar}, KeyVar: {"var", IDVar},
KeyNptr: {"nptr", IDNptr}, KeyNptr: {"nptr", IDNptr},
KeyPre: {"pre", IDPre}, KeyPre: {"pre", IDPre},
KeyInv: {"inv", IDInv}, KeyInv: {"inv", IDInv},
KeyPost: {"post", IDPost}, KeyPost: {"post", IDPost},
KeyVia: {"via", IDVia}, KeyVia: {"via", IDVia},
KeyPub: {"pub", IDPub}, KeyPub: {"pub", IDPub},
KeyPri: {"pri", IDPri}, KeyPri: {"pri", IDPri},
KeyError: {"error", IDError}, KeyError: {"error", IDError},
KeyStatus: {"status", IDStatus}, KeyStatus: {"status", IDStatus},
KeyLimit: {"limit", IDLimit}, KeyLimit: {"limit", IDLimit},
KeyPackageID: {"packageid", IDPackageID},
KeyFalse: {"false", IDFalse}, KeyFalse: {"false", IDFalse},
KeyTrue: {"true", IDTrue}, KeyTrue: {"true", IDTrue},
+7
View File
@@ -13,6 +13,13 @@ const (
maxTokenSize = 1023 maxTokenSize = 1023
) )
func Unescape(s string) (unescaped string, ok bool) {
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
return "", false
}
return s[1 : len(s)-1], true
}
type Map struct { type Map struct {
byName map[string]ID byName map[string]ID
byKey []string byKey []string
+2
View File
@@ -3,6 +3,8 @@
// The GIF spec is https://www.w3.org/Graphics/GIF/spec-gif89a.txt // The GIF spec is https://www.w3.org/Graphics/GIF/spec-gif89a.txt
packageid "gif "
pub error "bad GIF block" pub error "bad GIF block"
pub error "bad GIF extension label" pub error "bad GIF extension label"
pub error "bad GIF header" pub error "bad GIF header"