mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Consistently use a and t for ast and token
This commit is contained in:
1 parent
bdc47bd6e2
commit
f6159a1c95
5 files changed
+49
-45
No files matched your search
@@ -35,7 +35,8 @@ import (
|
||||
|
||||
"github.com/google/wuffs/lang/parse"
|
||||
"github.com/google/wuffs/lang/render"
|
||||
"github.com/google/wuffs/lang/token"
|
||||
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -118,8 +119,8 @@ func do(r io.Reader, filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
tm := &token.Map{}
|
||||
tokens, comments, err := token.Tokenize(tm, filename, src)
|
||||
tm := &t.Map{}
|
||||
tokens, comments, err := t.Tokenize(tm, filename, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/wuffs/lang/parse"
|
||||
"github.com/google/wuffs/lang/token"
|
||||
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
func TestString(tt *testing.T) {
|
||||
@@ -70,9 +71,9 @@ func TestString(tt *testing.T) {
|
||||
"x as [8 + (2 * N)] ptr [4] ptr pkg.T[i..j]",
|
||||
}
|
||||
|
||||
tm := &token.Map{}
|
||||
tm := &t.Map{}
|
||||
for _, tc := range testCases {
|
||||
tokens, _, err := token.Tokenize(tm, filename, []byte(tc))
|
||||
tokens, _, err := t.Tokenize(tm, filename, []byte(tc))
|
||||
if err != nil {
|
||||
tt.Errorf("Tokenize(%q): %v", tc, err)
|
||||
continue
|
||||
|
||||
+13
-12
@@ -23,14 +23,15 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/wuffs/lang/ast"
|
||||
"github.com/google/wuffs/lang/builtin"
|
||||
"github.com/google/wuffs/lang/parse"
|
||||
"github.com/google/wuffs/lang/render"
|
||||
"github.com/google/wuffs/lang/token"
|
||||
|
||||
a "github.com/google/wuffs/lang/ast"
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
func compareToWuffsfmt(tm *token.Map, tokens []token.Token, comments []string, src string) error {
|
||||
func compareToWuffsfmt(tm *t.Map, tokens []t.Token, comments []string, src string) error {
|
||||
buf := &bytes.Buffer{}
|
||||
if err := render.Render(buf, tm, tokens, comments); err != nil {
|
||||
return err
|
||||
@@ -95,9 +96,9 @@ func TestCheck(tt *testing.T) {
|
||||
}
|
||||
`) + "\n"
|
||||
|
||||
tm := &token.Map{}
|
||||
tm := &t.Map{}
|
||||
|
||||
tokens, comments, err := token.Tokenize(tm, filename, []byte(src))
|
||||
tokens, comments, err := t.Tokenize(tm, filename, []byte(src))
|
||||
if err != nil {
|
||||
tt.Fatalf("Tokenize: %v", err)
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func TestCheck(tt *testing.T) {
|
||||
tt.Fatalf("compareToWuffsfmt: %v", err)
|
||||
}
|
||||
|
||||
c, err := Check(tm, []*ast.File{file}, nil)
|
||||
c, err := Check(tm, []*a.File{file}, nil)
|
||||
if err != nil {
|
||||
tt.Fatalf("Check: %v", err)
|
||||
}
|
||||
@@ -192,11 +193,11 @@ func TestConstValues(tt *testing.T) {
|
||||
"var b bool = false or true": 1,
|
||||
}
|
||||
|
||||
tm := &token.Map{}
|
||||
tm := &t.Map{}
|
||||
for s, wantInt64 := range testCases {
|
||||
src := "packageid \"test\"\npri func foo()() {\n\t" + s + "\n}\n"
|
||||
|
||||
tokens, _, err := token.Tokenize(tm, filename, []byte(src))
|
||||
tokens, _, err := t.Tokenize(tm, filename, []byte(src))
|
||||
if err != nil {
|
||||
tt.Errorf("%q: Tokenize: %v", s, err)
|
||||
continue
|
||||
@@ -208,7 +209,7 @@ func TestConstValues(tt *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
c, err := Check(tm, []*ast.File{file}, nil)
|
||||
c, err := Check(tm, []*a.File{file}, nil)
|
||||
if err != nil {
|
||||
tt.Errorf("%q: Check: %v", s, err)
|
||||
continue
|
||||
@@ -229,8 +230,8 @@ func TestConstValues(tt *testing.T) {
|
||||
tt.Errorf("%q: Body: got %d elements, want 1", s, len(body))
|
||||
continue
|
||||
}
|
||||
if body[0].Kind() != ast.KVar {
|
||||
tt.Errorf("%q: Body[0]: got %s, want %s", s, body[0].Kind(), ast.KVar)
|
||||
if body[0].Kind() != a.KVar {
|
||||
tt.Errorf("%q: Body[0]: got %s, want %s", s, body[0].Kind(), a.KVar)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -285,7 +286,7 @@ func TestBuiltInTypeMap(tt *testing.T) {
|
||||
if got, want := len(builtInTypeMap), len(builtin.Types); got != want {
|
||||
tt.Fatalf("lengths: got %d, want %d", got, want)
|
||||
}
|
||||
tm := token.Map{}
|
||||
tm := t.Map{}
|
||||
for _, s := range builtin.Types {
|
||||
id := tm.ByName(s)
|
||||
if id == 0 {
|
||||
|
||||
@@ -25,13 +25,14 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/google/wuffs/lang/ast"
|
||||
"github.com/google/wuffs/lang/check"
|
||||
"github.com/google/wuffs/lang/parse"
|
||||
"github.com/google/wuffs/lang/token"
|
||||
|
||||
a "github.com/google/wuffs/lang/ast"
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
type Generator func(packageName string, tm *token.Map, c *check.Checker, files []*ast.File) ([]byte, error)
|
||||
type Generator func(packageName string, tm *t.Map, c *check.Checker, files []*a.File) ([]byte, error)
|
||||
|
||||
func Do(args []string, g Generator) error {
|
||||
flags := flag.FlagSet{}
|
||||
@@ -44,7 +45,7 @@ func Do(args []string, g Generator) error {
|
||||
return fmt.Errorf("prohibited package name %q", *packageName)
|
||||
}
|
||||
|
||||
tm := &token.Map{}
|
||||
tm := &t.Map{}
|
||||
files, err := parseFiles(tm, flags.Args())
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -86,14 +87,14 @@ func checkPackageName(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
func parseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error) {
|
||||
func parseFiles(tm *t.Map, filenames []string) (files []*a.File, err error) {
|
||||
if len(filenames) == 0 {
|
||||
const filename = "stdin"
|
||||
src, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokens, _, err := token.Tokenize(tm, filename, src)
|
||||
tokens, _, err := t.Tokenize(tm, filename, src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -101,18 +102,18 @@ func parseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []*ast.File{f}, nil
|
||||
return []*a.File{f}, nil
|
||||
}
|
||||
return ParseFiles(tm, filenames, nil)
|
||||
}
|
||||
|
||||
func ParseFiles(tm *token.Map, filenames []string, opts *parse.Options) (files []*ast.File, err error) {
|
||||
func ParseFiles(tm *t.Map, filenames []string, opts *parse.Options) (files []*a.File, err error) {
|
||||
for _, filename := range filenames {
|
||||
src, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokens, _, err := token.Tokenize(tm, filename, src)
|
||||
tokens, _, err := t.Tokenize(tm, filename, src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+18
-18
@@ -22,12 +22,12 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/google/wuffs/lang/token"
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
var newLine = []byte{'\n'}
|
||||
|
||||
func Render(w io.Writer, tm *token.Map, src []token.Token, comments []string) (err error) {
|
||||
func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error) {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func Render(w io.Writer, tm *token.Map, src []token.Token, comments []string) (e
|
||||
// Strip any trailing semi-colons.
|
||||
hanging := prevLineHanging
|
||||
prevLineHanging = true
|
||||
for len(lineTokens) > 0 && lineTokens[len(lineTokens)-1].ID == token.IDSemicolon {
|
||||
for len(lineTokens) > 0 && lineTokens[len(lineTokens)-1].ID == t.IDSemicolon {
|
||||
prevLineHanging = false
|
||||
lineTokens = lineTokens[:len(lineTokens)-1]
|
||||
}
|
||||
@@ -99,52 +99,52 @@ func Render(w io.Writer, tm *token.Map, src []token.Token, comments []string) (e
|
||||
indentAdjustment := 0
|
||||
if lineTokens[0].ID.IsClose() {
|
||||
indentAdjustment--
|
||||
} else if hanging && lineTokens[0].ID != token.IDOpenCurly {
|
||||
} else if hanging && lineTokens[0].ID != t.IDOpenCurly {
|
||||
indentAdjustment++
|
||||
}
|
||||
buf = appendTabs(buf, indent+indentAdjustment)
|
||||
|
||||
// Render the lineTokens.
|
||||
prevID, prevIsTightRight := token.ID(0), false
|
||||
for _, t := range lineTokens {
|
||||
prevID, prevIsTightRight := t.ID(0), false
|
||||
for _, tok := range lineTokens {
|
||||
const (
|
||||
flagsCIL = token.FlagsClose | token.FlagsIdent | token.FlagsLiteral
|
||||
flagsCIS = token.FlagsClose | token.FlagsIdent | token.FlagsStrLiteral
|
||||
flagsUB = token.FlagsUnaryOp | token.FlagsBinaryOp
|
||||
flagsCIL = t.FlagsClose | t.FlagsIdent | t.FlagsLiteral
|
||||
flagsCIS = t.FlagsClose | t.FlagsIdent | t.FlagsStrLiteral
|
||||
flagsUB = t.FlagsUnaryOp | t.FlagsBinaryOp
|
||||
)
|
||||
|
||||
if prevID != 0 && !prevIsTightRight && !t.IsTightLeft() {
|
||||
if prevID != 0 && !prevIsTightRight && !tok.IsTightLeft() {
|
||||
// The "(" token's tight-left-ness is context dependent. For
|
||||
// "f(x)", the "(" is tight-left. For "a * (b + c)", it is not.
|
||||
if t.ID.Key() != token.KeyOpenParen || prevID.Flags()&flagsCIS == 0 {
|
||||
if tok.ID.Key() != t.KeyOpenParen || prevID.Flags()&flagsCIS == 0 {
|
||||
buf = append(buf, ' ')
|
||||
}
|
||||
}
|
||||
|
||||
buf = append(buf, tm.ByToken(t)...)
|
||||
buf = append(buf, tm.ByToken(tok)...)
|
||||
|
||||
if t.ID.Key() == token.KeyOpenCurly {
|
||||
if tok.ID.Key() == t.KeyOpenCurly {
|
||||
if indent == maxIndent {
|
||||
return errors.New("render: too many \"{\" tokens")
|
||||
}
|
||||
indent++
|
||||
} else if t.ID.Key() == token.KeyCloseCurly {
|
||||
} else if tok.ID.Key() == t.KeyCloseCurly {
|
||||
if indent == 0 {
|
||||
return errors.New("render: too many \"}\" tokens")
|
||||
}
|
||||
indent--
|
||||
}
|
||||
|
||||
prevIsTightRight = t.ID.IsTightRight()
|
||||
prevIsTightRight = tok.ID.IsTightRight()
|
||||
// The "+" and "-" tokens' tight-right-ness is context dependent.
|
||||
// The unary flavor is tight-right, the binary flavor is not.
|
||||
if prevID != 0 && t.ID.Flags()&flagsUB == flagsUB {
|
||||
if prevID != 0 && tok.ID.Flags()&flagsUB == flagsUB {
|
||||
// Token-based (not ast.Node-based) heuristic for whether the
|
||||
// operator looks unary instead of binary.
|
||||
prevIsTightRight = prevID.Flags()&flagsCIL == 0
|
||||
}
|
||||
|
||||
prevID = t.ID
|
||||
prevID = tok.ID
|
||||
}
|
||||
|
||||
buf = appendComment(buf, comments, line, 0, false)
|
||||
@@ -154,7 +154,7 @@ func Render(w io.Writer, tm *token.Map, src []token.Token, comments []string) (e
|
||||
}
|
||||
commentLine = line + 1
|
||||
prevLine = line
|
||||
prevLineHanging = prevLineHanging && lineTokens[len(lineTokens)-1].ID != token.IDOpenCurly
|
||||
prevLineHanging = prevLineHanging && lineTokens[len(lineTokens)-1].ID != t.IDOpenCurly
|
||||
}
|
||||
|
||||
// Print any trailing comments.
|
||||
|
||||
Reference in new issue
Block a user