summaryrefslogtreecommitdiff
path: root/src/pkg/exp/datafmt
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
committerOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
commit3e45412327a2654a77944249962b3652e6142299 (patch)
treebc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/exp/datafmt
parentc533680039762cacbc37db8dc7eed074c3e497be (diff)
downloadgolang-upstream/2011.01.12.tar.gz
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/exp/datafmt')
-rw-r--r--src/pkg/exp/datafmt/Makefile2
-rw-r--r--src/pkg/exp/datafmt/datafmt.go8
-rw-r--r--src/pkg/exp/datafmt/datafmt_test.go10
-rw-r--r--src/pkg/exp/datafmt/parser.go38
4 files changed, 34 insertions, 24 deletions
diff --git a/src/pkg/exp/datafmt/Makefile b/src/pkg/exp/datafmt/Makefile
index 40543b195..aa9453897 100644
--- a/src/pkg/exp/datafmt/Makefile
+++ b/src/pkg/exp/datafmt/Makefile
@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
-include ../../../Make.$(GOARCH)
+include ../../../Make.inc
TARG=exp/datafmt
GOFILES=\
diff --git a/src/pkg/exp/datafmt/datafmt.go b/src/pkg/exp/datafmt/datafmt.go
index e77f445b5..46c412342 100644
--- a/src/pkg/exp/datafmt/datafmt.go
+++ b/src/pkg/exp/datafmt/datafmt.go
@@ -656,7 +656,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
// Eval formats each argument according to the format
// f and returns the resulting []byte and os.Error. If
-// an error occured, the []byte contains the partially
+// an error occurred, the []byte contains the partially
// formatted result. An environment env may be passed
// in which is available in custom formatters through
// the state parameter.
@@ -697,7 +697,7 @@ func (f Format) Eval(env Environment, args ...interface{}) ([]byte, os.Error) {
// written and an os.Error, if any.
//
func (f Format) Fprint(w io.Writer, env Environment, args ...interface{}) (int, os.Error) {
- data, err := f.Eval(env, args)
+ data, err := f.Eval(env, args...)
if err != nil {
// TODO should we print partial result in case of error?
return 0, err
@@ -711,7 +711,7 @@ func (f Format) Fprint(w io.Writer, env Environment, args ...interface{}) (int,
// number of bytes written and an os.Error, if any.
//
func (f Format) Print(args ...interface{}) (int, os.Error) {
- return f.Fprint(os.Stdout, nil, args)
+ return f.Fprint(os.Stdout, nil, args...)
}
@@ -722,7 +722,7 @@ func (f Format) Print(args ...interface{}) (int, os.Error) {
//
func (f Format) Sprint(args ...interface{}) string {
var buf bytes.Buffer
- _, err := f.Fprint(&buf, nil, args)
+ _, err := f.Fprint(&buf, nil, args...)
if err != nil {
var i interface{} = args
fmt.Fprintf(&buf, "--- Sprint(%s) failed: %v", fmt.Sprint(i), err)
diff --git a/src/pkg/exp/datafmt/datafmt_test.go b/src/pkg/exp/datafmt/datafmt_test.go
index 908894717..f6a09f820 100644
--- a/src/pkg/exp/datafmt/datafmt_test.go
+++ b/src/pkg/exp/datafmt/datafmt_test.go
@@ -7,11 +7,15 @@ package datafmt
import (
"fmt"
"testing"
+ "go/token"
)
+var fset = token.NewFileSet()
+
+
func parse(t *testing.T, form string, fmap FormatterMap) Format {
- f, err := Parse("", []byte(form), fmap)
+ f, err := Parse(fset, "", []byte(form), fmap)
if err != nil {
t.Errorf("Parse(%s): %v", form, err)
return nil
@@ -24,7 +28,7 @@ func verify(t *testing.T, f Format, expected string, args ...interface{}) {
if f == nil {
return // allow other tests to run
}
- result := f.Sprint(args)
+ result := f.Sprint(args...)
if result != expected {
t.Errorf(
"result : `%s`\nexpected: `%s`\n\n",
@@ -97,7 +101,7 @@ func check(t *testing.T, form, expected string, args ...interface{}) {
if f == nil {
return // allow other tests to run
}
- result := f.Sprint(args)
+ result := f.Sprint(args...)
if result != expected {
t.Errorf(
"format : %s\nresult : `%s`\nexpected: `%s`\n\n",
diff --git a/src/pkg/exp/datafmt/parser.go b/src/pkg/exp/datafmt/parser.go
index de1f1c2a6..a01378ea5 100644
--- a/src/pkg/exp/datafmt/parser.go
+++ b/src/pkg/exp/datafmt/parser.go
@@ -19,9 +19,10 @@ import (
type parser struct {
scanner.ErrorVector
scanner scanner.Scanner
- pos token.Position // token position
- tok token.Token // one token look-ahead
- lit []byte // token literal
+ file *token.File
+ pos token.Pos // token position
+ tok token.Token // one token look-ahead
+ lit []byte // token literal
packs map[string]string // PackageName -> ImportPath
rules map[string]expr // RuleName -> Expression
@@ -39,18 +40,23 @@ func (p *parser) next() {
}
-func (p *parser) init(filename string, src []byte) {
+func (p *parser) init(fset *token.FileSet, filename string, src []byte) {
p.ErrorVector.Reset()
- p.scanner.Init(filename, src, p, scanner.AllowIllegalChars) // return '@' as token.ILLEGAL w/o error message
- p.next() // initializes pos, tok, lit
+ p.file = p.scanner.Init(fset, filename, src, p, scanner.AllowIllegalChars) // return '@' as token.ILLEGAL w/o error message
+ p.next() // initializes pos, tok, lit
p.packs = make(map[string]string)
p.rules = make(map[string]expr)
}
-func (p *parser) errorExpected(pos token.Position, msg string) {
+func (p *parser) error(pos token.Pos, msg string) {
+ p.Error(p.file.Position(pos), msg)
+}
+
+
+func (p *parser) errorExpected(pos token.Pos, msg string) {
msg = "expected " + msg
- if pos.Offset == p.pos.Offset {
+ if pos == p.pos {
// the error happened at the current position;
// make the error message more specific
msg += ", found '" + p.tok.String() + "'"
@@ -58,11 +64,11 @@ func (p *parser) errorExpected(pos token.Position, msg string) {
msg += " " + string(p.lit)
}
}
- p.Error(pos, msg)
+ p.error(pos, msg)
}
-func (p *parser) expect(tok token.Token) token.Position {
+func (p *parser) expect(tok token.Token) token.Pos {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, "'"+tok.String()+"'")
@@ -87,7 +93,7 @@ func (p *parser) parseTypeName() (string, bool) {
if importPath, found := p.packs[name]; found {
name = importPath
} else {
- p.Error(pos, "package not declared: "+name)
+ p.error(pos, "package not declared: "+name)
}
p.next()
name, isIdent = name+"."+p.parseIdentifier(), false
@@ -303,11 +309,11 @@ func (p *parser) parseFormat() {
// add package declaration
if !isIdent {
- p.Error(pos, "illegal package name: "+name)
+ p.error(pos, "illegal package name: "+name)
} else if _, found := p.packs[name]; !found {
p.packs[name] = importPath
} else {
- p.Error(pos, "package already declared: "+name)
+ p.error(pos, "package already declared: "+name)
}
case token.ASSIGN:
@@ -319,7 +325,7 @@ func (p *parser) parseFormat() {
if _, found := p.rules[name]; !found {
p.rules[name] = x
} else {
- p.Error(pos, "format rule already declared: "+name)
+ p.error(pos, "format rule already declared: "+name)
}
default:
@@ -358,10 +364,10 @@ func remap(p *parser, name string) string {
// there are no errors, the result is a Format and the error is nil.
// Otherwise the format is nil and a non-empty ErrorList is returned.
//
-func Parse(filename string, src []byte, fmap FormatterMap) (Format, os.Error) {
+func Parse(fset *token.FileSet, filename string, src []byte, fmap FormatterMap) (Format, os.Error) {
// parse source
var p parser
- p.init(filename, src)
+ p.init(fset, filename, src)
p.parseFormat()
// add custom formatters, if any