4

所以我最近为一个项目完成了我自己的函数式编程语言,我想为该语言创建一个代码镜像模式,因为它可以转编译为 javascript。我已经为它编写了一个标记器,但我无法理解 codemirror 构建它们的标记器的方式。这是语言的基本结构:

<FUNCTION>(<ARGUMENT 1>, <ARGUMENT 2>....)

nested functions:

<FUNCTION 1>(<FUNCTION 2>(<ARGUEMNT 1>), <ARGUMENT 1>....)

series of functions:

<FUNCTION>(<ARGUMENT 1>, <ARGUMENT 2>...), <FUNCTION>()

strings:

`this is a string`

comments:

;this is a comment;

所以它的唯一形式是函数、参数、逗号、字符串和注释。

到目前为止,这就是我所拥有的模式:

(function(mod) {
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(require("../../lib/codemirror"));
  else if (typeof define == "function" && define.amd) // AMD
    define(["../../lib/codemirror"], mod);
  else // Plain browser env
    mod(CodeMirror);
})(function(CodeMirror) {
"use strict";

CodeMirror.defineMode("diff", function() {

  var TOKEN_NAMES = {
    '(': 'positive',
    ')': 'negative',
    ',': 'comma',
    ';': 'comment',
    '`': 'strings', 
  };

  return {
    token: function(stream) {
      var tw_pos = stream.string.search(/[\t ]+?$/);

      if (!stream.sol() || tw_pos === 0) {
        stream.skipToEnd();
        return ("error " + (
          TOKEN_NAMES[stream.string.charAt(0)] || '')).replace(/ $/, '');
      }

      var token_name = TOKEN_NAMES[stream.peek()] || stream.skipToEnd();

      if (tw_pos === -1) {
        stream.skipToEnd();
      } else {
        stream.pos = tw_pos;
      }

      return token_name;
    }
  };
});

CodeMirror.defineMIME("text/x-mylang", "mylang");

});

到目前为止它不起作用。如果有人能指出我正确的方向或展示这种语言的模式如何工作,那将有很大帮助。

4

0 回答 0