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