1 ;;; peg-mode.el --- emacs mode for editing PEG grammar files
3 ;; Copyright (C) 2008 Utz-Uwe Haus <lisp@uuhaus.de>
5 ;; This file is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This file is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
21 ;; This file provides a major mode for editing PEG grammar files.
22 ;; It includes font-lock definitions and commands for
23 ;; controlling indentation, re-indenting by subdivisions.
25 ;; To use, put the following in your .emacs:
27 ;; (autoload 'peg-mode "peg-mode" "Mode for editing PEG grammar files" t)
29 ;; You may also want something like:
31 ;; (setq auto-mode-alist
32 ;; (append '(("\\.peg$" . peg-mode))
38 (defconst peg-version
"0"
39 "`peg-mode' version number.")
44 "Support for editing PEG grammar files."
47 (defcustom peg-mode-hook nil
48 "*Hook to be run when `peg-mode' is entered."
54 (defconst peg-comment-re
"#"
55 "Regexp that starts a comment line.")
65 (defvar peg-mode-abbrev-table nil
66 "Abbreviation table used in `peg-mode' buffers.")
67 (define-abbrev-table 'peg-mode-abbrev-table
())
69 (defvar peg-mode-syntax-table nil
70 "Syntax table used in `peg-mode' buffers.")
71 (if peg-mode-syntax-table
73 (setq peg-mode-syntax-table
(make-syntax-table)))
74 ;; comments: starting at ', ending at end of line
75 (modify-syntax-entry ?
# "<" peg-mode-syntax-table
)
76 (modify-syntax-entry ?
\n ">" peg-mode-syntax-table
)
78 (modify-syntax-entry ?
\' "\"" peg-mode-syntax-table
)
79 (modify-syntax-entry ?
\" "\"" peg-mode-syntax-table
)
81 (modify-syntax-entry ?
& "." peg-mode-syntax-table
)
82 (modify-syntax-entry ?
! "." peg-mode-syntax-table
)
83 (modify-syntax-entry ?
+ "." peg-mode-syntax-table
)
84 (modify-syntax-entry ?
* "." peg-mode-syntax-table
)
85 (modify-syntax-entry ?
/ "." peg-mode-syntax-table
)
86 (modify-syntax-entry ?
{ "<" peg-mode-syntax-table
)
87 (modify-syntax-entry ?
} ">" peg-mode-syntax-table
)
88 (modify-syntax-entry ?
\\ "\\" peg-mode-syntax-table
)
90 (modify-syntax-entry ?
( "()" peg-mode-syntax-table
)
91 (modify-syntax-entry ?
[ "|" peg-mode-syntax-table
)
92 (modify-syntax-entry ?
] "|" peg-mode-syntax-table
)
93 (modify-syntax-entry ?
' "|" peg-mode-syntax-table
)
99 "Major mode for editing xrdb config files"
101 (kill-all-local-variables)
102 (set-syntax-table peg-mode-syntax-table
)
103 (setq major-mode
'peg-mode
105 local-abbrev-table peg-mode-abbrev-table
)
106 ;(use-local-map peg-mode-map)
107 (setq font-lock-defaults
'(peg-font-lock-keywords))
109 (make-local-variable 'parse-sexp-ignore-comments
)
110 (make-local-variable 'comment-start-skip
)
111 (make-local-variable 'comment-start
)
112 (make-local-variable 'comment-end
)
113 (make-local-variable 'paragraph-start
)
114 (make-local-variable 'paragraph-separate
)
115 (make-local-variable 'paragraph-ignore-fill-prefix
)
116 (make-local-variable 'indent-region-function
)
117 ;; now set their values
118 (setq parse-sexp-ignore-comments t
119 comment-start-skip
"![ \t]*"
122 (setq indent-region-function
'peg-indent-region
123 paragraph-ignore-fill-prefix t
124 paragraph-start
(concat "^[ \t]*$\\|^[ \t]*[!]\\|" page-delimiter
)
125 paragraph-separate paragraph-start
)
126 (run-hooks 'peg-mode-hook
))
128 ;; faces and font-locking
129 (defvar peg-rule-name-face
'peg-rule-name-face
130 "Face for a production of a PEG rule's production in a peg grammar file.")
133 (make-face 'peg-rule-name-face
)
134 ;(make-face 'xrdb-option-value-face)
136 (defun peg-font-lock-mode-hook ()
137 (or (face-differs-from-default-p 'peg-rule-name-face
)
138 (copy-face 'font-lock-keyword-face
'peg-rule-name-face
))
139 (remove-hook 'font-lock-mode-hook
'peg-font-lock-mode-hook
))
140 (add-hook 'font-lock-mode-hook
'peg-font-lock-mode-hook
)
142 (defvar peg-font-lock-keywords
143 (list '("\\([^ \t\n]+\\)[ \t]*<-"
144 (1 peg-rule-name-face
))
146 "Additional expressions to highlight in peg grammar file mode.")
147 (put 'peg-mode
'font-lock-defaults
'(peg-font-lock-keywords))
151 ;;; peg-mode.el ends here