restructure dialect configuration, part 1
[lisp-parkour.git] / parser / dialect / clojure / init.lua
blobf064d74be3af605f38bc432f94d368f0f27f6c63
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 ['defn'] = 1,
12 ['fn'] = 1,
13 ['dorun'] = 1,
14 ['doseq'] = 1,
15 ['loop'] = 1,
16 ['let'] = 1,
17 ['defmacro'] = 1,
18 ['doto'] = 1,
19 ['ns'] = 1,
20 [':import'] = 1,
21 ['defstruct'] = 1,
22 ['condp'] = 1,
23 ['comment'] = 1,
24 ['when'] = 1,
25 ['when-let'] = 1,
26 ['->'] = 1,
27 ['->>'] = 1,
28 ['extend-type'] = 1,
29 ['reify'] = 1,
30 ['binding'] = 1,
31 ['when-not'] = 1,
32 ['proxy'] = 1,
33 ['dotimes'] = 1,
34 ['try'] = 1,
35 ['finally'] = 1,
36 ['for'] = 1,
37 ['letfn'] = 1,
38 ['catch'] = 1,
39 ['iterate'] = 1,
40 ['while'] = 1,
41 ['with-local-vars'] = 1,
42 ['locking'] = 1,
43 ['defmulti'] = 1,
44 ['defmethod'] = 1,
45 ['extend'] = 1,
48 local squarewords = {
49 before_siblings = {
50 ['defn'] = 1,
51 ['fn'] = 0,
52 ['let'] = 0,
54 after_siblings = {
56 first_grandchild = {
57 ['defn'] = true,
61 local macro_prefix = P('#?@') + '#?' +
62 -- XXX: treating datum comments as text is wasteful; better parse them as code:
63 '#_' +
64 '#' + '^' + '@' + '`' + "'"
66 local opposite = {
67 ["("] = ")",
68 [")"] = "(",
69 ["{"] = "}",
70 ["}"] = "{",
71 ["["] = "]",
72 ["]"] = "[",
73 ['"'] = '"',
74 [";"] = "\n",
77 local number = P('-')^-1 * l.digit^1 * (S('./') * l.digit^1)^-1
79 local word = (l.alpha + S('-!?*$=-')) * (l.alnum + S('.-!?*$+-'))^0
80 local identifier = word
82 local clojure_keyword = ':' * S(':')^-1 * word * ('/' * word )^-1
83 local clojure_symbol = "\'" * word * ('/' * word )^-1
85 return {
86 lispwords = lispwords,
87 squarewords = squarewords,
88 prefix = macro_prefix,
89 opposite = opposite,
90 atom = function(pfx) return clojure_keyword + clojure_symbol + pfx * identifier + "λ" + number end,
91 word = number + identifier + "\\" * l.any