1 ;;; semantic/bovine/make.el --- Makefile parsing rules.
3 ;; Copyright (C) 2000-2004, 2008-2011 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;; Use the Semantic Bovinator to parse Makefiles.
25 ;; Concocted as an experiment for nonstandard languages.
30 (require 'semantic
/bovine
/make-by
)
31 (require 'semantic
/analyze
)
32 (require 'semantic
/dep
)
34 (declare-function semantic-analyze-possible-completions-default
35 "semantic/analyze/complete")
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
46 (goto-char (match-end 1))
48 ;; We want to skip BOL, so move to the next condition.
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."
54 (let ((start (match-end 0)))
55 (while (progn (end-of-line)
56 (save-excursion (forward-char -
1) (looking-at "\\\\")))
58 (semantic-lex-push-token
59 (semantic-lex-token 'shell-command start
(point)))))
61 (define-lex-regex-analyzer semantic-lex-make-ignore-automake-conditional
62 "An automake conditional seems to really bog down the parser.
64 "^@\\(\\w\\|\\s_\\)+@"
65 (setq semantic-lex-end-point
(match-end 0)))
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
75 semantic-lex-symbol-or-keyword
76 semantic-lex-charquote
77 semantic-lex-paren-or-list
78 semantic-lex-close-paren
80 semantic-lex-ignore-comments
81 semantic-lex-punctuation
82 semantic-lex-default-action
)
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
))
88 ;(message "Expanding %S" name)
89 ;(goto-char (semantic-tag-start tag))
92 (memq (semantic-tag-class tag
) '(function include
))
95 (setq xpand
(cons (semantic-tag-clone tag
(car name
)) xpand
)
97 ;; Else, only a single name.
99 (setcar tag
(car name
)))
100 (setq xpand
(list tag
)))
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."
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)))
114 (cond ((condition-case nil
116 (condition-case nil
(forward-sexp -
1)
119 (looking-at "\\$\\s("))
121 ;; We are in a variable reference
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
))
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
))
136 (cond ((eq class
'function
)
138 ((eq class
'filename
)
141 (semantic-format-tag-abbreviate-default tag parent color
)))))
143 (defvar-mode-local makefile-mode semantic-function-argument-separator
145 "Separator used between dependencies to rules.")
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
))
153 (cond ((eq class
'function
)
155 (semantic--format-tag-arguments
156 (semantic-tag-function-arguments tag
)
157 #'semantic-format-tag-prototype
159 ((eq class
'filename
)
162 (semantic-format-tag-prototype-default tag parent color
)))))
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
))
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
))
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."
181 (require 'semantic
/analyze
/complete
)
182 (set-buffer (oref context buffer
))
183 (let* ((normal (semantic-analyze-possible-completions-default context
))
184 (classes (oref context
:prefixclass
))
186 (when (memq 'filename classes
)
187 (let* ((prefix (car (oref context
:prefix
)))
188 (completetext (cond ((semantic-tag-p prefix
)
189 (semantic-tag-name prefix
))
192 ((stringp (car prefix
))
194 (files (directory-files default-directory nil
195 (concat "^" completetext
))))
196 (setq filetags
(mapcar (lambda (f) (semantic-tag f
'filename
))
198 ;; Return the normal completions found, plus any filenames
200 (append normal filetags
)
203 (defcustom-mode-local-semantic-dependency-system-include-path
204 makefile-mode semantic-makefile-dependency-system-include-path
206 "The system include path used by Makefiles language.")
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")
214 (include .
"Dependencies")
215 ;; File is a meta-type created
216 ;; to represent completions
217 ;; but not actually parsed.
220 semantic-tag-expand-function
'semantic-make-expand-tag
221 semantic-lex-syntax-modifications
'((?.
"_")
228 imenu-create-index-function
'semantic-create-imenu-index
230 (setq semantic-lex-analyzer
#'semantic-make-lexer
)
233 (provide 'semantic
/bovine
/make
)
236 ;; generated-autoload-file: "../loaddefs.el"
237 ;; generated-autoload-load-name: "semantic/bovine/make"
240 ;;; semantic/bovine/make.el ends here