Advertisement
Guest User

pbrain.js

a guest
Jan 14th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3.  
  4. // Brainfuck mode created by Michael Kaminsky https://github.com/mkaminsky11
  5.  
  6. (function(mod) {
  7.   if (typeof exports == "object" && typeof module == "object")
  8.     mod(require("../../lib/codemirror"))
  9.   else if (typeof define == "function" && define.amd)
  10.     define(["../../lib/codemirror"], mod)
  11.   else
  12.     mod(CodeMirror)
  13. })(function(CodeMirror) {
  14.   "use strict"
  15.   var reserve = "><+-.,[]():".split("");
  16.   /*
  17.   comments can be either:
  18.   placed behind lines
  19.  
  20.         +++    this is a comment
  21.  
  22.   where reserved characters cannot be used
  23.   or in a loop
  24.   [
  25.     this is ok to use [ ] and stuff
  26.   ]
  27.   or preceded by #
  28.   */
  29.   CodeMirror.defineMode("brainfuck", function() {
  30.     return {
  31.       startState: function() {
  32.         return {
  33.           commentLine: false,
  34.           left: 0,
  35.           right: 0,
  36.           commentLoop: false
  37.         }
  38.       },
  39.       token: function(stream, state) {
  40.         if (stream.eatSpace()) return null
  41.         if(stream.sol()){
  42.           state.commentLine = false;
  43.         }
  44.         var ch = stream.next().toString();
  45.         if(reserve.indexOf(ch) !== -1){
  46.           if(state.commentLine === true){
  47.             if(stream.eol()){
  48.               state.commentLine = false;
  49.             }
  50.             return "comment";
  51.           }
  52.           if(ch === "]" || ch === "["){
  53.             if(ch === "["){
  54.               state.left++;
  55.             }
  56.             else{
  57.               state.right++;
  58.             }
  59.             return "bracket";
  60.           }
  61.           else if(ch === "+" || ch === "-"){
  62.             return "keyword";
  63.           }
  64.           else if(ch === "<" || ch === ">"){
  65.             return "atom";
  66.           }
  67.           else if(ch === "." || ch === ","){
  68.             return "def";
  69.           }
  70.           else if(ch === "(" || ch === ")" || ch === ":") {
  71.             return "builtin";
  72.           }
  73.         }
  74.         else{
  75.           state.commentLine = true;
  76.           if(stream.eol()){
  77.             state.commentLine = false;
  78.           }
  79.           return "comment";
  80.         }
  81.         if(stream.eol()){
  82.           state.commentLine = false;
  83.         }
  84.       }
  85.     };
  86.   });
  87. CodeMirror.defineMIME("text/x-pbrain","pbrain")
  88. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement