vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

css.lua

(12222B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- CSS LPeg lexer.
      3 
      4 local lexer = lexer
      5 local P, S, B = lpeg.P, lpeg.S, lpeg.B
      6 
      7 local lex = lexer.new(..., {no_user_word_lists = true})
      8 
      9 -- Tags.
     10 local sel_prefix = B(S('#.'))
     11 local sel_suffix = lexer.space^0 * S('!>+.,:[{')
     12 lex:add_rule('tag', -sel_prefix * lex:tag(lexer.TAG, lex:word_match(lexer.TAG) * #sel_suffix))
     13 
     14 -- Properties.
     15 lex:add_rule('property', lex:tag('property', lex:word_match('property')) * #P(':'))
     16 
     17 -- Values.
     18 lex:add_rule('value',
     19 	-sel_prefix * lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match('value')) * -sel_suffix)
     20 
     21 -- Functions.
     22 lex:add_rule('function', lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN)))
     23 
     24 -- Colors.
     25 local color_name = lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match('color'))
     26 local xdigit = lexer.xdigit
     27 local color_value = lex:tag(lexer.NUMBER,
     28 	'#' * xdigit * xdigit * xdigit * (xdigit * xdigit * xdigit)^-1)
     29 lex:add_rule('color', color_name + color_value)
     30 
     31 -- Identifiers.
     32 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.alpha * (lexer.alnum + S('_-'))^0))
     33 
     34 -- Pseudo classes and pseudo elements.
     35 lex:add_rule('pseudoclass', ':' * lex:tag('pseudoclass', lex:word_match('pseudoclass')))
     36 lex:add_rule('pseudoelement', '::' * lex:tag('pseudoelement', lex:word_match('pseudoelement')))
     37 
     38 -- Strings.
     39 local sq_str = lexer.range("'")
     40 local dq_str = lexer.range('"')
     41 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str))
     42 
     43 -- Comments.
     44 lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.range('/*', '*/')))
     45 
     46 -- Numbers.
     47 local unit = lex:word_match('unit') + '%'
     48 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number * unit^-1))
     49 
     50 -- Operators.
     51 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('~!#*>+=|.,:;()[]{}')))
     52 
     53 -- At rule.
     54 lex:add_rule('at_rule', lex:tag(lexer.PREPROCESSOR, '@' * lex:word_match('at_rule')))
     55 
     56 -- Fold points.
     57 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     58 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     59 
     60 -- Word lists.
     61 lex:set_word_list(lexer.TAG, {
     62 	'a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'blockquote', 'body',
     63 	'button', 'canvas', 'caption', 'cite', 'code', 'colgroup', 'content', 'data', 'datalist', 'dd',
     64 	'decorator', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'element', 'em', 'fieldset',
     65 	'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header',
     66 	'html', 'i', 'iframe', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'menu',
     67 	'menuitem', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p',
     68 	'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'shadow',
     69 	'small', 'spacer', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td',
     70 	'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'u', 'ul', 'var', 'video', --
     71 	'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta',
     72 	'param', 'source', 'track', 'wbr'
     73 })
     74 
     75 lex:set_word_list('property', {
     76 	-- CSS 1.
     77 	'color', 'background-color', 'background-image', 'background-repeat', 'background-attachment',
     78 	'background-position', 'background', 'font-family', 'font-style', 'font-variant', 'font-weight',
     79 	'font-size', 'font', 'word-spacing', 'letter-spacing', 'text-decoration', 'vertical-align',
     80 	'text-transform', 'text-align', 'text-indent', 'line-height', 'margin-top', 'margin-right',
     81 	'margin-bottom', 'margin-left', 'margin', 'padding-top', 'padding-right', 'padding-bottom',
     82 	'padding-left', 'padding', 'border-top-width', 'border-right-width', 'border-bottom-width',
     83 	'border-left-width', 'border-width', 'border-top', 'border-right', 'border-bottom', 'border-left',
     84 	'border', 'border-color', 'border-style', 'width', 'height', 'float', 'clear', 'display',
     85 	'white-space', 'list-style-type', 'list-style-image', 'list-style-position', 'list-style',
     86 	-- CSS 2.
     87 	'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color',
     88 	'border-color', 'border-top-style', 'border-right-style', 'border-bottom-style',
     89 	'border-left-style', 'border-style', 'top', 'right', 'bottom', 'left', 'position', 'z-index',
     90 	'direction', 'unicode-bidi', 'min-width', 'max-width', 'min-height', 'max-height', 'overflow',
     91 	'clip', 'visibility', 'content', 'quotes', 'counter-reset', 'counter-increment', 'marker-offset',
     92 	'size', 'marks', 'page-break-before', 'page-break-after', 'page-break-inside', 'page', 'orphans',
     93 	'widows', 'font-stretch', 'font-size-adjust', 'unicode-range', 'units-per-em', 'src', 'panose-1',
     94 	'stemv', 'stemh', 'slope', 'cap-height', 'x-height', 'ascent', 'descent', 'widths', 'bbox',
     95 	'definition-src', 'baseline', 'centerline', 'mathline', 'topline', 'text-shadow', 'caption-side',
     96 	'table-layout', 'border-collapse', 'border-spacing', 'empty-cells', 'speak-header', 'cursor',
     97 	'outline', 'outline-width', 'outline-style', 'outline-color', 'volume', 'speak', 'pause-before',
     98 	'pause-after', 'pause', 'cue-before', 'cue-after', 'cue', 'play-during', 'azimuth', 'elevation',
     99 	'speech-rate', 'voice-family', 'pitch', 'pitch-range', 'stress', 'richness', 'speak-punctuation',
    100 	'speak-numeral',
    101 	-- CSS 3.
    102 	'flex', 'flex-basis', 'flex-direction', 'flex-flow', 'flex-grow', 'flex-shrink', 'flex-wrap',
    103 	'align-content', 'align-items', 'align-self', 'justify-content', 'order', 'border-radius',
    104 	'transition', 'transform', 'box-shadow', 'filter', 'opacity', 'resize', 'word-break', 'word-wrap',
    105 	'box-sizing', 'animation', 'text-overflow'
    106 })
    107 
    108 lex:set_word_list('value', {
    109 	-- CSS 1.
    110 	'auto', 'none', 'normal', 'italic', 'oblique', 'small-caps', 'bold', 'bolder', 'lighter',
    111 	'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller',
    112 	'transparent', 'repeat', 'repeat-x', 'repeat-y', 'no-repeat', 'scroll', 'fixed', 'top', 'bottom',
    113 	'left', 'center', 'right', 'justify', 'both', 'underline', 'overline', 'line-through', 'blink',
    114 	'baseline', 'sub', 'super', 'text-top', 'middle', 'text-bottom', 'capitalize', 'uppercase',
    115 	'lowercase', 'thin', 'medium', 'thick', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge',
    116 	'inset', 'outset', 'block', 'inline', 'list-item', 'pre', 'no-wrap', 'inside', 'outside', 'disc',
    117 	'circle', 'square', 'decimal', 'lower-roman', 'upper-roman', 'lower-alpha', 'upper-alpha', 'aqua',
    118 	'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red',
    119 	'silver', 'teal', 'white', 'yellow',
    120 	-- CSS 2.
    121 	'inherit', 'run-in', 'compact', 'marker', 'table', 'inline-table', 'table-row-group',
    122 	'table-header-group', 'table-footer-group', 'table-row', 'table-column-group', 'table-column',
    123 	'table-cell', 'table-caption', 'static', 'relative', 'absolute', 'fixed', 'ltr', 'rtl', 'embed',
    124 	'bidi-override', 'visible', 'hidden', 'scroll', 'collapse', 'open-quote', 'close-quote',
    125 	'no-open-quote', 'no-close-quote', 'decimal-leading-zero', 'lower-greek', 'lower-latin',
    126 	'upper-latin', 'hebrew', 'armenian', 'georgian', 'cjk-ideographic', 'hiragana', 'katakana',
    127 	'hiragana-iroha', 'katakana-iroha', 'landscape', 'portrait', 'crop', 'cross', 'always', 'avoid',
    128 	'wider', 'narrower', 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed',
    129 	'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded', 'caption', 'icon', 'menu',
    130 	'message-box', 'small-caption', 'status-bar', 'separate', 'show', 'hide', 'once', 'crosshair',
    131 	'default', 'pointer', 'move', 'text', 'wait', 'help', 'e-resize', 'ne-resize', 'nw-resize',
    132 	'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ActiveBorder', 'ActiveCaption',
    133 	'AppWorkspace', 'Background', 'ButtonFace', 'ButtonHighlight', 'ButtonShadow',
    134 	'InactiveCaptionText', 'ButtonText', 'CaptionText', 'GrayText', 'Highlight', 'HighlightText',
    135 	'InactiveBorder', 'InactiveCaption', 'InfoBackground', 'InfoText', 'Menu', 'MenuText',
    136 	'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight', 'ThreeDLightShadow',
    137 	'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText', 'silent', 'x-soft', 'soft', 'medium',
    138 	'loud', 'x-loud', 'spell-out', 'mix', 'left-side', 'far-left', 'center-left', 'center-right',
    139 	'far-right', 'right-side', 'behind', 'leftwards', 'rightwards', 'below', 'level', 'above',
    140 	'higher', 'lower', 'x-slow', 'slow', 'medium', 'fast', 'x-fast', 'faster', 'slower', 'male',
    141 	'female', 'child', 'x-low', 'low', 'high', 'x-high', 'code', 'digits', 'continous',
    142 	-- CSS 3.
    143 	'flex', 'row', 'column', 'ellipsis', 'inline-block'
    144 })
    145 
    146 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
    147 	'attr', 'blackness', 'blend', 'blenda', 'blur', 'brightness', 'calc', 'circle', 'color-mod',
    148 	'contrast', 'counter', 'cubic-bezier', 'device-cmyk', 'drop-shadow', 'ellipse', 'gray',
    149 	'grayscale', 'hsl', 'hsla', 'hue', 'hue-rotate', 'hwb', 'image', 'inset', 'invert', 'lightness',
    150 	'linear-gradient', 'matrix', 'matrix3d', 'opacity', 'perspective', 'polygon', 'radial-gradient',
    151 	'rect', 'repeating-linear-gradient', 'repeating-radial-gradient', 'rgb', 'rgba', 'rotate',
    152 	'rotate3d', 'rotateX', 'rotateY', 'rotateZ', 'saturate', 'saturation', 'scale', 'scale3d',
    153 	'scaleX', 'scaleY', 'scaleZ', 'sepia', 'shade', 'skewX', 'skewY', 'steps', 'tint', 'toggle',
    154 	'translate', 'translate3d', 'translateX', 'translateY', 'translateZ', 'url', 'whiteness', 'var'
    155 })
    156 
    157 lex:set_word_list('color', {
    158 	'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black',
    159 	'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse',
    160 	'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
    161 	'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta',
    162 	'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen',
    163 	'darkslateblue', 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink',
    164 	'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen',
    165 	'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey',
    166 	'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush',
    167 	'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow',
    168 	'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen',
    169 	'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime',
    170 	'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid',
    171 	'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise',
    172 	'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy',
    173 	'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen',
    174 	'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue',
    175 	'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown',
    176 	'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey',
    177 	'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'transparent',
    178 	'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen'
    179 })
    180 
    181 lex:set_word_list('pseudoclass', {
    182 	'active', 'checked', 'disabled', 'empty', 'enabled', 'first-child', 'first-of-type', 'focus',
    183 	'hover', 'in-range', 'invalid', 'lang', 'last-child', 'last-of-type', 'link', 'not', 'nth-child',
    184 	'nth-last-child', 'nth-last-of-type', 'nth-of-type', 'only-of-type', 'only-child', 'optional',
    185 	'out-of-range', 'read-only', 'read-write', 'required', 'root', 'target', 'valid', 'visited'
    186 })
    187 
    188 lex:set_word_list('pseudoelement', 'after before first-letter first-line selection')
    189 
    190 lex:set_word_list('unit', {
    191 	'ch', 'cm', 'deg', 'dpcm', 'dpi', 'dppx', 'em', 'ex', 'grad', 'Hz', 'in', 'kHz', 'mm', 'ms', 'pc',
    192 	'pt', 'px', 'q', 'rad', 'rem', 's', 'turn', 'vh', 'vmax', 'vmin', 'vw'
    193 })
    194 
    195 lex:set_word_list('at_rule', 'charset font-face media page import namespace keyframes')
    196 
    197 lexer.property['scintillua.comment'] = '/*|*/'
    198 lexer.property['scintillua.word.chars'] =
    199 	'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-'
    200 
    201 return lex