comparison markup/template.py @ 134:df44110ca91d

* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though). * Evaluation errors in expressions now include the original expression code in the traceback.
author cmlenz
date Sun, 06 Aug 2006 18:07:21 +0000
parents b9a0031d4bbb
children 54131cbb91a5
comparison
equal deleted inserted replaced
133:b9a0031d4bbb 134:df44110ca91d
810 @param text: the text to parse 810 @param text: the text to parse
811 @param lineno: the line number at which the text was found (optional) 811 @param lineno: the line number at which the text was found (optional)
812 @param offset: the column number at which the text starts in the source 812 @param offset: the column number at which the text starts in the source
813 (optional) 813 (optional)
814 """ 814 """
815 def _interpolate(text, patterns): 815 def _interpolate(text, patterns, filename=filename, lineno=lineno,
816 offset=offset):
816 for idx, group in enumerate(patterns.pop(0).split(text)): 817 for idx, group in enumerate(patterns.pop(0).split(text)):
817 if idx % 2: 818 if idx % 2:
818 try: 819 try:
819 yield EXPR, Expression(group, filename, lineno), \ 820 yield EXPR, Expression(group, filename, lineno), \
820 (lineno, offset) 821 (filename, lineno, offset)
821 except SyntaxError, err: 822 except SyntaxError, err:
822 raise TemplateSyntaxError(err, filename, lineno, 823 raise TemplateSyntaxError(err, filename, lineno,
823 offset + (err.offset or 0)) 824 offset + (err.offset or 0))
824 elif group: 825 elif group:
825 if patterns: 826 if patterns:
826 for result in _interpolate(group, patterns[:]): 827 for result in _interpolate(group, patterns[:]):
827 yield result 828 yield result
828 else: 829 else:
829 yield TEXT, group.replace('$$', '$'), (filename, lineno, 830 yield TEXT, group.replace('$$', '$'), \
830 offset) 831 (filename, lineno, offset)
832 if '\n' in group:
833 lines = group.splitlines()
834 lineno += len(lines) - 1
835 offset += len(lines[-1])
836 else:
837 offset += len(group)
831 return _interpolate(text, [cls._FULL_EXPR_RE, cls._SHORT_EXPR_RE]) 838 return _interpolate(text, [cls._FULL_EXPR_RE, cls._SHORT_EXPR_RE])
832 _interpolate = classmethod(_interpolate) 839 _interpolate = classmethod(_interpolate)
833 840
834 def generate(self, ctxt=None): 841 def generate(self, ctxt=None):
835 """Apply the template to the given context data. 842 """Apply the template to the given context data.
Copyright (C) 2012-2017 Edgewall Software