##// END OF EJS Templates
templater: tokenize decimal integer literal (issue4638) (BC)...
Yuya Nishihara -
r25002:829faf8a default
parent child Browse files
Show More
@@ -20,6 +20,7 b' elements = {'
20 "|": (5, None, ("|", 5)),
20 "|": (5, None, ("|", 5)),
21 "%": (6, None, ("%", 6)),
21 "%": (6, None, ("%", 6)),
22 ")": (0, None, None),
22 ")": (0, None, None),
23 "integer": (0, ("integer",), None),
23 "symbol": (0, ("symbol",), None),
24 "symbol": (0, ("symbol",), None),
24 "string": (0, ("string",), None),
25 "string": (0, ("string",), None),
25 "rawstring": (0, ("rawstring",), None),
26 "rawstring": (0, ("rawstring",), None),
@@ -59,6 +60,20 b' def tokenizer(data):'
59 pos += 1
60 pos += 1
60 else:
61 else:
61 raise error.ParseError(_("unterminated string"), s)
62 raise error.ParseError(_("unterminated string"), s)
63 elif c.isdigit() or c == '-':
64 s = pos
65 if c == '-': # simply take negate operator as part of integer
66 pos += 1
67 if pos >= end or not program[pos].isdigit():
68 raise error.ParseError(_("integer literal without digits"), s)
69 pos += 1
70 while pos < end:
71 d = program[pos]
72 if not d.isdigit():
73 break
74 pos += 1
75 yield ('integer', program[s:pos], s)
76 pos -= 1
62 elif c.isalnum() or c in '_':
77 elif c.isalnum() or c in '_':
63 s = pos
78 s = pos
64 pos += 1
79 pos += 1
@@ -135,6 +150,9 b' def gettemplate(exp, context):'
135 return context._load(exp[1])
150 return context._load(exp[1])
136 raise error.ParseError(_("expected template specifier"))
151 raise error.ParseError(_("expected template specifier"))
137
152
153 def runinteger(context, mapping, data):
154 return int(data)
155
138 def runstring(context, mapping, data):
156 def runstring(context, mapping, data):
139 return data.decode("string-escape")
157 return data.decode("string-escape")
140
158
@@ -567,6 +585,7 b' def word(context, mapping, args):'
567
585
568 # methods to interpret function arguments or inner expressions (e.g. {_(x)})
586 # methods to interpret function arguments or inner expressions (e.g. {_(x)})
569 exprmethods = {
587 exprmethods = {
588 "integer": lambda e, c: (runinteger, e[1]),
570 "string": lambda e, c: (runstring, e[1]),
589 "string": lambda e, c: (runstring, e[1]),
571 "rawstring": lambda e, c: (runrawstring, e[1]),
590 "rawstring": lambda e, c: (runrawstring, e[1]),
572 "symbol": lambda e, c: (runsymbol, e[1]),
591 "symbol": lambda e, c: (runsymbol, e[1]),
@@ -579,6 +598,7 b' exprmethods = {'
579
598
580 # methods to interpret top-level template (e.g. {x}, {x|_}, {x % "y"})
599 # methods to interpret top-level template (e.g. {x}, {x|_}, {x % "y"})
581 methods = exprmethods.copy()
600 methods = exprmethods.copy()
601 methods["integer"] = exprmethods["symbol"] # '{1}' as variable
582
602
583 funcs = {
603 funcs = {
584 "date": date,
604 "date": date,
@@ -2291,6 +2291,39 b' Test invalid date:'
2291 hg: parse error: date expects a date information
2291 hg: parse error: date expects a date information
2292 [255]
2292 [255]
2293
2293
2294 Test integer literal:
2295
2296 $ hg log -Ra -r0 -T '{(0)}\n'
2297 0
2298 $ hg log -Ra -r0 -T '{(123)}\n'
2299 123
2300 $ hg log -Ra -r0 -T '{(-4)}\n'
2301 -4
2302 $ hg log -Ra -r0 -T '{(-)}\n'
2303 hg: parse error at 2: integer literal without digits
2304 [255]
2305 $ hg log -Ra -r0 -T '{(-a)}\n'
2306 hg: parse error at 2: integer literal without digits
2307 [255]
2308
2309 top-level integer literal is interpreted as symbol (i.e. variable name):
2310
2311 $ hg log -Ra -r0 -T '{1}\n'
2312
2313 $ hg log -Ra -r0 -T '{if("t", "{1}")}\n'
2314
2315 $ hg log -Ra -r0 -T '{1|stringify}\n'
2316
2317
2318 unless explicit symbol is expected:
2319
2320 $ hg log -Ra -r0 -T '{desc|1}\n'
2321 hg: parse error: expected a symbol, got 'integer'
2322 [255]
2323 $ hg log -Ra -r0 -T '{1()}\n'
2324 hg: parse error: expected a symbol, got 'integer'
2325 [255]
2326
2294 Test string escaping:
2327 Test string escaping:
2295
2328
2296 $ hg log -R latesttag -r 0 --template '>\n<>\\n<{if(rev, "[>\n<>\\n<]")}>\n<>\\n<\n'
2329 $ hg log -R latesttag -r 0 --template '>\n<>\\n<{if(rev, "[>\n<>\\n<]")}>\n<>\\n<\n'
@@ -2737,8 +2770,13 b' Test word error messages for not enough '
2737 hg: parse error: word expects two or three arguments, got 7
2770 hg: parse error: word expects two or three arguments, got 7
2738 [255]
2771 [255]
2739
2772
2773 Test word for integer literal
2774
2775 $ hg log -R a --template "{word(2, desc)}\n" -r0
2776 line
2777
2740 Test word for invalid numbers
2778 Test word for invalid numbers
2741
2779
2742 $ hg log -Gv -R a --template "{word(2, desc)}"
2780 $ hg log -Gv -R a --template "{word('a', desc)}"
2743 hg: parse error: Use strings like '3' for numbers passed to word function
2781 hg: parse error: Use strings like '3' for numbers passed to word function
2744 [255]
2782 [255]
General Comments 0
You need to be logged in to leave comments. Login now