Consistently use a and t for ast and token

This commit is contained in:
Nigel Tao committed 2017-12-28 11:47:46 +11:00
1 parent bdc47bd6e2
commit f6159a1c95
5 files changed
+49 -45

No files matched your search

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