Add 2008 to copyright years.
[bpt/emacs.git] / lisp / progmodes / ebnf-yac.el
index bff241a..06aade6 100644 (file)
@@ -1,18 +1,18 @@
 ;;; ebnf-yac.el --- parser for Yacc/Bison
 
-;; Copyright (C) 1999, 2000, 2001 Free Sofware Foundation, Inc.
+;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+;;   Free Software Foundation, Inc.
 
-;; Author: Vinicius Jose Latorre <vinicius@cpqd.com.br>
-;; Maintainer: Vinicius Jose Latorre <vinicius@cpqd.com.br>
+;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
+;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Keywords: wp, ebnf, PostScript
-;; Time-stamp: <2001/08/15 17:15:15 vinicius>
-;; Version: 1.1
+;; Version: 1.3
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -22,8 +22,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -42,7 +42,9 @@
 ;;
 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
 ;;
-;; YACC-Definitions = "%token" [ "<" Name ">" ] Name-List
+;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
+;;                    [ "<" Name ">" ] Name-List
+;;                  | "%prec" Name
 ;;                  | "any other Yacc definition"
 ;;                  .
 ;;
 ;; Name = "[A-Za-z][A-Za-z0-9_.]*".
 ;;
 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
-;;         | "//" "any character" "\\n".
+;;         | "//" "any character, but the newline \"\\n\"" "\\n".
+;;
+;;
+;; In other words, a valid Name begins with a letter (upper or lower case)
+;; followed by letters, decimal digits, underscore (_) or point (.).  For
+;; example: this_is_a_valid.name, Another_EXAMPLE, mIxEd.CaSe.
+;;
+;;
+;; Acknowledgements
+;; ----------------
+;;
+;; Thanks to Matthew K. Junker <junker@alum.mit.edu> for the suggestion to deal
+;; with %right, %left and %prec pragmas.  His suggestion was extended to deal
+;; with %nonassoc pragma too.
 ;;
 ;;
 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 
 (defvar ebnf-yac-error nil
-  "Non-nil means \"error\" occured.")
+  "Non-nil means \"error\" occurred.")
 
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Syntatic analyzer
+;; Syntactic analyzer
 
 
 ;;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
     syntax-list))
 
 
-;;; YACC-Definitions = "%token" [ "<" Name ">" ] Name-List
+;;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
+;;;                    [ "<" Name ">" ] Name-List
+;;;                  | "%prec" Name
 ;;;                  | "any other Yacc definition"
 ;;;                  .
 
     (while (not (memq token '(yac-separator end-of-input)))
       (setq token
            (cond
-            ;; "%token" [ "<" Name ">" ] Name-List
+            ;; ( "%token" | "%left" | "%right" | "%nonassoc" )
+            ;; [ "<" Name ">" ] Name-List
             ((eq token 'yac-token)
              (setq token (ebnf-yac-lex))
              (when (eq token 'open-angle)
                    ebnf-yac-token-list (nconc (cdr token)
                                               ebnf-yac-token-list))
              (car token))
-            ;; "any other Yacc definition"
+            ;;  "%prec" Name
+            ((eq token 'yac-prec)
+             (or (eq (ebnf-yac-lex) 'non-terminal)
+                 (error "Missing prec name"))
+             (ebnf-yac-lex))
+            ;;  "any other Yacc definition"
             (t
              (ebnf-yac-lex))
             )))
                 factor (ebnf-yac-factor token))
       (setq seq (cons factor seq)))
     (cons token
-         (cond
-          ;; ignore error recovery
-          ((and ebnf-yac-ignore-error-recovery ebnf-yac-error)
-           nil)
-          ;; null sequence
-          ((null seq)
-           (ebnf-make-empty))
-          ;; sequence with only one element
-          ((= (length seq) 1)
-           (car seq))
-          ;; a real sequence
-          (t
-           (ebnf-make-sequence (nreverse seq)))
-          ))))
+         (if (and ebnf-yac-ignore-error-recovery ebnf-yac-error)
+             ;; ignore error recovery
+             nil
+           (ebnf-token-sequence seq)))))
 
 
 ;;; Factor = Name
 
 
 (defun ebnf-yac-lex ()
-  "Lexical analyser for Yacc/Bison.
+  "Lexical analyzer for Yacc/Bison.
 
 Return a lexical token.
 
@@ -332,7 +345,7 @@ See documentation for variable `ebnf-yac-lex'."
        'end-of-input)
        ;; error
        ((eq token 'error)
-       (error "Illegal character"))
+       (error "Invalid character"))
        ;; "string"
        ((eq token 'string)
        (setq ebnf-yac-lex (ebnf-get-string))
@@ -360,9 +373,13 @@ See documentation for variable `ebnf-yac-lex'."
         ((eq (following-char) ?%)
          (forward-char)
          'yac-separator)
-        ;; %TOKEN
-        ((string= (upcase (ebnf-buffer-substring "0-9A-Za-z_")) "TOKEN")
-         'yac-token)
+        ;; %TOKEN, %RIGHT, %LEFT,  %PREC, %NONASSOC
+        ((cdr (assoc (upcase (ebnf-buffer-substring "0-9A-Za-z_"))
+                     '(("TOKEN"    . yac-token)
+                       ("RIGHT"    . yac-token)
+                       ("LEFT"     . yac-token)
+                       ("NONASSOC" . yac-token)
+                       ("PREC"     . yac-prec)))))
         ;; other Yacc pragmas
         (t
          'yac-pragma)
@@ -383,14 +400,16 @@ See documentation for variable `ebnf-yac-lex'."
   (< (point) ebnf-limit))
 
 
+;; replace the range "\177-\377" (see `ebnf-range-regexp').
+(defconst ebnf-yac-skip-chars
+  (ebnf-range-regexp "^{}/'\"\000-\010\013\016-\037" ?\177 ?\377))
+
+
 (defun ebnf-yac-skip-code ()
   (forward-char)
   (let ((pair 1))
     (while (> pair 0)
-      ;; replace the range "\177-\377" (see `ebnf-range-regexp').
-      (skip-chars-forward (ebnf-range-regexp "^{}/'\"\000-\010\013\016-\037"
-                                            ?\177 ?\377)
-                         ebnf-limit)
+      (skip-chars-forward ebnf-yac-skip-chars ebnf-limit)
       (cond
        ((= (following-char) ?{)
        (forward-char)
@@ -405,7 +424,7 @@ See documentation for variable `ebnf-yac-lex'."
        ((= (following-char) ?\')
        (ebnf-string " -&(-~" ?\' "character"))
        (t
-       (error "Illegal character"))
+       (error "Invalid character"))
        )))
   (ebnf-yac-skip-spaces))
 
@@ -456,7 +475,7 @@ See documentation for variable `ebnf-yac-lex'."
               (forward-char)
               (setq not-end nil)))
            (t
-            (error "Illegal character"))
+            (error "Invalid character"))
            ))))
 
 
@@ -489,4 +508,5 @@ See documentation for variable `ebnf-yac-lex'."
 (provide 'ebnf-yac)
 
 
+;;; arch-tag: 8a96989c-0b1d-42ba-a020-b2901f9a2a4d
 ;;; ebnf-yac.el ends here