from mode import Fundamental
from point import Point
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule
from mode.lisp import Lisp, LispTabber

class StringGrammar(Grammar):
    rules = [
        PatternRule('octal', r'\\[0-7]{3}'),
        PatternRule('escaped', r'\\.'),
        PatternRule('data', r'[^\\"]+'),
    ]

# TODO: a lot of these rules need fixing
chr1 = '[a-zA-Z_]'
chr2 = '[a-zA-Z0-9-_]'
symb = chr1 + chr2 + '*'

class ELispGrammar(Grammar):
    rules = [
        PatternRule('comment', ';.*$'),
        PatternRule('delimiter', '[()]'),
        PatternRule('spaces', ' +'),
        PatternRule('eol', r'\n'),
        PatternRule('elisp_reserved', r'(?:t|nil)(?![^\"\' \t()])'),
        PatternRule('keyword', r'(?:while|when|unless|setq-default|setq|setcar|require|provide|or|not|mapcar|list|let\*|let|lambda|if|exists|equal|defvar|defun|defstruct|defface|defalias|count|cons|c[ad]+r|apply|and)(?![^\"\' \t()])'),
        PatternRule('elisp_symbol', "'" + symb),
        PatternRule('delimiter', r"'"),
        PatternRule('elisp_type', ":" + symb),
        PatternRule('attribute', "&" + symb),
        PatternRule("integer", r"(?<![\.0-9a-zA-Z_])(?:0|-?[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?(?![\.0-9a-zA-Z_])"),
        PatternRule("float", r"(?<![\.0-9a-zA-Z_])(?:[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+)(?![\.0-9a-zA-Z_])"),
        PatternRule("imaginary", r"(?<![\.0-9a-zA-Z_])(?:[0-9]+|(?:[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+)[jJ])(?![\.0-9a-zA-Z_])"),
        PatternRule('elisp_word', r"[^\"' \t()]+"),
        RegionRule('string', r'"', StringGrammar, r'"'),
        PatternRule('eol', r'\n$'),
    ]

class ELisp(Lisp):
    name        = 'ELisp'
    basenames   = ['.emacs']
    extensions  = ['.el']
    grammar     = ELispGrammar
    opentokens  = ('delimiter',)
    opentags    = {'(': ')', '[': ']', '{': '}'}
    closetokens = ('delimiter',)
    closetags   = {')': '(', ']': '[', '}': '{'}
    colors      = {
        'elisp_reserved': ('blue', 'default', 'bold'),
        'elisp_symbol':   ('magenta', 'default', 'bold'),
        'elisp_type':     ('blue', 'default', 'bold'),
        'elisp_word':     ('default', 'default'),
    }

install = ELisp.install