]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/gpr-wisi.el
Add ada-mode, wisi packages
[gnu-emacs-elpa] / packages / ada-mode / gpr-wisi.el
1 ;;; An indentation engine for gpr mode, using the wisent LALR parser
2 ;;
3 ;; [1] GNAT user guide (info "gnat_ugn")
4 ;;
5 ;; Copyright (C) 2013 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 ;; not using lexical-binding or cl-lib because we support Emacs 23
29 ;;
30 ;; I don't use 'pcase', because it gives _really_ confusing errors
31 ;; when I forget a ')' somewhere. Even worse, the error message is
32 ;; given when you use edebug on a defun, not when you eval it. This
33 ;; code is hard enough to debug!
34 ;;
35 ;;;;
36
37 ;; we reuse some stuff from ada-mode
38 (require 'ada-indent-user-options)
39 (require 'gpr-grammar-wy)
40 (require 'gpr-mode)
41 (require 'wisi)
42
43 (defconst gpr-wisi-class-list
44 '(
45 block-start
46 block-middle
47 block-end
48 close-paren
49 open-paren
50 statement-end
51 statement-other
52 statement-start
53 ))
54
55 (defun gpr-wisi-indent-cache (offset cache)
56 "Return indentation of OFFSET relative to CACHE 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 ))
92
93 (defun gpr-wisi-after-cache ()
94 (let ((cache (wisi-backward-cache)))
95 (if (not cache)
96 ;; bob
97 0
98 (cl-ecase (wisi-cache-class cache)
99 (block-end
100 (wisi-indent-current 0))
101
102 (block-middle
103 (cl-case (wisi-cache-token cache)
104 (WHEN
105 (gpr-wisi-indent-cache ada-indent-broken cache))
106 (t
107 (gpr-wisi-indent-cache ada-indent cache))
108 ))
109
110 (block-start
111 (cl-case (wisi-cache-token cache)
112 (EQUAL_GREATER
113 (gpr-wisi-indent-containing ada-indent cache))
114 (t
115 (gpr-wisi-indent-cache ada-indent cache))
116 ))
117
118 (open-paren
119 (1+ (current-column)))
120
121 (statement-end
122 (wisi-indent-start 0 cache))
123
124 ((statement-other close-paren)
125 ;; test/gpr/simple.gpr
126 ;; ) & Style_Checks
127 ;; & Standard_Common.Compiler'Default_Switches;
128 ;;
129 ;; for Source_Dirs use
130 ;; ("../auto",
131 (wisi-indent-start ada-indent-broken cache))
132
133 (statement-start
134 ;; test/gpr/simple.gpr
135 ;; type GNAT_Version_Type
136 ;; is ("7.0.1",
137 ;; hanging
138 (gpr-wisi-indent-cache ada-indent-broken cache))
139 ))
140 ))
141
142 (defun gpr-wisi-post-parse-fail ()
143 "For `wisi-post-parse-fail-hook'."
144 ;; keep it simple :)
145 nil)
146
147 (defun gpr-wisi-which-function ()
148 "For `gpr-which-function'."
149 (wisi-validate-cache (point))
150 (let ((cache (wisi-backward-cache)))
151 (while (and cache
152 (not (and
153 (memq (wisi-cache-nonterm cache) '(package_spec simple_project_declaration))
154 (eq (wisi-cache-class cache) 'statement-start))))
155 (setq cache (wisi-goto-containing cache)))
156 (when cache
157 (wisi-forward-token); package | project
158 (wisi-forward-token t); name
159 )))
160
161 ;;; debugging
162 (defun gpr-wisi-debug-keys ()
163 "Add debug key definitions to `gpr-mode-map'."
164 (interactive)
165 (define-key gpr-mode-map "\M-h" 'wisi-show-containing-or-previous-cache)
166 (define-key gpr-mode-map "\M-j" 'wisi-show-cache)
167 (define-key gpr-mode-map "\M-k" 'wisi-show-token)
168 )
169
170 ;;;;
171 ;;;###autoload
172 (defun gpr-wisi-setup ()
173 "Set up a buffer for parsing Ada files with wisi."
174 (wisi-setup '(gpr-wisi-before-cache
175 gpr-wisi-after-cache)
176 'gpr-wisi-post-parse-fail
177 gpr-wisi-class-list
178 gpr-grammar-wy--keyword-table
179 gpr-grammar-wy--token-table
180 gpr-grammar-wy--parse-table)
181
182 (setq gpr-indent-statement 'wisi-indent-statement)
183 (set (make-local-variable 'comment-indent-function) 'wisi-comment-indent)
184 )
185
186 (add-hook 'gpr-mode-hook 'gpr-wisi-setup)
187
188 (setq gpr-which-function 'gpr-wisi-which-function)
189
190 (setq gpr-show-parse-error 'wisi-show-parse-error)
191
192 (provide 'gpr-wisi)
193 (provide 'gpr-indent-engine)
194
195 ;; end of file