import color, mode2
from lex3 import Grammar, PatternRule, RegionRule, Grammar
from mode_perl import PerlGrammar
from mode_xml import OpenTagGrammar
from mode_perl import StringGrammar

class BDSGrammar(Grammar):
    rules = [
        RegionRule(r'comment', r'<!--', Grammar, r'-->'),
        RegionRule(r'opentag', r'<', OpenTagGrammar, r'/?>'),
        PatternRule(r'closetag', r'< */ *[ =>\n]+ *>'),
        PatternRule(r'delimiter', r'[\[\]\{\}\(\),\?:]'),
        PatternRule(r'derived', r'(?:FM|CD|FS|FM|TA)[0-9]{3}-[0-9]{3}-[0-9]{3}'),
        PatternRule(r'question', r'GQ[0-9]{3}-[0-9]{3}-[0-9]{3}:MQ[0-9]{3}-[0-9]{3}-[0-9]{3}'),
        PatternRule(r'bdsfunc', r'[A-Z_][A-Z0-9_]+(?= *\()'),
        PatternRule(r'perlfunc', r'[a-zA-Z_][a-zA-Z0-9_]+(?= *\()'),
        PatternRule(r'misquoted', r"'[A-Z]{2}[0-9]{3}-[0-9]{3}-[0-9]{3}(?::[A-Z]{2}[0-9]{3}-[0-9]{3}-[0-9]{3})?'"),
        PatternRule(r'misquoted', r'"[A-Z]{2}[0-9]{3}-[0-9]{3}-[0-9]{3}(?::[A-Z]{2}[0-9]{3}-[0-9]{3}-[0-9]{3})?"'),
        RegionRule(r'string', '"', StringGrammar, '"'),
        RegionRule(r'string', "'", Grammar, "'"),
        PatternRule(r'operator', r'(?:&gt;=|&lt;=|&gt;|&lt;|==|&amp;&amp;|\|\||eq|ne)'),
    ]

class BDS(mode2.Fundamental):
    grammar    = BDSGrammar
    opentokens  = ('delimiter',)
    opentags   = {'(': ')', '[': ']', '{': '}'}
    closetokens = ('delimiter',)
    closetags  = {')': '(', ']': '[', '}': '{'}
    def __init__(self, w):
        mode2.Fundamental.__init__(self, w)
        self.add_bindings('close-paren', (')',))
        self.add_bindings('close-brace', ('}',))
        self.add_bindings('close-bracket', (']',))
        self.colors = {
            'comment.start':        color.build('red', 'default'),
            'comment.null':         color.build('red', 'default'),
            'comment.end':          color.build('red', 'default'),
            'opentag.start':        color.build('default', 'default'),
            'opentag.namespace':    color.build('magenta', 'default'),
            'opentag.name':         color.build('blue', 'default'),
            'opentag.attrname':     color.build('blue', 'default'),
            'opentag.string.start': color.build('cyan', 'default'),
            'opentag.string.null':  color.build('cyan', 'default'),
            'opentag.string.end':   color.build('cyan', 'default'),
            'opentag.end':          color.build('default', 'default'),
            'string.start':         color.build('green', 'default'),
            'string.octal':         color.build('magenta', 'default'),
            'string.escaped':       color.build('magenta', 'default'),
            'string.null':          color.build('green', 'default'),
            'string.end':           color.build('green', 'default'),
            'derived':              color.build('yellow', 'default'),
            'question':             color.build('yellow', 'default'),
            'misquoted':            color.build('yellow', 'red'),
            'bdsfunc':              color.build('magenta', 'default'),
            'perlfunc':             color.build('magenta', 'default'),
            'operator':             color.build('magenta', 'default'),
        }
    def name(self):
        return "BDS"