code review 7191045: text/template/parse: move itemType.String from test cod... (issue 7191045)

15 views
Skip to first unread message

dsym...@golang.org

unread,
Jan 22, 2013, 7:53:32 PM1/22/13
to golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golan...@googlegroups.com,

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
text/template/parse: move itemType.String from test code into real code.

itemType is used in a few places to report errors to users of
text/template.
Without String you get errors such as
template: :96: expected %!s(parse.itemType=14) in else; got "/"
With String you get
template: :96: expected right delim in else; got "/"

Please review this at https://codereview.appspot.com/7191045/

Affected files:
M src/pkg/text/template/parse/lex.go
M src/pkg/text/template/parse/lex_test.go


Index: src/pkg/text/template/parse/lex.go
===================================================================
--- a/src/pkg/text/template/parse/lex.go
+++ b/src/pkg/text/template/parse/lex.go
@@ -81,6 +81,48 @@
"with": itemWith,
}

+// Make the types prettyprint.
+var itemName = map[itemType]string{
+ itemError: "error",
+ itemBool: "bool",
+ itemChar: "char",
+ itemCharConstant: "charconst",
+ itemComplex: "complex",
+ itemColonEquals: ":=",
+ itemEOF: "EOF",
+ itemField: "field",
+ itemIdentifier: "identifier",
+ itemLeftDelim: "left delim",
+ itemLeftParen: "(",
+ itemNumber: "number",
+ itemPipe: "pipe",
+ itemRawString: "raw string",
+ itemRightDelim: "right delim",
+ itemRightParen: ")",
+ itemSpace: "space",
+ itemString: "string",
+ itemVariable: "variable",
+
+ // keywords
+ itemDot: ".",
+ itemDefine: "define",
+ itemElse: "else",
+ itemIf: "if",
+ itemEnd: "end",
+ itemNil: "nil",
+ itemRange: "range",
+ itemTemplate: "template",
+ itemWith: "with",
+}
+
+func (i itemType) String() string {
+ s := itemName[i]
+ if s == "" {
+ return fmt.Sprintf("item%d", int(i))
+ }
+ return s
+}
+
const eof = -1

// stateFn represents the state of the scanner as a function that returns
the next state.
Index: src/pkg/text/template/parse/lex_test.go
===================================================================
--- a/src/pkg/text/template/parse/lex_test.go
+++ b/src/pkg/text/template/parse/lex_test.go
@@ -5,52 +5,9 @@
package parse

import (
- "fmt"
"testing"
)

-// Make the types prettyprint.
-var itemName = map[itemType]string{
- itemError: "error",
- itemBool: "bool",
- itemChar: "char",
- itemCharConstant: "charconst",
- itemComplex: "complex",
- itemColonEquals: ":=",
- itemEOF: "EOF",
- itemField: "field",
- itemIdentifier: "identifier",
- itemLeftDelim: "left delim",
- itemLeftParen: "(",
- itemNumber: "number",
- itemPipe: "pipe",
- itemRawString: "raw string",
- itemRightDelim: "right delim",
- itemRightParen: ")",
- itemSpace: "space",
- itemString: "string",
- itemVariable: "variable",
-
- // keywords
- itemDot: ".",
- itemDefine: "define",
- itemElse: "else",
- itemIf: "if",
- itemEnd: "end",
- itemNil: "nil",
- itemRange: "range",
- itemTemplate: "template",
- itemWith: "with",
-}
-
-func (i itemType) String() string {
- s := itemName[i]
- if s == "" {
- return fmt.Sprintf("item%d", int(i))
- }
- return s
-}
-
type lexTest struct {
name string
input string


David Symonds

unread,
Jan 22, 2013, 7:55:20 PM1/22/13
to golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
This is purely a copy-and-paste, plus the adjustment of the imports.

Russ Cox

unread,
Jan 22, 2013, 9:49:48 PM1/22/13
to David Symonds, golang-dev, re...@codereview-hr.appspotmail.com
I believe CL 7128054 already fixed the problem.

David Symonds

unread,
Jan 22, 2013, 9:55:20 PM1/22/13
to Russ Cox, golang-dev, re...@codereview-hr.appspotmail.com
Nuts. You're right. My client was stale.

dsym...@golang.org

unread,
Jan 22, 2013, 9:55:41 PM1/22/13
to golan...@googlegroups.com, r...@golang.org, re...@codereview-hr.appspotmail.com
Reply all
Reply to author
Forward
0 new messages