all repos — NoPaste @ 3e3c433f026c49e04d0efa0f0f39fc688ed32bb1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/pegjs/pegjs.js (view raw)

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