protobuf.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. function wordRegexp(words) {
  15. return new RegExp('^((' + words.join(')|(') + '))\\b', 'i')
  16. }
  17. var keywordArray = [
  18. 'package',
  19. 'message',
  20. 'import',
  21. 'syntax',
  22. 'required',
  23. 'optional',
  24. 'repeated',
  25. 'reserved',
  26. 'default',
  27. 'extensions',
  28. 'packed',
  29. 'bool',
  30. 'bytes',
  31. 'double',
  32. 'enum',
  33. 'float',
  34. 'string',
  35. 'int32',
  36. 'int64',
  37. 'uint32',
  38. 'uint64',
  39. 'sint32',
  40. 'sint64',
  41. 'fixed32',
  42. 'fixed64',
  43. 'sfixed32',
  44. 'sfixed64',
  45. 'option',
  46. 'service',
  47. 'rpc',
  48. 'returns',
  49. ]
  50. var keywords = wordRegexp(keywordArray)
  51. CodeMirror.registerHelper('hintWords', 'protobuf', keywordArray)
  52. var identifiers = new RegExp('^[_A-Za-z\xa1-\uffff][_A-Za-z0-9\xa1-\uffff]*')
  53. function tokenBase(stream) {
  54. // whitespaces
  55. if (stream.eatSpace()) return null
  56. // Handle one line Comments
  57. if (stream.match('//')) {
  58. stream.skipToEnd()
  59. return 'comment'
  60. }
  61. // Handle Number Literals
  62. if (stream.match(/^[0-9\.+-]/, false)) {
  63. if (stream.match(/^[+-]?0x[0-9a-fA-F]+/)) return 'number'
  64. if (stream.match(/^[+-]?\d*\.\d+([EeDd][+-]?\d+)?/)) return 'number'
  65. if (stream.match(/^[+-]?\d+([EeDd][+-]?\d+)?/)) return 'number'
  66. }
  67. // Handle Strings
  68. if (stream.match(/^"([^"]|(""))*"/)) {
  69. return 'string'
  70. }
  71. if (stream.match(/^'([^']|(''))*'/)) {
  72. return 'string'
  73. }
  74. // Handle words
  75. if (stream.match(keywords)) {
  76. return 'keyword'
  77. }
  78. if (stream.match(identifiers)) {
  79. return 'variable'
  80. }
  81. // Handle non-detected items
  82. stream.next()
  83. return null
  84. }
  85. CodeMirror.defineMode('protobuf', function () {
  86. return {
  87. token: tokenBase,
  88. fold: 'brace',
  89. }
  90. })
  91. CodeMirror.defineMIME('text/x-protobuf', 'protobuf')
  92. })