Debian Codegraph @

codemirror-legacy-modes/mode/brainfuck.js

raw
1 var reserve = "><+-.,[]".split("");
2 /*
3 comments can be either:
4 placed behind lines
5
6 +++ this is a comment
7
8 where reserved characters cannot be used
9 or in a loop
10 [
11 this is ok to use [ ] and stuff
12 ]
13 or preceded by #
14 */
15 export const brainfuck = {
16 name: "brainfuck",
17 startState: function() {
18 return {
19 commentLine: false,
20 left: 0,
21 right: 0,
22 commentLoop: false
23 }
24 },
25 token: function(stream, state) {
26 if (stream.eatSpace()) return null
27 if(stream.sol()){
28 state.commentLine = false;
29 }
30 var ch = stream.next().toString();
31 if(reserve.indexOf(ch) !== -1){
32 if(state.commentLine === true){
33 if(stream.eol()){
34 state.commentLine = false;
35 }
36 return "comment";
37 }
38 if(ch === "]" || ch === "["){
39 if(ch === "["){
40 state.left++;
41 }
42 else{
43 state.right++;
44 }
45 return "bracket";
46 }
47 else if(ch === "+" || ch === "-"){
48 return "keyword";
49 }
50 else if(ch === "<" || ch === ">"){
51 return "atom";
52 }
53 else if(ch === "." || ch === ","){
54 return "def";
55 }
56 }
57 else{
58 state.commentLine = true;
59 if(stream.eol()){
60 state.commentLine = false;
61 }
62 return "comment";
63 }
64 if(stream.eol()){
65 state.commentLine = false;
66 }
67 }
68 };