ui: add support for blink style attribute
[vis.git] / lexers / lisp.lua
blob2d8a9078eda1be2ca74e57a8f78fe9e5f89cdb93
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Lisp LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'lisp'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = ';' * l.nonnewline^0
15 local block_comment = '#|' * (l.any - '|#')^0 * P('|#')^-1
16 local comment = token(l.COMMENT, line_comment + block_comment)
18 local word = l.alpha * (l.alnum + '_' + '-')^0
20 -- Strings.
21 local literal = "'" * word
22 local dq_str = l.delimited_range('"')
23 local string = token(l.STRING, literal + dq_str)
25 -- Numbers.
26 local number = token(l.NUMBER, P('-')^-1 * l.digit^1 * (S('./') * l.digit^1)^-1)
28 -- Keywords.
29 local keyword = token(l.KEYWORD, word_match({
30 'defclass', 'defconstant', 'defgeneric', 'define-compiler-macro',
31 'define-condition', 'define-method-combination', 'define-modify-macro',
32 'define-setf-expander', 'define-symbol-macro', 'defmacro', 'defmethod',
33 'defpackage', 'defparameter', 'defsetf', 'defstruct', 'deftype', 'defun',
34 'defvar',
35 'abort', 'assert', 'block', 'break', 'case', 'catch', 'ccase', 'cerror',
36 'cond', 'ctypecase', 'declaim', 'declare', 'do', 'do*', 'do-all-symbols',
37 'do-external-symbols', 'do-symbols', 'dolist', 'dotimes', 'ecase', 'error',
38 'etypecase', 'eval-when', 'flet', 'handler-bind', 'handler-case', 'if',
39 'ignore-errors', 'in-package', 'labels', 'lambda', 'let', 'let*', 'locally',
40 'loop', 'macrolet', 'multiple-value-bind', 'proclaim', 'prog', 'prog*',
41 'prog1', 'prog2', 'progn', 'progv', 'provide', 'require', 'restart-bind',
42 'restart-case', 'restart-name', 'return', 'return-from', 'signal',
43 'symbol-macrolet', 'tagbody', 'the', 'throw', 'typecase', 'unless',
44 'unwind-protect', 'when', 'with-accessors', 'with-compilation-unit',
45 'with-condition-restarts', 'with-hash-table-iterator',
46 'with-input-from-string', 'with-open-file', 'with-open-stream',
47 'with-output-to-string', 'with-package-iterator', 'with-simple-restart',
48 'with-slots', 'with-standard-io-syntax',
49 't', 'nil'
50 }, '-'))
52 -- Identifiers.
53 local identifier = token(l.IDENTIFIER, word)
55 -- Operators.
56 local operator = token(l.OPERATOR, S('<>=*/+-`@%()'))
58 -- Entities.
59 local entity = token('entity', '&' * word)
61 M._rules = {
62 {'whitespace', ws},
63 {'keyword', keyword},
64 {'identifier', identifier},
65 {'string', string},
66 {'comment', comment},
67 {'number', number},
68 {'operator', operator},
69 {'entity', entity},
72 M._tokenstyles = {
73 entity = l.STYLE_VARIABLE
76 M._foldsymbols = {
77 _patterns = {'[%(%)%[%]{}]', '#|', '|#', ';'},
78 [l.OPERATOR] = {
79 ['('] = 1, [')'] = -1, ['['] = 1, [']'] = -1, ['{'] = 1, ['}'] = -1
81 [l.COMMENT] = {['#|'] = 1, ['|#'] = -1, [';'] = l.fold_line_comments(';')}
84 return M