OLD | NEW |
| (Empty) |
1 ;;; protobuf-mode.el --- major mode for editing protocol buffers. | |
2 | |
3 ;; Author: Alexandre Vassalotti <alexandre@peadrop.com> | |
4 ;; Created: 23-Apr-2009 | |
5 ;; Version: 0.3 | |
6 ;; Keywords: google protobuf languages | |
7 | |
8 ;; Redistribution and use in source and binary forms, with or without | |
9 ;; modification, are permitted provided that the following conditions are | |
10 ;; met: | |
11 ;; | |
12 ;; * Redistributions of source code must retain the above copyright | |
13 ;; notice, this list of conditions and the following disclaimer. | |
14 ;; * Redistributions in binary form must reproduce the above | |
15 ;; copyright notice, this list of conditions and the following disclaimer | |
16 ;; in the documentation and/or other materials provided with the | |
17 ;; distribution. | |
18 ;; * Neither the name of Google Inc. nor the names of its | |
19 ;; contributors may be used to endorse or promote products derived from | |
20 ;; this software without specific prior written permission. | |
21 ;; | |
22 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
25 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
26 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
27 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
28 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
29 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
30 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
31 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
32 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
33 | |
34 ;;; Commentary: | |
35 | |
36 ;; Installation: | |
37 ;; - Put `protobuf-mode.el' in your Emacs load-path. | |
38 ;; - Add this line to your .emacs file: | |
39 ;; (require 'protobuf-mode) | |
40 ;; | |
41 ;; You can customize this mode just like any mode derived from CC Mode. If | |
42 ;; you want to add customizations specific to protobuf-mode, you can use the | |
43 ;; `protobuf-mode-hook'. For example, the following would make protocol-mode | |
44 ;; use 2-space indentation: | |
45 ;; | |
46 ;; (defconst my-protobuf-style | |
47 ;; '((c-basic-offset . 2) | |
48 ;; (indent-tabs-mode . nil))) | |
49 ;; | |
50 ;; (add-hook 'protobuf-mode-hook | |
51 ;; (lambda () (c-add-style "my-style" my-protobuf-style t))) | |
52 ;; | |
53 ;; Refer to the documentation of CC Mode for more information about | |
54 ;; customization details and how to use this mode. | |
55 ;; | |
56 ;; TODO: | |
57 ;; - Make highlighting for enum values work properly. | |
58 ;; - Fix the parser to recognize extensions as identifiers and not | |
59 ;; as casts. | |
60 ;; - Improve the parsing of option assignment lists. For example: | |
61 ;; optional int32 foo = 1 [(my_field_option) = 4.5]; | |
62 ;; - Add support for fully-qualified identifiers (e.g., with a leading "."). | |
63 | |
64 ;;; Code: | |
65 | |
66 (require 'cc-mode) | |
67 | |
68 (eval-when-compile | |
69 (require 'cc-langs) | |
70 (require 'cc-fonts)) | |
71 | |
72 ;; This mode does not inherit properties from other modes. So, we do not use | |
73 ;; the usual `c-add-language' function. | |
74 (eval-and-compile | |
75 (put 'protobuf-mode 'c-mode-prefix "protobuf-")) | |
76 | |
77 ;; The following code uses of the `c-lang-defconst' macro define syntactic | |
78 ;; features of protocol buffer language. Refer to the documentation in the | |
79 ;; cc-langs.el file for information about the meaning of the -kwds variables. | |
80 | |
81 (c-lang-defconst c-primitive-type-kwds | |
82 protobuf '("double" "float" "int32" "int64" "uint32" "uint64" "sint32" | |
83 "sint64" "fixed32" "fixed64" "sfixed32" "sfixed64" "bool" | |
84 "string" "bytes" "group")) | |
85 | |
86 (c-lang-defconst c-modifier-kwds | |
87 protobuf '("required" "optional" "repeated")) | |
88 | |
89 (c-lang-defconst c-class-decl-kwds | |
90 protobuf '("message" "enum" "service")) | |
91 | |
92 (c-lang-defconst c-constant-kwds | |
93 protobuf '("true" "false")) | |
94 | |
95 (c-lang-defconst c-other-decl-kwds | |
96 protobuf '("package" "import")) | |
97 | |
98 (c-lang-defconst c-other-kwds | |
99 protobuf '("default" "max")) | |
100 | |
101 (c-lang-defconst c-identifier-ops | |
102 ;; Handle extended identifiers like google.protobuf.MessageOptions | |
103 protobuf '((left-assoc "."))) | |
104 | |
105 ;; The following keywords do not fit well in keyword classes defined by | |
106 ;; cc-mode. So, we approximate as best we can. | |
107 | |
108 (c-lang-defconst c-type-list-kwds | |
109 protobuf '("extensions" "to" "reserved")) | |
110 | |
111 (c-lang-defconst c-typeless-decl-kwds | |
112 protobuf '("extend" "rpc" "option" "returns")) | |
113 | |
114 | |
115 ;; Here we remove default syntax for loops, if-statements and other C | |
116 ;; syntactic features that are not supported by the protocol buffer language. | |
117 | |
118 (c-lang-defconst c-brace-list-decl-kwds | |
119 ;; Remove syntax for C-style enumerations. | |
120 protobuf nil) | |
121 | |
122 (c-lang-defconst c-block-stmt-1-kwds | |
123 ;; Remove syntax for "do" and "else" keywords. | |
124 protobuf nil) | |
125 | |
126 (c-lang-defconst c-block-stmt-2-kwds | |
127 ;; Remove syntax for "for", "if", "switch" and "while" keywords. | |
128 protobuf nil) | |
129 | |
130 (c-lang-defconst c-simple-stmt-kwds | |
131 ;; Remove syntax for "break", "continue", "goto" and "return" keywords. | |
132 protobuf nil) | |
133 | |
134 (c-lang-defconst c-paren-stmt-kwds | |
135 ;; Remove special case for the "(;;)" in for-loops. | |
136 protobuf nil) | |
137 | |
138 (c-lang-defconst c-label-kwds | |
139 ;; Remove case label syntax for the "case" and "default" keywords. | |
140 protobuf nil) | |
141 | |
142 (c-lang-defconst c-before-label-kwds | |
143 ;; Remove special case for the label in a goto statement. | |
144 protobuf nil) | |
145 | |
146 (c-lang-defconst c-cpp-matchers | |
147 ;; Disable all the C preprocessor syntax. | |
148 protobuf nil) | |
149 | |
150 (c-lang-defconst c-decl-prefix-re | |
151 ;; Same as for C, except it does not match "(". This is needed for disabling | |
152 ;; the syntax for casts. | |
153 protobuf "\\([\{\};,]+\\)") | |
154 | |
155 | |
156 ;; Add support for variable levels of syntax highlighting. | |
157 | |
158 (defconst protobuf-font-lock-keywords-1 (c-lang-const c-matchers-1 protobuf) | |
159 "Minimal highlighting for protobuf-mode.") | |
160 | |
161 (defconst protobuf-font-lock-keywords-2 (c-lang-const c-matchers-2 protobuf) | |
162 "Fast normal highlighting for protobuf-mode.") | |
163 | |
164 (defconst protobuf-font-lock-keywords-3 (c-lang-const c-matchers-3 protobuf) | |
165 "Accurate normal highlighting for protobuf-mode.") | |
166 | |
167 (defvar protobuf-font-lock-keywords protobuf-font-lock-keywords-3 | |
168 "Default expressions to highlight in protobuf-mode.") | |
169 | |
170 ;; Our syntax table is auto-generated from the keyword classes we defined | |
171 ;; previously with the `c-lang-const' macro. | |
172 (defvar protobuf-mode-syntax-table nil | |
173 "Syntax table used in protobuf-mode buffers.") | |
174 (or protobuf-mode-syntax-table | |
175 (setq protobuf-mode-syntax-table | |
176 (funcall (c-lang-const c-make-mode-syntax-table protobuf)))) | |
177 | |
178 (defvar protobuf-mode-abbrev-table nil | |
179 "Abbreviation table used in protobuf-mode buffers.") | |
180 | |
181 (defvar protobuf-mode-map nil | |
182 "Keymap used in protobuf-mode buffers.") | |
183 (or protobuf-mode-map | |
184 (setq protobuf-mode-map (c-make-inherited-keymap))) | |
185 | |
186 (easy-menu-define protobuf-menu protobuf-mode-map | |
187 "Protocol Buffers Mode Commands" | |
188 (cons "Protocol Buffers" (c-lang-const c-mode-menu protobuf))) | |
189 | |
190 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.proto\\'" . protobuf-mode)) | |
191 | |
192 ;;;###autoload | |
193 (defun protobuf-mode () | |
194 "Major mode for editing Protocol Buffers description language. | |
195 | |
196 The hook `c-mode-common-hook' is run with no argument at mode | |
197 initialization, then `protobuf-mode-hook'. | |
198 | |
199 Key bindings: | |
200 \\{protobuf-mode-map}" | |
201 (interactive) | |
202 (kill-all-local-variables) | |
203 (set-syntax-table protobuf-mode-syntax-table) | |
204 (setq major-mode 'protobuf-mode | |
205 mode-name "Protocol-Buffers" | |
206 local-abbrev-table protobuf-mode-abbrev-table | |
207 abbrev-mode t) | |
208 (use-local-map protobuf-mode-map) | |
209 (c-initialize-cc-mode t) | |
210 (if (fboundp 'c-make-emacs-variables-local) | |
211 (c-make-emacs-variables-local)) | |
212 (c-init-language-vars protobuf-mode) | |
213 (c-common-init 'protobuf-mode) | |
214 (easy-menu-add protobuf-menu) | |
215 (c-run-mode-hooks 'c-mode-common-hook 'protobuf-mode-hook) | |
216 (c-update-modeline)) | |
217 | |
218 (provide 'protobuf-mode) | |
219 | |
220 ;;; protobuf-mode.el ends here | |
OLD | NEW |