aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/text/template/parse/parse.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2012-08-24 12:37:23 -0700
committerRob Pike <r@golang.org>2012-08-24 12:37:23 -0700
commitcc842c738ea9a64570e306cbab37c3e3cf9a35dd (patch)
treed551a023206f14351558585b953835f87d623efc /src/pkg/text/template/parse/parse.go
parent3bd8684facfadb57ba649cae6b067e3a3ecb1208 (diff)
downloadgo-cc842c738ea9a64570e306cbab37c3e3cf9a35dd.tar.xz
text/template: allow grouping of pipelines using parentheses
Based on work by Russ Cox. From his CL: This is generally useful but especially helpful when trying to use the built-in boolean operators. It lets you write: {{if not (f 1)}} foo {{end}} {{if and (f 1) (g 2)}} bar {{end}} {{if or (f 1) (g 2)}} quux {{end}} instead of {{if f 1 | not}} foo {{end}} {{if f 1}}{{if g 2}} bar {{end}}{{end}} {{$do := 0}}{{if f 1}}{{$do := 1}}{{else if g 2}}{{$do := 1}}{{end}}{{if $do}} quux {{end}} The result can be a bit LISPy but the benefit in expressiveness and readability for such a small change justifies it. I believe no changes are required to html/template. Fixes #3276. R=golang-dev, adg, rogpeppe, minux.ma CC=golang-dev https://golang.org/cl/6482056
Diffstat (limited to 'src/pkg/text/template/parse/parse.go')
-rw-r--r--src/pkg/text/template/parse/parse.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/pkg/text/template/parse/parse.go b/src/pkg/text/template/parse/parse.go
index 7ddb6fff1e..6dc2f0fb78 100644
--- a/src/pkg/text/template/parse/parse.go
+++ b/src/pkg/text/template/parse/parse.go
@@ -349,10 +349,13 @@ func (t *Tree) pipeline(context string) (pipe *PipeNode) {
pipe = newPipeline(t.lex.lineNumber(), decl)
for {
switch token := t.next(); token.typ {
- case itemRightDelim:
+ case itemRightDelim, itemRightParen:
if len(pipe.Cmds) == 0 {
t.errorf("missing value for %s", context)
}
+ if token.typ == itemRightParen {
+ t.backup()
+ }
return
case itemBool, itemCharConstant, itemComplex, itemDot, itemField, itemIdentifier,
itemNumber, itemNil, itemRawString, itemString, itemVariable:
@@ -456,11 +459,17 @@ func (t *Tree) command() *CommandNode {
Loop:
for {
switch token := t.next(); token.typ {
- case itemRightDelim:
+ case itemRightDelim, itemRightParen:
t.backup()
break Loop
case itemPipe:
break Loop
+ case itemLeftParen:
+ p := t.pipeline("parenthesized expression")
+ if t.next().typ != itemRightParen {
+ t.errorf("missing right paren in parenthesized expression")
+ }
+ cmd.append(p)
case itemError:
t.errorf("%s", token.val)
case itemIdentifier: