1 ;;; protobuf-mode.el --- major mode for editing protocol buffers.
3 ;; Author: Alexandre Vassalotti <alexandre@peadrop.com>
4 ;; Created: 23-Apr-2009
6 ;; Keywords: google protobuf languages
8 ;; Redistribution and use in source and binary forms, with or without
9 ;; modification, are permitted provided that the following conditions are
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
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.
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.
37 ;; - Put `protobuf-mode.el' in your Emacs load-path.
38 ;; - Add this line to your .emacs file:
39 ;; (require 'protobuf-mode)
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:
46 ;; (defconst my-protobuf-style
47 ;; '((c-basic-offset . 2)
48 ;; (indent-tabs-mode . nil)))
50 ;; (add-hook 'protobuf-mode-hook
51 ;; (lambda () (c-add-style "my-style" my-protobuf-style t)))
53 ;; Refer to the documentation of CC Mode for more information about
54 ;; customization details and how to use this mode.
57 ;; - Make highlighting for enum values work properly.
58 ;; - Fix the parser to recognize extensions as identifiers and not
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 ".").
72 ;; This mode does not inherit properties from other modes. So, we do not use
73 ;; the usual `c-add-language' function.
75 (put 'protobuf-mode
'c-mode-prefix
"protobuf-"))
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.
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"))
86 (c-lang-defconst c-modifier-kwds
87 protobuf
'("required" "optional" "repeated"))
89 (c-lang-defconst c-class-decl-kwds
90 protobuf
'("message" "enum" "service"))
92 (c-lang-defconst c-constant-kwds
93 protobuf
'("true" "false"))
95 (c-lang-defconst c-other-decl-kwds
96 protobuf
'("package" "import"))
98 (c-lang-defconst c-other-kwds
99 protobuf
'("default" "max"))
101 (c-lang-defconst c-identifier-ops
102 ;; Handle extended identifiers like google.protobuf.MessageOptions
103 protobuf
'((left-assoc ".")))
105 ;; The following keywords do not fit well in keyword classes defined by
106 ;; cc-mode. So, we approximate as best we can.
108 (c-lang-defconst c-type-list-kwds
109 protobuf
'("extensions" "to"))
111 (c-lang-defconst c-typeless-decl-kwds
112 protobuf
'("extend" "rpc" "option" "returns"))
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.
118 (c-lang-defconst c-brace-list-decl-kwds
119 ;; Remove syntax for C-style enumerations.
122 (c-lang-defconst c-block-stmt-1-kwds
123 ;; Remove syntax for "do" and "else" keywords.
126 (c-lang-defconst c-block-stmt-2-kwds
127 ;; Remove syntax for "for", "if", "switch" and "while" keywords.
130 (c-lang-defconst c-simple-stmt-kwds
131 ;; Remove syntax for "break", "continue", "goto" and "return" keywords.
134 (c-lang-defconst c-paren-stmt-kwds
135 ;; Remove special case for the "(;;)" in for-loops.
138 (c-lang-defconst c-label-kwds
139 ;; Remove case label syntax for the "case" and "default" keywords.
142 (c-lang-defconst c-before-label-kwds
143 ;; Remove special case for the label in a goto statement.
146 (c-lang-defconst c-cpp-matchers
147 ;; Disable all the C preprocessor syntax.
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
"\\([\{\};,]+\\)")
156 ;; Add support for variable levels of syntax highlighting.
158 (defconst protobuf-font-lock-keywords-1
(c-lang-const c-matchers-1 protobuf
)
159 "Minimal highlighting for protobuf-mode.")
161 (defconst protobuf-font-lock-keywords-2
(c-lang-const c-matchers-2 protobuf
)
162 "Fast normal highlighting for protobuf-mode.")
164 (defconst protobuf-font-lock-keywords-3
(c-lang-const c-matchers-3 protobuf
)
165 "Accurate normal highlighting for protobuf-mode.")
167 (defvar protobuf-font-lock-keywords protobuf-font-lock-keywords-3
168 "Default expressions to highlight in protobuf-mode.")
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
))))
178 (defvar protobuf-mode-abbrev-table nil
179 "Abbreviation table used in protobuf-mode buffers.")
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)))
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
)))
190 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.proto\\'" . protobuf-mode))
193 (defun protobuf-mode ()
194 "Major mode for editing Protocol Buffers description language.
196 The hook `c-mode-common-hook' is run with no argument at mode
197 initialization, then `protobuf-mode-hook'.
200 \\{protobuf-mode-map}"
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
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
)
218 (provide 'protobuf-mode
)
220 ;;; protobuf-mode.el ends here