如何创建新的代码镜像模式?

时间:2016-12-15 17:56:36

标签: javascript tokenize codemirror codemirror-modes

所以我最近为一个项目完成了自己的函数式编程语言,并且我希望在转换为javascript时为该语言创建一个codemirror模式。我已经为它编写了一个tokenizer但我无法理解codemirror构造其tokenizer的方式。这是该语言的基本结构:

<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");

});

到目前为止还没有工作。如果有人能指出我正确的方向,或者说明这种语言的模式如何起作用将会有很大的帮助。

0 个答案:

没有答案
相关问题