]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/gpr-wisi.el
Merge commit '078f88ecb797b6cf2cd597417402274dd82402ce' from diff-hl
[gnu-emacs-elpa] / packages / ada-mode / gpr-wisi.el
1 ;;; gpr-wisi.el --- An indentation engine for gpr mode, using the wisent LALR parser -*- lexical-binding:t -*-
2 ;;
3 ;; [1] GNAT user guide (info "gnat_ugn")
4 ;;
5 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
6 ;;
7 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
8 ;;
9 ;; This file is part of GNU Emacs.
10 ;;
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15 ;;
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;
24 ;;; History: first version Jan 2013
25 ;;
26 ;;; code style
27 ;;
28 ;; I don't use 'pcase', because it gives _really_ confusing errors
29 ;; when I forget a ')' somewhere. Even worse, the error message is
30 ;; given when you use edebug on a defun, not when you eval it. This
31 ;; code is hard enough to debug!
32 ;;
33 ;;;;
34
35 ;; we reuse some stuff from ada-mode
36 (require 'ada-indent-user-options)
37 (require 'gpr-grammar-wy)
38 (require 'gpr-mode)
39 (require 'wisi)
40
41 (defconst gpr-wisi-class-list
42 '(
43 block-start
44 block-middle
45 block-end
46 close-paren
47 list-break
48 open-paren
49 statement-end
50 statement-other
51 statement-start
52 ))
53
54 (defun gpr-wisi-indent-cache (offset cache)
55 "Return indentation of OFFSET relative to indentation of line containing CACHE
56 or containing ancestor of CACHE that is at a line beginning."
57 (let ((indent (current-indentation)))
58 (while (and cache
59 (not (= (current-column) indent)))
60 (when (eq 'WHEN (wisi-cache-token cache))
61 (setq offset (+ offset ada-indent-when)))
62 (setq cache (wisi-goto-containing cache))
63 (setq indent (current-indentation)))
64 (+ (current-indentation) offset)
65 ))
66
67 (defun gpr-wisi-indent-containing (offset cache)
68 "Return indentation of OFFSET relative to containing ancestor of CACHE that is at a line beginning."
69 (gpr-wisi-indent-cache offset (wisi-goto-containing cache)))
70
71 (defun gpr-wisi-before-cache ()
72 (let ((cache (wisi-get-cache (point))))
73 (when cache
74 (cl-ecase (wisi-cache-class cache)
75 (block-start (wisi-indent-start ada-indent (wisi-backward-cache)))
76 (block-end (wisi-indent-start 0 cache))
77 (block-middle
78 (wisi-indent-start
79 (if (eq (wisi-cache-token cache) 'WHEN) ada-indent-when 0)
80 ;; FIXME (later): need test of ada-indent-when in gpr
81 cache))
82 (close-paren (wisi-indent-paren 0))
83 (open-paren nil); let after-keyword handle it
84 (statement-start
85 (if (not (wisi-get-containing-cache cache))
86 ;; at bob
87 0
88 ;; not at bob
89 (gpr-wisi-indent-containing ada-indent cache)))
90
91 (statement-end
92 (gpr-wisi-indent-containing ada-indent-broken cache))
93 ))
94 ))
95
96 (defun gpr-wisi-after-cache ()
97 (let ((cache (wisi-backward-cache)))
98 (if (not cache)
99 ;; bob
100 0
101 (cl-ecase (wisi-cache-class cache)
102 (block-end
103 (wisi-indent-current 0))
104
105 (block-middle
106 (cl-case (wisi-cache-token cache)
107 (WHEN
108 (gpr-wisi-indent-cache ada-indent-broken cache))
109 (t
110 (gpr-wisi-indent-cache ada-indent cache))
111 ))
112
113 (block-start
114 (cl-case (wisi-cache-token cache)
115 (EQUAL_GREATER
116 (gpr-wisi-indent-containing ada-indent cache))
117 (t
118 (gpr-wisi-indent-cache ada-indent cache))
119 ))
120
121 (list-break
122 ;; test/gpr/simple.gpr
123 ;; type GNAT_Version_Type
124 ;; is ("7.0.1",
125 ;; "6.2.2", "6.2.1",
126 ;; "GPL-2012", "GPL-2011");
127 ;;
128 ;; for Source_Dirs use
129 ;; ("../auto",
130 ;; External ("GNAT_VERSION") & "/foo",
131 ;; "../../1553");
132 (wisi-goto-containing cache)
133 (1+ (current-column)))
134
135 (open-paren
136 (1+ (current-column)))
137
138 (statement-end
139 (wisi-indent-start 0 cache))
140
141 ((statement-other close-paren)
142 ;; test/gpr/simple.gpr
143 ;; ) & Style_Checks
144 ;; & Standard_Common.Compiler'Default_Switches;
145 ;;
146 ;; for Source_Dirs use
147 ;; ("../auto",
148 (wisi-indent-start ada-indent-broken cache))
149
150 (statement-start
151 ;; test/gpr/simple.gpr
152 ;; type GNAT_Version_Type
153 ;; is ("7.0.1",
154 ;; hanging
155 (gpr-wisi-indent-cache ada-indent-broken cache))
156 ))
157 ))
158
159 (defun gpr-wisi-post-parse-fail ()
160 "For `wisi-post-parse-fail-hook'."
161 ;; keep it simple :)
162 nil)
163
164 (defun gpr-wisi-which-function ()
165 "For `gpr-which-function'."
166 (wisi-validate-cache (point))
167 (let ((cache (wisi-backward-cache)))
168 (while (and cache
169 (not (and
170 (memq (wisi-cache-nonterm cache) '(package_spec simple_project_declaration))
171 (eq (wisi-cache-class cache) 'statement-start))))
172 (setq cache (wisi-goto-containing cache)))
173 (when cache
174 (wisi-forward-token); package | project
175 (wisi-token-text (wisi-forward-token)); name
176 )))
177
178 ;;; debugging
179 (defun gpr-wisi-debug-keys ()
180 "Add debug key definitions to `gpr-mode-map'."
181 (interactive)
182 (define-key gpr-mode-map "\M-h" 'wisi-show-containing-or-previous-cache)
183 (define-key gpr-mode-map "\M-j" 'wisi-show-cache)
184 (define-key gpr-mode-map "\M-k" 'wisi-show-token)
185 )
186
187 ;;;;
188 (defun gpr-wisi-setup ()
189 "Set up a buffer for parsing Ada files with wisi."
190 (wisi-setup '(gpr-wisi-before-cache
191 gpr-wisi-after-cache)
192 'gpr-wisi-post-parse-fail
193 gpr-wisi-class-list
194 gpr-grammar-wy--keyword-table
195 gpr-grammar-wy--token-table
196 gpr-grammar-wy--parse-table)
197
198 (setq gpr-indent-statement 'wisi-indent-statement)
199 (set (make-local-variable 'comment-indent-function) 'wisi-comment-indent)
200 )
201
202 (add-hook 'gpr-mode-hook 'gpr-wisi-setup)
203
204 (setq gpr-which-function 'gpr-wisi-which-function)
205
206 (setq gpr-show-parse-error 'wisi-show-parse-error)
207
208 (provide 'gpr-wisi)
209 (provide 'gpr-indent-engine)
210
211 ;; end of file