Update CEDET from upstream.
[bpt/emacs.git] / admin / grammars / bovine-grammar.el
CommitLineData
469d2149
CY
1;;; bovine-grammar.el --- Bovine's input grammar mode
2;;
acaf905b 3;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
469d2149
CY
4;;
5;; Author: David Ponce <david@dponce.com>
6;; Maintainer: David Ponce <david@dponce.com>
7;; Created: 26 Aug 2002
8;; Keywords: syntax
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26;;
27;; Major mode for editing Bovine's input grammar (.by) files.
28
29;;; History:
30
31;;; Code:
32(require 'semantic)
33(require 'semantic/grammar)
34(require 'semantic/find)
f79fbbc7
CY
35(require 'semantic/lex)
36(require 'semantic/wisent)
37(require 'semantic/bovine)
469d2149
CY
38
39(defun bovine-grammar-EXPAND (bounds nonterm)
40 "Expand call to EXPAND grammar macro.
41Return the form to parse from within a nonterminal between BOUNDS.
42NONTERM is the nonterminal symbol to start with."
43 `(semantic-bovinate-from-nonterminal
44 (car ,bounds) (cdr ,bounds) ',nonterm))
45
46(defun bovine-grammar-EXPANDFULL (bounds nonterm)
47 "Expand call to EXPANDFULL grammar macro.
48Return the form to recursively parse the area between BOUNDS.
49NONTERM is the nonterminal symbol to start with."
50 `(semantic-parse-region
51 (car ,bounds) (cdr ,bounds) ',nonterm 1))
52
53(defun bovine-grammar-TAG (name class &rest attributes)
54 "Expand call to TAG grammar macro.
55Return the form to create a generic semantic tag.
56See the function `semantic-tag' for the meaning of arguments NAME,
57CLASS and ATTRIBUTES."
58 `(semantic-tag ,name ,class ,@attributes))
59
60(defun bovine-grammar-VARIABLE-TAG (name type default-value &rest attributes)
61 "Expand call to VARIABLE-TAG grammar macro.
62Return the form to create a semantic tag of class variable.
63See the function `semantic-tag-new-variable' for the meaning of
64arguments NAME, TYPE, DEFAULT-VALUE and ATTRIBUTES."
65 `(semantic-tag-new-variable ,name ,type ,default-value ,@attributes))
66
67(defun bovine-grammar-FUNCTION-TAG (name type arg-list &rest attributes)
68 "Expand call to FUNCTION-TAG grammar macro.
69Return the form to create a semantic tag of class function.
70See the function `semantic-tag-new-function' for the meaning of
71arguments NAME, TYPE, ARG-LIST and ATTRIBUTES."
72 `(semantic-tag-new-function ,name ,type ,arg-list ,@attributes))
73
74(defun bovine-grammar-TYPE-TAG (name type members parents &rest attributes)
75 "Expand call to TYPE-TAG grammar macro.
76Return the form to create a semantic tag of class type.
77See the function `semantic-tag-new-type' for the meaning of arguments
78NAME, TYPE, MEMBERS, PARENTS and ATTRIBUTES."
79 `(semantic-tag-new-type ,name ,type ,members ,parents ,@attributes))
80
81(defun bovine-grammar-INCLUDE-TAG (name system-flag &rest attributes)
82 "Expand call to INCLUDE-TAG grammar macro.
83Return the form to create a semantic tag of class include.
84See the function `semantic-tag-new-include' for the meaning of
85arguments NAME, SYSTEM-FLAG and ATTRIBUTES."
86 `(semantic-tag-new-include ,name ,system-flag ,@attributes))
87
88(defun bovine-grammar-PACKAGE-TAG (name detail &rest attributes)
89 "Expand call to PACKAGE-TAG grammar macro.
90Return the form to create a semantic tag of class package.
91See the function `semantic-tag-new-package' for the meaning of
92arguments NAME, DETAIL and ATTRIBUTES."
93 `(semantic-tag-new-package ,name ,detail ,@attributes))
94
95(defun bovine-grammar-CODE-TAG (name detail &rest attributes)
96 "Expand call to CODE-TAG grammar macro.
97Return the form to create a semantic tag of class code.
98See the function `semantic-tag-new-code' for the meaning of arguments
99NAME, DETAIL and ATTRIBUTES."
100 `(semantic-tag-new-code ,name ,detail ,@attributes))
101
102(defun bovine-grammar-ALIAS-TAG (name aliasclass definition &rest attributes)
103 "Expand call to ALIAS-TAG grammar macro.
104Return the form to create a semantic tag of class alias.
105See the function `semantic-tag-new-alias' for the meaning of arguments
106NAME, ALIASCLASS, DEFINITION and ATTRIBUTES."
107 `(semantic-tag-new-alias ,name ,aliasclass ,definition ,@attributes))
108
109;; Cache of macro definitions currently in use.
110(defvar bovine--grammar-macros nil)
111
112(defun bovine-grammar-expand-form (form quotemode &optional inplace)
113 "Expand FORM into a new one suitable to the bovine parser.
114FORM is a list in which we are substituting.
115Argument QUOTEMODE is non-nil if we are in backquote mode.
116When non-nil, optional argument INPLACE indicates that FORM is being
117expanded from elsewhere."
469d2149
CY
118 (when (eq (car form) 'quote)
119 (setq form (cdr form))
120 (cond
121 ((and (= (length form) 1) (listp (car form)))
122 (insert "\n(append")
123 (bovine-grammar-expand-form (car form) quotemode nil)
124 (insert ")")
125 (setq form nil inplace nil)
126 )
127 ((and (= (length form) 1) (symbolp (car form)))
128 (insert "\n'" (symbol-name (car form)))
129 (setq form nil inplace nil)
130 )
131 (t
132 (insert "\n(list")
133 (setq inplace t)
134 )))
135 (let ((macro (assq (car form) bovine--grammar-macros))
136 inlist first n q x)
137 (if macro
138 (bovine-grammar-expand-form
139 (apply (cdr macro) (cdr form))
140 quotemode t)
141 (if inplace (insert "\n("))
142 (while form
143 (setq first (car form)
144 form (cdr form))
72b8747b
DE
145 ;; Hack for dealing with new reading of unquotes outside of
146 ;; backquote (introduced in rev. 102591 in emacs-bzr).
62a81506 147 (when (and (>= emacs-major-version 24)
72b8747b
DE
148 (listp first)
149 (or (equal (car first) '\,)
150 (equal (car first) '\,@)))
151 (if (listp (cadr first))
152 (setq form (append (cdr first) form)
153 first (car first))
154 (setq first (intern (concat (symbol-name (car first))
155 (symbol-name (cadr first)))))))
469d2149
CY
156 (cond
157 ((eq first nil)
158 (when (and (not inlist) (not inplace))
159 (insert "\n(list")
160 (setq inlist t))
161 (insert " nil")
162 )
163 ((listp first)
164 ;;(let ((fn (and (symbolp (caar form)) (fboundp (caar form)))))
165 (when (and (not inlist) (not inplace))
166 (insert "\n(list")
167 (setq inlist t))
168 ;;(if (and inplace (not fn) (not (eq (caar form) 'EXPAND)))
169 ;; (insert " (append"))
170 (bovine-grammar-expand-form
171 first quotemode t) ;;(and fn (not (eq fn 'quote))))
172 ;;(if (and inplace (not fn) (not (eq (caar form) 'EXPAND)))
173 ;; (insert ")"))
174 ;;)
175 )
176 ((symbolp first)
177 (setq n (symbol-name first) ;the name
178 q quotemode ;implied quote flag
179 x nil) ;expand flag
180 (if (eq (aref n 0) ?,)
181 (if quotemode
182 ;; backquote mode needs the @
183 (if (eq (aref n 1) ?@)
184 (setq n (substring n 2)
185 q nil
186 x t)
187 ;; non backquote mode behaves normally.
188 (setq n (substring n 1)
189 q nil))
190 (setq n (substring n 1)
191 x t)))
192 (if (string= n "")
193 (progn
194 ;; We expand only the next item in place (a list?)
195 ;; A regular inline-list...
196 (bovine-grammar-expand-form (car form) quotemode t)
197 (setq form (cdr form)))
198 (if (and (eq (aref n 0) ?$)
199 ;; Don't expand $ tokens in implied quote mode.
200 ;; This acts like quoting in other symbols.
201 (not q))
202 (progn
203 (cond
204 ((and (not x) (not inlist) (not inplace))
205 (insert "\n(list"))
206 ((and x inlist (not inplace))
207 (insert ")")
208 (setq inlist nil)))
209 (insert "\n(nth " (int-to-string
210 (1- (string-to-number
211 (substring n 1))))
212 " vals)")
213 (and (not x) (not inplace)
214 (setq inlist t)))
215
216 (when (and (not inlist) (not inplace))
217 (insert "\n(list")
218 (setq inlist t))
219 (or (char-equal (char-before) ?\()
220 (insert " "))
221 (insert (if (or inplace (eq first t))
222 "" "'")
223 n))) ;; " "
224 )
225 (t
226 (when (and (not inlist) (not inplace))
227 (insert "\n(list")
228 (setq inlist t))
229 (insert (format "\n%S" first))
230 )
231 ))
232 (if inlist (insert ")"))
233 (if inplace (insert ")")))
f79fbbc7 234 ))
469d2149
CY
235
236(defun bovine-grammar-expand-action (textform quotemode)
237 "Expand semantic action string TEXTFORM into Lisp code.
238QUOTEMODE is the mode in which quoted symbols are slurred."
239 (if (string= "" textform)
240 nil
241 (let ((sexp (read textform)))
469d2149
CY
242 ;; We converted the lambda string into a list. Now write it
243 ;; out as the bovine lambda expression, and do macro-like
244 ;; conversion upon it.
245 (insert "\n")
246 (cond
247 ((eq (car sexp) 'EXPAND)
248 (insert ",(lambda (vals start end)")
249 ;; The EXPAND macro definition is mandatory
250 (bovine-grammar-expand-form
251 (apply (cdr (assq 'EXPAND bovine--grammar-macros)) (cdr sexp))
252 quotemode t)
253 )
254 ((and (listp (car sexp)) (eq (caar sexp) 'EVAL))
255 ;; The user wants to evaluate the following args.
256 ;; Use a simpler expander
257 )
258 (t
259 (insert ",(semantic-lambda")
260 (bovine-grammar-expand-form sexp quotemode)
261 ))
262 (insert ")\n")))
263)
264
265(defun bovine-grammar-parsetable-builder ()
266 "Return the parser table expression as a string value.
267The format of a bovine parser table is:
268
269 ( ( NONTERMINAL-SYMBOL1 MATCH-LIST1 )
270 ( NONTERMINAL-SYMBOL2 MATCH-LIST2 )
271 ...
272 ( NONTERMINAL-SYMBOLn MATCH-LISTn )
273
274Where each NONTERMINAL-SYMBOL is an artificial symbol which can appear
275in any child state. As a starting place, one of the NONTERMINAL-SYMBOLS
276must be `bovine-toplevel'.
277
278A MATCH-LIST is a list of possible matches of the form:
279
280 ( STATE-LIST1
281 STATE-LIST2
282 ...
283 STATE-LISTN )
284
285where STATE-LIST is of the form:
286 ( TYPE1 [ \"VALUE1\" ] TYPE2 [ \"VALUE2\" ] ... LAMBDA )
287
288where TYPE is one of the returned types of the token stream.
289VALUE is a value, or range of values to match against. For
290example, a SYMBOL might need to match \"foo\". Some TYPES will not
291have matching criteria.
292
99d99081 293LAMBDA is a lambda expression which is evalled with the text of the
469d2149
CY
294type when it is found. It is passed the list of all buffer text
295elements found since the last lambda expression. It should return a
296semantic element (see below.)
297
298For consistency between languages, try to use common return values
299from your parser. Please reference the chapter \"Writing Parsers\" in
300the \"Language Support Developer's Guide -\" in the semantic texinfo
301manual."
302 (let* ((start (semantic-grammar-start))
303 (scopestart (semantic-grammar-scopestart))
304 (quotemode (semantic-grammar-quotemode))
305 (tags (semantic-find-tags-by-class
306 'token (current-buffer)))
307 (nterms (semantic-find-tags-by-class
308 'nonterminal (current-buffer)))
309 ;; Setup the cache of macro definitions.
310 (bovine--grammar-macros (semantic-grammar-macros))
311 nterm rules items item actn prec tag type regex)
312
313 ;; Check some trivial things
314 (cond
315 ((null nterms)
316 (error "Bad input grammar"))
317 (start
318 (if (cdr start)
319 (message "Extra start symbols %S ignored" (cdr start)))
320 (setq start (symbol-name (car start)))
321 (unless (semantic-find-first-tag-by-name start nterms)
322 (error "start symbol `%s' has no rule" start)))
323 (t
324 ;; Default to the first grammar rule.
325 (setq start (semantic-tag-name (car nterms)))))
326 (when scopestart
327 (setq scopestart (symbol-name scopestart))
328 (unless (semantic-find-first-tag-by-name scopestart nterms)
329 (error "scopestart symbol `%s' has no rule" scopestart)))
330
331 ;; Generate the grammar Lisp form.
332 (with-temp-buffer
333 (erase-buffer)
334 (insert "`(")
335 ;; Insert the start/scopestart rules
336 (insert "\n(bovine-toplevel \n("
337 start
338 ")\n) ;; end bovine-toplevel\n")
339 (when scopestart
340 (insert "\n(bovine-inner-scope \n("
341 scopestart
342 ")\n) ;; end bovine-inner-scope\n"))
343 ;; Process each nonterminal
344 (while nterms
345 (setq nterm (car nterms)
346 ;; We can't use the override form because the current buffer
347 ;; is not the originator of the tag.
348 rules (semantic-tag-components-semantic-grammar-mode nterm)
349 nterm (semantic-tag-name nterm)
350 nterms (cdr nterms))
351 (when (member nterm '("bovine-toplevel" "bovine-inner-scope"))
352 (error "`%s' is a reserved internal name" nterm))
353 (insert "\n(" nterm)
469d2149
CY
354 ;; Process each rule
355 (while rules
356 (setq items (semantic-tag-get-attribute (car rules) :value)
357 prec (semantic-tag-get-attribute (car rules) :prec)
358 actn (semantic-tag-get-attribute (car rules) :expr)
359 rules (cdr rules))
360 ;; Process each item
361 (insert "\n(")
362 (if (null items)
363 ;; EMPTY rule
364 (insert ";;EMPTY" (if actn "" "\n"))
365 ;; Expand items
366 (while items
367 (setq item (car items)
368 items (cdr items))
369 (if (consp item) ;; mid-rule action
370 (message "Mid-rule action %S ignored" item)
371 (or (char-equal (char-before) ?\()
372 (insert "\n"))
373 (cond
374 ((member item '("bovine-toplevel" "bovine-inner-scope"))
375 (error "`%s' is a reserved internal name" item))
376 ;; Replace ITEM by its %token definition.
377 ;; If a '%token TYPE ITEM [REGEX]' definition exists
378 ;; in the grammar, ITEM is replaced by TYPE [REGEX].
379 ((setq tag (semantic-find-first-tag-by-name
380 item tags)
381 type (semantic-tag-get-attribute tag :type))
382 (insert type)
383 (if (setq regex (semantic-tag-get-attribute tag :value))
384 (insert (format "\n%S" regex))))
385 ;; Don't change ITEM
386 (t
387 (insert (semantic-grammar-item-text item)))
388 ))))
469d2149
CY
389 (if prec
390 (message "%%prec %S ignored" prec))
391 (if actn
392 (bovine-grammar-expand-action actn quotemode))
393 (insert ")"))
394 (insert "\n) ;; end " nterm "\n"))
395 (insert ")\n")
396 (buffer-string))))
397
398(defun bovine-grammar-setupcode-builder ()
399 "Return the text of the setup code."
400 (format
401 "(setq semantic--parse-table %s\n\
402 semantic-debug-parser-source %S\n\
403 semantic-debug-parser-class 'semantic-bovine-debug-parser
404 semantic-flex-keywords-obarray %s\n\
405 %s)"
406 (semantic-grammar-parsetable)
407 (buffer-name)
408 (semantic-grammar-keywordtable)
409 (let ((mode (semantic-grammar-languagemode)))
410 ;; Is there more than one major mode?
411 (if (and (listp mode) (> (length mode) 1))
412 (format "semantic-equivalent-major-modes '%S\n" mode)
413 ""))))
414
415(defvar bovine-grammar-menu
416 '("BY Grammar"
417 )
418 "BY mode specific grammar menu.
419Menu items are appended to the common grammar menu.")
420
421(define-derived-mode bovine-grammar-mode semantic-grammar-mode "BY"
422 "Major mode for editing Bovine grammars."
423 (semantic-grammar-setup-menu bovine-grammar-menu)
424 (semantic-install-function-overrides
425 '((grammar-parsetable-builder . bovine-grammar-parsetable-builder)
426 (grammar-setupcode-builder . bovine-grammar-setupcode-builder)
427 )))
428
509c74bd 429(add-to-list 'auto-mode-alist '("\\.by\\'" . bovine-grammar-mode))
469d2149
CY
430
431(defvar-mode-local bovine-grammar-mode semantic-grammar-macros
432 '(
433 (ASSOC . semantic-grammar-ASSOC)
434 (EXPAND . bovine-grammar-EXPAND)
435 (EXPANDFULL . bovine-grammar-EXPANDFULL)
436 (TAG . bovine-grammar-TAG)
437 (VARIABLE-TAG . bovine-grammar-VARIABLE-TAG)
438 (FUNCTION-TAG . bovine-grammar-FUNCTION-TAG)
439 (TYPE-TAG . bovine-grammar-TYPE-TAG)
440 (INCLUDE-TAG . bovine-grammar-INCLUDE-TAG)
441 (PACKAGE-TAG . bovine-grammar-PACKAGE-TAG)
442 (CODE-TAG . bovine-grammar-CODE-TAG)
443 (ALIAS-TAG . bovine-grammar-ALIAS-TAG)
444 )
445 "Semantic grammar macros used in bovine grammars.")
446
447(provide 'semantic/bovine/grammar)
448
78adbf9c
CY
449(defun bovine-make-parsers ()
450 "Generate Emacs' built-in Bovine-based parser files."
62a81506 451 (interactive)
78adbf9c
CY
452 (semantic-mode 1)
453 ;; Loop through each .by file in current directory, and run
454 ;; `semantic-grammar-batch-build-one-package' to build the grammar.
509c74bd 455 (dolist (f (directory-files default-directory nil "\\.by\\'"))
78adbf9c
CY
456 (let ((packagename
457 (condition-case err
458 (with-current-buffer (find-file-noselect f)
459 (semantic-grammar-create-package))
460 (error (message "%s" (error-message-string err)) nil)))
62a81506 461 lang filename)
78adbf9c 462 (when (and packagename
62a81506 463 (string-match "^.*/\\(.*\\)-by\\.el\\'" packagename))
78adbf9c 464 (setq lang (match-string 1 packagename))
62a81506 465 (setq filename (concat lang "-by.el"))
78adbf9c 466 (with-temp-buffer
62a81506
CY
467 (insert-file-contents filename)
468 (setq buffer-file-name (expand-file-name filename))
78adbf9c
CY
469 ;; Fix copyright header:
470 (goto-char (point-min))
471 (re-search-forward "^;; Author:")
472 (setq copyright-end (match-beginning 0))
473 (re-search-forward "^;;; Code:\n")
474 (delete-region copyright-end (match-end 0))
475 (goto-char copyright-end)
476 (insert ";; This file is part of GNU Emacs.
477
478;; GNU Emacs is free software: you can redistribute it and/or modify
479;; it under the terms of the GNU General Public License as published by
480;; the Free Software Foundation, either version 3 of the License, or
481;; (at your option) any later version.
482
483;; GNU Emacs is distributed in the hope that it will be useful,
484;; but WITHOUT ANY WARRANTY; without even the implied warranty of
485;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
486;; GNU General Public License for more details.
487
488;; You should have received a copy of the GNU General Public License
489;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
490
491;;; Commentary:
492;;
493;; This file was generated from admin/grammars/"
494 lang ".by.
495
496;;; Code:
62a81506 497")
78adbf9c
CY
498 (goto-char (point-min))
499 (delete-region (point-min) (line-end-position))
62a81506
CY
500 (insert ";;; " packagename
501 " --- Generated parser support file")
78adbf9c 502 (delete-trailing-whitespace)
62a81506
CY
503 (re-search-forward ";;; \\(.*\\) ends here")
504 (replace-match packagename nil nil nil 1)
78adbf9c
CY
505 (save-buffer))))))
506
62f43d66 507;;; bovine-grammar.el ends here