import color, mode2
from lex3 import Grammar, PatternRule, RegionRule

class TagGrammar(Grammar):
    rules = [
        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 XMLGrammar(Grammar):
    rules = [
        # TODO: how does cdata work again?
        RegionRule(r'comment', r'<!--', Grammar, r'-->'),
        RegionRule(r'tag', r'<', TagGrammar, r'/?>'),
    ]

class XML(mode2.Fundamental):
    grammar = XMLGrammar
    colors = {
        'comment.start':    ('red', 'default'),
        'comment.null':     ('red', 'default'),
        'comment.end':      ('red', 'default'),
        'tag.start':        ('default', 'default'),
        'tag.namespace':    ('magenta', 'default'),
        'tag.name':         ('blue', 'default'),
        'tag.attrname':     ('cyan', 'default'),
        'tag.string.start': ('green', 'default'),
        'tag.string.null':  ('green', 'default'),
        'tag.string.end':   ('green', 'default'),
        'tag.end':          ('default', 'default'),
    }
    def name(self):
        return "XML"