import color, mode from lex import Grammar, PatternRule, RegionRule from mode.perl import StringGrammar class DirectiveGrammar(Grammar): rules = [ PatternRule(r'comment', r'#(?:[^%]|%(?!\]))*'), PatternRule(r'keyword', r'BLOCK|CALL|CASE|CATCH|CLEAR|DEBUG|DEFAULT|FINAL|FILTER|FOREACH|ELSIF|ELSE|END|GET|IF|INCLUDE|INSERT|IN|LAST|MACRO|META|NEXT|PERL|PROCESS|RAWPERL|RETURN|SET|STOP|SWITCH|TAGS|THROW|TRY|UNLESS|USE|WHILE|WRAPPER'), RegionRule(r'string', r'"', StringGrammar, r'"'), RegionRule(r'string', r"'", StringGrammar, r"'"), ] class TagGrammar(Grammar): rules = [ RegionRule(r'directive', r'\[\%', DirectiveGrammar, r'%%\]'), RegionRule(r'string', r'"', Grammar, r'"'), RegionRule(r'string', r"'", Grammar, r"'"), PatternRule(r'namespace', r'[a-zA-Z_]+:'), PatternRule(r'attrname', r'[^ =>\n]+(?==)'), PatternRule(r'name', r'[^\[\] =>\n]+'), ] class TemplateGrammar(Grammar): rules = [ RegionRule(r'comment', r''), RegionRule(r'directive', r'\[\%', DirectiveGrammar, r'%%\]'), RegionRule(r'tt_tag', r''), ] class Template(mode.Fundamental): modename = 'Template' extensions = ['.tt'] grammar = TemplateGrammar colors = { 'directive.start': ('magenta', 'default', 'bold'), 'directive.comment': ('red', 'default', 'bold'), 'directive.keyword': ('cyan', 'default', 'bold'), 'directive.string.start': ('green', 'default', 'bold'), 'directive.string.escaped': ('magenta', 'default', 'bold'), 'directive.string.octal': ('magenta', 'default', 'bold'), 'directive.string.null': ('green', 'default', 'bold'), 'directive.string.end': ('green', 'default', 'bold'), 'directive.null': ('magenta', 'default', 'bold'), 'directive.end': ('magenta', 'default', 'bold'), 'tt_tag.start': ('default', 'default', 'bold'), 'tt_tag.namespace': ('magenta', 'default', 'bold'), 'tt_tag.name': ('blue', 'default', 'bold'), 'tt_tag.attrname': ('cyan', 'default', 'bold'), 'tt_tag.string.start': ('green', 'default', 'bold'), 'tt_tag.string.null': ('green', 'default', 'bold'), 'tt_tag.string.end': ('green', 'default', 'bold'), 'tt_tag.end': ('default', 'default', 'bold'), } def __init__(self, w): mode.Fundamental.__init__(self, w) self.add_bindings('close-paren', (')',)) self.add_bindings('close-brace', ('}',)) self.add_bindings('close-bracket', (']',)) install = Template.install