/
racc-mode.el
203 lines (177 loc) · 6.32 KB
/
racc-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
;; racc-mode.el
;;
;; Copyright (c) 2001 sheepman
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. The name of the author may not be used to endorse or promote products
;; derived from this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;
;; write the following in your .emacs file
;; racc-mode
;; (autoload 'racc-mode "racc-mode" "alternate mode for editing racc files" t)
;; (setq auto-mode-alist (append '(("\\.ry$" . racc-mode)) auto-mode-alist))
;;
(require 'derived)
(define-derived-mode racc-mode ruby-mode "Racc"
"Major mode for Racc editing."
(interactive)
(setq indent-line-function 'racc-indent-line)
(define-key racc-mode-map "\t" 'racc-indent-command)
(define-key racc-mode-map "\C-j" 'racc-reindent-newline-and-indent)
(run-hooks 'racc-mode-hook))
(defun racc-indent-command ()
(interactive)
(racc-indent-line t))
(defun racc-reindent-newline-and-indent ()
(interactive)
(racc-indent-command)
(newline)
(racc-indent-command))
(defun racc-indent-line (&optional flag)
(if (string-match "^[ \t]*rule[ \t]*$" (current-line))
(racc-indent-to 0)
(if (point-in-rule)
(racc-indent-to (racc-calculate-indent))
(if (point-before-rule)
(racc-indent-to (racc-calculate-indent-before-rule))
(ruby-indent-line t)))))
(defun beginning-of-rule ()
(beginning-of-buffer)
(re-search-forward "^rule")
(beginning-of-line))
(defun end-of-rule ()
(beginning-of-buffer)
(re-search-forward "^end")
(end-of-line))
(defun point-in-rule ()
(save-excursion
(and (re-search-backward-without-moving-point "^rule")
(or (not (re-search-backward-without-moving-point "^end"))
(< (re-search-backward-without-moving-point "^end")
(re-search-backward-without-moving-point "^rule"))))))
(defun point-before-rule ()
(save-excursion
(and (re-search-backward-without-moving-point "^class"))
(re-search-forward-without-moving-point "^rule")))
(defun point-in-prec ()
(save-excursion
(and (point-before-rule)
(or (and (re-search-backward-without-moving-point "^prechigh")
(re-search-forward-without-moving-point "^preclow"))
(and (re-search-forward-without-moving-point "^prechigh")
(re-search-backward-without-moving-point "^preclow"))
))))
(defun point-in-convert ()
(save-excursion
(point-before-rule)
(and (re-search-backward-without-moving-point "^convert")
(re-search-forward-without-moving-point "^end"))))
(defun point-in-token-statement ()
(save-excursion
(defun match-token-line (str)
(or (string-match "^[ \t]*|[ \t]*"
str)
(string-match "^[ \t]*[^ \t\n]+[ \t]*:"
str)))
(and (point-in-rule)
(or (match-token-line (current-line))
(and (not (string-match "^[ \t]*[{}][ \t]*$" (current-line)))
(save-excursion
(forward-line -1)
(match-token-line (current-line)))
(save-excursion
(forward-line 1)
(match-token-line (current-line))))))))
(defun point-in-action-statement ()
(and (point-in-rule)
(not (point-in-token-statement))))
(defun beginning-of-action ()
(if (point-in-action-statement)
(re-search-backward "^[ \t]*{" nil t)
nil))
(defvar racc-indent-level 2)
(setq racc-token-length-max 11)
(defun racc-indent-to (x)
(ruby-indent-to x))
(defun racc-calculate-indent ()
(save-excursion
(if (point-in-token-statement)
(if (string-match "^[ \t]*|[ \t]*" (current-line))
(+ racc-indent-level
racc-token-length-max)
(if (string-match "\\([^ \t\n]+[ \t]*\\):" (current-line))
(let ((strlen (string-length
(match-string 1 (current-line))))
(ind (+ racc-indent-level
racc-token-length-max)))
(if (> strlen ind)
0
(- ind strlen)))
(+ racc-indent-level
racc-token-length-max
(* 2 racc-indent-level))))
(if (string-match "^[ \t]*[{}][ \t]*$" (current-line))
(+ racc-indent-level
racc-token-length-max
(* 2 racc-indent-level))
(ruby-calculate-indent (max (save-excursion
(ruby-beginning-of-indent)
(point))
(save-excursion
(beginning-of-action)
(point))))))))
(defun racc-calculate-indent-before-rule ()
(cond ((or (string-match "^[ \t]*prechigh" (current-line))
(string-match "^[ \t]*preclow" (current-line)))
0)
((point-in-prec)
racc-indent-level)
((string-match "^[ \t]*expect" (current-line))
0)
((or (string-match "^[ \t]*convert" (current-line))
(string-match "^[ \t]*end" (current-line)))
0)
((point-in-convert)
racc-indent-level)
((string-match "^[ \t]*token" (current-line))
0)
((string-match "^[ \t]*start" (current-line))
0)
(t 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;
;; general functions
;;;;;;;;;;;;;;;;;;;;;;;;
(defun current-line ()
(save-excursion
(buffer-substring (save-excursion (beginning-of-line)
(point))
(save-excursion (end-of-line)
(1+ (point))))))
(defun string-length (str)
(- (string-match "$" str) (string-match "^" str)))
(defun re-search-forward-without-moving-point (str)
(save-excursion
(re-search-forward str nil t)))
(defun re-search-backward-without-moving-point (str)
(save-excursion
(re-search-backward str nil t)))
(provide 'racc-mode)