From 775625d162faab5f628872a0ca625f671a80d700 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 12 Jan 2021 15:04:47 -0500 Subject: Move error formatting code from repl.js to bqn.js --- docs/bqn.js | 23 ++++++++++++++++++++++- docs/repl.js | 24 ++---------------------- 2 files changed, 24 insertions(+), 23 deletions(-) (limited to 'docs') diff --git a/docs/bqn.js b/docs/bqn.js index db3085c0..cbb2802a 100644 --- a/docs/bqn.js +++ b/docs/bqn.js @@ -233,7 +233,28 @@ let fmt1 = run( )(list([type, decompose, glyph, fmtnum])); let fmt = x => fmt1(x).map(c=>c===0?" ":c).join(""); +let fmtErr = (s,e) => { + let r=e.src, w=e.message, loc=[]; + while (w&&w.loc||(r!=='!'&&w.sh&&w.sh[0]===2)) { + let is; [is,w]=w; + let n=is.sh?is.sh[0]:0, i=n?is[0]:is; + let pair=n&&is.sh.length>1; if (pair) n*=2; + let to=i=>s.slice(0,i).join('').split('\n').map(l=>Array.from(l)); + let ll=to(i), l=ll.length-1, j=ll[l].length, m=to()[l]; + let k=1,o=i-j,cl=j; while (kt?'^':' ').join('')]; + loc = add.concat(ol?['(and other lines)']:[], loc); + } + if (r==='!') w=w?fmt(w).replace(/^/gm,'! '):'! Error'; + else w=w.sh?w.join(''):w; + return [w].concat(loc).join('\n'); +} + if (typeof module!=='undefined') { - bqn.fmt=fmt; bqn.compile=compile; bqn.run=run; + bqn.fmt=fmt; bqn.fmtErr=fmtErr; bqn.compile=compile; bqn.run=run; module.exports=bqn; } diff --git a/docs/repl.js b/docs/repl.js index 2fc88caa..4fbbda8e 100644 --- a/docs/repl.js +++ b/docs/repl.js @@ -19,27 +19,6 @@ if (doc.count) { } let setExplain = e=>e; -let showErr = (s,e)=>{ - let r=e.src, w=e.message, loc=[]; - while (w&&w.loc||(r!=='!'&&w.sh&&w.sh[0]===2)) { - let is; [is,w]=w; - let n=is.sh?is.sh[0]:0, i=n?is[0]:is; - let pair=n&&is.sh.length>1; if (pair) n*=2; - let to=i=>s.slice(0,i).join('').split('\n').map(l=>Array.from(l)); - let ll=to(i), l=ll.length-1, j=ll[l].length, m=to()[l]; - let k=1,o=i-j,cl=j; while (kt?'^':' ').join('')]; - loc = add.concat(ol?['(and other lines)']:[], loc); - } - if (r==='!') w=w?fmt(w).replace(/^/gm,'! '):'! Error'; - else w=w.sh?w.join(''):w; - doc.rslt.classList.add('err'); - doc.rslt.textContent=[w].concat(loc).join('\n'); -} let repl = ()=>{ let s=Array.from(doc.code.value), src=str(s); doc.rslt.classList.remove('err'); @@ -51,7 +30,8 @@ let repl = ()=>{ setExplain(src,c); doc.rslt.textContent=fmt(run.apply(null,c)); } catch(e) { - showErr(src,e); + doc.rslt.classList.add('err'); + doc.rslt.textContent=fmtErr(src,e); } }, 0); } -- cgit v1.2.3