summaryrefslogtreecommitdiff
path: root/src/text/template/parse/lex_test.go
Commit message (Collapse)AuthorAgeFilesLines
* text/template: allow newlines inside action delimitersRuss Cox2020-10-121-1/+1
| | | | | | | | | | | | | | | | | | | | | This allows multiline constructs like: {{"hello" | printf}} Now that unclosed actions can span multiple lines, track and report the start of the action when reporting errors. Also clean up a few "unexpected <error message>" to be just "<error message>". Fixes #29770. Change-Id: I54c6c016029a8328b7902a4b6d85eab713ec3285 Reviewed-on: https://go-review.googlesource.com/c/go/+/254257 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* text/template: add CommentNode to template parse treeAriel Mashraki2020-08-281-2/+5
| | | | | | | | | | Fixes #34652 Change-Id: Icf6e3eda593fed826736f34f95a9d66f5450cc98 Reviewed-on: https://go-review.googlesource.com/c/go/+/229398 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
* text/template: accept new number syntaxRuss Cox2019-02-261-1/+11
| | | | | | | | | | | | | | | | | | | | | This CL updates text/template's scanner to accept the new number syntaxes: - Hexadecimal floating-point values. - Digit-separating underscores. - Leading 0b and 0o prefixes. See golang.org/design/19308-number-literals for background. For #12711. For #19308. For #28493. For #29008. Change-Id: I68c16ea35c3f506701063781388de72bafee6b8d Reviewed-on: https://go-review.googlesource.com/c/160248 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* text/template: add variable assignmentsDaniel Martí2018-04-041-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Variables can be declared and shadowing is supported, but modifying existing variables via assignments was not available. This meant that modifying a variable from a nested block was not possible: {{ $v := "init" }} {{ if true }} {{ $v := "changed" }} {{ end }} v: {{ $v }} {{/* "init" */}} Introduce the "=" assignment token, such that one can now do: {{ $v := "init" }} {{ if true }} {{ $v = "changed" }} {{ end }} v: {{ $v }} {{/* "changed" */}} To avoid confusion, rename PipeNode.Decl to PipeNode.Vars, as the variables may not always be declared after this change. Also change a few other names to better reflect the added ambiguity of variables in pipelines. Modifying the text/template/parse package in a backwards incompatible manner is acceptable, given that the package godoc clearly states that it isn't intended for general use. It's the equivalent of an internal package, back when internal packages didn't exist yet. To make the changes to the parse package sit well with the cmd/api test, update except.txt with the changes that we aren't worried about. Fixes #10608. Change-Id: I1f83a4297ee093fd45f9993cebb78fc9a9e81295 Reviewed-on: https://go-review.googlesource.com/84480 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* text/template: revert CL 66410 "add break, continue actions in ranges"Ian Lance Taylor2018-02-061-5/+1
| | | | | | | | | | | | | | | | | | | | | | | | | The new break and continue actions do not work in html/template, and fixing them requires thinking about security issues that seem too tricky at this stage of the release. We will try again for 1.11. Original CL description: text/template: add break, continue actions in ranges Adds the two range control actions "break" and "continue". They act the same as the Go keywords break and continue, but are simplified in that only the innermost range statement can be broken out of or continued. Fixes #20531 Updates #20531 Updates #23683 Change-Id: Ia7fd3c409163e3bcb5dc42947ae90b15bdf89853 Reviewed-on: https://go-review.googlesource.com/92155 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* text/template: add break, continue actions in rangesTim Cooper2017-10-171-1/+5
| | | | | | | | | | | | Adds the two range control actions "break" and "continue". They act the same as the Go keywords break and continue, but are simplified in that only the innermost range statement can be broken out of or continued. Fixes #20531 Change-Id: I4412b3bbfd4dadb0ab74ae718e308c1ac7a0a1e9 Reviewed-on: https://go-review.googlesource.com/66410 Reviewed-by: Rob Pike <r@golang.org>
* text/template: fix pos info when trimming newlinesDaniel Martí2017-09-071-2/+24
| | | | | | | | | | | | | | | | | | | The lexer keeps the byte offset and the line for the rune it's currently on. This was simple enough up until whitespace trimming was introduced. With whitespace trimming, we might skip over newlines. In that case, the lexer wasn't properly updating the line counter. Fix it. Also, TestPos now checks that the line is correct too, which it was ignoring before. This was necessary to test this scenario in the lexer. Fixes #21778. Change-Id: I3880f3adf02662eac8f818d5caa6935cca9cb33b Reviewed-on: https://go-review.googlesource.com/61870 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* all: remove some unused parameters in test codeDaniel Martí2017-04-251-2/+2
| | | | | | | | | | | | Mostly unnecessary *testing.T arguments. Found with github.com/mvdan/unparam. Change-Id: Ifb955cb88f2ce8784ee4172f4f94d860fa36ae9a Reviewed-on: https://go-review.googlesource.com/41691 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* text/template: efficient reporting of line numbersRob Pike2016-11-151-126/+133
| | | | | | | | | | | | | | | | Instead of scanning the text to count newlines, which is n², keep track as we go and store the line number in the token. benchmark old ns/op new ns/op delta BenchmarkParseLarge-4 1589721293 38783310 -97.56% Fixes #17851 Change-Id: I231225c61e667535e2ce55cd2facea6d279cc59d Reviewed-on: https://go-review.googlesource.com/33234 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* Revert "text/template: efficient reporting of line numbers"Brad Fitzpatrick2016-11-141-133/+126
| | | | | | | | | | This reverts commit 794fb71d9c1018c4beae1657baca5229e6a02ad0. Reason for revert: submitted without TryBots and it broke all three race builders. Change-Id: I80a1e566616f0ee8fa3529d4eeee04268f8a713b Reviewed-on: https://go-review.googlesource.com/33232 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* text/template: efficient reporting of line numbersRob Pike2016-11-141-126/+133
| | | | | | | | | | | | | | Instead of scanning the text to count newlines, which is n², keep track as we go and store the line number in the token. benchmark old ns/op new ns/op delta BenchmarkParseLarge-4 1589721293 38783310 -97.56% Fixes #17851 Change-Id: Ieaf89a35e371b405ad92e38baa1e3fa98d18cfb4 Reviewed-on: https://go-review.googlesource.com/32923 Reviewed-by: Robert Griesemer <gri@golang.org>
* text/template, html/template: add block keyword and permit template redefinitionAndrew Gerrand2015-09-281-5/+11
| | | | | | | | | | | | | | | | | | This change adds a new "block" keyword that permits the definition of templates inline inside existing templates, and loosens the restriction on template redefinition. Templates may now be redefined, but in the html/template package they may only be redefined before the template is executed (and therefore escaped). The intention is that such inline templates can be redefined by subsequent template definitions, permitting a kind of template "inheritance" or "overlay". (See the example for details.) Fixes #3812 Change-Id: I733cb5332c1c201c235f759cc64333462e70dc27 Reviewed-on: https://go-review.googlesource.com/14005 Reviewed-by: Rob Pike <r@golang.org>
* text/template: provide a way to trim leading and trailing space between actionsRob Pike2015-09-091-1/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | Borrowing a suggestion from the issue listed below, we modify the lexer to trim spaces at the beginning (end) of a block of text if the action immediately before (after) is marked with a minus sign. To avoid parsing/lexing ambiguity, we require an ASCII space between the minus sign and the rest of the action. Thus: {{23 -}} < {{- 45}} produces the output 23<45 All the work is done in the lexer. The modification is invisible to the parser or any outside package (except I guess for noticing some gaps in the input if one tracks error positions). Thus it slips in without worry in text/template and html/template both. Fixes long-requested issue #9969. Change-Id: I3774be650bfa6370cb993d0899aa669c211de7b2 Reviewed-on: https://go-review.googlesource.com/14391 Reviewed-by: Andrew Gerrand <adg@golang.org>
* text/template: shut down lexing goroutine on errorRob Pike2015-05-051-0/+28
| | | | | | | | | | | | | | | | | | | When a parse error occurred, the lexing goroutine would lay idle. It's not likely a problem but if the program is for some reason accepting badly formed data repeatedly, it's wasteful. The solution is easy: Just drain the input on error. We know this will succeed because the input is always a string and is therefore guaranteed finite. With debugging prints in the package tests I've shown this is effective, shutting down 79 goroutines that would otherwise linger, out of 123 total. Fixes #10574. Change-Id: I8aa536e327b219189a7e7f604a116fa562ae1c39 Reviewed-on: https://go-review.googlesource.com/9658 Reviewed-by: Russ Cox <rsc@golang.org>
* text/template: allow newlines in raw quotesRob Pike2015-05-011-13/+16
| | | | | | | | | | | This was disallowed for error-checking reasons but people ask for it, it's easy, and it's clear what it all means. Fixes #7323. Change-Id: I26542f5ac6519e45b335ad789713a4d9e356279b Reviewed-on: https://go-review.googlesource.com/9537 Reviewed-by: Russ Cox <rsc@golang.org>
* build: move package sources from src/pkg to srcRuss Cox2014-09-081-0/+465
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.