90 lines
3.9 KiB
Python
90 lines
3.9 KiB
Python
import color, mode
|
|
from lex import Grammar, PatternRule, NocasePatternRule, RegionRule, NocaseRegionRule
|
|
from point import Point
|
|
|
|
class StringGrammar1(Grammar):
|
|
rules = [
|
|
PatternRule(r'octal', r'\\[0-7]{3}'),
|
|
PatternRule(r'escaped', r'\\.'),
|
|
PatternRule(r'data', r"[^']+"),
|
|
]
|
|
class StringGrammar2(Grammar):
|
|
rules = [
|
|
PatternRule(r'octal', r'\\[0-7]{3}'),
|
|
PatternRule(r'escaped', r'\\.'),
|
|
PatternRule(r'data', r'[^"]+'),
|
|
]
|
|
|
|
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'"'),
|
|
]
|
|
|
|
class CSSGrammar(Grammar):
|
|
rules = [
|
|
RegionRule(r'comment', '/\*', CommentGrammar1, '\*/'),
|
|
RegionRule(r'comment', '<!--', CommentGrammar2, '-->'),
|
|
NocasePatternRule(r'css_dimension', r'[+-]?(?:[0-9]+|[0-9]*\.[0-9]+)[-a-z_][-a-z0-9_]*'),
|
|
NocasePatternRule(r'css_percentage', r'[+-]?(?:[0-9]+|[0-9]*\.[0-9]+)%%'),
|
|
NocasePatternRule(r'css_length', r'[+-]?(?:[0-9]+|[0-9]*\.[0-9]+)(?:em|ex|px|in|cm|mm|pt|pc)'),
|
|
NocasePatternRule(r'css_hash', r'#[-a-z0-9_]+'),
|
|
NocasePatternRule(r'css_real', r'[+-]?[0-9]*\.[0-9]+'),
|
|
NocasePatternRule(r'css_int', r'[+-]?[0-9]+'),
|
|
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|#[0-9]{6}|#[0-9]{3})'),
|
|
|
|
NocasePatternRule(r'css_keyword', r'(?:url|rgb|counter)'),
|
|
NocaseRegionRule(r'css_keyword', '(?:(?<=url)|(?<=rgb)|(?<=counter))\(', KeywordGrammar, '\)'),
|
|
|
|
NocasePatternRule(r'css_label', r"\.?[-a-zA-Z0-9_]+(?= *{)"),
|
|
NocasePatternRule(r'css_ident', r"-?[a-z_][-a-z0-9_]*"),
|
|
NocasePatternRule(r'css_name', r"[-a-z0-9_]+"),
|
|
NocasePatternRule(r'delimiter', r'[:;,{}()\[\]]|~=|\|=|='),
|
|
RegionRule(r'string', "'", StringGrammar1, r"'"),
|
|
RegionRule(r'string', '"', StringGrammar2, r'"'),
|
|
]
|
|
|
|
class CSS(mode.Fundamental):
|
|
modename = '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', 'bold'),
|
|
'css_name': ('default', 'default', 'bold'),
|
|
'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'),
|
|
}
|
|
def __init__(self, w):
|
|
mode.Fundamental.__init__(self, w)
|
|
self.add_bindings('close-paren', (')',))
|
|
self.add_bindings('close-brace', ('}',))
|
|
self.add_bindings('close-bracket', (']',))
|
|
|
|
install = CSS.install
|