pmacs3/mode/css.py

100 lines
3.9 KiB
Python

import color, mode
from lex import Grammar, PatternRule, NocasePatternRule, RegionRule, NocaseRegionRule
from point import Point
class StringGrammar(Grammar):
rules = [
PatternRule(r'octal', r'\\[0-7]{3}'),
PatternRule(r'escaped', r'\\.'),
]
class KeywordGrammar(Grammar):
rules = [
PatternRule(r'octal', r'\\[0-7]{3}'),
PatternRule(r'escaped', r'\\.'),
RegionRule('string', "'", StringGrammar, r"'"),
RegionRule('string', '"', StringGrammar, r'"'),
]
class CSSGrammar(Grammar):
rules = [
RegionRule(r'comment', '/\*', Grammar, '\*/'),
RegionRule(r'htmlcomment', '<!--', Grammar, '-->'),
NocasePatternRule(r'dimension', r'[+-]?(?:[0-9]+|[0-9]*\.[0-9]+)[-a-z_][-a-z0-9_]*'),
NocasePatternRule(r'percentage', r'[+-]?(?:[0-9]+|[0-9]*\.[0-9]+)%%'),
NocasePatternRule(r'length', r'[+-]?(?:[0-9]+|[0-9]*\.[0-9]+)(?:em|ex|px|in|cm|mm|pt|pc)'),
NocasePatternRule(r'hash', r'#[-a-z0-9_]+'),
NocasePatternRule(r'real', r'[+-]?[0-9]*\.[0-9]+'),
NocasePatternRule(r'int', r'[+-]?[0-9]+'),
NocasePatternRule(r'rule', r'@(?:page|media|import)'),
NocasePatternRule(r'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'ident', r"-?[a-z_][-a-z0-9_]*"),
NocasePatternRule(r'name', r"[-a-z0-9_]+"),
NocasePatternRule(r'delimiter', r'[:;,{}()\[\]]|~=|\|=|='),
RegionRule(r'string', "'", StringGrammar, r"'"),
RegionRule(r'string', '"', StringGrammar, r'"'),
]
class CSS(mode.Fundamental):
modename = 'CSS'
extensions = ['.css']
grammar = CSSGrammar
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
colors = {
#'comment': ('red', 'default'),
#'comment.start': ('red', 'default'),
#'comment.null': ('red', 'default'),
#'comment.end': ('red', 'default'),
'htmlcomment': ('red', 'default'),
'htmlcomment.start': ('red', 'default'),
'htmlcomment.null': ('red', 'default'),
'htmlcomment.end': ('red', 'default'),
'dimension': ('magenta', 'default'),
'percentage': ('magenta', 'default'),
'length': ('magenta', 'default'),
'real': ('magenta', 'default'),
'int': ('magenta', 'default'),
'color': ('magenta', 'default'),
'hash': ('cyan', 'default'),
'css_label': ('cyan', 'default'),
'rule': ('cyan', 'default'),
'css_keyword': ('cyan', 'default'),
'ident': ('default', 'default'),
'name': ('default', 'default'),
'delimiter': ('default', 'default'),
'css_keyword': ('cyan', 'default'),
'css_keyword.start': ('default', 'default'),
'css_keyword.null': ('cyan', 'default'),
'css_keyword.octal': ('magenta', 'default'),
'css_keyword.escaped': ('magenta', 'default'),
'css_keyword.end': ('default', 'default'),
#'string.start': ('green', 'default'),
#'string.null': ('green', 'default'),
#'string.octal': ('magenta', 'default'),
#'string.escaped': ('magenta', 'default'),
#'string.end': ('green', 'default'),
}
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