Commit | Line | Data |
---|---|---|
4feec2f5 CY |
1 | ;;; semantic/bovine/make.el --- Makefile parsing rules. |
2 | ||
acaf905b | 3 | ;; Copyright (C) 2000-2004, 2008-2012 Free Software Foundation, Inc. |
4feec2f5 CY |
4 | |
5 | ;; Author: Eric M. Ludlam <zappo@gnu.org> | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
9 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation, either version 3 of the License, or | |
12 | ;; (at your option) any later version. | |
13 | ||
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
21 | ||
22 | ;;; Commentary: | |
23 | ;; | |
24 | ;; Use the Semantic Bovinator to parse Makefiles. | |
25 | ;; Concocted as an experiment for nonstandard languages. | |
26 | ||
27 | (require 'make-mode) | |
28 | ||
29 | (require 'semantic) | |
30 | (require 'semantic/bovine/make-by) | |
31 | (require 'semantic/analyze) | |
b90caf50 | 32 | (require 'semantic/dep) |
4feec2f5 | 33 | |
b90caf50 CY |
34 | (declare-function semantic-analyze-possible-completions-default |
35 | "semantic/analyze/complete") | |
4feec2f5 CY |
36 | |
37 | ;;; Code: | |
38 | (define-lex-analyzer semantic-lex-make-backslash-no-newline | |
39 | "Detect and create a beginning of line token (BOL)." | |
40 | (and (looking-at "\\(\\\\\n\\s-*\\)") | |
41 | ;; We have a \ at eol. Push it as whitespace, but pretend | |
42 | ;; it never happened so we can skip the BOL tokenizer. | |
43 | (semantic-lex-push-token (semantic-lex-token 'whitespace | |
44 | (match-beginning 1) | |
45 | (match-end 1))) | |
46 | (goto-char (match-end 1)) | |
47 | nil) ;; CONTINUE | |
48 | ;; We want to skip BOL, so move to the next condition. | |
49 | nil) | |
50 | ||
51 | (define-lex-regex-analyzer semantic-lex-make-command | |
52 | "A command in a Makefile consists of a line starting with TAB, and ending at the newline." | |
53 | "^\\(\t\\)" | |
54 | (let ((start (match-end 0))) | |
55 | (while (progn (end-of-line) | |
56 | (save-excursion (forward-char -1) (looking-at "\\\\"))) | |
57 | (forward-char 1)) | |
58 | (semantic-lex-push-token | |
59 | (semantic-lex-token 'shell-command start (point))))) | |
60 | ||
61 | (define-lex-regex-analyzer semantic-lex-make-ignore-automake-conditional | |
62 | "An automake conditional seems to really bog down the parser. | |
63 | Ignore them." | |
64 | "^@\\(\\w\\|\\s_\\)+@" | |
65 | (setq semantic-lex-end-point (match-end 0))) | |
66 | ||
67 | (define-lex semantic-make-lexer | |
68 | "Lexical analyzer for Makefiles." | |
69 | semantic-lex-beginning-of-line | |
70 | semantic-lex-make-ignore-automake-conditional | |
71 | semantic-lex-make-command | |
72 | semantic-lex-make-backslash-no-newline | |
73 | semantic-lex-whitespace | |
74 | semantic-lex-newline | |
75 | semantic-lex-symbol-or-keyword | |
76 | semantic-lex-charquote | |
77 | semantic-lex-paren-or-list | |
78 | semantic-lex-close-paren | |
79 | semantic-lex-string | |
80 | semantic-lex-ignore-comments | |
81 | semantic-lex-punctuation | |
82 | semantic-lex-default-action) | |
83 | ||
84 | (defun semantic-make-expand-tag (tag) | |
85 | "Expand TAG into a list of equivalent tags, or nil." | |
86 | (let ((name (semantic-tag-name tag)) | |
87 | xpand) | |
88 | ;(message "Expanding %S" name) | |
89 | ;(goto-char (semantic-tag-start tag)) | |
90 | ;(sit-for 0) | |
91 | (if (and (consp name) | |
92 | (memq (semantic-tag-class tag) '(function include)) | |
93 | (> (length name) 1)) | |
94 | (while name | |
95 | (setq xpand (cons (semantic-tag-clone tag (car name)) xpand) | |
96 | name (cdr name))) | |
97 | ;; Else, only a single name. | |
98 | (when (consp name) | |
99 | (setcar tag (car name))) | |
100 | (setq xpand (list tag))) | |
101 | xpand)) | |
102 | ||
103 | (define-mode-local-override semantic-get-local-variables | |
104 | makefile-mode (&optional point) | |
105 | "Override `semantic-get-local-variables' so it does not throw an error. | |
106 | We never have local variables in Makefiles." | |
107 | nil) | |
108 | ||
109 | (define-mode-local-override semantic-ctxt-current-class-list | |
110 | makefile-mode (&optional point) | |
111 | "List of classes that are valid to place at point." | |
112 | (let ((tag (semantic-current-tag))) | |
113 | (when tag | |
114 | (cond ((condition-case nil | |
115 | (save-excursion | |
116 | (condition-case nil (forward-sexp -1) | |
117 | (error nil)) | |
118 | (forward-char -2) | |
119 | (looking-at "\\$\\s(")) | |
120 | (error nil)) | |
121 | ;; We are in a variable reference | |
122 | '(variable)) | |
123 | ((semantic-tag-of-class-p tag 'function) | |
124 | ;; Note: variables are handled above. | |
125 | '(function filename)) | |
126 | ((semantic-tag-of-class-p tag 'variable) | |
127 | '(function filename)) | |
128 | )))) | |
129 | ||
130 | (define-mode-local-override semantic-format-tag-abbreviate | |
131 | makefile-mode (tag &optional parent color) | |
132 | "Return an abbreviated string describing tag for Makefiles." | |
133 | (let ((class (semantic-tag-class tag)) | |
134 | (name (semantic-format-tag-name tag parent color)) | |
135 | ) | |
136 | (cond ((eq class 'function) | |
137 | (concat name ":")) | |
138 | ((eq class 'filename) | |
139 | (concat "./" name)) | |
140 | (t | |
141 | (semantic-format-tag-abbreviate-default tag parent color))))) | |
142 | ||
143 | (defvar-mode-local makefile-mode semantic-function-argument-separator | |
144 | " " | |
145 | "Separator used between dependencies to rules.") | |
146 | ||
147 | (define-mode-local-override semantic-format-tag-prototype | |
148 | makefile-mode (tag &optional parent color) | |
149 | "Return a prototype string describing tag for Makefiles." | |
150 | (let* ((class (semantic-tag-class tag)) | |
151 | (name (semantic-format-tag-name tag parent color)) | |
152 | ) | |
153 | (cond ((eq class 'function) | |
154 | (concat name ": " | |
a60f2e7b | 155 | (semantic--format-tag-arguments |
4feec2f5 CY |
156 | (semantic-tag-function-arguments tag) |
157 | #'semantic-format-tag-prototype | |
158 | color))) | |
159 | ((eq class 'filename) | |
160 | (concat "./" name)) | |
161 | (t | |
162 | (semantic-format-tag-prototype-default tag parent color))))) | |
163 | ||
164 | (define-mode-local-override semantic-format-tag-concise-prototype | |
165 | makefile-mode (tag &optional parent color) | |
166 | "Return a concise prototype string describing tag for Makefiles. | |
167 | This is the same as a regular prototype." | |
168 | (semantic-format-tag-prototype tag parent color)) | |
169 | ||
170 | (define-mode-local-override semantic-format-tag-uml-prototype | |
171 | makefile-mode (tag &optional parent color) | |
172 | "Return a UML prototype string describing tag for Makefiles. | |
173 | This is the same as a regular prototype." | |
174 | (semantic-format-tag-prototype tag parent color)) | |
175 | ||
176 | (define-mode-local-override semantic-analyze-possible-completions | |
177 | makefile-mode (context) | |
178 | "Return a list of possible completions in a Makefile. | |
179 | Uses default implementation, and also gets a list of filenames." | |
180 | (save-excursion | |
b90caf50 | 181 | (require 'semantic/analyze/complete) |
4feec2f5 CY |
182 | (set-buffer (oref context buffer)) |
183 | (let* ((normal (semantic-analyze-possible-completions-default context)) | |
184 | (classes (oref context :prefixclass)) | |
185 | (filetags nil)) | |
186 | (when (memq 'filename classes) | |
187 | (let* ((prefix (car (oref context :prefix))) | |
188 | (completetext (cond ((semantic-tag-p prefix) | |
189 | (semantic-tag-name prefix)) | |
190 | ((stringp prefix) | |
191 | prefix) | |
192 | ((stringp (car prefix)) | |
193 | (car prefix)))) | |
194 | (files (directory-files default-directory nil | |
195 | (concat "^" completetext)))) | |
196 | (setq filetags (mapcar (lambda (f) (semantic-tag f 'filename)) | |
197 | files)))) | |
198 | ;; Return the normal completions found, plus any filenames | |
199 | ;; that match. | |
200 | (append normal filetags) | |
201 | ))) | |
202 | ||
203 | (defcustom-mode-local-semantic-dependency-system-include-path | |
204 | makefile-mode semantic-makefile-dependency-system-include-path | |
205 | nil | |
91abaf51 | 206 | "The system include path used by Makefiles language.") |
4feec2f5 | 207 | |
a60f2e7b | 208 | ;;;###autoload |
4feec2f5 CY |
209 | (defun semantic-default-make-setup () |
210 | "Set up a Makefile buffer for parsing with semantic." | |
211 | (semantic-make-by--install-parser) | |
212 | (setq semantic-symbol->name-assoc-list '((variable . "Variables") | |
213 | (function . "Rules") | |
214 | (include . "Dependencies") | |
215 | ;; File is a meta-type created | |
216 | ;; to represent completions | |
217 | ;; but not actually parsed. | |
218 | (file . "File")) | |
219 | semantic-case-fold t | |
220 | semantic-tag-expand-function 'semantic-make-expand-tag | |
221 | semantic-lex-syntax-modifications '((?. "_") | |
222 | (?= ".") | |
223 | (?/ "_") | |
224 | (?$ ".") | |
225 | (?+ ".") | |
226 | (?\\ ".") | |
227 | ) | |
228 | imenu-create-index-function 'semantic-create-imenu-index | |
229 | ) | |
230 | (setq semantic-lex-analyzer #'semantic-make-lexer) | |
231 | ) | |
232 | ||
4feec2f5 CY |
233 | (provide 'semantic/bovine/make) |
234 | ||
a60f2e7b CY |
235 | ;; Local variables: |
236 | ;; generated-autoload-file: "../loaddefs.el" | |
a60f2e7b CY |
237 | ;; generated-autoload-load-name: "semantic/bovine/make" |
238 | ;; End: | |
239 | ||
4feec2f5 | 240 | ;;; semantic/bovine/make.el ends here |