eric6/ThirdParty/Pygments/pygments/lexers/css.py

changeset 8258
82b608e352ec
parent 8257
28146736bbfc
child 8259
2bbec88047dd
equal deleted inserted replaced
8257:28146736bbfc 8258:82b608e352ec
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.css
4 ~~~~~~~~~~~~~~~~~~~
5
6 Lexers for CSS and related stylesheet formats.
7
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13 import copy
14
15 from pygments.lexer import ExtendedRegexLexer, RegexLexer, include, bygroups, \
16 default, words, inherit
17 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
18 Number, Punctuation
19
20 __all__ = ['CssLexer', 'SassLexer', 'ScssLexer', 'LessCssLexer']
21
22
23 # List of vendor prefixes obtained from:
24 # https://www.w3.org/TR/CSS21/syndata.html#vendor-keyword-history
25 _vendor_prefixes = (
26 '-ms-', 'mso-', '-moz-', '-o-', '-xv-', '-atsc-', '-wap-', '-khtml-',
27 '-webkit-', 'prince-', '-ah-', '-hp-', '-ro-', '-rim-', '-tc-',
28 )
29
30 # List of CSS properties obtained from:
31 # https://www.w3.org/Style/CSS/all-properties.en.html
32 # Note: handle --* separately
33 _css_properties = (
34 'align-content', 'align-items', 'align-self', 'alignment-baseline', 'all',
35 'animation', 'animation-delay', 'animation-direction',
36 'animation-duration', 'animation-fill-mode', 'animation-iteration-count',
37 'animation-name', 'animation-play-state', 'animation-timing-function',
38 'appearance', 'azimuth', 'backface-visibility', 'background',
39 'background-attachment', 'background-blend-mode', 'background-clip',
40 'background-color', 'background-image', 'background-origin',
41 'background-position', 'background-repeat', 'background-size',
42 'baseline-shift', 'bookmark-label', 'bookmark-level', 'bookmark-state',
43 'border', 'border-bottom', 'border-bottom-color',
44 'border-bottom-left-radius', 'border-bottom-right-radius',
45 'border-bottom-style', 'border-bottom-width', 'border-boundary',
46 'border-collapse', 'border-color', 'border-image', 'border-image-outset',
47 'border-image-repeat', 'border-image-slice', 'border-image-source',
48 'border-image-width', 'border-left', 'border-left-color',
49 'border-left-style', 'border-left-width', 'border-radius', 'border-right',
50 'border-right-color', 'border-right-style', 'border-right-width',
51 'border-spacing', 'border-style', 'border-top', 'border-top-color',
52 'border-top-left-radius', 'border-top-right-radius', 'border-top-style',
53 'border-top-width', 'border-width', 'bottom', 'box-decoration-break',
54 'box-shadow', 'box-sizing', 'box-snap', 'box-suppress', 'break-after',
55 'break-before', 'break-inside', 'caption-side', 'caret', 'caret-animation',
56 'caret-color', 'caret-shape', 'chains', 'clear', 'clip', 'clip-path',
57 'clip-rule', 'color', 'color-interpolation-filters', 'column-count',
58 'column-fill', 'column-gap', 'column-rule', 'column-rule-color',
59 'column-rule-style', 'column-rule-width', 'column-span', 'column-width',
60 'columns', 'content', 'counter-increment', 'counter-reset', 'counter-set',
61 'crop', 'cue', 'cue-after', 'cue-before', 'cursor', 'direction', 'display',
62 'dominant-baseline', 'elevation', 'empty-cells', 'filter', 'flex',
63 'flex-basis', 'flex-direction', 'flex-flow', 'flex-grow', 'flex-shrink',
64 'flex-wrap', 'float', 'float-defer', 'float-offset', 'float-reference',
65 'flood-color', 'flood-opacity', 'flow', 'flow-from', 'flow-into', 'font',
66 'font-family', 'font-feature-settings', 'font-kerning',
67 'font-language-override', 'font-size', 'font-size-adjust', 'font-stretch',
68 'font-style', 'font-synthesis', 'font-variant', 'font-variant-alternates',
69 'font-variant-caps', 'font-variant-east-asian', 'font-variant-ligatures',
70 'font-variant-numeric', 'font-variant-position', 'font-weight',
71 'footnote-display', 'footnote-policy', 'glyph-orientation-vertical',
72 'grid', 'grid-area', 'grid-auto-columns', 'grid-auto-flow',
73 'grid-auto-rows', 'grid-column', 'grid-column-end', 'grid-column-gap',
74 'grid-column-start', 'grid-gap', 'grid-row', 'grid-row-end',
75 'grid-row-gap', 'grid-row-start', 'grid-template', 'grid-template-areas',
76 'grid-template-columns', 'grid-template-rows', 'hanging-punctuation',
77 'height', 'hyphenate-character', 'hyphenate-limit-chars',
78 'hyphenate-limit-last', 'hyphenate-limit-lines', 'hyphenate-limit-zone',
79 'hyphens', 'image-orientation', 'image-resolution', 'initial-letter',
80 'initial-letter-align', 'initial-letter-wrap', 'isolation',
81 'justify-content', 'justify-items', 'justify-self', 'left',
82 'letter-spacing', 'lighting-color', 'line-break', 'line-grid',
83 'line-height', 'line-snap', 'list-style', 'list-style-image',
84 'list-style-position', 'list-style-type', 'margin', 'margin-bottom',
85 'margin-left', 'margin-right', 'margin-top', 'marker-side',
86 'marquee-direction', 'marquee-loop', 'marquee-speed', 'marquee-style',
87 'mask', 'mask-border', 'mask-border-mode', 'mask-border-outset',
88 'mask-border-repeat', 'mask-border-slice', 'mask-border-source',
89 'mask-border-width', 'mask-clip', 'mask-composite', 'mask-image',
90 'mask-mode', 'mask-origin', 'mask-position', 'mask-repeat', 'mask-size',
91 'mask-type', 'max-height', 'max-lines', 'max-width', 'min-height',
92 'min-width', 'mix-blend-mode', 'motion', 'motion-offset', 'motion-path',
93 'motion-rotation', 'move-to', 'nav-down', 'nav-left', 'nav-right',
94 'nav-up', 'object-fit', 'object-position', 'offset-after', 'offset-before',
95 'offset-end', 'offset-start', 'opacity', 'order', 'orphans', 'outline',
96 'outline-color', 'outline-offset', 'outline-style', 'outline-width',
97 'overflow', 'overflow-style', 'overflow-wrap', 'overflow-x', 'overflow-y',
98 'padding', 'padding-bottom', 'padding-left', 'padding-right', 'padding-top',
99 'page', 'page-break-after', 'page-break-before', 'page-break-inside',
100 'page-policy', 'pause', 'pause-after', 'pause-before', 'perspective',
101 'perspective-origin', 'pitch', 'pitch-range', 'play-during', 'polar-angle',
102 'polar-distance', 'position', 'presentation-level', 'quotes',
103 'region-fragment', 'resize', 'rest', 'rest-after', 'rest-before',
104 'richness', 'right', 'rotation', 'rotation-point', 'ruby-align',
105 'ruby-merge', 'ruby-position', 'running', 'scroll-snap-coordinate',
106 'scroll-snap-destination', 'scroll-snap-points-x', 'scroll-snap-points-y',
107 'scroll-snap-type', 'shape-image-threshold', 'shape-inside', 'shape-margin',
108 'shape-outside', 'size', 'speak', 'speak-as', 'speak-header',
109 'speak-numeral', 'speak-punctuation', 'speech-rate', 'stress', 'string-set',
110 'tab-size', 'table-layout', 'text-align', 'text-align-last',
111 'text-combine-upright', 'text-decoration', 'text-decoration-color',
112 'text-decoration-line', 'text-decoration-skip', 'text-decoration-style',
113 'text-emphasis', 'text-emphasis-color', 'text-emphasis-position',
114 'text-emphasis-style', 'text-indent', 'text-justify', 'text-orientation',
115 'text-overflow', 'text-shadow', 'text-space-collapse', 'text-space-trim',
116 'text-spacing', 'text-transform', 'text-underline-position', 'text-wrap',
117 'top', 'transform', 'transform-origin', 'transform-style', 'transition',
118 'transition-delay', 'transition-duration', 'transition-property',
119 'transition-timing-function', 'unicode-bidi', 'user-select',
120 'vertical-align', 'visibility', 'voice-balance', 'voice-duration',
121 'voice-family', 'voice-pitch', 'voice-range', 'voice-rate', 'voice-stress',
122 'voice-volume', 'volume', 'white-space', 'widows', 'width', 'will-change',
123 'word-break', 'word-spacing', 'word-wrap', 'wrap-after', 'wrap-before',
124 'wrap-flow', 'wrap-inside', 'wrap-through', 'writing-mode', 'z-index',
125 )
126
127 # List of keyword values obtained from:
128 # http://cssvalues.com/
129 _keyword_values = (
130 'absolute', 'alias', 'all', 'all-petite-caps', 'all-scroll',
131 'all-small-caps', 'allow-end', 'alpha', 'alternate', 'alternate-reverse',
132 'always', 'armenian', 'auto', 'avoid', 'avoid-column', 'avoid-page',
133 'backwards', 'balance', 'baseline', 'below', 'blink', 'block', 'bold',
134 'bolder', 'border-box', 'both', 'bottom', 'box-decoration', 'break-word',
135 'capitalize', 'cell', 'center', 'circle', 'clip', 'clone', 'close-quote',
136 'col-resize', 'collapse', 'color', 'color-burn', 'color-dodge', 'column',
137 'column-reverse', 'compact', 'condensed', 'contain', 'container',
138 'content-box', 'context-menu', 'copy', 'cover', 'crisp-edges', 'crosshair',
139 'currentColor', 'cursive', 'darken', 'dashed', 'decimal',
140 'decimal-leading-zero', 'default', 'descendants', 'difference', 'digits',
141 'disc', 'distribute', 'dot', 'dotted', 'double', 'double-circle', 'e-resize',
142 'each-line', 'ease', 'ease-in', 'ease-in-out', 'ease-out', 'edges',
143 'ellipsis', 'end', 'ew-resize', 'exclusion', 'expanded', 'extra-condensed',
144 'extra-expanded', 'fantasy', 'fill', 'fill-box', 'filled', 'first', 'fixed',
145 'flat', 'flex', 'flex-end', 'flex-start', 'flip', 'force-end', 'forwards',
146 'from-image', 'full-width', 'geometricPrecision', 'georgian', 'groove',
147 'hanging', 'hard-light', 'help', 'hidden', 'hide', 'horizontal', 'hue',
148 'icon', 'infinite', 'inherit', 'initial', 'ink', 'inline', 'inline-block',
149 'inline-flex', 'inline-table', 'inset', 'inside', 'inter-word', 'invert',
150 'isolate', 'italic', 'justify', 'large', 'larger', 'last', 'left',
151 'lighten', 'lighter', 'line-through', 'linear', 'list-item', 'local',
152 'loose', 'lower-alpha', 'lower-greek', 'lower-latin', 'lower-roman',
153 'lowercase', 'ltr', 'luminance', 'luminosity', 'mandatory', 'manipulation',
154 'manual', 'margin-box', 'match-parent', 'medium', 'mixed', 'monospace',
155 'move', 'multiply', 'n-resize', 'ne-resize', 'nesw-resize',
156 'no-close-quote', 'no-drop', 'no-open-quote', 'no-repeat', 'none', 'normal',
157 'not-allowed', 'nowrap', 'ns-resize', 'nw-resize', 'nwse-resize', 'objects',
158 'oblique', 'off', 'on', 'open', 'open-quote', 'optimizeLegibility',
159 'optimizeSpeed', 'outset', 'outside', 'over', 'overlay', 'overline',
160 'padding-box', 'page', 'pan-down', 'pan-left', 'pan-right', 'pan-up',
161 'pan-x', 'pan-y', 'paused', 'petite-caps', 'pixelated', 'pointer',
162 'preserve-3d', 'progress', 'proximity', 'relative', 'repeat',
163 'repeat no-repeat', 'repeat-x', 'repeat-y', 'reverse', 'ridge', 'right',
164 'round', 'row', 'row-resize', 'row-reverse', 'rtl', 'ruby', 'ruby-base',
165 'ruby-base-container', 'ruby-text', 'ruby-text-container', 'run-in',
166 'running', 's-resize', 'sans-serif', 'saturation', 'scale-down', 'screen',
167 'scroll', 'se-resize', 'semi-condensed', 'semi-expanded', 'separate',
168 'serif', 'sesame', 'show', 'sideways', 'sideways-left', 'sideways-right',
169 'slice', 'small', 'small-caps', 'smaller', 'smooth', 'snap', 'soft-light',
170 'solid', 'space', 'space-around', 'space-between', 'spaces', 'square',
171 'start', 'static', 'step-end', 'step-start', 'sticky', 'stretch', 'strict',
172 'stroke-box', 'style', 'sw-resize', 'table', 'table-caption', 'table-cell',
173 'table-column', 'table-column-group', 'table-footer-group',
174 'table-header-group', 'table-row', 'table-row-group', 'text', 'thick',
175 'thin', 'titling-caps', 'to', 'top', 'triangle', 'ultra-condensed',
176 'ultra-expanded', 'under', 'underline', 'unicase', 'unset', 'upper-alpha',
177 'upper-latin', 'upper-roman', 'uppercase', 'upright', 'use-glyph-orientation',
178 'vertical', 'vertical-text', 'view-box', 'visible', 'w-resize', 'wait',
179 'wavy', 'weight', 'weight style', 'wrap', 'wrap-reverse', 'x-large',
180 'x-small', 'xx-large', 'xx-small', 'zoom-in', 'zoom-out',
181 )
182
183 # List of extended color keywords obtained from:
184 # https://drafts.csswg.org/css-color/#named-colors
185 _color_keywords = (
186 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige',
187 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown',
188 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral',
189 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
190 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki',
191 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
192 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
193 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue',
194 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite',
195 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod',
196 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred',
197 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
198 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
199 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey',
200 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
201 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow',
202 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine',
203 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen',
204 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise',
205 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin',
206 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange',
207 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise',
208 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum',
209 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue',
210 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna',
211 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow',
212 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise',
213 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen',
214 ) + ('transparent',)
215
216 # List of other keyword values from other sources:
217 _other_keyword_values = (
218 'above', 'aural', 'behind', 'bidi-override', 'center-left', 'center-right',
219 'cjk-ideographic', 'continuous', 'crop', 'cross', 'embed', 'far-left',
220 'far-right', 'fast', 'faster', 'hebrew', 'high', 'higher', 'hiragana',
221 'hiragana-iroha', 'katakana', 'katakana-iroha', 'landscape', 'left-side',
222 'leftwards', 'level', 'loud', 'low', 'lower', 'message-box', 'middle',
223 'mix', 'narrower', 'once', 'portrait', 'right-side', 'rightwards', 'silent',
224 'slow', 'slower', 'small-caption', 'soft', 'spell-out', 'status-bar',
225 'super', 'text-bottom', 'text-top', 'wider', 'x-fast', 'x-high', 'x-loud',
226 'x-low', 'x-soft', 'yes', 'pre', 'pre-wrap', 'pre-line',
227 )
228
229 # List of functional notation and function keyword values:
230 _functional_notation_keyword_values = (
231 'attr', 'blackness', 'blend', 'blenda', 'blur', 'brightness', 'calc',
232 'circle', 'color-mod', 'contrast', 'counter', 'cubic-bezier', 'device-cmyk',
233 'drop-shadow', 'ellipse', 'gray', 'grayscale', 'hsl', 'hsla', 'hue',
234 'hue-rotate', 'hwb', 'image', 'inset', 'invert', 'lightness',
235 'linear-gradient', 'matrix', 'matrix3d', 'opacity', 'perspective',
236 'polygon', 'radial-gradient', 'rect', 'repeating-linear-gradient',
237 'repeating-radial-gradient', 'rgb', 'rgba', 'rotate', 'rotate3d', 'rotateX',
238 'rotateY', 'rotateZ', 'saturate', 'saturation', 'scale', 'scale3d',
239 'scaleX', 'scaleY', 'scaleZ', 'sepia', 'shade', 'skewX', 'skewY', 'steps',
240 'tint', 'toggle', 'translate', 'translate3d', 'translateX', 'translateY',
241 'translateZ', 'whiteness',
242 )
243 # Note! Handle url(...) separately.
244
245 # List of units obtained from:
246 # https://www.w3.org/TR/css3-values/
247 _angle_units = (
248 'deg', 'grad', 'rad', 'turn',
249 )
250 _frequency_units = (
251 'Hz', 'kHz',
252 )
253 _length_units = (
254 'em', 'ex', 'ch', 'rem',
255 'vh', 'vw', 'vmin', 'vmax',
256 'px', 'mm', 'cm', 'in', 'pt', 'pc', 'q',
257 )
258 _resolution_units = (
259 'dpi', 'dpcm', 'dppx',
260 )
261 _time_units = (
262 's', 'ms',
263 )
264 _all_units = _angle_units + _frequency_units + _length_units + \
265 _resolution_units + _time_units
266
267
268 class CssLexer(RegexLexer):
269 """
270 For CSS (Cascading Style Sheets).
271 """
272
273 name = 'CSS'
274 aliases = ['css']
275 filenames = ['*.css']
276 mimetypes = ['text/css']
277
278 tokens = {
279 'root': [
280 include('basics'),
281 ],
282 'basics': [
283 (r'\s+', Text),
284 (r'/\*(?:.|\n)*?\*/', Comment),
285 (r'\{', Punctuation, 'content'),
286 (r'(\:{1,2})([\w-]+)', bygroups(Punctuation, Name.Decorator)),
287 (r'(\.)([\w-]+)', bygroups(Punctuation, Name.Class)),
288 (r'(\#)([\w-]+)', bygroups(Punctuation, Name.Namespace)),
289 (r'(@)([\w-]+)', bygroups(Punctuation, Keyword), 'atrule'),
290 (r'[\w-]+', Name.Tag),
291 (r'[~^*!%&$\[\]()<>|+=@:;,./?-]', Operator),
292 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
293 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
294 ],
295 'atrule': [
296 (r'\{', Punctuation, 'atcontent'),
297 (r';', Punctuation, '#pop'),
298 include('basics'),
299 ],
300 'atcontent': [
301 include('basics'),
302 (r'\}', Punctuation, '#pop:2'),
303 ],
304 'content': [
305 (r'\s+', Text),
306 (r'\}', Punctuation, '#pop'),
307 (r';', Punctuation),
308 (r'^@.*?$', Comment.Preproc),
309
310 (words(_vendor_prefixes,), Keyword.Pseudo),
311 (r'('+r'|'.join(_css_properties)+r')(\s*)(\:)',
312 bygroups(Keyword, Text, Punctuation), 'value-start'),
313 (r'([-]+[a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name.Variable, Text, Punctuation),
314 'value-start'),
315 (r'([a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name, Text, Punctuation),
316 'value-start'),
317
318 (r'/\*(?:.|\n)*?\*/', Comment),
319 ],
320 'value-start': [
321 (r'\s+', Text),
322 (words(_vendor_prefixes,), Name.Builtin.Pseudo),
323 include('urls'),
324 (r'('+r'|'.join(_functional_notation_keyword_values)+r')(\()',
325 bygroups(Name.Builtin, Punctuation), 'function-start'),
326 (r'([a-zA-Z_][\w-]+)(\()',
327 bygroups(Name.Function, Punctuation), 'function-start'),
328 (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
329 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
330 (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
331 # for transition-property etc.
332 (words(_css_properties, suffix=r'\b'), Keyword),
333 (r'\!important', Comment.Preproc),
334 (r'/\*(?:.|\n)*?\*/', Comment),
335
336 include('numeric-values'),
337
338 (r'[~^*!%&<>|+=@:./?-]+', Operator),
339 (r'[\[\](),]+', Punctuation),
340 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
341 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
342 (r'[a-zA-Z_][\w-]*', Name),
343 (r';', Punctuation, '#pop'),
344 (r'\}', Punctuation, '#pop:2'),
345 ],
346 'function-start': [
347 (r'\s+', Text),
348 (r'[-]+([\w+]+[-]*)+', Name.Variable),
349 include('urls'),
350 (words(_vendor_prefixes,), Keyword.Pseudo),
351 (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
352 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
353 (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
354
355 # function-start may be entered recursively
356 (r'(' + r'|'.join(_functional_notation_keyword_values) + r')(\()',
357 bygroups(Name.Builtin, Punctuation), 'function-start'),
358 (r'([a-zA-Z_][\w-]+)(\()',
359 bygroups(Name.Function, Punctuation), 'function-start'),
360
361 (r'/\*(?:.|\n)*?\*/', Comment),
362 include('numeric-values'),
363 (r'[*+/-]', Operator),
364 (r',', Punctuation),
365 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
366 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
367 (r'[a-zA-Z_-]\w*', Name),
368 (r'\)', Punctuation, '#pop'),
369 ],
370 'urls': [
371 (r'(url)(\()(".*?")(\))', bygroups(Name.Builtin, Punctuation,
372 String.Double, Punctuation)),
373 (r"(url)(\()('.*?')(\))", bygroups(Name.Builtin, Punctuation,
374 String.Single, Punctuation)),
375 (r'(url)(\()(.*?)(\))', bygroups(Name.Builtin, Punctuation,
376 String.Other, Punctuation)),
377 ],
378 'numeric-values': [
379 (r'\#[a-zA-Z0-9]{1,6}', Number.Hex),
380 (r'[+\-]?[0-9]*[.][0-9]+', Number.Float, 'numeric-end'),
381 (r'[+\-]?[0-9]+', Number.Integer, 'numeric-end'),
382 ],
383 'numeric-end': [
384 (words(_all_units, suffix=r'\b'), Keyword.Type),
385 (r'%', Keyword.Type),
386 default('#pop'),
387 ],
388 }
389
390
391 common_sass_tokens = {
392 'value': [
393 (r'[ \t]+', Text),
394 (r'[!$][\w-]+', Name.Variable),
395 (r'url\(', String.Other, 'string-url'),
396 (r'[a-z_-][\w-]*(?=\()', Name.Function),
397 (words(_css_properties + (
398 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline',
399 'behind', 'below', 'bidi-override', 'blink', 'block', 'bold', 'bolder', 'both',
400 'capitalize', 'center-left', 'center-right', 'center', 'circle',
401 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous',
402 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero',
403 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed',
404 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left',
405 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help',
406 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon',
407 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic',
408 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large',
409 'left-side', 'leftwards', 'level', 'lighter', 'line-through', 'list-item',
410 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr',
411 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace',
412 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote',
413 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once',
414 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px',
415 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side',
416 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize',
417 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent',
418 'slow', 'slower', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid',
419 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize',
420 'table-caption', 'table-cell', 'table-column', 'table-column-group',
421 'table-footer-group', 'table-header-group', 'table-row',
422 'table-row-group', 'text', 'text-bottom', 'text-top', 'thick', 'thin',
423 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline',
424 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url',
425 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud',
426 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'),
427 Name.Constant),
428 (words(_color_keywords, suffix=r'\b'), Name.Entity),
429 (words((
430 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green',
431 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua'), suffix=r'\b'),
432 Name.Builtin),
433 (r'\!(important|default)', Name.Exception),
434 (r'(true|false)', Name.Pseudo),
435 (r'(and|or|not)', Operator.Word),
436 (r'/\*', Comment.Multiline, 'inline-comment'),
437 (r'//[^\n]*', Comment.Single),
438 (r'\#[a-z0-9]{1,6}', Number.Hex),
439 (r'(-?\d+)(\%|[a-z]+)?', bygroups(Number.Integer, Keyword.Type)),
440 (r'(-?\d*\.\d+)(\%|[a-z]+)?', bygroups(Number.Float, Keyword.Type)),
441 (r'#\{', String.Interpol, 'interpolation'),
442 (r'[~^*!&%<>|+=@:,./?-]+', Operator),
443 (r'[\[\]()]+', Punctuation),
444 (r'"', String.Double, 'string-double'),
445 (r"'", String.Single, 'string-single'),
446 (r'[a-z_-][\w-]*', Name),
447 ],
448
449 'interpolation': [
450 (r'\}', String.Interpol, '#pop'),
451 include('value'),
452 ],
453
454 'selector': [
455 (r'[ \t]+', Text),
456 (r'\:', Name.Decorator, 'pseudo-class'),
457 (r'\.', Name.Class, 'class'),
458 (r'\#', Name.Namespace, 'id'),
459 (r'[\w-]+', Name.Tag),
460 (r'#\{', String.Interpol, 'interpolation'),
461 (r'&', Keyword),
462 (r'[~^*!&\[\]()<>|+=@:;,./?-]', Operator),
463 (r'"', String.Double, 'string-double'),
464 (r"'", String.Single, 'string-single'),
465 ],
466
467 'string-double': [
468 (r'(\\.|#(?=[^\n{])|[^\n"#])+', String.Double),
469 (r'#\{', String.Interpol, 'interpolation'),
470 (r'"', String.Double, '#pop'),
471 ],
472
473 'string-single': [
474 (r"(\\.|#(?=[^\n{])|[^\n'#])+", String.Single),
475 (r'#\{', String.Interpol, 'interpolation'),
476 (r"'", String.Single, '#pop'),
477 ],
478
479 'string-url': [
480 (r'(\\#|#(?=[^\n{])|[^\n#)])+', String.Other),
481 (r'#\{', String.Interpol, 'interpolation'),
482 (r'\)', String.Other, '#pop'),
483 ],
484
485 'pseudo-class': [
486 (r'[\w-]+', Name.Decorator),
487 (r'#\{', String.Interpol, 'interpolation'),
488 default('#pop'),
489 ],
490
491 'class': [
492 (r'[\w-]+', Name.Class),
493 (r'#\{', String.Interpol, 'interpolation'),
494 default('#pop'),
495 ],
496
497 'id': [
498 (r'[\w-]+', Name.Namespace),
499 (r'#\{', String.Interpol, 'interpolation'),
500 default('#pop'),
501 ],
502
503 'for': [
504 (r'(from|to|through)', Operator.Word),
505 include('value'),
506 ],
507 }
508
509
510 def _indentation(lexer, match, ctx):
511 indentation = match.group(0)
512 yield match.start(), Text, indentation
513 ctx.last_indentation = indentation
514 ctx.pos = match.end()
515
516 if hasattr(ctx, 'block_state') and ctx.block_state and \
517 indentation.startswith(ctx.block_indentation) and \
518 indentation != ctx.block_indentation:
519 ctx.stack.append(ctx.block_state)
520 else:
521 ctx.block_state = None
522 ctx.block_indentation = None
523 ctx.stack.append('content')
524
525
526 def _starts_block(token, state):
527 def callback(lexer, match, ctx):
528 yield match.start(), token, match.group(0)
529
530 if hasattr(ctx, 'last_indentation'):
531 ctx.block_indentation = ctx.last_indentation
532 else:
533 ctx.block_indentation = ''
534
535 ctx.block_state = state
536 ctx.pos = match.end()
537
538 return callback
539
540
541 class SassLexer(ExtendedRegexLexer):
542 """
543 For Sass stylesheets.
544
545 .. versionadded:: 1.3
546 """
547
548 name = 'Sass'
549 aliases = ['sass']
550 filenames = ['*.sass']
551 mimetypes = ['text/x-sass']
552
553 flags = re.IGNORECASE | re.MULTILINE
554
555 tokens = {
556 'root': [
557 (r'[ \t]*\n', Text),
558 (r'[ \t]*', _indentation),
559 ],
560
561 'content': [
562 (r'//[^\n]*', _starts_block(Comment.Single, 'single-comment'),
563 'root'),
564 (r'/\*[^\n]*', _starts_block(Comment.Multiline, 'multi-comment'),
565 'root'),
566 (r'@import', Keyword, 'import'),
567 (r'@for', Keyword, 'for'),
568 (r'@(debug|warn|if|while)', Keyword, 'value'),
569 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'),
570 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'),
571 (r'@extend', Keyword, 'selector'),
572 (r'@[\w-]+', Keyword, 'selector'),
573 (r'=[\w-]+', Name.Function, 'value'),
574 (r'\+[\w-]+', Name.Decorator, 'value'),
575 (r'([!$][\w-]\w*)([ \t]*(?:(?:\|\|)?=|:))',
576 bygroups(Name.Variable, Operator), 'value'),
577 (r':', Name.Attribute, 'old-style-attr'),
578 (r'(?=.+?[=:]([^a-z]|$))', Name.Attribute, 'new-style-attr'),
579 default('selector'),
580 ],
581
582 'single-comment': [
583 (r'.+', Comment.Single),
584 (r'\n', Text, 'root'),
585 ],
586
587 'multi-comment': [
588 (r'.+', Comment.Multiline),
589 (r'\n', Text, 'root'),
590 ],
591
592 'import': [
593 (r'[ \t]+', Text),
594 (r'\S+', String),
595 (r'\n', Text, 'root'),
596 ],
597
598 'old-style-attr': [
599 (r'[^\s:="\[]+', Name.Attribute),
600 (r'#\{', String.Interpol, 'interpolation'),
601 (r'[ \t]*=', Operator, 'value'),
602 default('value'),
603 ],
604
605 'new-style-attr': [
606 (r'[^\s:="\[]+', Name.Attribute),
607 (r'#\{', String.Interpol, 'interpolation'),
608 (r'[ \t]*[=:]', Operator, 'value'),
609 ],
610
611 'inline-comment': [
612 (r"(\\#|#(?=[^\n{])|\*(?=[^\n/])|[^\n#*])+", Comment.Multiline),
613 (r'#\{', String.Interpol, 'interpolation'),
614 (r"\*/", Comment, '#pop'),
615 ],
616 }
617 for group, common in common_sass_tokens.items():
618 tokens[group] = copy.copy(common)
619 tokens['value'].append((r'\n', Text, 'root'))
620 tokens['selector'].append((r'\n', Text, 'root'))
621
622
623 class ScssLexer(RegexLexer):
624 """
625 For SCSS stylesheets.
626 """
627
628 name = 'SCSS'
629 aliases = ['scss']
630 filenames = ['*.scss']
631 mimetypes = ['text/x-scss']
632
633 flags = re.IGNORECASE | re.DOTALL
634 tokens = {
635 'root': [
636 (r'\s+', Text),
637 (r'//.*?\n', Comment.Single),
638 (r'/\*.*?\*/', Comment.Multiline),
639 (r'@import', Keyword, 'value'),
640 (r'@for', Keyword, 'for'),
641 (r'@(debug|warn|if|while)', Keyword, 'value'),
642 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'),
643 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'),
644 (r'@extend', Keyword, 'selector'),
645 (r'(@media)(\s+)', bygroups(Keyword, Text), 'value'),
646 (r'@[\w-]+', Keyword, 'selector'),
647 (r'(\$[\w-]*\w)([ \t]*:)', bygroups(Name.Variable, Operator), 'value'),
648 # TODO: broken, and prone to infinite loops.
649 # (r'(?=[^;{}][;}])', Name.Attribute, 'attr'),
650 # (r'(?=[^;{}:]+:[^a-z])', Name.Attribute, 'attr'),
651 default('selector'),
652 ],
653
654 'attr': [
655 (r'[^\s:="\[]+', Name.Attribute),
656 (r'#\{', String.Interpol, 'interpolation'),
657 (r'[ \t]*:', Operator, 'value'),
658 default('#pop'),
659 ],
660
661 'inline-comment': [
662 (r"(\\#|#(?=[^{])|\*(?=[^/])|[^#*])+", Comment.Multiline),
663 (r'#\{', String.Interpol, 'interpolation'),
664 (r"\*/", Comment, '#pop'),
665 ],
666 }
667 for group, common in common_sass_tokens.items():
668 tokens[group] = copy.copy(common)
669 tokens['value'].extend([(r'\n', Text), (r'[;{}]', Punctuation, '#pop')])
670 tokens['selector'].extend([(r'\n', Text), (r'[;{}]', Punctuation, '#pop')])
671
672
673 class LessCssLexer(CssLexer):
674 """
675 For `LESS <http://lesscss.org/>`_ styleshets.
676
677 .. versionadded:: 2.1
678 """
679
680 name = 'LessCss'
681 aliases = ['less']
682 filenames = ['*.less']
683 mimetypes = ['text/x-less-css']
684
685 tokens = {
686 'root': [
687 (r'@\w+', Name.Variable),
688 inherit,
689 ],
690 'content': [
691 (r'\{', Punctuation, '#push'),
692 inherit,
693 ],
694 }

eric ide

mercurial