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.

85 lines
2.1 KiB

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