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.

111 lines
3.4 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../javascript/javascript"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../javascript/javascript"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("pegjs", function (config) {
  13. var jsMode = CodeMirror.getMode(config, "javascript");
  14. function identifier(stream) {
  15. return stream.match(/^[a-zA-Z_][a-zA-Z0-9_]*/);
  16. }
  17. return {
  18. startState: function () {
  19. return {
  20. inString: false,
  21. stringType: null,
  22. inComment: false,
  23. inCharacterClass: false,
  24. braced: 0,
  25. lhs: true,
  26. localState: null
  27. };
  28. },
  29. token: function (stream, state) {
  30. //check for state changes
  31. if (!state.inString && !state.inComment && ((stream.peek() == '"') || (stream.peek() == "'"))) {
  32. state.stringType = stream.peek();
  33. stream.next(); // Skip quote
  34. state.inString = true; // Update state
  35. }
  36. if (!state.inString && !state.inComment && stream.match('/*')) {
  37. state.inComment = true;
  38. }
  39. if (state.inString) {
  40. while (state.inString && !stream.eol()) {
  41. if (stream.peek() === state.stringType) {
  42. stream.next(); // Skip quote
  43. state.inString = false; // Clear flag
  44. } else if (stream.peek() === '\\') {
  45. stream.next();
  46. stream.next();
  47. } else {
  48. stream.match(/^.[^\\\"\']*/);
  49. }
  50. }
  51. return state.lhs ? "property string" : "string"; // Token style
  52. } else if (state.inComment) {
  53. while (state.inComment && !stream.eol()) {
  54. if (stream.match('*/')) {
  55. state.inComment = false; // Clear flag
  56. } else {
  57. stream.match(/^.[^\*]*/);
  58. }
  59. }
  60. return "comment";
  61. } else if (state.inCharacterClass) {
  62. while (state.inCharacterClass && !stream.eol()) {
  63. if (!(stream.match(/^[^\]\\]+/) || stream.match(/^\\./))) {
  64. state.inCharacterClass = false;
  65. }
  66. }
  67. } else if (stream.peek() === '[') {
  68. stream.next();
  69. state.inCharacterClass = true;
  70. return 'bracket';
  71. } else if (stream.match('//')) {
  72. stream.skipToEnd();
  73. return "comment";
  74. } else if (state.braced || stream.peek() === '{') {
  75. if (state.localState === null) {
  76. state.localState = CodeMirror.startState(jsMode);
  77. }
  78. var token = jsMode.token(stream, state.localState);
  79. var text = stream.current();
  80. if (!token) {
  81. for (var i = 0; i < text.length; i++) {
  82. if (text[i] === '{') {
  83. state.braced++;
  84. } else if (text[i] === '}') {
  85. state.braced--;
  86. }
  87. };
  88. }
  89. return token;
  90. } else if (identifier(stream)) {
  91. if (stream.peek() === ':') {
  92. return 'variable';
  93. }
  94. return 'variable-2';
  95. } else if (['[', ']', '(', ')'].indexOf(stream.peek()) != -1) {
  96. stream.next();
  97. return 'bracket';
  98. } else if (!stream.eatSpace()) {
  99. stream.next();
  100. }
  101. return null;
  102. }
  103. };
  104. }, "javascript");
  105. });