##// END OF EJS Templates
templater: unify "string" and "rawstring"...
Yuya Nishihara -
r25785:f976b7dc default
parent child Browse files
Show More
@@ -22,7 +22,7 b' elements = {'
22 22 ")": (0, None, None),
23 23 "integer": (0, ("integer",), None),
24 24 "symbol": (0, ("symbol",), None),
25 "rawstring": (0, ("rawstring",), None),
25 "string": (0, ("string",), None),
26 26 "template": (0, ("template",), None),
27 27 "end": (0, None, None),
28 28 }
@@ -50,7 +50,7 b' def tokenize(program, start, end):'
50 50 pos += 2
51 51 continue
52 52 if d == c:
53 yield ('rawstring', program[s:pos], s)
53 yield ('string', program[s:pos], s)
54 54 break
55 55 pos += 1
56 56 else:
@@ -83,7 +83,7 b' def tokenize(program, start, end):'
83 83 # escaped quoted string
84 84 if c == 'r':
85 85 pos += 1
86 token = 'rawstring'
86 token = 'string'
87 87 else:
88 88 token = 'template'
89 89 quote = program[pos:pos + 2]
@@ -136,7 +136,7 b' def _parsetemplate(tmpl, start, stop, qu'
136 136 >>> _parsetemplate(r'foo\"bar"baz', 0, 12, quote='"')
137 137 ([('string', 'foo"'), ('string', 'bar')], 9)
138 138 >>> _parsetemplate(r'foo\\"bar', 0, 10, quote='"')
139 ([('string', 'foo\\\\')], 6)
139 ([('string', 'foo\\')], 6)
140 140 """
141 141 parsed = []
142 142 sepchars = '{' + quote
@@ -146,18 +146,19 b' def _parsetemplate(tmpl, start, stop, qu'
146 146 n = min((tmpl.find(c, pos, stop) for c in sepchars),
147 147 key=lambda n: (n < 0, n))
148 148 if n < 0:
149 parsed.append(('string', tmpl[pos:stop]))
149 parsed.append(('string', tmpl[pos:stop].decode('string-escape')))
150 150 pos = stop
151 151 break
152 152 c = tmpl[n]
153 153 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
154 154 if bs % 2 == 1:
155 155 # escaped (e.g. '\{', '\\\{', but not '\\{')
156 parsed.append(('string', (tmpl[pos:n - 1] + c)))
156 parsed.append(('string',
157 tmpl[pos:n - 1].decode('string-escape') + c))
157 158 pos = n + 1
158 159 continue
159 160 if n > pos:
160 parsed.append(('string', tmpl[pos:n]))
161 parsed.append(('string', tmpl[pos:n].decode('string-escape')))
161 162 if c == quote:
162 163 return parsed, n + 1
163 164
@@ -212,9 +213,6 b' def runinteger(context, mapping, data):'
212 213 return int(data)
213 214
214 215 def runstring(context, mapping, data):
215 return data.decode("string-escape")
216
217 def runrawstring(context, mapping, data):
218 216 return data
219 217
220 218 def runsymbol(context, mapping, key):
@@ -659,7 +657,6 b' def word(context, mapping, args):'
659 657 exprmethods = {
660 658 "integer": lambda e, c: (runinteger, e[1]),
661 659 "string": lambda e, c: (runstring, e[1]),
662 "rawstring": lambda e, c: (runrawstring, e[1]),
663 660 "symbol": lambda e, c: (runsymbol, e[1]),
664 661 "template": buildtemplate,
665 662 "group": lambda e, c: compileexp(e[1], c, exprmethods),
General Comments 0
You need to be logged in to leave comments. Login now