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