vis: move text object definitions to separate file
[vis.git] / lexers / css.lua
blobcb3e26befe93f4d9be3e75d3857cd22aebeb0c17
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- CSS LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
8 local M = {_NAME = 'css'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local comment = token(l.COMMENT, '/*' * (l.any - '*/')^0 * P('*/')^-1)
16 -- Strings.
17 local sq_str = l.delimited_range("'")
18 local dq_str = l.delimited_range('"')
19 local string = token(l.STRING, sq_str + dq_str)
21 -- Numbers.
22 local number = token(l.NUMBER, l.digit^1)
24 -- Keywords.
25 local css1_property = word_match({
26 'color', 'background-color', 'background-image', 'background-repeat',
27 'background-attachment', 'background-position', 'background', 'font-family',
28 'font-style', 'font-variant', 'font-weight', 'font-size', 'font',
29 'word-spacing', 'letter-spacing', 'text-decoration', 'vertical-align',
30 'text-transform', 'text-align', 'text-indent', 'line-height', 'margin-top',
31 'margin-right', 'margin-bottom', 'margin-left', 'margin', 'padding-top',
32 'padding-right', 'padding-bottom', 'padding-left', 'padding',
33 'border-top-width', 'border-right-width', 'border-bottom-width',
34 'border-left-width', 'border-width', 'border-top', 'border-right',
35 'border-bottom', 'border-left', 'border', 'border-color', 'border-style',
36 'width', 'height', 'float', 'clear', 'display', 'white-space',
37 'list-style-type', 'list-style-image', 'list-style-position', 'list-style'
38 }, '-')
39 local css1_value = word_match({
40 'auto', 'none', 'normal', 'italic', 'oblique', 'small-caps', 'bold', 'bolder',
41 'lighter', 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large',
42 'xx-large', 'larger', 'smaller', 'transparent', 'repeat', 'repeat-x',
43 'repeat-y', 'no-repeat', 'scroll', 'fixed', 'top', 'bottom', 'left', 'center',
44 'right', 'justify', 'both', 'underline', 'overline', 'line-through', 'blink',
45 'baseline', 'sub', 'super', 'text-top', 'middle', 'text-bottom', 'capitalize',
46 'uppercase', 'lowercase', 'thin', 'medium', 'thick', 'dotted', 'dashed',
47 'solid', 'double', 'groove', 'ridge', 'inset', 'outset', 'block', 'inline',
48 'list-item', 'pre', 'no-wrap', 'inside', 'outside', 'disc', 'circle',
49 'square', 'decimal', 'lower-roman', 'upper-roman', 'lower-alpha',
50 'upper-alpha', 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
51 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'white',
52 'yellow'
53 }, '-')
54 local css2_property = word_match({
55 'border-top-color', 'border-right-color', 'border-bottom-color',
56 'border-left-color', 'border-color', 'border-top-style', 'border-right-style',
57 'border-bottom-style', 'border-left-style', 'border-style', 'top', 'right',
58 'bottom', 'left', 'position', 'z-index', 'direction', 'unicode-bidi',
59 'min-width', 'max-width', 'min-height', 'max-height', 'overflow', 'clip',
60 'visibility', 'content', 'quotes', 'counter-reset', 'counter-increment',
61 'marker-offset', 'size', 'marks', 'page-break-before', 'page-break-after',
62 'page-break-inside', 'page', 'orphans', 'widows', 'font-stretch',
63 'font-size-adjust', 'unicode-range', 'units-per-em', 'src', 'panose-1',
64 'stemv', 'stemh', 'slope', 'cap-height', 'x-height', 'ascent', 'descent',
65 'widths', 'bbox', 'definition-src', 'baseline', 'centerline', 'mathline',
66 'topline', 'text-shadow', 'caption-side', 'table-layout', 'border-collapse',
67 'border-spacing', 'empty-cells', 'speak-header', 'cursor', 'outline',
68 'outline-width', 'outline-style', 'outline-color', 'volume', 'speak',
69 'pause-before', 'pause-after', 'pause', 'cue-before', 'cue-after', 'cue',
70 'play-during', 'azimuth', 'elevation', 'speech-rate', 'voice-family', 'pitch',
71 'pitch-range', 'stress', 'richness', 'speak-punctuation', 'speak-numeral'
72 }, '-')
73 local css2_value = word_match({
74 'inherit', 'run-in', 'compact', 'marker', 'table', 'inline-table',
75 'table-row-group', 'table-header-group', 'table-footer-group', 'table-row',
76 'table-column-group', 'table-column', 'table-cell', 'table-caption', 'static',
77 'relative', 'absolute', 'fixed', 'ltr', 'rtl', 'embed', 'bidi-override',
78 'visible', 'hidden', 'scroll', 'collapse', 'open-quote', 'close-quote',
79 'no-open-quote', 'no-close-quote', 'decimal-leading-zero', 'lower-greek',
80 'lower-latin', 'upper-latin', 'hebrew', 'armenian', 'georgian',
81 'cjk-ideographic', 'hiragana', 'katakana', 'hiragana-iroha', 'katakana-iroha',
82 'landscape', 'portrait', 'crop', 'cross', 'always', 'avoid', 'wider',
83 'narrower', 'ultra-condensed', 'extra-condensed', 'condensed',
84 'semi-condensed', 'semi-expanded', 'expanded', 'extra-expanded',
85 'ultra-expanded', 'caption', 'icon', 'menu', 'message-box', 'small-caption',
86 'status-bar', 'separate', 'show', 'hide', 'once', 'crosshair', 'default',
87 'pointer', 'move', 'text', 'wait', 'help', 'e-resize', 'ne-resize',
88 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize',
89 'ActiveBorder', 'ActiveCaption', 'AppWorkspace', 'Background', 'ButtonFace',
90 'ButtonHighlight', 'ButtonShadow', 'InactiveCaptionText', 'ButtonText',
91 'CaptionText', 'GrayText', 'Highlight', 'HighlightText', 'InactiveBorder',
92 'InactiveCaption', 'InfoBackground', 'InfoText', 'Menu', 'MenuText',
93 'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight',
94 'ThreeDLightShadow', 'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText',
95 'silent', 'x-soft', 'soft', 'medium', 'loud', 'x-loud', 'spell-out', 'mix',
96 'left-side', 'far-left', 'center-left', 'center-right', 'far-right',
97 'right-side', 'behind', 'leftwards', 'rightwards', 'below', 'level', 'above',
98 'higher', 'lower', 'x-slow', 'slow', 'medium', 'fast', 'x-fast', 'faster',
99 'slower', 'male', 'female', 'child', 'x-low', 'low', 'high', 'x-high', 'code',
100 'digits', 'continous'
101 }, '-')
102 local property = token(l.KEYWORD, css1_property + css2_property)
103 local value = token('value', css1_value + css2_value)
104 local keyword = property + value
106 -- Identifiers.
107 local identifier = token(l.IDENTIFIER, l.alpha * (l.alnum + S('_-'))^0)
109 -- Operators.
110 local operator = token(l.OPERATOR, S('~!#*>+=|.,:;()[]{}'))
112 -- At rule.
113 local at_rule = token('at_rule', P('@') * word_match{
114 'charset', 'font-face', 'media', 'page', 'import'
117 -- Colors.
118 local xdigit = l.xdigit
119 local hex_color = '#' * xdigit * xdigit * xdigit * (xdigit * xdigit * xdigit)^-1
120 local color_name = word_match{
121 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy',
122 'olive', 'orange', 'purple', 'red', 'silver', 'teal', 'white', 'yellow'
124 local color = token('color', hex_color + color_name)
126 -- Pseudo.
127 local pseudo = token(l.CONSTANT, word_match({
128 -- Pseudo elements.
129 'first-line', 'first-letter', 'before', 'after',
130 -- Pseudo classes.
131 'first-child', 'link', 'visited', 'hover', 'active', 'focus', 'lang',
132 }, '-'))
134 -- Units.
135 local unit = token('unit', word_match{
136 'em', 'ex', 'px', 'pt', 'pc', 'in', 'ft', 'mm', 'cm', 'kHz', 'Hz', 'deg',
137 'rad', 'grad', 'ms', 's'
138 } + '%')
140 M._rules = {
141 {'whitespace', ws},
142 {'keyword', keyword},
143 {'pseudo', pseudo},
144 {'color', color},
145 {'identifier', identifier},
146 {'string', string},
147 {'comment', comment},
148 {'number', number * unit^-1},
149 {'operator', operator},
150 {'at_rule', at_rule},
153 M._tokenstyles = {
154 unit = l.STYLE_LABEL,
155 value = l.STYLE_CONSTANT,
156 color = l.STYLE_NUMBER,
157 at_rule = l.STYLE_PREPROCESSOR
160 M._foldsymbols = {
161 _patterns = {'[{}]', '/%*', '%*/'},
162 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
163 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1}
166 return M