diff options
Diffstat (limited to 'lua/lexers/lisp.lua')
| -rw-r--r-- | lua/lexers/lisp.lua | 114 |
1 files changed, 46 insertions, 68 deletions
diff --git a/lua/lexers/lisp.lua b/lua/lexers/lisp.lua index 824e561..6a0e680 100644 --- a/lua/lexers/lisp.lua +++ b/lua/lexers/lisp.lua @@ -1,84 +1,62 @@ --- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE. +-- Copyright 2006-2022 Mitchell. See LICENSE. -- Lisp LPeg lexer. -local l = require('lexer') -local token, word_match = l.token, l.word_match -local P, R, S = lpeg.P, lpeg.R, lpeg.S +local lexer = require('lexer') +local token, word_match = lexer.token, lexer.word_match +local P, S = lpeg.P, lpeg.S -local M = {_NAME = 'lisp'} +local lex = lexer.new('lisp') -- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) - --- Comments. -local line_comment = ';' * l.nonnewline^0 -local block_comment = '#|' * (l.any - '|#')^0 * P('|#')^-1 -local comment = token(l.COMMENT, line_comment + block_comment) - -local word = l.alpha * (l.alnum + '_' + '-')^0 - --- Strings. -local literal = "'" * word -local dq_str = l.delimited_range('"') -local string = token(l.STRING, literal + dq_str) - --- Numbers. -local number = token(l.NUMBER, P('-')^-1 * l.digit^1 * (S('./') * l.digit^1)^-1) +lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) -- Keywords. -local keyword = token(l.KEYWORD, word_match({ - 'defclass', 'defconstant', 'defgeneric', 'define-compiler-macro', - 'define-condition', 'define-method-combination', 'define-modify-macro', - 'define-setf-expander', 'define-symbol-macro', 'defmacro', 'defmethod', - 'defpackage', 'defparameter', 'defsetf', 'defstruct', 'deftype', 'defun', - 'defvar', - 'abort', 'assert', 'block', 'break', 'case', 'catch', 'ccase', 'cerror', - 'cond', 'ctypecase', 'declaim', 'declare', 'do', 'do*', 'do-all-symbols', - 'do-external-symbols', 'do-symbols', 'dolist', 'dotimes', 'ecase', 'error', - 'etypecase', 'eval-when', 'flet', 'handler-bind', 'handler-case', 'if', - 'ignore-errors', 'in-package', 'labels', 'lambda', 'let', 'let*', 'locally', - 'loop', 'macrolet', 'multiple-value-bind', 'proclaim', 'prog', 'prog*', - 'prog1', 'prog2', 'progn', 'progv', 'provide', 'require', 'restart-bind', - 'restart-case', 'restart-name', 'return', 'return-from', 'signal', - 'symbol-macrolet', 'tagbody', 'the', 'throw', 'typecase', 'unless', - 'unwind-protect', 'when', 'with-accessors', 'with-compilation-unit', - 'with-condition-restarts', 'with-hash-table-iterator', - 'with-input-from-string', 'with-open-file', 'with-open-stream', - 'with-output-to-string', 'with-package-iterator', 'with-simple-restart', - 'with-slots', 'with-standard-io-syntax', +lex:add_rule('keyword', token(lexer.KEYWORD, word_match{ + 'defclass', 'defconstant', 'defgeneric', 'define-compiler-macro', 'define-condition', + 'define-method-combination', 'define-modify-macro', 'define-setf-expander', 'define-symbol-macro', + 'defmacro', 'defmethod', 'defpackage', 'defparameter', 'defsetf', 'defstruct', 'deftype', 'defun', + 'defvar', -- + 'abort', 'assert', 'block', 'break', 'case', 'catch', 'ccase', 'cerror', 'cond', 'ctypecase', + 'declaim', 'declare', 'do', 'do*', 'do-all-symbols', 'do-external-symbols', 'do-symbols', + 'dolist', 'dotimes', 'ecase', 'error', 'etypecase', 'eval-when', 'flet', 'handler-bind', + 'handler-case', 'if', 'ignore-errors', 'in-package', 'labels', 'lambda', 'let', 'let*', 'locally', + 'loop', 'macrolet', 'multiple-value-bind', 'proclaim', 'prog', 'prog*', 'prog1', 'prog2', 'progn', + 'progv', 'provide', 'require', 'restart-bind', 'restart-case', 'restart-name', 'return', + 'return-from', 'signal', 'symbol-macrolet', 'tagbody', 'the', 'throw', 'typecase', 'unless', + 'unwind-protect', 'when', 'with-accessors', 'with-compilation-unit', 'with-condition-restarts', + 'with-hash-table-iterator', 'with-input-from-string', 'with-open-file', 'with-open-stream', + 'with-output-to-string', 'with-package-iterator', 'with-simple-restart', 'with-slots', + 'with-standard-io-syntax', -- 't', 'nil' -}, '-')) +})) -- Identifiers. -local identifier = token(l.IDENTIFIER, word) +local word = lexer.alpha * (lexer.alnum + '_' + '-')^0 +lex:add_rule('identifier', token(lexer.IDENTIFIER, word)) --- Operators. -local operator = token(l.OPERATOR, S('<>=*/+-`@%()')) +-- Strings. +lex:add_rule('string', token(lexer.STRING, "'" * word + lexer.range('"') + '#\\' * lexer.any)) --- Entities. -local entity = token('entity', '&' * word) +-- Comments. +local line_comment = lexer.to_eol(';') +local block_comment = lexer.range('#|', '|#') +lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment)) -M._rules = { - {'whitespace', ws}, - {'keyword', keyword}, - {'identifier', identifier}, - {'string', string}, - {'comment', comment}, - {'number', number}, - {'operator', operator}, - {'entity', entity}, -} +-- Numbers. +lex:add_rule('number', token(lexer.NUMBER, P('-')^-1 * lexer.digit^1 * (S('./') * lexer.digit^1)^-1)) -M._tokenstyles = { - entity = l.STYLE_VARIABLE -} +-- Entities. +lex:add_rule('entity', token('entity', '&' * word)) +lex:add_style('entity', lexer.styles.variable) + +-- Operators. +lex:add_rule('operator', token(lexer.OPERATOR, S('<>=*/+-`@%()'))) -M._foldsymbols = { - _patterns = {'[%(%)%[%]{}]', '#|', '|#', ';'}, - [l.OPERATOR] = { - ['('] = 1, [')'] = -1, ['['] = 1, [']'] = -1, ['{'] = 1, ['}'] = -1 - }, - [l.COMMENT] = {['#|'] = 1, ['|#'] = -1, [';'] = l.fold_line_comments(';')} -} +-- Fold points. +lex:add_fold_point(lexer.OPERATOR, '(', ')') +lex:add_fold_point(lexer.OPERATOR, '[', ']') +lex:add_fold_point(lexer.OPERATOR, '{', '}') +lex:add_fold_point(lexer.COMMENT, '#|', '|#') +lex:add_fold_point(lexer.COMMENT, lexer.fold_consecutive_lines(';')) -return M +return lex |
