Changeset 12

Show
Ignore:
Timestamp:
12/08/04 15:17:03 (8 years ago)
Author:
steve
Message:

single and multi-line comments

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore set to
      *.pyc
  • trunk/airspeed.py

    r11 r12  
    88 
    99class Tokeniser: 
    10     PLAIN, IF, PLACEHOLDER, FOREACH, END, SET = range(6) 
     10    PLAIN, IF, PLACEHOLDER, FOREACH, END, SET, ELSE = range(7) 
    1111 
    1212    UP_TO_NEXT_TEMPLATE_BIT = re.compile('^(.*?)((?:#|\$).*)', re.MULTILINE + re.DOTALL) 
     
    1414    NAME = '[a-z0-9_]+' 
    1515    NAME_OR_CALL = NAME + '(?:\(\))?' 
     16    RE_FLAGS = re.IGNORECASE + re.DOTALL + re.MULTILINE 
    1617    EXPRESSION = '(' + NAME_OR_CALL + '(?:\.' + NAME_OR_CALL + ')*)' 
    17     PLACEHOLDER_PATTERN = re.compile('^\$(!?)({?)' + EXPRESSION + '(}?)' + REST, re.IGNORECASE + re.DOTALL + re.MULTILINE) 
    18     SET_PATTERN = re.compile('^#set[ \t]*\([ \t]*\$(' + NAME + ')[ \t]*=[ \t]*(\d+|"[^"]+")[ \t]*\)' + REST, re.IGNORECASE + re.DOTALL + re.MULTILINE) 
    19     BEGIN_IF_PATTERN = re.compile('^#if[ \t]*\([ \t]*\$' + EXPRESSION + '[ \t]*\)' + REST, re.IGNORECASE + re.DOTALL + re.MULTILINE) 
    20     BEGIN_FOREACH_PATTERN = re.compile('^#foreach[ \t]*\([ \t]*\$(' + NAME + ')[ \t]+in[ \t]+\$' + EXPRESSION + '[ \t]*\)' + REST, re.IGNORECASE + re.DOTALL + re.MULTILINE) 
    21     END_PATTERN = re.compile('^#end' + REST, re.IGNORECASE + re.DOTALL + re.MULTILINE) 
     18    PLACEHOLDER_PATTERN = re.compile('^\$(!?)({?)' + EXPRESSION + '(}?)' + REST, RE_FLAGS) 
     19    SET_PATTERN = re.compile('^#set[ \t]*\([ \t]*\$(' + NAME + ')[ \t]*=[ \t]*(\d+|"[^"]+")[ \t]*\)' + REST, RE_FLAGS) 
     20    BEGIN_IF_PATTERN = re.compile('^#if[ \t]*\([ \t]*\$' + EXPRESSION + '[ \t]*\)' + REST, RE_FLAGS) 
     21    BEGIN_FOREACH_PATTERN = re.compile('^#foreach[ \t]*\([ \t]*\$(' + NAME + ')[ \t]+in[ \t]+\$' + EXPRESSION + '[ \t]*\)' + REST, RE_FLAGS) 
     22    END_PATTERN = re.compile('^#end' + REST, RE_FLAGS) 
     23    ELSE_PATTERN = re.compile('^#else' + REST, RE_FLAGS) 
     24    COMMENT_PATTERN = re.compile('^##.*?(?:\n|$)' + REST, RE_FLAGS) 
     25    MULTI_LINE_COMMENT_PATTERN = re.compile('^#\*.*?\*#(?:[ \t]*\n)?' + REST, RE_FLAGS) 
    2226 
    2327    def tokenise(self, text): 
     
    5357                (var_name, rvalue, text) = m.groups() 
    5458                yield self.SET, (var_name, rvalue) 
     59                continue 
     60            m = self.ELSE_PATTERN.match(interesting) 
     61            if m: 
     62                (text,) = m.groups() 
     63                yield self.ELSE, None 
     64                continue 
     65            m = self.COMMENT_PATTERN.match(interesting) 
     66            if m: 
     67                (text,) = m.groups() 
     68                continue 
     69            m = self.MULTI_LINE_COMMENT_PATTERN.match(interesting) 
     70            if m: 
     71                (text,) = m.groups() 
    5572                continue 
    5673            if interesting.startswith('$'): 
  • trunk/airspeed_test.py

    r11 r12  
    131131        self.assertEquals("Steve", template.merge({})) 
    132132 
     133    def test_single_line_comments_skipped(self): 
     134        template = airspeed.Template('## comment\nStuff\nMore stuff## more comments $blah') 
     135        self.assertEquals("Stuff\nMore stuff", template.merge({})) 
     136 
     137    def test_multi_line_comments_skipped(self): 
     138        template = airspeed.Template('Stuff#*\n more comments *#\n and more stuff') 
     139        self.assertEquals("Stuff and more stuff", template.merge({})) 
    133140 
    134141if __name__ == '__main__':