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