import color, mode from lex import Grammar, PatternRule, RegionRule from mode.perl import StringGrammar, PerlGrammar class PerlDirectiveGrammar(Grammar): rules = [PatternRule(r'tt.perl', r'(?:PERL|RAWPERL|END)(?![A-Z_])')] class DirectiveGrammar(Grammar): rules = [ PatternRule(r'tt.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)(?![A-Z_])'), PatternRule(r'attribute', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *=)'), RegionRule(r'string', r'"', StringGrammar, r'"'), RegionRule(r'string', r"'", StringGrammar, r"'"), ] class TagGrammar(Grammar): rules = [ RegionRule(r'tt.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 = [ PatternRule(r'escaped', r'&[^;]+;'), RegionRule(r'comment', r''), RegionRule(r'comment', r'\[\%#', Grammar, r'%%\]'), PatternRule(r'tt.data', r'[^\[<&]+'), RegionRule(r'tt.directive', r'\[\%(?= *PERL)', PerlDirectiveGrammar, r'%%\]', PerlGrammar, r'\[%%', PerlDirectiveGrammar, '%%\]'), RegionRule(r'tt.directive', r'\[\%', DirectiveGrammar, r'%%\]'), RegionRule(r'tt.tag', r''), ] class Template(mode.Fundamental): name = 'Template' extensions = ['.tt'] grammar = TemplateGrammar colors = { 'tt.directive.start': ('magenta', 'default', 'bold'), 'tt.directive.comment': ('red', 'default', 'bold'), 'tt.directive.keyword': ('yellow', 'default', 'bold'), 'tt.directive.attribute': ('magenta', 'default', 'bold'), 'tt.directive.string.start': ('green', 'default', 'bold'), 'tt.directive.string.escaped': ('magenta', 'default', 'bold'), 'tt.directive.string.octal': ('magenta', 'default', 'bold'), 'tt.directive.string.null': ('green', 'default', 'bold'), 'tt.directive.string.end': ('green', 'default', 'bold'), 'tt.directive.null': ('magenta', 'default', 'bold'), 'tt.directive.middle0': ('magenta', 'default', 'bold'), 'tt.directive.middle1': ('magenta', 'default', 'bold'), 'tt.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'), 'tt.perl': ('red', 'default', 'bold'), } _bindings = { 'close-paren': (')',), 'close-brace': ('}',), 'close-bracket': (']',), } install = Template.install