scripts/CodeMirror/mode/brainfuck/brainfuck.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://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 }
71 else{
72 state.commentLine = true;
73 if(stream.eol()){
74 state.commentLine = false;
75 }
76 return "comment";
77 }
78 if(stream.eol()){
79 state.commentLine = false;
80 }
81 }
82 };
83 });
84CodeMirror.defineMIME("text/x-brainfuck","brainfuck")
85});