mirror of
https://github.com/golang/go.git
synced 2024-09-30 14:57:10 +00:00
gofmt the last outstanding files in src/pkg
- added a list of issues to printer/nodes.go R=rsc http://go/go-review/1024002
This commit is contained in:
parent
39fd52d3a0
commit
0ea9dd815a
@ -232,13 +232,13 @@ type Formatter func(state *State, value interface{}, ruleName string) bool
|
||||
// A FormatterMap is a set of custom formatters.
|
||||
// It maps a rule name to a formatter function.
|
||||
//
|
||||
type FormatterMap map [string] Formatter;
|
||||
type FormatterMap map[string]Formatter
|
||||
|
||||
|
||||
// A parsed format expression is built from the following nodes.
|
||||
//
|
||||
type (
|
||||
expr interface {};
|
||||
expr interface{};
|
||||
|
||||
alternatives []expr; // x | y | z
|
||||
|
||||
@ -265,7 +265,7 @@ type (
|
||||
|
||||
custom struct {
|
||||
ruleName string;
|
||||
fun Formatter
|
||||
fun Formatter;
|
||||
};
|
||||
)
|
||||
|
||||
@ -273,7 +273,7 @@ type (
|
||||
// A Format is the result of parsing a format specification.
|
||||
// The format may be applied repeatedly to format values.
|
||||
//
|
||||
type Format map [string] expr;
|
||||
type Format map[string]expr
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@ -290,7 +290,7 @@ type Format map [string] expr;
|
||||
// the receiver, and thus can be very light-weight.
|
||||
//
|
||||
type Environment interface {
|
||||
Copy() Environment
|
||||
Copy() Environment;
|
||||
}
|
||||
|
||||
|
||||
@ -365,14 +365,14 @@ func (s *State) Write(data []byte) (int, os.Error) {
|
||||
// write text segment and indentation
|
||||
n1, _ := s.output.Write(data[i0 : i+1]);
|
||||
n2, _ := s.output.Write(s.indent.Bytes());
|
||||
n += n1 + n2;
|
||||
i0 = i + 1;
|
||||
n += n1+n2;
|
||||
i0 = i+1;
|
||||
s.linePos.Offset = s.output.Len();
|
||||
s.linePos.Line++;
|
||||
}
|
||||
}
|
||||
n3, _ := s.output.Write(data[i0 : len(data)]);
|
||||
return n + n3, nil;
|
||||
n3, _ := s.output.Write(data[i0:len(data)]);
|
||||
return n+n3, nil;
|
||||
}
|
||||
|
||||
|
||||
@ -505,7 +505,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
|
||||
// segment contains a %-format at the beginning
|
||||
if lit[1] == '%' {
|
||||
// "%%" is printed as a single "%"
|
||||
s.Write(lit[1 : len(lit)]);
|
||||
s.Write(lit[1:len(lit)]);
|
||||
} else {
|
||||
// use s instead of s.output to get indentation right
|
||||
fmt.Fprintf(s, string(lit), value.Interface());
|
||||
@ -580,7 +580,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
|
||||
ruleName := t.ruleName;
|
||||
if ruleName == "" {
|
||||
// no alternate rule name, value type determines rule
|
||||
ruleName = typename(value.Type())
|
||||
ruleName = typename(value.Type());
|
||||
}
|
||||
fexpr = s.getFormat(ruleName);
|
||||
|
||||
@ -687,7 +687,7 @@ func (f Format) Eval(env Environment, args ...) ([]byte, os.Error) {
|
||||
errors <- nil; // no errors
|
||||
}();
|
||||
|
||||
err := <- errors;
|
||||
err := <-errors;
|
||||
return s.output.Bytes(), err;
|
||||
}
|
||||
|
||||
|
@ -29,8 +29,7 @@ func verify(t *testing.T, f Format, expected string, args ...) {
|
||||
if result != expected {
|
||||
t.Errorf(
|
||||
"result : `%s`\nexpected: `%s`\n\n",
|
||||
result, expected
|
||||
)
|
||||
result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,9 +61,9 @@ func formatter(s *State, value interface{}, rule_name string) bool {
|
||||
|
||||
|
||||
func TestCustomFormatters(t *testing.T) {
|
||||
fmap0 := FormatterMap{ "/": formatter };
|
||||
fmap1 := FormatterMap{ "int": formatter, "blank": formatter, "nil": formatter };
|
||||
fmap2 := FormatterMap{ "testing.T": formatter };
|
||||
fmap0 := FormatterMap{"/": formatter};
|
||||
fmap1 := FormatterMap{"int": formatter, "blank": formatter, "nil": formatter};
|
||||
fmap2 := FormatterMap{"testing.T": formatter};
|
||||
|
||||
f := parse(t, `int=`, fmap0);
|
||||
verify(t, f, ``, 1, 2, 3);
|
||||
@ -103,8 +102,7 @@ func check(t *testing.T, form, expected string, args ...) {
|
||||
if result != expected {
|
||||
t.Errorf(
|
||||
"format : %s\nresult : `%s`\nexpected: `%s`\n\n",
|
||||
form, result, expected
|
||||
)
|
||||
form, result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,7 +162,7 @@ func TestChanTypes(t *testing.T) {
|
||||
check(t, `chan="chan"`, `chan`, c0);
|
||||
|
||||
c1 := make(chan int);
|
||||
go func(){ c1 <- 42 }();
|
||||
go func() { c1 <- 42 }();
|
||||
check(t, `chan="chan"`, `chan`, c1);
|
||||
// check(t, `chan=*`, `42`, c1); // reflection support for chans incomplete
|
||||
}
|
||||
@ -174,14 +172,14 @@ func TestFuncTypes(t *testing.T) {
|
||||
var f0 func() int;
|
||||
check(t, `func="func"`, `func`, f0);
|
||||
|
||||
f1 := func() int { return 42; };
|
||||
f1 := func() int { return 42 };
|
||||
check(t, `func="func"`, `func`, f1);
|
||||
// check(t, `func=*`, `42`, f1); // reflection support for funcs incomplete
|
||||
}
|
||||
|
||||
|
||||
func TestInterfaceTypes(t *testing.T) {
|
||||
var i0 interface{};
|
||||
var i0 interface{}
|
||||
check(t, `interface="interface"`, `interface`, i0);
|
||||
|
||||
i0 = "foo";
|
||||
@ -234,8 +232,7 @@ type T1 struct {
|
||||
a int;
|
||||
}
|
||||
|
||||
const F1 =
|
||||
`datafmt "datafmt";`
|
||||
const F1 = `datafmt "datafmt";`
|
||||
`int = "%d";`
|
||||
`datafmt.T1 = "<" a ">";`
|
||||
|
||||
@ -252,17 +249,15 @@ type T2 struct {
|
||||
p *T1;
|
||||
}
|
||||
|
||||
const F2a =
|
||||
F1 +
|
||||
const F2a = F1 +
|
||||
`string = "%s";`
|
||||
`ptr = *;`
|
||||
`datafmt.T2 = s ["-" p "-"];`
|
||||
|
||||
const F2b =
|
||||
F1 +
|
||||
const F2b = F1 +
|
||||
`string = "%s";`
|
||||
`ptr = *;`
|
||||
`datafmt.T2 = s ("-" p "-" | "empty");`;
|
||||
`datafmt.T2 = s ("-" p "-" | "empty");`
|
||||
|
||||
func TestStruct2(t *testing.T) {
|
||||
check(t, F2a, "foo", T2{"foo", nil});
|
||||
@ -279,14 +274,12 @@ type T3 struct {
|
||||
a []int;
|
||||
}
|
||||
|
||||
const F3a =
|
||||
`datafmt "datafmt";`
|
||||
const F3a = `datafmt "datafmt";`
|
||||
`default = "%v";`
|
||||
`array = *;`
|
||||
`datafmt.T3 = s {" " a a / ","};`
|
||||
|
||||
const F3b =
|
||||
`datafmt "datafmt";`
|
||||
const F3b = `datafmt "datafmt";`
|
||||
`int = "%d";`
|
||||
`string = "%s";`
|
||||
`array = *;`
|
||||
@ -310,8 +303,7 @@ type T4 struct {
|
||||
a []int;
|
||||
}
|
||||
|
||||
const F4a =
|
||||
`datafmt "datafmt";`
|
||||
const F4a = `datafmt "datafmt";`
|
||||
`int = "%d";`
|
||||
`ptr = *;`
|
||||
`array = *;`
|
||||
@ -319,8 +311,7 @@ const F4a =
|
||||
`empty = *:nil;`
|
||||
`datafmt.T4 = "<" (x:empty x | "-") ">" `
|
||||
|
||||
const F4b =
|
||||
`datafmt "datafmt";`
|
||||
const F4b = `datafmt "datafmt";`
|
||||
`int = "%d";`
|
||||
`ptr = *;`
|
||||
`array = *;`
|
||||
@ -345,8 +336,7 @@ type Point struct {
|
||||
x, y int;
|
||||
}
|
||||
|
||||
const FPoint =
|
||||
`datafmt "datafmt";`
|
||||
const FPoint = `datafmt "datafmt";`
|
||||
`int = "%d";`
|
||||
`hexInt = "0x%x";`
|
||||
`string = "---%s---";`
|
||||
@ -361,8 +351,7 @@ func TestStructPoint(t *testing.T) {
|
||||
// ----------------------------------------------------------------------------
|
||||
// Formatting a slice (documentation example)
|
||||
|
||||
const FSlice =
|
||||
`int = "%b";`
|
||||
const FSlice = `int = "%b";`
|
||||
`array = { * / ", " }`
|
||||
|
||||
func TestSlice(t *testing.T) {
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
|
||||
|
||||
// noPos is used when there is no corresponding source position for a token.
|
||||
var noPos token.Position;
|
||||
var noPos token.Position
|
||||
|
||||
|
||||
// The mode parameter to the Parse* functions is a set of flags (or 0).
|
||||
@ -27,7 +27,7 @@ var noPos token.Position;
|
||||
// parser functionality.
|
||||
//
|
||||
const (
|
||||
PackageClauseOnly uint = 1 << iota; // parsing stops after package clause
|
||||
PackageClauseOnly uint = 1<<iota; // parsing stops after package clause
|
||||
ImportsOnly; // parsing stops after import declarations
|
||||
ParseComments; // parse comments and add them to AST
|
||||
Trace; // print a trace of parsed productions
|
||||
@ -63,7 +63,7 @@ type parser struct {
|
||||
pkgScope *ast.Scope;
|
||||
fileScope *ast.Scope;
|
||||
topScope *ast.Scope;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// scannerMode returns the scanner mode bits given the parser's mode bits.
|
||||
@ -79,7 +79,7 @@ func (p *parser) init(filename string, src []byte, mode uint) {
|
||||
p.ErrorVector.Init();
|
||||
p.scanner.Init(filename, src, p, scannerMode(mode));
|
||||
p.mode = mode;
|
||||
p.trace = mode & Trace != 0; // for convenience (p.trace is used frequently)
|
||||
p.trace = mode&Trace != 0; // for convenience (p.trace is used frequently)
|
||||
p.next();
|
||||
}
|
||||
|
||||
@ -88,16 +88,15 @@ func (p *parser) init(filename string, src []byte, mode uint) {
|
||||
// Parsing support
|
||||
|
||||
func (p *parser) printTrace(a ...) {
|
||||
const dots =
|
||||
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
|
||||
const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
|
||||
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ";
|
||||
const n = uint(len(dots));
|
||||
fmt.Printf("%5d:%3d: ", p.pos.Line, p.pos.Column);
|
||||
i := 2*p.indent;
|
||||
i := 2 * p.indent;
|
||||
for ; i > n; i -= n {
|
||||
fmt.Print(dots);
|
||||
}
|
||||
fmt.Print(dots[0 : i]);
|
||||
fmt.Print(dots[0:i]);
|
||||
fmt.Println(a);
|
||||
}
|
||||
|
||||
@ -109,7 +108,8 @@ func trace(p *parser, msg string) *parser {
|
||||
}
|
||||
|
||||
|
||||
func un/*trace*/(p *parser) {
|
||||
// Usage pattern: defer un(trace(p, "..."));
|
||||
func un(p *parser) {
|
||||
p.indent--;
|
||||
p.printTrace(")");
|
||||
}
|
||||
@ -127,7 +127,7 @@ func (p *parser) next0() {
|
||||
case p.tok.IsLiteral():
|
||||
p.printTrace(s, string(p.lit));
|
||||
case p.tok.IsOperator(), p.tok.IsKeyword():
|
||||
p.printTrace("\"" + s + "\"");
|
||||
p.printTrace("\""+s+"\"");
|
||||
default:
|
||||
p.printTrace(s);
|
||||
}
|
||||
@ -246,7 +246,7 @@ func (p *parser) errorExpected(pos token.Position, msg string) {
|
||||
// make the error message more specific
|
||||
msg += ", found '" + p.tok.String() + "'";
|
||||
if p.tok.IsLiteral() {
|
||||
msg += " " + string(p.lit);
|
||||
msg += " "+string(p.lit);
|
||||
}
|
||||
}
|
||||
p.Error(pos, msg);
|
||||
@ -272,7 +272,8 @@ func openScope(p *parser) *parser {
|
||||
}
|
||||
|
||||
|
||||
func close/*Scope*/(p *parser) {
|
||||
// Usage pattern: defer close(openScope(p));
|
||||
func close(p *parser) {
|
||||
p.topScope = p.topScope.Outer;
|
||||
}
|
||||
|
||||
@ -757,14 +758,22 @@ func (p *parser) parseChanType() *ast.ChanType {
|
||||
|
||||
func (p *parser) tryRawType(ellipsisOk bool) ast.Expr {
|
||||
switch p.tok {
|
||||
case token.IDENT: return p.parseTypeName();
|
||||
case token.LBRACK: return p.parseArrayType(ellipsisOk);
|
||||
case token.STRUCT: return p.parseStructType();
|
||||
case token.MUL: return p.parsePointerType();
|
||||
case token.FUNC: return p.parseFuncType();
|
||||
case token.INTERFACE: return p.parseInterfaceType();
|
||||
case token.MAP: return p.parseMapType();
|
||||
case token.CHAN, token.ARROW: return p.parseChanType();
|
||||
case token.IDENT:
|
||||
return p.parseTypeName();
|
||||
case token.LBRACK:
|
||||
return p.parseArrayType(ellipsisOk);
|
||||
case token.STRUCT:
|
||||
return p.parseStructType();
|
||||
case token.MUL:
|
||||
return p.parsePointerType();
|
||||
case token.FUNC:
|
||||
return p.parseFuncType();
|
||||
case token.INTERFACE:
|
||||
return p.parseInterfaceType();
|
||||
case token.MAP:
|
||||
return p.parseMapType();
|
||||
case token.CHAN, token.ARROW:
|
||||
return p.parseChanType();
|
||||
case token.LPAREN:
|
||||
lparen := p.pos;
|
||||
p.next();
|
||||
@ -1097,9 +1106,12 @@ func isTypeName(x ast.Expr) bool {
|
||||
switch t := x.(type) {
|
||||
case *ast.BadExpr:
|
||||
case *ast.Ident:
|
||||
case *ast.ParenExpr: return isTypeName(t.X); // TODO(gri): should (TypeName) be illegal?
|
||||
case *ast.SelectorExpr: return isTypeName(t.X);
|
||||
default: return false; // all other nodes are not type names
|
||||
case *ast.ParenExpr:
|
||||
return isTypeName(t.X); // TODO(gri): should (TypeName) be illegal?
|
||||
case *ast.SelectorExpr:
|
||||
return isTypeName(t.X);
|
||||
default:
|
||||
return false; // all other nodes are not type names
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -1111,12 +1123,15 @@ func isCompositeLitType(x ast.Expr) bool {
|
||||
switch t := x.(type) {
|
||||
case *ast.BadExpr:
|
||||
case *ast.Ident:
|
||||
case *ast.ParenExpr: return isCompositeLitType(t.X);
|
||||
case *ast.SelectorExpr: return isTypeName(t.X);
|
||||
case *ast.ParenExpr:
|
||||
return isCompositeLitType(t.X);
|
||||
case *ast.SelectorExpr:
|
||||
return isTypeName(t.X);
|
||||
case *ast.ArrayType:
|
||||
case *ast.StructType:
|
||||
case *ast.MapType:
|
||||
default: return false; // all other nodes are not legal composite literal types
|
||||
default:
|
||||
return false; // all other nodes are not legal composite literal types
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -1154,9 +1169,12 @@ func (p *parser) parsePrimaryExpr() ast.Expr {
|
||||
x := p.parseOperand();
|
||||
L: for {
|
||||
switch p.tok {
|
||||
case token.PERIOD: x = p.parseSelectorOrTypeAssertion(p.checkExpr(x));
|
||||
case token.LBRACK: x = p.parseIndex(p.checkExpr(x));
|
||||
case token.LPAREN: x = p.parseCallOrConversion(p.checkExprOrType(x));
|
||||
case token.PERIOD:
|
||||
x = p.parseSelectorOrTypeAssertion(p.checkExpr(x));
|
||||
case token.LBRACK:
|
||||
x = p.parseIndex(p.checkExpr(x));
|
||||
case token.LPAREN:
|
||||
x = p.parseCallOrConversion(p.checkExprOrType(x));
|
||||
case token.LBRACE:
|
||||
if isCompositeLitType(x) && (p.exprLev >= 0 || !isTypeName(x)) {
|
||||
x = p.parseCompositeLit(x);
|
||||
@ -1206,7 +1224,7 @@ func (p *parser) parseBinaryExpr(prec1 int) ast.Expr {
|
||||
for p.tok.Precedence() == prec {
|
||||
pos, op := p.pos, p.tok;
|
||||
p.next();
|
||||
y := p.parseBinaryExpr(prec + 1);
|
||||
y := p.parseBinaryExpr(prec+1);
|
||||
x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)};
|
||||
}
|
||||
}
|
||||
@ -1637,7 +1655,7 @@ func (p *parser) parseForStmt() ast.Stmt {
|
||||
}
|
||||
if rhs, isUnary := as.Rhs[0].(*ast.UnaryExpr); isUnary && rhs.Op == token.RANGE {
|
||||
// rhs is range expression; check lhs
|
||||
return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, rhs.X, body}
|
||||
return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, rhs.X, body};
|
||||
} else {
|
||||
p.errorExpected(s2.Pos(), "range clause");
|
||||
return &ast.BadStmt{pos};
|
||||
|
@ -24,6 +24,15 @@ const (
|
||||
)
|
||||
|
||||
|
||||
// Other outstanding formatting issues:
|
||||
// - replacement of expression spacing algorithm with rsc's algorithm
|
||||
// - support for one-line composite types (e.g. structs) as composite literals types
|
||||
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
|
||||
// when the comment spans multiple lines
|
||||
// - formatting of expression lists; especially for string lists (stringListMode)
|
||||
// - blank after { and before } in one-line composite literals probably looks better
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Common AST nodes.
|
||||
|
||||
|
@ -22,9 +22,12 @@ const /* class */ (
|
||||
|
||||
func tokenclass(tok token.Token) int {
|
||||
switch {
|
||||
case tok.IsLiteral(): return literal;
|
||||
case tok.IsOperator(): return operator;
|
||||
case tok.IsKeyword(): return keyword;
|
||||
case tok.IsLiteral():
|
||||
return literal;
|
||||
case tok.IsOperator():
|
||||
return operator;
|
||||
case tok.IsKeyword():
|
||||
return keyword;
|
||||
}
|
||||
return special;
|
||||
}
|
||||
@ -39,127 +42,130 @@ type elt struct {
|
||||
|
||||
var tokens = [...]elt{
|
||||
// Special tokens
|
||||
elt{ token.COMMENT, "/* a comment */", special },
|
||||
elt{ token.COMMENT, "// a comment \n", special },
|
||||
elt{token.COMMENT, "/* a comment */", special},
|
||||
elt{token.COMMENT, "// a comment \n", special},
|
||||
|
||||
// Identifiers and basic type literals
|
||||
elt{ token.IDENT, "foobar", literal },
|
||||
elt{ token.IDENT, "a۰۱۸", literal },
|
||||
elt{ token.IDENT, "foo६४", literal },
|
||||
elt{ token.IDENT, "bar9876", literal },
|
||||
elt{ token.INT, "0", literal },
|
||||
elt{ token.INT, "01234567", literal },
|
||||
elt{ token.INT, "0xcafebabe", literal },
|
||||
elt{ token.FLOAT, "0.", literal },
|
||||
elt{ token.FLOAT, ".0", literal },
|
||||
elt{ token.FLOAT, "3.14159265", literal },
|
||||
elt{ token.FLOAT, "1e0", literal },
|
||||
elt{ token.FLOAT, "1e+100", literal },
|
||||
elt{ token.FLOAT, "1e-100", literal },
|
||||
elt{ token.FLOAT, "2.71828e-1000", literal },
|
||||
elt{ token.CHAR, "'a'", literal },
|
||||
elt{ token.CHAR, "'\\000'", literal },
|
||||
elt{ token.CHAR, "'\\xFF'", literal },
|
||||
elt{ token.CHAR, "'\\uff16'", literal },
|
||||
elt{ token.CHAR, "'\\U0000ff16'", literal },
|
||||
elt{ token.STRING, "`foobar`", literal },
|
||||
elt{ token.STRING, "`" `foo
|
||||
bar` "`", literal },
|
||||
elt{token.IDENT, "foobar", literal},
|
||||
elt{token.IDENT, "a۰۱۸", literal},
|
||||
elt{token.IDENT, "foo६४", literal},
|
||||
elt{token.IDENT, "bar9876", literal},
|
||||
elt{token.INT, "0", literal},
|
||||
elt{token.INT, "01234567", literal},
|
||||
elt{token.INT, "0xcafebabe", literal},
|
||||
elt{token.FLOAT, "0.", literal},
|
||||
elt{token.FLOAT, ".0", literal},
|
||||
elt{token.FLOAT, "3.14159265", literal},
|
||||
elt{token.FLOAT, "1e0", literal},
|
||||
elt{token.FLOAT, "1e+100", literal},
|
||||
elt{token.FLOAT, "1e-100", literal},
|
||||
elt{token.FLOAT, "2.71828e-1000", literal},
|
||||
elt{token.CHAR, "'a'", literal},
|
||||
elt{token.CHAR, "'\\000'", literal},
|
||||
elt{token.CHAR, "'\\xFF'", literal},
|
||||
elt{token.CHAR, "'\\uff16'", literal},
|
||||
elt{token.CHAR, "'\\U0000ff16'", literal},
|
||||
elt{token.STRING, "`foobar`", literal},
|
||||
elt{token.STRING, "`" `foo
|
||||
bar`
|
||||
"`",
|
||||
literal,
|
||||
},
|
||||
|
||||
// Operators and delimitors
|
||||
elt{ token.ADD, "+", operator },
|
||||
elt{ token.SUB, "-", operator },
|
||||
elt{ token.MUL, "*", operator },
|
||||
elt{ token.QUO, "/", operator },
|
||||
elt{ token.REM, "%", operator },
|
||||
elt{token.ADD, "+", operator},
|
||||
elt{token.SUB, "-", operator},
|
||||
elt{token.MUL, "*", operator},
|
||||
elt{token.QUO, "/", operator},
|
||||
elt{token.REM, "%", operator},
|
||||
|
||||
elt{ token.AND, "&", operator },
|
||||
elt{ token.OR, "|", operator },
|
||||
elt{ token.XOR, "^", operator },
|
||||
elt{ token.SHL, "<<", operator },
|
||||
elt{ token.SHR, ">>", operator },
|
||||
elt{ token.AND_NOT, "&^", operator },
|
||||
elt{token.AND, "&", operator},
|
||||
elt{token.OR, "|", operator},
|
||||
elt{token.XOR, "^", operator},
|
||||
elt{token.SHL, "<<", operator},
|
||||
elt{token.SHR, ">>", operator},
|
||||
elt{token.AND_NOT, "&^", operator},
|
||||
|
||||
elt{ token.ADD_ASSIGN, "+=", operator },
|
||||
elt{ token.SUB_ASSIGN, "-=", operator },
|
||||
elt{ token.MUL_ASSIGN, "*=", operator },
|
||||
elt{ token.QUO_ASSIGN, "/=", operator },
|
||||
elt{ token.REM_ASSIGN, "%=", operator },
|
||||
elt{token.ADD_ASSIGN, "+=", operator},
|
||||
elt{token.SUB_ASSIGN, "-=", operator},
|
||||
elt{token.MUL_ASSIGN, "*=", operator},
|
||||
elt{token.QUO_ASSIGN, "/=", operator},
|
||||
elt{token.REM_ASSIGN, "%=", operator},
|
||||
|
||||
elt{ token.AND_ASSIGN, "&=", operator },
|
||||
elt{ token.OR_ASSIGN, "|=", operator },
|
||||
elt{ token.XOR_ASSIGN, "^=", operator },
|
||||
elt{ token.SHL_ASSIGN, "<<=", operator },
|
||||
elt{ token.SHR_ASSIGN, ">>=", operator },
|
||||
elt{ token.AND_NOT_ASSIGN, "&^=", operator },
|
||||
elt{token.AND_ASSIGN, "&=", operator},
|
||||
elt{token.OR_ASSIGN, "|=", operator},
|
||||
elt{token.XOR_ASSIGN, "^=", operator},
|
||||
elt{token.SHL_ASSIGN, "<<=", operator},
|
||||
elt{token.SHR_ASSIGN, ">>=", operator},
|
||||
elt{token.AND_NOT_ASSIGN, "&^=", operator},
|
||||
|
||||
elt{ token.LAND, "&&", operator },
|
||||
elt{ token.LOR, "||", operator },
|
||||
elt{ token.ARROW, "<-", operator },
|
||||
elt{ token.INC, "++", operator },
|
||||
elt{ token.DEC, "--", operator },
|
||||
elt{token.LAND, "&&", operator},
|
||||
elt{token.LOR, "||", operator},
|
||||
elt{token.ARROW, "<-", operator},
|
||||
elt{token.INC, "++", operator},
|
||||
elt{token.DEC, "--", operator},
|
||||
|
||||
elt{ token.EQL, "==", operator },
|
||||
elt{ token.LSS, "<", operator },
|
||||
elt{ token.GTR, ">", operator },
|
||||
elt{ token.ASSIGN, "=", operator },
|
||||
elt{ token.NOT, "!", operator },
|
||||
elt{token.EQL, "==", operator},
|
||||
elt{token.LSS, "<", operator},
|
||||
elt{token.GTR, ">", operator},
|
||||
elt{token.ASSIGN, "=", operator},
|
||||
elt{token.NOT, "!", operator},
|
||||
|
||||
elt{ token.NEQ, "!=", operator },
|
||||
elt{ token.LEQ, "<=", operator },
|
||||
elt{ token.GEQ, ">=", operator },
|
||||
elt{ token.DEFINE, ":=", operator },
|
||||
elt{ token.ELLIPSIS, "...", operator },
|
||||
elt{token.NEQ, "!=", operator},
|
||||
elt{token.LEQ, "<=", operator},
|
||||
elt{token.GEQ, ">=", operator},
|
||||
elt{token.DEFINE, ":=", operator},
|
||||
elt{token.ELLIPSIS, "...", operator},
|
||||
|
||||
elt{ token.LPAREN, "(", operator },
|
||||
elt{ token.LBRACK, "[", operator },
|
||||
elt{ token.LBRACE, "{", operator },
|
||||
elt{ token.COMMA, ",", operator },
|
||||
elt{ token.PERIOD, ".", operator },
|
||||
elt{token.LPAREN, "(", operator},
|
||||
elt{token.LBRACK, "[", operator},
|
||||
elt{token.LBRACE, "{", operator},
|
||||
elt{token.COMMA, ",", operator},
|
||||
elt{token.PERIOD, ".", operator},
|
||||
|
||||
elt{ token.RPAREN, ")", operator },
|
||||
elt{ token.RBRACK, "]", operator },
|
||||
elt{ token.RBRACE, "}", operator },
|
||||
elt{ token.SEMICOLON, ";", operator },
|
||||
elt{ token.COLON, ":", operator },
|
||||
elt{token.RPAREN, ")", operator},
|
||||
elt{token.RBRACK, "]", operator},
|
||||
elt{token.RBRACE, "}", operator},
|
||||
elt{token.SEMICOLON, ";", operator},
|
||||
elt{token.COLON, ":", operator},
|
||||
|
||||
// Keywords
|
||||
elt{ token.BREAK, "break", keyword },
|
||||
elt{ token.CASE, "case", keyword },
|
||||
elt{ token.CHAN, "chan", keyword },
|
||||
elt{ token.CONST, "const", keyword },
|
||||
elt{ token.CONTINUE, "continue", keyword },
|
||||
elt{token.BREAK, "break", keyword},
|
||||
elt{token.CASE, "case", keyword},
|
||||
elt{token.CHAN, "chan", keyword},
|
||||
elt{token.CONST, "const", keyword},
|
||||
elt{token.CONTINUE, "continue", keyword},
|
||||
|
||||
elt{ token.DEFAULT, "default", keyword },
|
||||
elt{ token.DEFER, "defer", keyword },
|
||||
elt{ token.ELSE, "else", keyword },
|
||||
elt{ token.FALLTHROUGH, "fallthrough", keyword },
|
||||
elt{ token.FOR, "for", keyword },
|
||||
elt{token.DEFAULT, "default", keyword},
|
||||
elt{token.DEFER, "defer", keyword},
|
||||
elt{token.ELSE, "else", keyword},
|
||||
elt{token.FALLTHROUGH, "fallthrough", keyword},
|
||||
elt{token.FOR, "for", keyword},
|
||||
|
||||
elt{ token.FUNC, "func", keyword },
|
||||
elt{ token.GO, "go", keyword },
|
||||
elt{ token.GOTO, "goto", keyword },
|
||||
elt{ token.IF, "if", keyword },
|
||||
elt{ token.IMPORT, "import", keyword },
|
||||
elt{token.FUNC, "func", keyword},
|
||||
elt{token.GO, "go", keyword},
|
||||
elt{token.GOTO, "goto", keyword},
|
||||
elt{token.IF, "if", keyword},
|
||||
elt{token.IMPORT, "import", keyword},
|
||||
|
||||
elt{ token.INTERFACE, "interface", keyword },
|
||||
elt{ token.MAP, "map", keyword },
|
||||
elt{ token.PACKAGE, "package", keyword },
|
||||
elt{ token.RANGE, "range", keyword },
|
||||
elt{ token.RETURN, "return", keyword },
|
||||
elt{token.INTERFACE, "interface", keyword},
|
||||
elt{token.MAP, "map", keyword},
|
||||
elt{token.PACKAGE, "package", keyword},
|
||||
elt{token.RANGE, "range", keyword},
|
||||
elt{token.RETURN, "return", keyword},
|
||||
|
||||
elt{ token.SELECT, "select", keyword },
|
||||
elt{ token.STRUCT, "struct", keyword },
|
||||
elt{ token.SWITCH, "switch", keyword },
|
||||
elt{ token.TYPE, "type", keyword },
|
||||
elt{ token.VAR, "var", keyword },
|
||||
elt{token.SELECT, "select", keyword},
|
||||
elt{token.STRUCT, "struct", keyword},
|
||||
elt{token.SWITCH, "switch", keyword},
|
||||
elt{token.TYPE, "type", keyword},
|
||||
elt{token.VAR, "var", keyword},
|
||||
}
|
||||
|
||||
|
||||
const whitespace = " \t \n\n\n"; // to separate tokens
|
||||
const whitespace = " \t \n\n\n" // to separate tokens
|
||||
|
||||
type TestErrorHandler struct {
|
||||
t *testing.T
|
||||
t *testing.T;
|
||||
}
|
||||
|
||||
func (h *TestErrorHandler) Error(pos token.Position, msg string) {
|
||||
@ -188,7 +194,7 @@ func checkPos(t *testing.T, lit string, pos, expected token.Position) {
|
||||
if pos.Line != expected.Line {
|
||||
t.Errorf("bad line for %s: got %d, expected %d", lit, pos.Line, expected.Line);
|
||||
}
|
||||
if pos.Column!= expected.Column {
|
||||
if pos.Column != expected.Column {
|
||||
t.Errorf("bad column for %s: got %d, expected %d", lit, pos.Column, expected.Column);
|
||||
}
|
||||
}
|
||||
@ -207,7 +213,7 @@ func TestScan(t *testing.T) {
|
||||
index := 0;
|
||||
epos := token.Position{"", 0, 1, 1};
|
||||
nerrors := Tokenize("", strings.Bytes(src), &TestErrorHandler{t}, ScanComments,
|
||||
func (pos token.Position, tok token.Token, litb []byte) bool {
|
||||
func(pos token.Position, tok token.Token, litb []byte) bool {
|
||||
e := elt{token.EOF, "", special};
|
||||
if index < len(tokens) {
|
||||
e = tokens[index];
|
||||
@ -227,7 +233,7 @@ func TestScan(t *testing.T) {
|
||||
if tokenclass(tok) != e.class {
|
||||
t.Errorf("bad class for %s: got %d, expected %d", lit, tokenclass(tok), e.class);
|
||||
}
|
||||
epos.Offset += len(lit) + len(whitespace);
|
||||
epos.Offset += len(lit)+len(whitespace);
|
||||
epos.Line += NewlineCount(lit) + whitespace_linecount;
|
||||
if tok == token.COMMENT && litb[1] == '/' {
|
||||
// correct for unaccounted '/n' in //-style comment
|
||||
@ -236,8 +242,7 @@ func TestScan(t *testing.T) {
|
||||
}
|
||||
index++;
|
||||
return tok != token.EOF;
|
||||
}
|
||||
);
|
||||
});
|
||||
if nerrors != 0 {
|
||||
t.Errorf("found %d errors", nerrors);
|
||||
}
|
||||
@ -253,20 +258,20 @@ type seg struct {
|
||||
|
||||
var segments = []seg{
|
||||
// exactly one token per line since the test consumes one token per segment
|
||||
seg{ " line1", "TestLineComments", 1 },
|
||||
seg{ "\nline2", "TestLineComments", 2 },
|
||||
seg{ "\nline3 //line File1.go:100", "TestLineComments", 3 }, // bad line comment, ignored
|
||||
seg{ "\nline4", "TestLineComments", 4 },
|
||||
seg{ "\n//line File1.go:100\n line100", "File1.go", 100 },
|
||||
seg{ "\n//line File2.go:200\n line200", "File2.go", 200 },
|
||||
seg{ "\n//line :1\n line1", "", 1 },
|
||||
seg{ "\n//line foo:42\n line42", "foo", 42 },
|
||||
seg{ "\n //line foo:42\n line44", "foo", 44 }, // bad line comment, ignored
|
||||
seg{ "\n//line foo 42\n line46", "foo", 46 }, // bad line comment, ignored
|
||||
seg{ "\n//line foo:42 extra text\n line48", "foo", 48 }, // bad line comment, ignored
|
||||
seg{ "\n//line foo:42\n line42", "foo", 42 },
|
||||
seg{ "\n//line foo:42\n line42", "foo", 42 },
|
||||
seg{ "\n//line File1.go:100\n line100", "File1.go", 100 },
|
||||
seg{" line1", "TestLineComments", 1},
|
||||
seg{"\nline2", "TestLineComments", 2},
|
||||
seg{"\nline3 //line File1.go:100", "TestLineComments", 3}, // bad line comment, ignored
|
||||
seg{"\nline4", "TestLineComments", 4},
|
||||
seg{"\n//line File1.go:100\n line100", "File1.go", 100},
|
||||
seg{"\n//line File2.go:200\n line200", "File2.go", 200},
|
||||
seg{"\n//line :1\n line1", "", 1},
|
||||
seg{"\n//line foo:42\n line42", "foo", 42},
|
||||
seg{"\n //line foo:42\n line44", "foo", 44}, // bad line comment, ignored
|
||||
seg{"\n//line foo 42\n line46", "foo", 46}, // bad line comment, ignored
|
||||
seg{"\n//line foo:42 extra text\n line48", "foo", 48}, // bad line comment, ignored
|
||||
seg{"\n//line foo:42\n line42", "foo", 42},
|
||||
seg{"\n//line foo:42\n line42", "foo", 42},
|
||||
seg{"\n//line File1.go:100\n line100", "File1.go", 100},
|
||||
}
|
||||
|
||||
|
||||
@ -340,23 +345,19 @@ func TestIllegalChars(t *testing.T) {
|
||||
|
||||
|
||||
func TestStdErrorHander(t *testing.T) {
|
||||
const src =
|
||||
"@\n" // illegal character, cause an error
|
||||
const src = "@\n" // illegal character, cause an error
|
||||
"@ @\n" // two errors on the same line
|
||||
"//line File2:20\n"
|
||||
"@\n" // different file, but same line
|
||||
"//line File2:1\n"
|
||||
"@ @\n" // same file, decreasing line number
|
||||
"//line File1:1\n"
|
||||
"@ @ @" // original file, line 1 again
|
||||
;
|
||||
"@ @ @"; // original file, line 1 again
|
||||
|
||||
|
||||
v := NewErrorVector();
|
||||
nerrors := Tokenize("File1", strings.Bytes(src), v, 0,
|
||||
func (pos token.Position, tok token.Token, litb []byte) bool {
|
||||
return tok != token.EOF;
|
||||
}
|
||||
);
|
||||
func(pos token.Position, tok token.Token, litb []byte) bool { return tok != token.EOF });
|
||||
|
||||
list := v.GetErrorList(Raw);
|
||||
if len(list) != 9 {
|
||||
|
@ -16,7 +16,8 @@ const testInput = `
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<body xmlns:foo="ns1" xmlns="ns2" xmlns:tag="ns3" ` "\r\n\t" ` >
|
||||
<body xmlns:foo="ns1" xmlns="ns2" xmlns:tag="ns3" `
|
||||
"\r\n\t" ` >
|
||||
<hello lang="en">World <>'" 白鵬翔</hello>
|
||||
<goodbye />
|
||||
<outer foo:attr="value" xmlns:tag="ns4">
|
||||
@ -116,7 +117,7 @@ func (r *stringReader) ReadByte() (b byte, err os.Error) {
|
||||
}
|
||||
|
||||
func StringReader(s string) io.Reader {
|
||||
return &stringReader{s, 0}
|
||||
return &stringReader{s, 0};
|
||||
}
|
||||
|
||||
func TestRawToken(t *testing.T) {
|
||||
@ -146,4 +147,3 @@ func TestToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user