fix prolog mode

This commit is contained in:
Vitor Santos Costa 2018-12-10 22:55:52 +00:00
parent 18e437f038
commit 13a6cc8ece

View File

@ -2,14 +2,14 @@
// Distributed under an MIT license: http://codemirror.net/LICENSE // Distributed under an MIT license: http://codemirror.net/LICENSE
(function(mod) { (function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require([ "codemirror/lib/codemirror", "codemirror/addon/lint/lint" ])); mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD else if (typeof define == "function" && define.amd) // AMD
define([ "codemirror/lib/codemirror", "codemirror/addon/lint/lint" ], mod); define(["../../lib/codemirror"], mod);
else // Plain browser env else // Plain browser env
mod(CodeMirror); mod(CodeMirror);
})(function(CodeMirror) { })(function(CodeMirror) {
"use strict"; "use strict";
CodeMirror.defineMode("prolog", function(conf, parserConfig) { CodeMirror.defineMode("prolog", function(conf, parserConfig) {
function chain(stream, state, f) { function chain(stream, state, f) {
@ -17,7 +17,8 @@ CodeMirror.defineMode("prolog", function(conf, parserConfig) {
return f(stream, state); return f(stream, state);
} }
var cm_; var cm_ = null;
var document = CodeMirror.doc;
var curLine; var curLine;
/******************************* /*******************************
@ -72,21 +73,20 @@ parserConfig.backQuote === "chars")
var exportedMsgs = []; var exportedMsgs = [];
function getLine(stream) { function getLine(stream) {
if (stream)
return stream.lineOracle.line; return stream.lineOracle.line;
// return cm_.getDoc().getCursor().line; if (document == null)
return 0;
return document.getCursor().line;
} }
// var ed = // var ed =
// window.document.getElementsByClassName("CodeMirror")[0].CodeMirror.doc.getEditor(); // window.document.getElementsByClassName("CodeMirror")[0].CodeMirror.doc.getEditor();
function rmError(stream) { function rmError(document,stream) {
if (cm_ == null)
return;
var doc = cm_.getDoc();
var l = getLine(stream); var l = getLine(stream);
// stream.lineOracle.line;
for (var i = 0; i < errorFound.length; i++) { for (var i = 0; i < errorFound.length; i++) {
var elLine = doc.getLineNumber(errorFound[i].line); var elLine = errorFound[i].document.getLineNumber(errorFound[i].line);
if (elLine == null || l === elLine) { if (elLine == null || l === elLine) {
errorFound.splice(i, 1); errorFound.splice(i, 1);
i -= 1; i -= 1;
@ -97,7 +97,7 @@ parserConfig.backQuote === "chars")
function mkError(stream, severity, msg) { function mkError(stream, severity, msg) {
if (stream.pos == 0) if (stream.pos == 0)
return; return;
var l = cm_.getDoc().getLineHandle(getLine(stream)); var l = getLine(stream);
var found = errorFound.find(function( var found = errorFound.find(function(
element) { return element.line === l && element.to == stream.pos; }); element) { return element.line === l && element.to == stream.pos; });
if (!found) { if (!found) {
@ -107,20 +107,19 @@ parserConfig.backQuote === "chars")
"from" : stream.start, "from" : stream.start,
"to" : stream.pos, "to" : stream.pos,
severity : severity, severity : severity,
message : msg message : msg,
document: document
}); });
} }
} }
function exportErrors(text) { function exportErrors(text) {
if (cm_ == null) if (document == null)
return; return;
var doc = cm_.getDoc();
exportedMsgs.length = 0; exportedMsgs.length = 0;
for (var i = 0; i < errorFound.length; i += 1) { for (var i = 0; i < errorFound.length; i += 1) {
var e = errorFound[i]; var e = errorFound[i];
var l = doc.getLineNumber(e.line); var l = document.getLineNumber(e.line);
if (l == null) { if (l == null) {
errorFound.splice(i, 1); errorFound.splice(i, 1);
i -= 1; i -= 1;
@ -324,6 +323,7 @@ parserConfig.backQuote === "chars")
if (ch == "{" && state.lastType == "tag") { if (ch == "{" && state.lastType == "tag") {
state.nesting.push({ state.nesting.push({
marker: ch,
tag : state.tagName, tag : state.tagName,
column : stream.column(), column : stream.column(),
leftCol : state.tagColumn, leftCol : state.tagColumn,
@ -334,8 +334,12 @@ parserConfig.backQuote === "chars")
return ret("dict_open", "bracket"); return ret("dict_open", "bracket");
} }
if (ch == "/" && stream.eat("*")) if (ch == "/") {
return chain(stream, state, plTokenComment); var next = stream.peek();
if (next == '*') {
return chain(stream, state, plTokenComment);
}
}
if (ch == "%") { if (ch == "%") {
stream.skipToEnd(); stream.skipToEnd();
@ -347,14 +351,22 @@ parserConfig.backQuote === "chars")
if (isSoloChar.test(ch)) { if (isSoloChar.test(ch)) {
switch (ch) { switch (ch) {
case ")": { case ")": {
if (state.nesting.marker != "(") {
mkError(stream, "error", state.nesting.marker + " closed by )");
}
state.nesting.pop(); state.nesting.pop();
} break; } break;
case "]": case "]":
if (state.nesting.marker != "[") {
mkError(stream, "error", state.nesting.marker + " closed by ]");
}
state.nesting.pop(); state.nesting.pop();
return ret("list_close", "bracket"); return ret("list_close", "bracket");
case "}": { case "}": {
var nest = nesting(state); if (state.nesting.marker != "{") {
mkError(stream, "error", state.nesting.marker + " closed by }");
}
var nest = nesting(state);
var type = (nest && nest.tag) ? "dict_close" : "brace_term_close"; var type = (nest && nest.tag) ? "dict_close" : "brace_term_close";
state.nesting.pop(); state.nesting.pop();
@ -490,6 +502,7 @@ parserConfig.backQuote === "chars")
state.goalStart = true; state.goalStart = true;
outputSingletonVars(stream); outputSingletonVars(stream);
stream.eat(ch); stream.eat(ch);
state.headStart = true;
return ret("fullstop", "def", atom); return ret("fullstop", "def", atom);
} else { } else {
@ -502,7 +515,7 @@ parserConfig.backQuote === "chars")
} else if (isNeck.test(atom)) { } else if (isNeck.test(atom)) {
state.inBody = true; state.inBody = true;
state.goalStart = true; state.goalStart = true;
return ret("neck", "property", atom); return ret("neck", "def", atom);
} else if (isControl(state) && isControlOp.test(atom)) { } else if (isControl(state) && isControlOp.test(atom)) {
state.goalStart = true; state.goalStart = true;
return ret("symbol", "meta", atom); return ret("symbol", "meta", atom);
@ -510,7 +523,7 @@ parserConfig.backQuote === "chars")
return ret("symbol", "meta", atom); return ret("symbol", "meta", atom);
} }
} }
stream.eatWhile(/[\w_]/); stream.eatWhile(/\w/);
if (composeGoalWithDots) { if (composeGoalWithDots) {
while (stream.peek() == ".") { while (stream.peek() == ".") {
stream.eat('.'); stream.eat('.');
@ -519,8 +532,8 @@ parserConfig.backQuote === "chars")
stream.backUp(1); stream.backUp(1);
break; break;
} else if (/[\w_]/.test(ch)) { } else if (/\w/.test(ch)) {
stream.eatWhile(/[\w_]/); stream.eatWhile(/\w/);
} else if (ch == "'") { } else if (ch == "'") {
stream.eat(); stream.eat();
@ -550,17 +563,20 @@ parserConfig.backQuote === "chars")
maybeSingleton(stream, word); maybeSingleton(stream, word);
return ret("var", "variable-1", word); return ret("var", "variable-1", word);
} }
if (stream.peek() == "(") { if (state.headStart) {
state.functorName = word; /* tmp state extension */
state.functorColumn = stream.column();
if (state.headStart) {
state.headStart = false; state.headStart = false;
if (state.headFunctor != word) { if (state.headFunctor !== word) {
state.headFunctor = word; state.headFunctor = word;
return ret("functor", "def", word); return ret("functor", "def", word);
} }
return ret("functor", "atom", word);
} }
if (builtins[word] && isControl(state))
if (stream.peek() == "(") {
state.functorName = word; /* tmp state extension */
state.functorColumn = stream.column();
if (builtins[word] && isControl(state))
return ret("functor", "keyword", word); return ret("functor", "keyword", word);
return ret("functor", "atom", word); return ret("functor", "atom", word);
} else if ((extra = stream.eatSpace())) { } else if ((extra = stream.eatSpace())) {
@ -732,8 +748,8 @@ IfTrue
CodeMirror.defineOption( CodeMirror.defineOption(
"prologKeys", true, function(cm, editor, prev) { "prologKeys", true, function(cm, editor, prev) {
cm_ = cm; document = cm.getDoc();
if (prev && prev != CodeMirror.Init) if (nnprev && prev != CodeMirror.Init)
cm.removeKeyMap("prolog"); cm.removeKeyMap("prolog");
if (true) { if (true) {
var map = { var map = {
@ -1402,8 +1418,6 @@ IfTrue
setArgAlignment(state); setArgAlignment(state);
return null; return null;
} }
if (state.curLine == null || state.pos == 0)
rmError(stream);
var style = state.tokenize(stream, state); var style = state.tokenize(stream, state);
//console.log(state.curToken); //console.log(state.curToken);