-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflymake-rakudo.el
138 lines (118 loc) · 5.45 KB
/
flymake-rakudo.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;; flymake-rakudo.el --- Flymake syntax checker for Rakudo -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Siavash Askari Nasr
;; Author: Siavash Askari Nasr <[email protected]>
;; URL: https://github.com/Raku/flymake-rakudo
;; Keywords: language, tools, convenience
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1") (flymake-collection "2.0.0") (let-alist "1.0"))
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Raku syntax checking support for Flymake.
;;; Code:
(require 'flymake)
(require 'flymake-collection)
(require 'project)
(eval-when-compile (require 'flymake-collection-define))
(defgroup flymake-rakudo nil
"Rakudo (Raku implementation) support for Flymake."
:prefix "flymake-rakudo-"
:group 'flymake
:link '(url-link :tag "Github" "https://github.com/Raku/flymake-rakudo"))
(defcustom flymake-rakudo-include-path nil
"A list of include directories for Raku.
The value of this variable is a list of strings, where each
string is a directory to add to the include path of Raku.
Relative paths are relative to the file being checked."
:type '(repeat (directory :tag "Include directory")))
(defun flymake-rakudo--find-elements-with-line (alist)
"Return elements of ALIST which contain .line."
(cl-loop for element in alist
if (nested-alist-p element)
if (assq 'line element) return (list element)
else append (flymake-rakudo--find-elements-with-line element)))
(defun flymake-rakudo--parse-panic (panic)
"Parse PANIC field from JSON."
(if (string-match "\\`\\(Undeclared \\(?:routine\\|name\\)\\)s?:" panic)
(let ((error-type (match-string 1 panic)) errs)
(while (string-match "\s*\\(.+?\\) at lines? \\([^\n]+\n\\)" panic (match-end 0))
(let ((msg (concat error-type " " (match-string 1 panic)))
(rest-of-line (match-string 2 panic))
(rest-start-pos 0)
line-numbers)
(save-match-data
(while (string-match "\\([[:digit:]]+\\)\\(?:, \\)?" rest-of-line rest-start-pos)
(setq rest-start-pos (match-end 0))
(push (string-to-number (match-string 1 rest-of-line))
line-numbers))
(string-match "\\([^\n]*\\)\n" rest-of-line (match-end 0))
(mapc
(lambda (line-number)
(push
`(panic . ((line . ,line-number) (message . ,(concat msg (match-string 1 rest-of-line)))))
errs))
line-numbers))))
errs)))
;;;###autoload (autoload 'flymake-rakudo "flymake-rakudo")
(flymake-collection-define-enumerate flymake-rakudo
"A Raku syntax checker."
:title "Rakudo"
:pre-let ((rakudo-exec (executable-find "rakudo"))
(_env-var (progn
(make-local-variable 'process-environment)
(setenv "RAKU_EXCEPTIONS_HANDLER" "JSON"))))
:pre-check (unless rakudo-exec
(error "Cannot find rakudo executable"))
:write-type 'pipe
:command `(,rakudo-exec
"-c"
,@(let ((include-paths flymake-rakudo-include-path))
;; Add project root to path
(let ((current-project (project-current)))
(if current-project
(push (expand-file-name (project-root current-project))
include-paths)))
(cl-loop for path in include-paths
collect (concat "-I" path))))
:generator
(let ((json-alist
(flymake-collection-parse-json
(buffer-substring-no-properties
(point-min) (point-max)))))
(let ((errors (flymake-rakudo--find-elements-with-line json-alist)))
(let-alist (cdaar json-alist)
(when .panic
(setq errors (append errors (flymake-rakudo--parse-panic .panic)))))
errors))
:enumerate-parser
(let-alist it
(let (column expected)
(if .pos
(with-current-buffer flymake-collection-source
(setq column (save-excursion (goto-char .pos) (current-column)))))
(if .highexpect
(let ((padded_expect (mapconcat (lambda (str) (concat " " str)) .highexpect "\n")))
(setq expected (concat "\nexpecting any of:\n" padded_expect))))
(let ((loc (flymake-diag-region flymake-collection-source .line column))
(message (concat
(propertize (symbol-name (car it)) 'face 'flymake-collection-diag-id)
"\n"
.message
expected)))
(list flymake-collection-source (car loc) (cdr loc) :error message)))))
;;;###autoload
(defun flymake-rakudo-setup ()
"Add `flymake-rakudo' to `flymake-diagnostic-functions'."
(interactive)
(add-hook 'flymake-diagnostic-functions #'flymake-rakudo nil t))
(provide 'flymake-rakudo)
;;; flymake-rakudo.el ends here