protobuf-mode.el 8.0 KB

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