smart square <=> round opening bracket rewrite
[lisp-parkour.git] / parser / dialect / lisp.lua
blobfbfc5b638680be22d504f2a488d8620c7570326e
1 require'lpeg'
2 local lpeg = lpeg
3 local P, S = lpeg.P, lpeg.S
4 local cwd = (...):match'(.-)[^%.]+$'
5 local l = require(cwd..'_lexer')
7 -- The LPeg patterns for atoms are taken from the scintillua lexer:
8 -- Copyright 2006-2017 Mitchell mitchell.att.foicica.com.
10 local lispwords = {
11 [':implementation'] = 1,
12 [':method'] = 1,
13 ['case'] = 1,
14 ['defclass'] = 1,
15 ['defconstant'] = 1,
16 ['defgeneric'] = 1,
17 ['defimplementation'] = 1,
18 ['define-condition'] = 1,
19 ['define-implementation-package'] = 1,
20 ['definterface'] = 1,
21 ['defmacro'] = 1,
22 ['defmethod'] = 1,
23 ['defpackage'] = 1,
24 ['defproject'] = 1,
25 ['deftype'] = 1,
26 ['defun'] = 1,
27 ['defvar'] = 1,
28 ['do-external-symbols'] = 1,
29 ['dolist'] = 1,
30 ['dotimes'] = 1,
31 ['ecase'] = 1,
32 ['etypecase'] = 1,
33 ['flet'] = 1,
34 ['handler-bind'] = 1,
35 ['if'] = 1,
36 ['lambda'] = 1,
37 ['let'] = 1,
38 ['let*'] = 1,
39 ['print-unreadable-object'] = 1,
40 ['macrolet'] = 1,
41 ['defparameter'] = 1,
42 ['with-slots'] = 1,
43 ['typecase'] = 1,
44 ['loop'] = 1,
45 ['when'] = 1,
46 ['prog1'] = 1,
47 ['unless'] = 1,
48 ['with-open-file'] = 1,
49 ['with-output-to-string'] = 1,
50 ['with-input-from-string'] = 1,
51 ['block'] = 1,
52 ['handler-case'] = 1,
53 ['defstruct'] = 1,
54 ['eval-when'] = 1,
55 ['tagbody'] = 1,
56 ['ignore-errors'] = 1,
57 ['labels'] = 1,
58 ['multiple-value-bind'] = 2,
59 ['progn'] = 1,
60 ['unwind-protect'] = 1,
61 ['collect'] = 1,
62 ['destructuring-bind'] = 2,
63 ['do'] = 2,
64 ['do*'] = 2,
67 local macro_prefix = P('#.') + ',@' + ',.' + ',' + '`' + "'"
69 local opposite = {
70 ["("] = ")",
71 [")"] = "(",
72 ['"'] = '"',
73 ["|"] = "|",
74 [";"] = "\n",
75 ["#|"] = "|#",
78 local number = P('-')^-1 * l.digit^1 * (S('./') * l.digit^1)^-1
79 local word = l.alpha * (l.alnum + '_' + '-')^0
80 local entity = '&' * word
81 local identifier = word
83 return {
84 lispwords = lispwords,
85 prefix = macro_prefix,
86 opposite = opposite,
87 atom = function(pfx) return pfx * identifier + number + entity + "..." + "." end,
88 word = number + identifier + "\\" * l.any