comparison mercurial/templater.py @ 25654:af329a84310c

parser: accept iterator of tokens instead of tokenizer function and program This can simplify the interface of parse() function. Our tokenizer tends to have optional arguments other than the message to be parsed. Before this patch, the "lookup" argument existed only for the revset, and the templater had to pack [program, start, end] to be passed to its tokenizer.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 21 Jun 2015 00:49:26 +0900
parents 695b93a79d17
children 328739ea70c3
comparison
equal deleted inserted replaced
25653:9d1e04f5dca7 25654:af329a84310c
25 "string": (0, ("template",), None), 25 "string": (0, ("template",), None),
26 "rawstring": (0, ("rawstring",), None), 26 "rawstring": (0, ("rawstring",), None),
27 "end": (0, None, None), 27 "end": (0, None, None),
28 } 28 }
29 29
30 def tokenizer(data): 30 def tokenize(program, start, end):
31 program, start, end = data
32 pos = start 31 pos = start
33 while pos < end: 32 while pos < end:
34 c = program[pos] 33 c = program[pos]
35 if c.isspace(): # skip inter-token whitespace 34 if c.isspace(): # skip inter-token whitespace
36 pass 35 pass
94 yield ('end', None, pos) 93 yield ('end', None, pos)
95 94
96 def compiletemplate(tmpl, context): 95 def compiletemplate(tmpl, context):
97 parsed = [] 96 parsed = []
98 pos, stop = 0, len(tmpl) 97 pos, stop = 0, len(tmpl)
99 p = parser.parser(tokenizer, elements) 98 p = parser.parser(elements)
100 while pos < stop: 99 while pos < stop:
101 n = tmpl.find('{', pos) 100 n = tmpl.find('{', pos)
102 if n < 0: 101 if n < 0:
103 parsed.append(('string', tmpl[pos:])) 102 parsed.append(('string', tmpl[pos:]))
104 break 103 break
109 pos = n + 1 108 pos = n + 1
110 continue 109 continue
111 if n > pos: 110 if n > pos:
112 parsed.append(('string', tmpl[pos:n])) 111 parsed.append(('string', tmpl[pos:n]))
113 112
114 pd = [tmpl, n + 1, stop] 113 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop))
115 parseres, pos = p.parse(pd)
116 parsed.append(parseres) 114 parsed.append(parseres)
117 115
118 return [compileexp(e, context, methods) for e in parsed] 116 return [compileexp(e, context, methods) for e in parsed]
119 117
120 def compileexp(exp, context, curmethods): 118 def compileexp(exp, context, curmethods):