From b07380063525463d19b54f6cd1a8a538f379771d Mon Sep 17 00:00:00 2001 From: kungfooman Date: Tue, 2 Feb 2021 14:44:18 +0100 Subject: [PATCH 1/4] Better error lines by adding regex for empty/only-spaces lines --- examples/coffeeshop.html | 14 ++++++++++++-- ganja.js | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/coffeeshop.html b/examples/coffeeshop.html index 5b9af83..6d1c5be 100644 --- a/examples/coffeeshop.html +++ b/examples/coffeeshop.html @@ -584,13 +584,23 @@

The CoffeeShop

var row = e.lineno; // e.lineno is one off in same cases... fixing that, otherwise it looks strange // Chrome - if (e.message.includes("Unexpected identifier")) { + if ( + e.message.includes("Unexpected identifier") || + e.message.includes("Uncaught SyntaxError:") + ) { row--; } // Mozilla - if (e.message.includes("unexpected token: identifier")) { + if ( + e.message.includes("unexpected token: identifier") || + e.message.includes("SyntaxError: unexpected token: '") || + e.message.includes("SyntaxError: invalid arrow-function arguments") // var D = asd ()=>L ^ M; + ) { row--; } + if (e.message.includes("ReferenceError:")) { + row++; + } editor.getSession().setAnnotations([{ row, column: e.colno, diff --git a/ganja.js b/ganja.js index 013ca7d..93d850d 100644 --- a/ganja.js +++ b/ganja.js @@ -1535,7 +1535,8 @@ /^\d+[.]{0,1}\d*[ei][\+\-_]{0,1}\d*|^\.\d+[ei][\+\-_]{0,1}\d*|^e_\d*/g, // 2: literal numbers in scientific notation (with small hack for i and e_ asciimath) /^\d+[.]{0,1}\d*[E][+-]{0,1}\d*|^\.\d+[E][+-]{0,1}\d*|^0x\d+|^\d+[.]{0,1}\d*|^\.\d+|^\(\/.*[^\\]\/\)/g, // 3: literal hex, nonsci numbers and regex (surround regex with extra brackets!) /^(\.Normalized|\.Length|\.\.\.|>>>=|===|!==|>>>|<<=|>>=|=>|\|\||[<>\+\-\*%&|^\/!\=]=|\*\*|\+\+|\-\-|<<|>>|\&\&|\^\^|^[{}()\[\];.,<>\+\-\*%|&^!~?:=\/]{1})/g, // 4: punctuator - /^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*/gu] // 5: identifier + /^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*/gu, // 5: identifier + /\s+/g] // 6: empty lines while (txt.length) for(t in tokens) if(resi=txt.match(tokens[t])){ tok.push([t|0,resi[0]]); txt=txt.slice(resi[0].length); break;} // tokenise // Translate algebraic literals. (scientific e-notation to "this.Coeff" tok=tok.map(t=>(t[0]==2)?[2,'Element.Coeff('+basis.indexOf((!options.Cayley?simplify:(x)=>x)('e'+t[1].split(/e_|e|i/)[1]||1).replace('-',''))+','+(simplify(t[1].split(/e_|e|i/)[1]||1).match('-')?"-1*":"")+parseFloat(t[1][0]=='e'?1:t[1].split(/e_|e|i/)[0])+')']:t); From c06d7ec6b1ee8fad8d036cbbf84da97eccab724d Mon Sep 17 00:00:00 2001 From: kungfooman Date: Tue, 9 Feb 2021 17:11:32 +0100 Subject: [PATCH 2/4] Line detection offsetted from where Algebra starts --- examples/coffeeshop.html | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/coffeeshop.html b/examples/coffeeshop.html index 6d1c5be..1a2f12a 100644 --- a/examples/coffeeshop.html +++ b/examples/coffeeshop.html @@ -578,10 +578,27 @@

The CoffeeShop

