comparison mercurial/templater.py @ 25676:ec9c258e666d stable

templater: parse \"...\" as string for 2.9.2-3.4 compatibility (issue4733) As of Mercurial 3.4, there were several syntax rules to process nested template strings. Unfortunately, they were inconsistent and conflicted each other. a. buildmap() rule - template string is _parsed_ as string, and parsed as template - <\"> is not allowed in nested template: {xs % "{f(\"{x}\")}"} -> parse error - template escaping <\{> is handled consistently: {xs % "\{x}"} -> escaped b. _evalifliteral() rule - template string is _interpreted_ as string, and parsed as template in crafted environment to avoid double processing of escape sequences - <\"> is allowed in nested template: {if(x, "{f(\"{x}\")}")} - <\{> and escape sequences in string literal in nested template are not handled well c. pad() rule - template string is first interpreted as string, and parsed as template, which means escape sequences are processed twice - <\"> is allowed in nested template: {pad("{xs % \"{x}\"}', 10)} Because of the issue of template escaping, issue4714, 7298da81f5a9 (in stable) unified the rule (b) to (a). Then, 576d6c74784b (in default) unified the rule (c) to (b) = (a). But they disabled the following syntax that was somewhat considered valid. {if(rev, "{if(rev, \"{rev}\")}")} {pad("{files % \"{file}\"}", 10)} So, this patch introduces \"...\" literal to work around the escaped-quoted nested template strings. Because this parsing rule exists only for the backward compatibility, it is designed to copy the behavior of old _evalifliteral() as possible. Future patches will introduce a better parsing rule similar to a command substitution of POSIX shells or a string interpolation of Ruby, where extra escapes won't be necessary at all. {pad("{files % "{file}"}", 10)} ~~~~~~~~~~~~~~~~~~ parsed as a template, not as a string Because <\> character wasn't allowed in a template fragment, this patch won't introduce more breakages. But the syntax of nested templates are interpreted differently by people, there might be unknown issues. So if we want, we could instead remove db7463aa080f, 890845af1ac2 and 7298da81f5a9 from the stable branch as the bug fixed by these patches existed for longer periods. 554d6fcc3c8, "strip single backslash before quotation mark in quoted template", should be superseded by this patch. I'll remove it later.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 25 Jun 2015 22:07:38 +0900
parents 6047b60cdd09
children ce3d4b858420
comparison
equal deleted inserted replaced
25665:dc05a10e1e45 25676:ec9c258e666d
57 yield ('string', program[s:pos], s) 57 yield ('string', program[s:pos], s)
58 break 58 break
59 pos += 1 59 pos += 1
60 else: 60 else:
61 raise error.ParseError(_("unterminated string"), s) 61 raise error.ParseError(_("unterminated string"), s)
62 elif (c == '\\' and program[pos:pos + 2] in (r"\'", r'\"')
63 or c == 'r' and program[pos:pos + 3] in (r"r\'", r'r\"')):
64 # handle escaped quoted strings for compatibility with 2.9.2-3.4,
65 # where some of nested templates were preprocessed as strings and
66 # then compiled. therefore, \"...\" was allowed. (issue4733)
67 #
68 # processing flow of _evalifliteral() at 5ab28a2e9962:
69 # outer template string -> stringify() -> compiletemplate()
70 # ------------------------ ------------ ------------------
71 # {f("\\\\ {g(\"\\\"\")}"} \\ {g("\"")} [r'\\', {g("\"")}]
72 # ~~~~~~~~
73 # escaped quoted string
74 if c == 'r':
75 pos += 1
76 token = 'rawstring'
77 else:
78 token = 'string'
79 quote = program[pos:pos + 2]
80 s = pos = pos + 2
81 while pos < end: # find closing escaped quote
82 if program.startswith('\\\\\\', pos, end):
83 pos += 4 # skip over double escaped characters
84 continue
85 if program.startswith(quote, pos, end):
86 try:
87 # interpret as if it were a part of an outer string
88 data = program[s:pos].decode('string-escape')
89 except ValueError: # unbalanced escapes
90 raise error.ParseError(_("syntax error"), s)
91 yield (token, data, s)
92 pos += 1
93 break
94 pos += 1
95 else:
96 raise error.ParseError(_("unterminated string"), s)
62 elif c.isalnum() or c in '_': 97 elif c.isalnum() or c in '_':
63 s = pos 98 s = pos
64 pos += 1 99 pos += 1
65 while pos < end: # find end of symbol 100 while pos < end: # find end of symbol
66 d = program[pos] 101 d = program[pos]