from mode import Fundamental from lex import Grammar, PatternRule, NocasePatternRule, RegionRule, NocaseRegionRule from mode.python import StringGrammar1, StringGrammar2 class CommentGrammar1(Grammar): rules = [PatternRule(r'data', r'(?:[^*]|\*(?!/))+')] class CommentGrammar2(Grammar): rules = [PatternRule(r'data', r'(?:[^-]|-(?!-)|--(?!>))+')] class KeywordGrammar(Grammar): rules = [ PatternRule(r'octal', r'\\[0-7]{3}'), PatternRule(r'escaped', r'\\.'), RegionRule(r'string', "'", StringGrammar1, r"'"), RegionRule(r'string', '"', StringGrammar2, r'"'), ] chr2 = '[-a-z0-9_]' class CSSGrammar(Grammar): rules = [ RegionRule(r'comment', '/\*', CommentGrammar1, '\*/'), RegionRule(r'comment', ''), NocasePatternRule(r'css.dimension', r'[+-]?(?:\d+|\d*\.\d+)[-a-z_][-a-z0-9_]*'), NocasePatternRule(r'css.percentage', r'[+-]?(?:\d+|\d*\.\d+)%%'), NocasePatternRule(r'css.length', r'[+-]?(?:\d+|\d*\.\d+)(?:em|ex|px|in|cm|mm|pt|pc)'), NocasePatternRule(r'css.hash', r'#' + chr2 + '+'), NocasePatternRule(r'css.real', r'[+-]?\d*\.\d+'), NocasePatternRule(r'css.int', r'[+-]?\d+'), NocasePatternRule(r'css.rule', r'@(?:page|media|import)'), NocasePatternRule(r'css.color', r'(?:aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|#\d{6}|#\d{3})(?!' + chr2 + ')'), NocasePatternRule(r'css.keyword', r'(?:url|rgba|rgb|counter)'), NocaseRegionRule(r'css.keyword', '(?:(?<=url)|(?<=rgba)|(?<=rgb)|(?<=counter))\(', KeywordGrammar, '\)'), NocasePatternRule(r'css.label', r"\.?" + chr2 + "+(?= *{)"), NocasePatternRule(r'css.ident', r"-?[a-z_]" + chr2 + "*"), NocasePatternRule(r'css.name', chr2 + "+"), NocasePatternRule(r'delimiter', r'[:;,{}()\[\]]|~=|\|=|='), RegionRule(r'css.string', "'", StringGrammar1, r"'"), RegionRule(r'css.string', '"', StringGrammar2, r'"'), ] class CSS(Fundamental): name = 'CSS' extensions = ['.css'] grammar = CSSGrammar opentokens = ('delimiter',) opentags = {'(': ')', '[': ']', '{': '}'} closetokens = ('delimiter',) closetags = {')': '(', ']': '[', '}': '{'} colors = { 'css.dimension': ('magenta', 'default', 'bold'), 'css.percentage': ('magenta', 'default', 'bold'), 'css.length': ('magenta', 'default', 'bold'), 'css.real': ('magenta', 'default', 'bold'), 'css.int': ('magenta', 'default', 'bold'), 'css.color': ('magenta', 'default', 'bold'), 'css.hash': ('cyan', 'default', 'bold'), 'css.rule': ('cyan', 'default', 'bold'), 'css.label': ('cyan', 'default', 'bold'), 'css.keyword': ('cyan', 'default', 'bold'), 'css.ident': ('default', 'default'), 'css.name': ('default', 'default'), 'css.keyword': ('cyan', 'default', 'bold'), 'css.keyword.start': ('default', 'default', 'bold'), 'css.keyword.null': ('cyan', 'default', 'bold'), 'css.keyword.octal': ('magenta', 'default', 'bold'), 'css.keyword.escaped': ('magenta', 'default', 'bold'), 'css.keyword.end': ('default', 'default', 'bold'), } _bindings = { 'close-paren': (')',), 'close-brace': ('}',), 'close-bracket': (']',), } install = CSS.install