protobuf-mode.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;;; protobuf-mode.el --- major mode for editing protocol buffers.
  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. (require 'cl)
  62. (eval-when-compile
  63. (require 'cc-langs)
  64. (require 'cc-fonts))
  65. ;; This mode does not inherit properties from other modes. So, we do not use
  66. ;; the usual `c-add-language' function.
  67. (eval-and-compile
  68. (put 'protobuf-mode 'c-mode-prefix "protobuf-"))
  69. ;; The following code uses of the `c-lang-defconst' macro define syntactic
  70. ;; features of protocol buffer language. Refer to the documentation in the
  71. ;; cc-langs.el file for information about the meaning of the -kwds variables.
  72. (c-lang-defconst c-primitive-type-kwds
  73. protobuf '("double" "float" "int32" "int64" "uint32" "uint64" "sint32"
  74. "sint64" "fixed32" "fixed64" "sfixed32" "sfixed64" "bool"
  75. "string" "bytes" "group"))
  76. (c-lang-defconst c-modifier-kwds
  77. protobuf '("required" "optional" "repeated"))
  78. (c-lang-defconst c-class-decl-kwds
  79. protobuf '("message" "enum" "service"))
  80. (c-lang-defconst c-constant-kwds
  81. protobuf '("true" "false"))
  82. (c-lang-defconst c-other-decl-kwds
  83. protobuf '("package" "import"))
  84. (c-lang-defconst c-other-kwds
  85. protobuf '("default" "max"))
  86. (c-lang-defconst c-identifier-ops
  87. ;; Handle extended identifiers like google.protobuf.MessageOptions
  88. protobuf '((left-assoc ".")))
  89. ;; The following keywords do not fit well in keyword classes defined by
  90. ;; cc-mode. So, we approximate as best we can.
  91. (c-lang-defconst c-type-list-kwds
  92. protobuf '("extensions" "to" "reserved"))
  93. (c-lang-defconst c-typeless-decl-kwds
  94. protobuf '("extend" "rpc" "option" "returns"))
  95. ;; Here we remove default syntax for loops, if-statements and other C
  96. ;; syntactic features that are not supported by the protocol buffer language.
  97. (c-lang-defconst c-brace-list-decl-kwds
  98. ;; Remove syntax for C-style enumerations.
  99. protobuf nil)
  100. (c-lang-defconst c-block-stmt-1-kwds
  101. ;; Remove syntax for "do" and "else" keywords.
  102. protobuf nil)
  103. (c-lang-defconst c-block-stmt-2-kwds
  104. ;; Remove syntax for "for", "if", "switch" and "while" keywords.
  105. protobuf nil)
  106. (c-lang-defconst c-simple-stmt-kwds
  107. ;; Remove syntax for "break", "continue", "goto" and "return" keywords.
  108. protobuf nil)
  109. (c-lang-defconst c-paren-stmt-kwds
  110. ;; Remove special case for the "(;;)" in for-loops.
  111. protobuf nil)
  112. (c-lang-defconst c-label-kwds
  113. ;; Remove case label syntax for the "case" and "default" keywords.
  114. protobuf nil)
  115. (c-lang-defconst c-before-label-kwds
  116. ;; Remove special case for the label in a goto statement.
  117. protobuf nil)
  118. (c-lang-defconst c-cpp-matchers
  119. ;; Disable all the C preprocessor syntax.
  120. protobuf nil)
  121. (c-lang-defconst c-decl-prefix-re
  122. ;; Same as for C, except it does not match "(". This is needed for disabling
  123. ;; the syntax for casts.
  124. protobuf "\\([\{\};,]+\\)")
  125. ;; Add support for variable levels of syntax highlighting.
  126. (defconst protobuf-font-lock-keywords-1 (c-lang-const c-matchers-1 protobuf)
  127. "Minimal highlighting for protobuf-mode.")
  128. (defconst protobuf-font-lock-keywords-2 (c-lang-const c-matchers-2 protobuf)
  129. "Fast normal highlighting for protobuf-mode.")
  130. (defconst protobuf-font-lock-keywords-3 (c-lang-const c-matchers-3 protobuf)
  131. "Accurate normal highlighting for protobuf-mode.")
  132. (defvar protobuf-font-lock-keywords protobuf-font-lock-keywords-3
  133. "Default expressions to highlight in protobuf-mode.")
  134. ;; Our syntax table is auto-generated from the keyword classes we defined
  135. ;; previously with the `c-lang-const' macro.
  136. (defvar protobuf-mode-syntax-table nil
  137. "Syntax table used in protobuf-mode buffers.")
  138. (or protobuf-mode-syntax-table
  139. (setq protobuf-mode-syntax-table
  140. (funcall (c-lang-const c-make-mode-syntax-table protobuf))))
  141. (defvar protobuf-mode-abbrev-table nil
  142. "Abbreviation table used in protobuf-mode buffers.")
  143. (defvar protobuf-mode-map nil
  144. "Keymap used in protobuf-mode buffers.")
  145. (or protobuf-mode-map
  146. (setq protobuf-mode-map (c-make-inherited-keymap)))
  147. (easy-menu-define protobuf-menu protobuf-mode-map
  148. "Protocol Buffers Mode Commands"
  149. (cons "Protocol Buffers" (c-lang-const c-mode-menu protobuf)))
  150. ;;;###autoload (add-to-list 'auto-mode-alist '("\\.proto\\'" . protobuf-mode))
  151. ;;;###autoload
  152. (defun protobuf-mode ()
  153. "Major mode for editing Protocol Buffers description language.
  154. The hook `c-mode-common-hook' is run with no argument at mode
  155. initialization, then `protobuf-mode-hook'.
  156. Key bindings:
  157. \\{protobuf-mode-map}"
  158. (interactive)
  159. (kill-all-local-variables)
  160. (set-syntax-table protobuf-mode-syntax-table)
  161. (setq major-mode 'protobuf-mode
  162. mode-name "Protocol-Buffers"
  163. local-abbrev-table protobuf-mode-abbrev-table
  164. abbrev-mode t)
  165. (use-local-map protobuf-mode-map)
  166. (c-initialize-cc-mode t)
  167. (if (fboundp 'c-make-emacs-variables-local)
  168. (c-make-emacs-variables-local))
  169. (c-init-language-vars protobuf-mode)
  170. (c-common-init 'protobuf-mode)
  171. (easy-menu-add protobuf-menu)
  172. (c-run-mode-hooks 'c-mode-common-hook 'protobuf-mode-hook)
  173. (c-update-modeline))
  174. (provide 'protobuf-mode)
  175. ;;; protobuf-mode.el ends here