els.sample.contentDocument.body.style = "margin:0; padding:0;"; postRun(); els.sample.contentWindow.addEventListener('error', function(e) { + //console.log(e); + var rowOffset = 0; + if (e.filename == "") { + // Figure out line where `Algebra(..., ()=>{...}` starts in ACE Editor (because error is offsetted from there) + var lines = editor.getValue().split("\n"); + rowOffset = -1; + for (var line of lines) { + if (line.match(/Algebra\s?\(/)) { + break; + } + rowOffset++; + } + if (rowOffset == -1) { + rowOffset = 0; + } + } // show error in HTML els.sample.contentDocument.body.innerHTML+=`
${e.message}
`; // show error in ace editor - var row = e.lineno; + var row = rowOffset + e.lineno; + //console.log("rowOffset", rowOffset, "e.lineno", e.lineno) // e.lineno is one off in same cases... fixing that, otherwise it looks strange // Chrome if ( @@ -598,9 +615,6 @@

The CoffeeShop

) { row--; } - if (e.message.includes("ReferenceError:")) { - row++; - } editor.getSession().setAnnotations([{ row, column: e.colno, From eba797b33f821a09559ff4c0c90d54e130ada19e Mon Sep 17 00:00:00 2001 From: kungfooman Date: Wed, 10 Feb 2021 15:07:51 +0100 Subject: [PATCH 3/4] Improve Algebra regex Remove console.log's Remove Chrome/Firefox specific error line "fixings" --- examples/coffeeshop.html | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/examples/coffeeshop.html b/examples/coffeeshop.html index 1a2f12a..872c8bd 100644 --- a/examples/coffeeshop.html +++ b/examples/coffeeshop.html @@ -578,43 +578,22 @@

The CoffeeShop

els.sample.contentDocument.body.style = "margin:0; padding:0;"; postRun(); els.sample.contentWindow.addEventListener('error', function(e) { - //console.log(e); var rowOffset = 0; if (e.filename == "") { // Figure out line where `Algebra(..., ()=>{...}` starts in ACE Editor (because error is offsetted from there) var lines = editor.getValue().split("\n"); - rowOffset = -1; + rowOffset = 0; for (var line of lines) { - if (line.match(/Algebra\s?\(/)) { + if (line.match(/\bAlgebra\s*\(/)) { break; } rowOffset++; } - if (rowOffset == -1) { - rowOffset = 0; - } } // show error in HTML els.sample.contentDocument.body.innerHTML+=`
${e.message}
`; // show error in ace editor - var row = rowOffset + e.lineno; - //console.log("rowOffset", rowOffset, "e.lineno", e.lineno) - // e.lineno is one off in same cases... fixing that, otherwise it looks strange - // Chrome - if ( - e.message.includes("Unexpected identifier") || - e.message.includes("Uncaught SyntaxError:") - ) { - row--; - } - // Mozilla - if ( - e.message.includes("unexpected token: identifier") || - e.message.includes("SyntaxError: unexpected token: '") || - e.message.includes("SyntaxError: invalid arrow-function arguments") // var D = asd ()=>L ^ M; - ) { - row--; - } + var row = rowOffset + e.lineno - 1; editor.getSession().setAnnotations([{ row, column: e.colno, From b127dc046d6ea257d82d76af6f48e4e6669ed335 Mon Sep 17 00:00:00 2001 From: kungfooman Date: Wed, 10 Feb 2021 15:14:38 +0100 Subject: [PATCH 4/4] Remove superfluous line: rowOffset = 0 in if(...) --- examples/coffeeshop.html | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/coffeeshop.html b/examples/coffeeshop.html index 872c8bd..d8ac2ba 100644 --- a/examples/coffeeshop.html +++ b/examples/coffeeshop.html @@ -582,7 +582,6 @@

The CoffeeShop

if (e.filename == "") { // Figure out line where `Algebra(..., ()=>{...}` starts in ACE Editor (because error is offsetted from there) var lines = editor.getValue().split("\n"); - rowOffset = 0; for (var line of lines) { if (line.match(/\bAlgebra\s*\(/)) { break;