vis

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

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

lisp.lua

(2597B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- Lisp LPeg lexer.
      3 
      4 local lexer = lexer
      5 local P, S = lpeg.P, lpeg.S
      6 
      7 local lex = lexer.new(...)
      8 
      9 -- Keywords.
     10 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
     11 
     12 -- Identifiers.
     13 local word = lexer.alpha * (lexer.alnum + S('_-'))^0
     14 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, word))
     15 
     16 -- Strings.
     17 local character = lexer.word_match{
     18 	'backspace', 'linefeed', 'newline', 'page', 'return', 'rubout', 'space', 'tab'
     19 } + 'x' * lexer.xdigit^1 + lexer.any
     20 lex:add_rule('string', lex:tag(lexer.STRING, "'" * word + lexer.range('"') + '#\\' * character))
     21 
     22 -- Comments.
     23 local line_comment = lexer.to_eol(';')
     24 local block_comment = lexer.range('#|', '|#')
     25 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
     26 
     27 -- Numbers.
     28 lex:add_rule('number',
     29 	lex:tag(lexer.NUMBER, P('-')^-1 * lexer.digit^1 * (S('./') * lexer.digit^1)^-1))
     30 
     31 -- Operators.
     32 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('<>=*/+-`@%()')))
     33 
     34 -- Fold points.
     35 lex:add_fold_point(lexer.OPERATOR, '(', ')')
     36 lex:add_fold_point(lexer.OPERATOR, '[', ']')
     37 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     38 lex:add_fold_point(lexer.COMMENT, '#|', '|#')
     39 
     40 -- Word lists
     41 lex:set_word_list(lexer.KEYWORD, {
     42 	'defclass', 'defconstant', 'defgeneric', 'define-compiler-macro', 'define-condition',
     43 	'define-method-combination', 'define-modify-macro', 'define-setf-expander', 'define-symbol-macro',
     44 	'defmacro', 'defmethod', 'defpackage', 'defparameter', 'defsetf', 'defstruct', 'deftype', 'defun',
     45 	'defvar', --
     46 	'abort', 'assert', 'block', 'break', 'case', 'catch', 'ccase', 'cerror', 'cond', 'ctypecase',
     47 	'declaim', 'declare', 'do', 'do*', 'do-all-symbols', 'do-external-symbols', 'do-symbols',
     48 	'dolist', 'dotimes', 'ecase', 'error', 'etypecase', 'eval-when', 'flet', 'handler-bind',
     49 	'handler-case', 'if', 'ignore-errors', 'in-package', 'labels', 'lambda', 'let', 'let*', 'locally',
     50 	'loop', 'macrolet', 'multiple-value-bind', 'proclaim', 'prog', 'prog*', 'prog1', 'prog2', 'progn',
     51 	'progv', 'provide', 'require', 'restart-bind', 'restart-case', 'restart-name', 'return',
     52 	'return-from', 'signal', 'symbol-macrolet', 'tagbody', 'the', 'throw', 'typecase', 'unless',
     53 	'unwind-protect', 'when', 'with-accessors', 'with-compilation-unit', 'with-condition-restarts',
     54 	'with-hash-table-iterator', 'with-input-from-string', 'with-open-file', 'with-open-stream',
     55 	'with-output-to-string', 'with-package-iterator', 'with-simple-restart', 'with-slots',
     56 	'with-standard-io-syntax', --
     57 	't', 'nil'
     58 })
     59 
     60 lexer.property['scintillua.comment'] = ';'
     61 
     62 return lex