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.

115 lines
3.5KB

  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") // 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. if (stream)
  31. //check for state changes
  32. if (!state.inString && !state.inComment && ((stream.peek() == '"') || (stream.peek() == "'"))) {
  33. state.stringType = stream.peek();
  34. stream.next(); // Skip quote
  35. state.inString = true; // Update state
  36. }
  37. if (!state.inString && !state.inComment && stream.match('/*')) {
  38. state.inComment = true;
  39. }
  40. //return state
  41. if (state.inString) {
  42. while (state.inString && !stream.eol()) {
  43. if (stream.peek() === state.stringType) {
  44. stream.next(); // Skip quote
  45. state.inString = false; // Clear flag
  46. } else if (stream.peek() === '\\') {
  47. stream.next();
  48. stream.next();
  49. } else {
  50. stream.match(/^.[^\\\"\']*/);
  51. }
  52. }
  53. return state.lhs ? "property string" : "string"; // Token style
  54. } else if (state.inComment) {
  55. while (state.inComment && !stream.eol()) {
  56. if (stream.match('*/')) {
  57. state.inComment = false; // Clear flag
  58. } else {
  59. stream.match(/^.[^\*]*/);
  60. }
  61. }
  62. return "comment";
  63. } else if (state.inCharacterClass) {
  64. while (state.inCharacterClass && !stream.eol()) {
  65. if (!(stream.match(/^[^\]\\]+/) || stream.match(/^\\./))) {
  66. state.inCharacterClass = false;
  67. }
  68. }
  69. } else if (stream.peek() === '[') {
  70. stream.next();
  71. state.inCharacterClass = true;
  72. return 'bracket';
  73. } else if (stream.match('//')) {
  74. stream.skipToEnd();
  75. return "comment";
  76. } else if (state.braced || stream.peek() === '{') {
  77. if (state.localState === null) {
  78. state.localState = CodeMirror.startState(jsMode);
  79. }
  80. var token = jsMode.token(stream, state.localState);
  81. var text = stream.current();
  82. if (!token) {
  83. for (var i = 0; i < text.length; i++) {
  84. if (text[i] === '{') {
  85. state.braced++;
  86. } else if (text[i] === '}') {
  87. state.braced--;
  88. }
  89. };
  90. }
  91. return token;
  92. } else if (identifier(stream)) {
  93. if (stream.peek() === ':') {
  94. return 'variable';
  95. }
  96. return 'variable-2';
  97. } else if (['[', ']', '(', ')'].indexOf(stream.peek()) != -1) {
  98. stream.next();
  99. return 'bracket';
  100. } else if (!stream.eatSpace()) {
  101. stream.next();
  102. }
  103. return null;
  104. }
  105. };
  106. }, "javascript");
  107. });