This commit is contained in:
Vitor Santos Costa 2019-03-19 20:51:11 +00:00
parent 044d455597
commit e626847e93

View File

@ -3,9 +3,9 @@
(function(mod) { (function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror")); mod(require(["codemirror/lib/codemirror","codemirror/addon/lint/lint"]));
else if (typeof define == "function" && define.amd) // AMD else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod); define([ "codemirror/lib/codemirror","codemirror/addon/lint/lint" ], mod);
else // Plain browser env else // Plain browser env
mod(CodeMirror); mod(CodeMirror);
})(function(CodeMirror) { })(function(CodeMirror) {
@ -17,8 +17,7 @@ CodeMirror.defineMode("prolog", function(conf, parserConfig) {
return f(stream, state); return f(stream, state);
} }
var cm_ = null; var cm_;
var document = CodeMirror.doc;
var curLine; var curLine;
/******************************* /*******************************
@ -35,25 +34,9 @@ var document = CodeMirror.doc;
parserConfig.groupedIntegers || false; /* tag{k:v, ...} */ parserConfig.groupedIntegers || false; /* tag{k:v, ...} */
var unicodeEscape = var unicodeEscape =
parserConfig.unicodeEscape || true; /* \uXXXX and \UXXXXXXXX */ parserConfig.unicodeEscape || true; /* \uXXXX and \UXXXXXXXX */
var multiLineQuoted = parserConfig.multiLineQuotedd || true; var multiLineQuoted = parserConfig.multiLineQuoted || true; /* "...\n..." */
var singleQuoted = "atom"; var quoteType = parserConfig.quoteType ||
if (parserConfig.singleQuote === "string" || {'"' : "string", "'" : "qatom", "`" : "bqstring"};
parserConfig.singleQuote === "codes" ||
parserConfig.singleQuote === "chars")
singleQuoted = parserConfig.singleQuote;
var doubleQuoted = "string";
if (parserConfig.doubleQuote === "atom" ||
parserConfig.doubleQuote === "codes" ||
parserConfig.doubleQuote === "chars")
doubleQuoted = parserConfig.doubleQuote;
var backQuoted = "atom";
if (parserConfig.backQuote === "string" ||
parserConfig.backQuote === "codes" ||
parserConfig.backQuote === "chars")
backQuoted = parserConfig.backQuote;
var quoteType = {"\"" : doubleQuoted, "`" : backQuoted, "'" : singleQuoted};
var singletonVars = new Map(); var singletonVars = new Map();
var isSingleEscChar = /[abref\\'"nrtsv]/; var isSingleEscChar = /[abref\\'"nrtsv]/;
@ -73,20 +56,21 @@ parserConfig.backQuote === "chars")
var exportedMsgs = []; var exportedMsgs = [];
function getLine(stream) { function getLine(stream) {
if (stream)
return stream.lineOracle.line; return stream.lineOracle.line;
if (document == null) // return cm_.getDoc().getCursor().line;
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(document,stream) { function rmError(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 = errorFound[i].document.getLineNumber(errorFound[i].line); var elLine = doc.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,29 +81,30 @@ if (stream)
function mkError(stream, severity, msg) { function mkError(stream, severity, msg) {
if (stream.pos == 0) if (stream.pos == 0)
return; return;
var l = getLine(stream); var l = cm_.getDoc().getLineHandle(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) {
//console.log(getLine(stream)); console.log( getLine(stream) );
errorFound.push({ errorFound.push({
"line" : l, "line" : l,
"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 (document == null) if (cm_ == 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 = document.getLineNumber(e.line); var l = doc.getLineNumber(e.line);
if (l == null) { if (l == null) {
errorFound.splice(i, 1); errorFound.splice(i, 1);
i -= 1; i -= 1;
@ -136,27 +121,28 @@ document: document
} }
function maybeSingleton( stream, key ) { function maybeSingleton( stream, key ) {
//console.log(key); console.log(key);
var v = singletonVars.get(key); var v = singletonVars.get(key);
if (v!= undefined) { if (v!= undefined) {
v.singleton = false; v.singleton = false;
} else { } else {
singletonVars.set( singletonVars.set(key, { 'singleton': true,
key, {'singleton' : true, 'from' : stream.start, to : stream.pos}); 'from': stream.start, to: stream.pos } );
} }
//console.log(singletonVars); console.log(singletonVars);
} }
function outputSingletonVars(stream) { function outputSingletonVars(stream) {
var key,v; var key,v;
for (var key in singletonVars.keys()) { for ( [key,v] of singletonVars.entries()) {
var v = singletonVars[key];
if (v!=undefined && v.singleton) { if (v!=undefined && v.singleton) {
mkError(stream,"warning", key+" singleton variable"); mkError(stream,"warning", key+" singleton variable");
} }
} }
singletonVars.clear(); singletonVars.clear();
// console.log("reset"); console.log("reset");
} }
CodeMirror.registerHelper("lint", "prolog", exportErrors); CodeMirror.registerHelper("lint", "prolog", exportErrors);
@ -323,7 +309,6 @@ document: document
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,12 +319,8 @@ document: document
return ret("dict_open", "bracket"); return ret("dict_open", "bracket");
} }
if (ch == "/") { if (ch == "/" && stream.eat("*"))
var next = stream.peek();
if (next == '*') {
return chain(stream, state, plTokenComment); return chain(stream, state, plTokenComment);
}
}
if (ch == "%") { if (ch == "%") {
stream.skipToEnd(); stream.skipToEnd();
@ -351,28 +332,21 @@ if (next == '*') {
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 "}": {
if (state.nesting.marker != "{") {
mkError(stream, "error", state.nesting.marker + " closed by }");
}
var nest = nesting(state); 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();
return ret(type, null); return ret(type, null);
} break; } break;
case ",": { case ",":
{
if (stream.eol()) if (stream.eol())
state.commaAtEOL = true; state.commaAtEOL = true;
nextArg(state); nextArg(state);
@ -496,13 +470,11 @@ if (state.nesting.marker != "[") {
state.nesting = []; state.nesting = [];
} }
// var start = cm_.getCursor("end"); // var start = cm_.getCursor("end");
// cm_.setBookmark(start, {"widget" : //cm_.setBookmark(start, {"widget" : document.createTextNode("&bull;")});
// document.createTextNode("&bull;")});
state.inBody = false; state.inBody = false;
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 {
@ -515,7 +487,7 @@ state.headStart = true;
} 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", "def", atom); return ret("neck", "property", 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);
@ -523,7 +495,7 @@ state.headStart = true;
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('.');
@ -532,8 +504,8 @@ state.headStart = true;
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();
@ -563,19 +535,16 @@ state.headStart = true;
maybeSingleton(stream,word); maybeSingleton(stream,word);
return ret("var", "variable-1", word); return ret("var", "variable-1", word);
} }
if (state.headStart) {
state.headStart = false;
if (state.headFunctor !== word) {
state.headFunctor = word;
return ret("functor", "def", word);
}
return ret("functor", "atom", word);
}
if (stream.peek() == "(") { if (stream.peek() == "(") {
state.functorName = word; /* tmp state extension */ state.functorName = word; /* tmp state extension */
state.functorColumn = stream.column(); state.functorColumn = stream.column();
if (state.headStart) {
state.headStart = false;
if (state.headFunctor != word) {
state.headFunctor = word;
return ret("functor", "def", word);
}
}
if (builtins[word] && isControl(state)) 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);
@ -604,6 +573,7 @@ return ret("functor", "atom", word);
return ret("atom", "keyword", word); return ret("atom", "keyword", word);
} }
return ret("atom", "atom", word); return ret("atom", "atom", word);
} }
function plTokenString(quote) { function plTokenString(quote) {
@ -748,7 +718,7 @@ IfTrue
CodeMirror.defineOption( CodeMirror.defineOption(
"prologKeys", true, function(cm, editor, prev) { "prologKeys", true, function(cm, editor, prev) {
document = cm.getDoc(); cm_ = cm;
if (prev && prev != CodeMirror.Init) if (prev && prev != CodeMirror.Init)
cm.removeKeyMap("prolog"); cm.removeKeyMap("prolog");
if (true) { if (true) {
@ -1418,9 +1388,11 @@ 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);
if (stream.eol()) { if (stream.eol()) {
if (stream.pos > 0) if (stream.pos > 0)