Update copyright notices for 2013.
[bpt/emacs.git] / lisp / cedet / semantic / ede-grammar.el
1 ;;; semantic/ede-grammar.el --- EDE support for Semantic Grammar Files
2
3 ;; Copyright (C) 2003-2004, 2007-2013 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; Handle .by or .wy files.
26
27 (require 'semantic)
28 (require 'ede/proj)
29 (require 'ede/pmake)
30 (require 'ede/pconf)
31 (require 'ede/proj-elisp)
32 (require 'semantic/grammar)
33
34 ;;; Code:
35 (defclass semantic-ede-proj-target-grammar (ede-proj-target-elisp)
36 ((menu :initform nil)
37 (keybindings :initform nil)
38 (phony :initform t)
39 (sourcetype :initform
40 (semantic-ede-source-grammar-wisent
41 semantic-ede-source-grammar-bovine
42 ))
43 (availablecompilers :initform
44 (semantic-ede-grammar-compiler-wisent
45 semantic-ede-grammar-compiler-bovine
46 ))
47 (aux-packages :initform '("semantic" "cedet-compat"))
48 (pre-load-packages :initform '("cedet-compat" "semantic/grammar" "semantic/bovine/grammar" "semantic/wisent/grammar"))
49 )
50 "This target consists of a group of grammar files.
51 A grammar target consists of grammar files that build Emacs Lisp programs for
52 parsing different languages.")
53
54 (defmethod ede-proj-makefile-dependencies ((this semantic-ede-proj-target-grammar))
55 "Return a string representing the dependencies for THIS.
56 Some compilers only use the first element in the dependencies, others
57 have a list of intermediates (object files), and others don't care.
58 This allows customization of how these elements appear.
59 For Emacs Lisp, return addsuffix command on source files."
60 (let ((source (car (oref this source))))
61 (cond
62 ((string-match "\\.wy$" source)
63 (format "$(addsuffix -wy.elc, $(basename $(%s)))"
64 (ede-proj-makefile-sourcevar this)))
65 ((string-match "\\.by$" source)
66 (format "$(addsuffix -by.elc, $(basename $(%s)))"
67 (ede-proj-makefile-sourcevar this))))))
68
69 (defvar semantic-ede-source-grammar-wisent
70 (ede-sourcecode "semantic-ede-grammar-source-wisent"
71 :name "Wisent Grammar"
72 :sourcepattern "\\.wy$"
73 :garbagepattern '("*-wy.el")
74 )
75 "Semantic Grammar source code definition for wisent.")
76
77 (defclass semantic-ede-grammar-compiler-class (ede-compiler)
78 nil
79 "Specialized compiler for semantic grammars.")
80
81 (defvar semantic-ede-grammar-compiler-wisent
82 (semantic-ede-grammar-compiler-class
83 "ede-emacs-wisent-compiler"
84 :name "emacs"
85 :variables '(("EMACS" . "emacs")
86 ("EMACSFLAGS" . "-batch --no-site-file --eval '(setq debug-on-error t)'")
87 ("require" . "$(foreach r,$(1),(require (quote $(r))))"))
88 :rules (list (ede-makefile-rule
89 "elisp-inference-rule"
90 :target "%-wy.el"
91 :dependencies "%.wy"
92 :rules '("$(EMACS) $(EMACSFLAGS) $(addprefix -L ,$(LOADPATH)) \
93 --eval '(progn $(call require,$(PRELOADS)))' -f semantic-grammar-batch-build-packages $^")))
94 :sourcetype '(semantic-ede-source-grammar-wisent)
95 :objectextention "-wy.el"
96 )
97 "Compile Emacs Lisp programs.")
98
99
100 (defvar semantic-ede-source-grammar-bovine
101 (ede-sourcecode "semantic-ede-grammar-source-bovine"
102 :name "Bovine Grammar"
103 :sourcepattern "\\.by$"
104 :garbagepattern '("*-by.el")
105 )
106 "Semantic Grammar source code definition for the bovinator.")
107
108 (defvar semantic-ede-grammar-compiler-bovine
109 (semantic-ede-grammar-compiler-class
110 "ede-emacs-wisent-compiler"
111 :name "emacs"
112 :variables '(("EMACS" . "emacs")
113 ("EMACSFLAGS" . "-batch --no-site-file --eval '(setq debug-on-error t)'")
114 ("require" . "$(foreach r,$(1),(require (quote $(r))))"))
115 :rules (list (ede-makefile-rule
116 "elisp-inference-rule"
117 :target "%-by.el"
118 :dependencies "%.by"
119 :rules '("$(EMACS) $(EMACSFLAGS) $(addprefix -L ,$(LOADPATH)) \
120 --eval '(progn $(call require,$(PRELOADS)))' -f semantic-grammar-batch-build-packages $^")))
121 :sourcetype '(semantic-ede-source-grammar-bovine)
122 :objectextention "-by.el"
123 )
124 "Compile Emacs Lisp programs.")
125
126 ;;; Target options.
127 (defmethod ede-buffer-mine ((this semantic-ede-proj-target-grammar) buffer)
128 "Return t if object THIS lays claim to the file in BUFFER.
129 Lays claim to all -by.el, and -wy.el files."
130 ;; We need to be a little more careful than this, but at the moment it
131 ;; is common to have only one target of this class per directory.
132 (if (string-match "-[bw]y\\.elc?$" (buffer-file-name buffer))
133 t
134 (call-next-method) ; The usual thing.
135 ))
136
137 (defmethod project-compile-target ((obj semantic-ede-proj-target-grammar))
138 "Compile all sources in a Lisp target OBJ."
139 (let* ((cb (current-buffer))
140 (proj (ede-target-parent obj))
141 (default-directory (oref proj directory))
142 (comp 0)
143 (utd 0))
144 (mapc (lambda (src)
145 (with-current-buffer (find-file-noselect src)
146 (let* ((package (semantic-grammar-create-package))
147 (fname (progn (string-match ".*/\\(.+\\.el\\)" package)
148 (match-string 1 package)))
149 (src (with-current-buffer fname (buffer-file-name)))
150 (csrc (concat (file-name-sans-extension src) ".elc")))
151 (if (< emacs-major-version 24)
152 ;; Does not have `byte-recompile-file'
153 (if (or (not (file-exists-p csrc))
154 (file-newer-than-file-p src csrc))
155 (progn
156 (setq comp (1+ comp))
157 (byte-compile-file src))
158 (setq utd (1+ utd)))
159 ;; Emacs 24 and newer
160 (with-no-warnings
161 (if (eq (byte-recompile-file src nil 0) t)
162 (setq comp (1+ comp))
163 (setq utd (1+ utd))))))))
164 (oref obj source))
165 (message "All Semantic Grammar sources are up to date in %s" (object-name obj))
166 (cons comp utd)))
167
168 ;;; Makefile generation functions
169 ;;
170 (defmethod ede-proj-makefile-sourcevar ((this semantic-ede-proj-target-grammar))
171 "Return the variable name for THIS's sources."
172 (cond ((ede-proj-automake-p)
173 (error "No Automake support for Semantic Grammars"))
174 (t (concat (ede-pmake-varname this) "_SEMANTIC_GRAMMAR"))))
175
176 (defmethod ede-proj-makefile-insert-variables :AFTER ((this semantic-ede-proj-target-grammar))
177 "Insert variables needed by target THIS."
178 (ede-proj-makefile-insert-loadpath-items
179 (ede-proj-elisp-packages-to-loadpath
180 (list "eieio" "semantic" "inversion" "ede")))
181 ;; eieio for object system needed in ede
182 ;; semantic because it is
183 ;; Inversion for versioning system.
184 ;; ede for project regeneration
185 (ede-pmake-insert-variable-shared
186 (concat (ede-pmake-varname this) "_SEMANTIC_GRAMMAR_EL")
187 (insert
188 (mapconcat (lambda (src)
189 (with-current-buffer (find-file-noselect src)
190 (concat (semantic-grammar-package) ".el")))
191 (oref this source)
192 " ")))
193 )
194
195 (defmethod ede-proj-makefile-insert-rules :after ((this semantic-ede-proj-target-grammar))
196 "Insert rules needed by THIS target.
197 This raises `max-specpdl-size' and `max-lisp-eval-depth', which can be
198 needed for the compilation of the resulting parsers."
199 (insert (format "%s: EMACSFLAGS+= --eval '(setq max-specpdl-size 1500 \
200 max-lisp-eval-depth 700)'\n"
201 (oref this name))))
202
203 (defmethod ede-proj-makefile-insert-dist-dependencies ((this semantic-ede-proj-target-grammar))
204 "Insert dist dependencies, or intermediate targets.
205 This makes sure that all grammar lisp files are created before the dist
206 runs, so they are always up to date.
207 Argument THIS is the target that should insert stuff."
208 (call-next-method)
209 (insert " $(" (ede-pmake-varname this) "_SEMANTIC_GRAMMAR_EL)")
210 )
211
212 ;; (autoload 'ede-proj-target-elisp "ede/proj-elisp"
213 ;; "Target class for Emacs/Semantic grammar files." nil nil)
214
215 (ede-proj-register-target "semantic grammar"
216 semantic-ede-proj-target-grammar)
217
218 (provide 'semantic/ede-grammar)
219
220 ;;; semantic/ede-grammar.el ends here