-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.py
635 lines (507 loc) · 21.4 KB
/
tmp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# -*- coding: utf-8 -*-
"""
tmp - tiberius' Text Markup Processor
-----------------------------------------------------------------------
Comment : Main source file (parser, built-in markups)
Version : $Id: tmp.py 85 2006-04-24 16:55:44Z tiberius $
Copyright : (C) 2005 by Tiberius Teng <[email protected]>
-----------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or any
later version.
-----------------------------------------------------------------------
"""
import copy, glob, sys, tpg
from pprint import pprint
try:
from IPython.Debugger import Pdb
def trace():
Pdb().set_trace()
except:
import pdb
def trace():
pdb.set_trace()
class MarkupError(tpg.Error):
pass
class TMP(tpg.Parser):
r"""
set lexer = ContextSensitiveLexer
# allows \[ \\ \]
token normal_text '(\\\[|[^\\\]]|\\\\|\\\])+' $lambda b: b.replace('\\\\', '\\').replace('\\]', ']').replace('\\[', '[')
# allow anything except \ ( ) { } [ ] <spaces>
token markup_name '[^\\\(\)\{\}\][\s]+';
token old_markup_param '([^,\\)]|\\\\|\\,|\\\))*';
token identifier '[a-zA-Z_][a-zA-Z0-9_]*';
# allow anything except ; \ but allow \; \\
token decl_literal '([^,\\\)]|\\,|\\\\|\\\))+' $lambda b: b.replace('\\;', ';').replace('\\\\', '\\').replace('\)', ')')
# allows \; \\ \}
token param_literal '([^,\\\}]|\\,|\\\\|\\})+' $lambda b: b.replace('\\,', ',').replace('\\\\', '\\').replace('\\}', '}')
token verbatim_literal '([^\\\]]|\\\])+' $lambda b: b.replace('\\]', ']')
token esc_r_bracket '\](?!\])';
token comma ',';
token colon ':';
token equals '=';
token back_slash '\\';
token l_paren '\(';
token r_paren '\)';
token l_bracket '\[';
token r_bracket '\]';
token l_curly '\{';
token r_curly '\}';
token spaces '\s*';
token define_cmd 'define';
token verbatim_cmd 'v';
# $self.parseTree(r)$
START/$self.parseTree(r)$ ->
$r = []
( normal_text/v $r += [[0, v]]
| verbatim/v $r += [[0, v]]
| markup_call/v $r += v
| r_bracket/v $r += [[0, v]]
)*
;
short_content/r ->
$r = []
( normal_text/v $r += [[0, v]]
| verbatim/v $r += [[0, v]]
| markup_call/v $r += v
)*
;
long_content/r ->
$r = []
( normal_text/v $r += [[0, v]]
| esc_r_bracket/v $r += [[0, v]]
| verbatim/v $r += [[0, v]]
| markup_call/v $r += v
)*
;
arg_content/r ->
$r = []
( param_literal/v $r += [[0, v]]
| markup_call/v $r += v
)*
;
markup_param_decl/r ->
spaces
identifier/i $r = [i, None, None]
spaces
(
(
colon
spaces
identifier/t $r[1] = t # type converter
spaces
)?
(
equals
spaces
decl_literal/d $r[2] = d # default value
spaces
)?
)?
;
markup_call/$[[t, name, param, body, line, column]]$ ->
$t = 0
back_slash $line = self.line(); column = self.column()
(
# markup signature for definition
$t = -3
define_cmd/n $name = n
l_curly
markup_name $param = [markup_name, []]
l_paren
(
markup_param_decl/v $param[1] += [v]
(
comma markup_param_decl/v $param[1] += [v]
)*
)?
spaces
r_paren
r_curly
|
markup_name/n $name = n
(
# old calling method
$t = -1
l_paren
old_markup_param/p $param = [p]
(
comma
old_markup_param/p $param += [p]
)*
r_paren/v
|
# new calling method
$t = -2
l_curly
arg_content/p $param = [p]
(
comma
arg_content/p $param += [p]
)*
r_curly
|
$param = ''; t = -1
)
)
(
l_bracket l_bracket
long_content/s $body = s
r_bracket r_bracket
|
l_bracket
short_content/s $body = s
r_bracket
|
verbatim/v $body = [[0, v]]
|
markup_call/m $body = m
|
$body = []
)
;
verbatim/r ->
back_slash verbatim_cmd l_bracket l_bracket
verbatim_content/v $r = v
r_bracket r_bracket
;
verbatim_content/r ->
$r = ''
( verbatim_literal/v $r += v
| esc_r_bracket/v $r += v
| back_slash/v $r += v
)*
;
"""
def __init__(self, fileName):
tpg.Parser.__init__(self)
# defined markups (commands)
# format: markup_name: [func, signature, required_params_count]
# signature: [typeConverter, (typeConverter, defaultValue), ... ]
# will generate metadata for built-in commands later
self.markups = {
'ifnval': self.ifnval,
'ifval': self.ifval,
'include': self.includeFile,
'strip': self.strip,
'use': self.useMarkupPack,
}
# defined macros (lazy-evaluated strings)
# format: macro_name: [file, line, column, signature, content]
# signature: [[name, type, default], ...]
self.macros = {}
self.fileName = fileName
self.fileNames = [fileName]
# prepare metadata for built-in commands
for k in self.markups:
self.markups[k] = [self.markups[k], eval(self.markups[k].im_func.func_doc), 1]
self.markups['strip'][2] = 0
## Built-in Markups
###################
def includeFile(self, line, column, args, **kw):
"(str,)"
try:
f = file(args[0]).read()
except:
raise MarkupError((line, column), 'Error opening file "%s" for include' % args[0])
x = TMP(args[0])
x.fileNames = self.fileNames
x.markups.update(self.markups)
self.fileNames.append(args[0])
ret = x(f)
self.fileNames.pop()
self.macros.update(x.macros)
return ret
def useMarkupPack(self, args, line, column, **kw):
"(str,)"
loadedMarkups = __import__(args[0]).__dict__
reload(__import__(args[0]))
if callable(loadedMarkups.get('reset')):
loadedMarkups['reset']()
# Pre-process markup definition file
for k in loadedMarkups:
n = loadedMarkups[k]
# Check for markup name duplications
if k in self.markups:
raise MarkupError((line, column), 'Markup name "%s" conflict with built-in or previously loaded markup' % args[0])
# Try converting callable objects into markup
# (check parameter-specification strings, calcuate mandatory parameter counts)
if callable(n):
try:
# Get & check spec string
markupParamSpec = eval(n.func_doc)
if type(markupParamSpec) != tuple: continue
# Count mandatory parameters
reqParams = 0; optional = 0; valid = 1
for p in markupParamSpec:
if (optional and type(p) != tuple):
raise MarkupError((line, column), 'Markup "%s" with illegal parameter spec (non-default after default)' % \
args[0])
#valid = 0
#break
elif ((not optional) and type(p) != tuple):
reqParams += 1
elif ((not optional) and type(p) == tuple):
optional = 1
if valid:
loadedMarkups[k] = [loadedMarkups[k], markupParamSpec, reqParams]
except:
pass
self.markups.update(loadedMarkups)
def strip(self, body, **kw):
"()"
l = len(body)
for i in [xrange(l), xrange(l-1, -1, -1)]:
for j in i:
body[j][1] = body[j][1].strip()
if body[j][1]: break
return body
def ifval(self, params, body, **kw):
"((str,''),)"
if params[0]:
return body
else:
return []
def ifnval(self, params, body, **kw):
"((str,''),)"
if not params[0]:
return body
else:
return []
def expandMacro(self, macroName, params, body, line, column):
mFile, mLine, mColumn, mSignature, mContent = self.macros[macroName]
# avoid modifying original subtree
mContent = copy.deepcopy(mContent)
#print 'calling (macro) %s' % macroName
#pprint(params)
# parameters check
paramCount = len(params)
if paramCount < mSignature[1]:
raise MarkupError((line, column), 'Called macro "%s" with too few parameters: expected %s, got %s' % \
(macroName, mSignature[1], paramCount))
if paramCount > len(mSignature[0]):
raise MarkupError((line, column), 'Too many arguments for macro "%s"; at most %s, got %s' % (macroName, len(mSignature[0]), paramCount))
# Do type conversion
paramPos = 0; realParams = {'body': body}
for s in mSignature[0]:
try:
if s[2] != None:
if (paramPos >= paramCount) or (not params[paramPos]):
realParams[s[0]] = s[2]
else:
realParams[s[0]] = params[paramPos]
else:
realParams[s[0]] = params[paramPos]
except:
raise MarkupError((line, column), 'Error converting argument "%s" of macro "%s"; was trying to convert %s into %s' % \
(s[0], macroName, repr(params[paramPos]), repr(s[1])))
paramPos += 1
return self.expandTreeMacros(mContent, realParams, '%s@%s:%s:%s' % (macroName, mFile, mLine, mColumn))
def callMarkup(self, markupName, args, body, line, column):
"""Calls a markup, performing argument sanity checks.
"""
# Get the function object of specified markup
try:
markup = self.markups[markupName]
except KeyError:
raise MarkupError((line, column), 'Markup "%s" not found' % markupName)
#return []
if callable(markup[0]):
# Handle callable-type markups (method instance, function, class, ...)
#######################################################################
# Get markup's parameter specification
try:
meta = self.markups[markupName][1:3]
except KeyError:
raise MarkupError((line, column), 'Callable "%s" is not available as a markup (no parameter spec found)' % \
markupName)
spec = meta[0]
argCount = len(args)
args = map(lambda x: x.replace('\\,', ',').replace('\\)', ')').replace('\\\\', '\\'), args)
# Check parameter numbers
if argCount < meta[1]:
raise MarkupError((line, column), 'Called markup "%s" with too few parameters: expected %s, got %s' % \
(markupName, meta[1], len(args)))
# Do type conversion
argPos = 0; realArgs = []
for x in spec:
try:
if type(x) == tuple:
if (argPos >= argCount) or (not args[argPos]):
realArgs.append(x[1])
else:
realArgs.append(x[0](args[argPos]))
else:
realArgs.append(x(args[argPos]))
except:
raise MarkupError((line, column), 'Error converting parameter %s of markup "%s"; was trying to convert %s into %s' % \
(x, markupName, repr(args[argPos]), repr(x)))
argPos += 1
# Pass additional parameters as-is
# (to let markup implement their own, possibly more complex, checks)
while argPos < argCount:
realArgs.append(args[argPos])
argPos += 1
ret = markup[0](**{'args': realArgs, 'body': body, 'fileName': self.fileName, 'params': realArgs, 'parser': self, 'line': line, 'column': column})
if ret == None:
return []
else:
return ret
elif type(markup) == str:
# Simple string substitution markup
return [[1, markup]] + body
def defineMacro(self, macroName, params, body, line, column):
"""Defines a lazy-evaluated macro.
"""
if macroName != 'define':
raise MarkupError((line, column), 'Macro signature is only valid for \\define.')
macroName = params[0]
signature = params[1] # format is [name, type, default]
if macroName in self.macros:
p = self.macros[macroName]
raise MarkupError((line, column), 'Redefinition of macro "%s": Previous defined at %s:%s:%s' % (macroName, p[0], p[1], p[2]))
# Count mandatory parameters and evaluate converters/default values
req = 0; optional = 0; valid = 1
for p in signature:
if (optional and p[2] == None):
raise MarkupError((line, column), 'Macro "%s" with illegal parameter spec (non-default after default)' % macroName)
elif ((not optional) and p[2] == None):
req += 1
elif ((not optional) and p[2] != None):
optional = 1
try:
if p[1]: p[1] = eval(p[1])
if p[2]: p[2] = eval(p[2])
except:
raise MarkupError((line, column), 'Argument "%s" of macro "%s" is invalid' % (p[0], macroName))
self.macros[macroName] = [self.fileName, line, column, [signature, req], body]
def expandTreeMacros(self, l, params={}, macro=None):
"""First pass - Including files, defining macros, expanding macros.
"""
r = []
for node in l:
if node[0] == -1:
# old calling syntax
if node[1] in params:
# expanding parameter
if not (node[2] == '' and node[3] == []):
raise MarkupError((node[4], node[5]), 'Expanding parameter "%s" of macro "%s" with arguments/contents -- possibly misuse' %
(node[1], macro))
v = params[node[1]]
if type(v) == list:
r += self.expandTreeMacros(v, params, macro)
else:
r += [[0, str(params[node[1]])]]
else:
if node[1] in self.macros:
# a macro to expand
r += self.expandMacro(node[1], node[2], self.expandTreeMacros(node[3], params, macro), node[4], node[5])
else:
# calling markup - not now, excluding 'include'
if node[1] == 'include':
r += self.callMarkup(node[1], node[2], self.expandTreeMacros(node[3], params, macro), node[4], node[5])
else:
r += [[-1, node[1], node[2], self.expandTreeMacros(node[3], params, macro), node[4], node[5]]]
elif node[0] == -2:
# new calling syntax
if node[1] == 'define' and len(node[2]) == 1 and len(node[2][0]) == 1 and node[2][0][0][0] == 0:
# defining a new macro (without parameters)
self.defineMacro(node[1], [node[2][0][0][1], []], node[3], node[4], node[5])
else:
if node[1] in self.macros:
# a macro to expand
#args = map(lambda x: ''.join(map(lambda y: y[1], self.expandTreeMacros(x, params, macro))), node[2])
#args = map(lambda x: self.expandTreeMacros(x, params, macro), node[2])
args = [self.expandTreeMacros(x, params, macro) for x in node[2]]
r += self.expandMacro(node[1], args, self.expandTreeMacros(node[3], params, macro), node[4], node[5])
else:
# won't actually call markups now, only expands body, except 'include'
# but since new syntax allows markup/macros in arguments, so resolve them now
#args = map(lambda x: self.expandTreeMacros(x, params, macro), node[2])
args = [self.expandTreeMacros(x, params, macro) for x in node[2]]
if node[1] == 'include':
#args = map(lambda x: ''.join(map(lambda y: y[1], x)), args)
args = [''.join([y[1] for y in x]) for x in args]
r += self.callMarkup(node[1], args, self.expandTreeMacros(node[3], params, macro), node[4], node[5])
else:
r += [[-2, node[1], args, self.expandTreeMacros(node[3], params, macro), node[4], node[5]]]
elif node[0] == -3:
# defining a new macro (with parameters)
self.defineMacro(node[1], node[2], node[3], node[4], node[5])
else:
# text/metadata node
r += [node]
return r
def callTreeMarkups(self, l):
"""Second pass - execute all markups.
"""
r = []
for node in l:
if node[0] == -1:
# call markup (old way)
#print '(old) %s' % node[2]
r += self.callMarkup(node[1], node[2], self.callTreeMarkups(node[3]), node[4], node[5])
elif node[0] == -2:
# call markup (new way)
args = map(lambda x: ''.join(map(lambda y: y[1], self.callTreeMarkups(x))), node[2])
r += self.callMarkup(node[1], args, self.callTreeMarkups(node[3]), node[4], node[5])
else:
# text/metadata node
r += [node]
return r
def parseTree(self, l):
#pprint(l, file('%s.0.txt' % self.fileName, 'w'))
l = self.expandTreeMacros(l)
#pprint(l, file('%s.1.txt' % self.fileName, 'w'))
l = self.callTreeMarkups(l)
#pprint(l, file('%s.2.txt' % self.fileName, 'w'))
return l
#######################################################################
## Command-line execution handler
def filterMeta(x):
if x[0] < 2:
return x[1]
else:
return ''
if __name__ == '__main__':
import os
import translator_config
try:
import psyco
psyco.profile(watermark=0.02)
print 'Psyco enabled.'
except:
pass
translator_config.snapshot = 1
print sys.argv
if len(sys.argv) < 2:
print "Usage: %s <infile> [<infile2> ...]" % sys.argv[0]
sys.exit(1)
if len(sys.argv) == 2:
files = glob.glob(sys.argv[1])
else:
files = sys.argv[1:]
for fileName in files:
try:
tmp = TMP(fileName)
tmp.verbose = 2
r = tmp(file(fileName).read())
#print '<doc>' + ''.join(map(xmltag, r)) + '</doc>'
#print ''.join(map(filterMeta, r)).strip()
file(fileName[:fileName.rfind('.')] + '.htm', 'w').write(''.join(map(filterMeta, r)).strip())
print 'Processed %s' % fileName
except tpg.Error, x:
srcPath = os.path.abspath(tmp.fileNames[-1])
src = file(srcPath).read()
if (x.column > 40):
prefix = '... '
start = x.column - 20
else:
prefix = ''
start = 0
print 'while processing %s:' % srcPath
print '%s\n' % str(x)
print '%s%s' % (prefix, src.split('\n')[x.line-1][start:start+65])
print '%s%s^\n' % (prefix, ' ' * (x.column - start - 1))