1 ;;; bovine-grammar.el --- Bovine's input grammar mode
3 ;; Copyright (C) 2002, 2003, 2004, 2007 David Ponce
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 26 Aug 2002
9 ;; X-RCS: $Id: bovine-grammar.el.upstream,v 1.1 2007-11-26 15:11:50 michaels Exp $
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This software 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 GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Major mode for editing Bovine's input grammar (.by) files.
36 (require 'semantic-grammar)
37 (require 'bovine-grammar-macros)
39 ;; Cache of macro definitions currently in use.
40 (defvar bovine--grammar-macros nil)
42 (defun bovine-grammar-expand-form (form quotemode &optional inplace)
43 "Expand FORM into a new one suitable to the bovine parser.
44 FORM is a list in which we are substituting.
45 Argument QUOTEMODE is non-nil if we are in backquote mode.
46 When non-nil, optional argument INPLACE indicates that FORM is being
47 expanded from elsewhere."
48 (when (eq (car form) 'quote)
49 (setq form (cdr form))
51 ((and (= (length form) 1) (listp (car form)))
53 (bovine-grammar-expand-form (car form) quotemode nil)
55 (setq form nil inplace nil)
57 ((and (= (length form) 1) (symbolp (car form)))
58 (insert "\n'" (symbol-name (car form)))
59 (setq form nil inplace nil)
65 (let ((macro (assq (car form) bovine--grammar-macros))
66 inlist first n q x val)
68 (bovine-grammar-expand-form
69 (apply (cdr macro) (cdr form))
71 (if inplace (insert "\n("))
73 (setq first (car form)
77 (when (and (not inlist) (not inplace))
83 ;;(let ((fn (and (symbolp (caar form)) (fboundp (caar form)))))
84 (when (and (not inlist) (not inplace))
87 ;;(if (and inplace (not fn) (not (eq (caar form) 'EXPAND)))
88 ;; (insert " (append"))
89 (bovine-grammar-expand-form
90 first quotemode t) ;;(and fn (not (eq fn 'quote))))
91 ;;(if (and inplace (not fn) (not (eq (caar form) 'EXPAND)))
96 (setq n (symbol-name first) ;the name
97 q quotemode ;implied quote flag
99 (if (eq (aref n 0) ?,)
101 ;; backquote mode needs the @
102 (if (eq (aref n 1) ?@)
103 (setq n (substring n 2)
106 ;; non backquote mode behaves normally.
107 (setq n (substring n 1)
109 (setq n (substring n 1)
113 ;; We expand only the next item in place (a list?)
114 ;; A regular inline-list...
115 (bovine-grammar-expand-form (car form) quotemode t)
116 (setq form (cdr form)))
117 (if (and (eq (aref n 0) ?$)
118 ;; Don't expand $ tokens in implied quote mode.
119 ;; This acts like quoting in other symbols.
123 ((and (not x) (not inlist) (not inplace))
125 ((and x inlist (not inplace))
128 (insert "\n(nth " (int-to-string
129 (1- (string-to-number
132 (and (not x) (not inplace)
135 (when (and (not inlist) (not inplace))
138 (or (char-equal (char-before) ?\()
140 (insert (if (or inplace (eq first t))
145 (when (and (not inlist) (not inplace))
148 (insert (format "\n%S" first))
151 (if inlist (insert ")"))
152 (if inplace (insert ")")))
155 (defun bovine-grammar-expand-action (textform quotemode)
156 "Expand semantic action string TEXTFORM into Lisp code.
157 QUOTEMODE is the mode in which quoted symbols are slurred."
158 (if (string= "" textform)
160 (let ((sexp (read textform)))
161 ;; We converted the lambda string into a list. Now write it
162 ;; out as the bovine lambda expression, and do macro-like
163 ;; conversion upon it.
166 ((eq (car sexp) 'EXPAND)
167 (insert ",(lambda (vals start end)")
168 ;; The EXPAND macro definition is mandatory
169 (bovine-grammar-expand-form
170 (apply (cdr (assq 'EXPAND bovine--grammar-macros)) (cdr sexp))
173 ((and (listp (car sexp)) (eq (caar sexp) 'EVAL))
174 ;; The user wants to evaluate the following args.
175 ;; Use a simpler expander
178 (insert ",(semantic-lambda")
179 (bovine-grammar-expand-form sexp quotemode)
183 (defun bovine-grammar-parsetable-builder ()
184 "Return the parser table expression as a string value.
185 The format of a bovine parser table is:
187 ( ( NONTERMINAL-SYMBOL1 MATCH-LIST1 )
188 ( NONTERMINAL-SYMBOL2 MATCH-LIST2 )
190 ( NONTERMINAL-SYMBOLn MATCH-LISTn )
192 Where each NONTERMINAL-SYMBOL is an artificial symbol which can appear
193 in any child state. As a starting place, one of the NONTERMINAL-SYMBOLS
194 must be `bovine-toplevel'.
196 A MATCH-LIST is a list of possible matches of the form:
203 where STATE-LIST is of the form:
204 ( TYPE1 [ \"VALUE1\" ] TYPE2 [ \"VALUE2\" ] ... LAMBDA )
206 where TYPE is one of the returned types of the token stream.
207 VALUE is a value, or range of values to match against. For
208 example, a SYMBOL might need to match \"foo\". Some TYPES will not
209 have matching criteria.
211 LAMBDA is a lambda expression which is evaled with the text of the
212 type when it is found. It is passed the list of all buffer text
213 elements found since the last lambda expression. It should return a
214 semantic element (see below.)
216 For consistency between languages, try to use common return values
217 from your parser. Please reference the chapter \"Writing Parsers\" in
218 the \"Language Support Developer's Guide -\" in the semantic texinfo
220 (let* ((start (semantic-grammar-start))
221 (scopestart (semantic-grammar-scopestart))
222 (quotemode (semantic-grammar-quotemode))
223 (tags (semantic-find-tags-by-class
224 'token (current-buffer)))
225 (nterms (semantic-find-tags-by-class
226 'nonterminal (current-buffer)))
227 ;; Setup the cache of macro definitions.
228 (bovine--grammar-macros (semantic-grammar-macros))
229 nterm rules items item actn prec tag type regex)
231 ;; Check some trivial things
234 (error "Bad input grammar"))
237 (message "Extra start symbols %S ignored" (cdr start)))
238 (setq start (symbol-name (car start)))
239 (unless (semantic-find-first-tag-by-name start nterms)
240 (error "start symbol `%s' has no rule" start)))
242 ;; Default to the first grammar rule.
243 (setq start (semantic-tag-name (car nterms)))))
245 (setq scopestart (symbol-name scopestart))
246 (unless (semantic-find-first-tag-by-name scopestart nterms)
247 (error "scopestart symbol `%s' has no rule" scopestart)))
249 ;; Generate the grammar Lisp form.
253 ;; Insert the start/scopestart rules
254 (insert "\n(bovine-toplevel \n("
256 ")\n) ;; end bovine-toplevel\n")
258 (insert "\n(bovine-inner-scope \n("
260 ")\n) ;; end bovine-inner-scope\n"))
261 ;; Process each nonterminal
263 (setq nterm (car nterms)
264 ;; We can't use the override form because the current buffer
265 ;; is not the originator of the tag.
266 rules (semantic-tag-components-semantic-grammar-mode nterm)
267 nterm (semantic-tag-name nterm)
269 (when (member nterm '("bovine-toplevel" "bovine-inner-scope"))
270 (error "`%s' is a reserved internal name" nterm))
274 (setq items (semantic-tag-get-attribute (car rules) :value)
275 prec (semantic-tag-get-attribute (car rules) :prec)
276 actn (semantic-tag-get-attribute (car rules) :expr)
282 (insert ";;EMPTY" (if actn "" "\n"))
285 (setq item (car items)
287 (if (consp item) ;; mid-rule action
288 (message "Mid-rule action %S ignored" item)
289 (or (char-equal (char-before) ?\()
292 ((member item '("bovine-toplevel" "bovine-inner-scope"))
293 (error "`%s' is a reserved internal name" item))
294 ;; Replace ITEM by its %token definition.
295 ;; If a '%token TYPE ITEM [REGEX]' definition exists
296 ;; in the grammar, ITEM is replaced by TYPE [REGEX].
297 ((setq tag (semantic-find-first-tag-by-name
299 type (semantic-tag-get-attribute tag :type))
301 (if (setq regex (semantic-tag-get-attribute tag :value))
302 (insert (format "\n%S" regex))))
305 (insert (semantic-grammar-item-text item)))
308 (message "%%prec %S ignored" prec))
310 (bovine-grammar-expand-action actn quotemode))
312 (insert "\n) ;; end " nterm "\n"))
316 (defun bovine-grammar-setupcode-builder ()
317 "Return the text of the setup code."
319 "(setq semantic--parse-table %s\n\
320 semantic-debug-parser-source %S\n\
321 semantic-debug-parser-class 'semantic-bovine-debug-parser
322 semantic-flex-keywords-obarray %s\n\
324 (semantic-grammar-parsetable)
326 (semantic-grammar-keywordtable)
327 (let ((mode (semantic-grammar-languagemode)))
328 ;; Is there more than one major mode?
329 (if (and (listp mode) (> (length mode) 1))
330 (format "semantic-equivalent-major-modes '%S\n" mode)
333 (defvar bovine-grammar-menu
336 "BY mode specific grammar menu.
337 Menu items are appended to the common grammar menu.")
340 (define-derived-mode bovine-grammar-mode semantic-grammar-mode "BY"
341 "Major mode for editing Bovine grammars."
342 (semantic-grammar-setup-menu bovine-grammar-menu)
343 (semantic-install-function-overrides
344 '((grammar-parsetable-builder . bovine-grammar-parsetable-builder)
345 (grammar-setupcode-builder . bovine-grammar-setupcode-builder)
349 (add-to-list 'auto-mode-alist '("\\.by$" . bovine-grammar-mode))
352 (eval-after-load "speedbar"
353 '(speedbar-add-supported-extension ".by"))
355 (provide 'bovine-grammar)
357 ;;; bovine-grammar.el ends here