Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.6 KiB

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