mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 09:01:35 +01:00
Add a packageid keyword.
This commit is contained in:
1 parent
6d5e2f0193
commit
17b518376f
8 files changed
+97
-8
No files matched your search
@@ -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`
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
+22
-1
@@ -30,6 +30,7 @@ const (
|
|||||||
KFunc
|
KFunc
|
||||||
KIf
|
KIf
|
||||||
KJump
|
KJump
|
||||||
|
KPackageID
|
||||||
KReturn
|
KReturn
|
||||||
KStatus
|
KStatus
|
||||||
KStruct
|
KStruct
|
||||||
@@ -58,6 +59,7 @@ var kindStrings = [...]string{
|
|||||||
KFunc: "KFunc",
|
KFunc: "KFunc",
|
||||||
KIf: "KIf",
|
KIf: "KIf",
|
||||||
KJump: "KJump",
|
KJump: "KJump",
|
||||||
|
KPackageID: "KPackageID",
|
||||||
KReturn: "KReturn",
|
KReturn: "KReturn",
|
||||||
KStatus: "KStatus",
|
KStatus: "KStatus",
|
||||||
KStruct: "KStruct",
|
KStruct: "KStruct",
|
||||||
@@ -113,6 +115,7 @@ 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) PackageID() *PackageID { return (*PackageID)(n) }
|
||||||
func (n *Node) Raw() *Raw { return (*Raw)(n) }
|
func (n *Node) Raw() *Raw { return (*Raw)(n) }
|
||||||
func (n *Node) Return() *Return { return (*Return)(n) }
|
func (n *Node) Return() *Return { return (*Return)(n) }
|
||||||
func (n *Node) Status() *Status { return (*Status)(n) }
|
func (n *Node) Status() *Status { return (*Status)(n) }
|
||||||
@@ -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) }
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
+16
-2
@@ -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:]
|
||||||
|
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
|
return a.NewUse(p.filename, line, path).Node(), nil
|
||||||
|
}
|
||||||
|
|
||||||
case t.KeyPub:
|
case t.KeyPub:
|
||||||
flags |= a.FlagsPublic
|
flags |= a.FlagsPublic
|
||||||
|
|||||||
@@ -210,6 +210,7 @@ const (
|
|||||||
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)
|
||||||
@@ -368,6 +369,7 @@ const (
|
|||||||
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)
|
||||||
@@ -546,6 +548,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
|||||||
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},
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
Reference in new issue
Block a user