X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/idiomatic.git/blobdiff_plain/682a7215ae646bc71baf0be528b9c7d55b1cb445..291e0dae80776eed417ab084896e7fa2390d5387:/index.js?ds=sidebyside diff --git a/index.js b/index.js index 18f87de..9f1a2b7 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,11 @@ const express = require('express') const acorn = require('acorn') const fs = require('fs') -// const fs = require('node:fs/promises'); const visitor = require('./visitor.js') + +// P A R S E + const dir = '../wiki-client/lib' const mods = [] fs.readdir(dir, async (err, files) => { @@ -22,77 +24,110 @@ async function load(file) { // P A G E S -const style = '' +const style = (title,here='') => ` + +
${title}  ${escape(here)} 
` const app = express() -app.get('/index', async (req,res,next) => { - console.log(new Date().toLocaleTimeString(), 'index') +app.get('/index', async (req,res) => { const reductions = counter() - const doit = branch => {reductions.count(branch.type)} - visitor.wander(mods,doit) + visitor.walk(mods,branch => + reductions.count(branch.type)) const result = `

${reductions.size()} non-terminals
${reductions.total()} reductions -

${reductions.tally() - .map(([k,v]) => `${v} ${link(k)}`) - .join("
")}` - res.send(style+result); - next() - }) - -function link(key) { - if(key.match(/^Ident/)) return `${key}` - if(key.match(/^(As|B|L|U).*Ex/)) return `${key}` - if(key.match(/^Lit/)) return `${key}` - return key -} - +

${reductions.tally() + .map(([k,v]) => `
${v}${link(k)}`) + .join("\n")}
` + res.send(style('index')+result); + + function link(key) { + if(key.match(/^Ident/)) return `${key}` + if(key.match(/^(As|B|L|U).*Ex/)) return `${key}` + if(key.match(/^Lit/)) return `${key}` + return key + } +}) app.get('/terminal', (req,res) => { const {type,field} = req.query - const lits = counter() - const doit = branch => {if(branch.type==type) lits.count(branch[field])} - visitor.wander(mods,doit) - const result = ` -

${lits.size()} uniques -
${lits.total()} total -

${lits.tally() - .map(([k,v]) => `${v} ${escape(k)}`) - .join("
")}` - res.send(style+result) + const terms = counter() + visitor.walk(mods,branch => { + if(branch.type==type) + terms.count(branch[field])}) + const result = style('terminal',type)+` +

${terms.size()} uniques +
${terms.total()} total +

${terms.tally() + .map(([k,v]) => `
${v}${escape(k)}`) + .join("\n")}
` + res.send(result) }) app.get('/usage', (req,res) => { - const {type,field,key} = req.query + const {type,field,key,width,depth} = req.query const list = [] - const doit = (branch,stack) => { + const files = counter() + visitor.walk(mods,(branch,stack) => { if(branch.type==type && branch[field]==key)list.push(` - + ${stack.at(-1)} - ${sxpr(stack[2],3)}`) - } - visitor.wander(mods,doit) - res.send(style+`

${JSON.stringify(req.query,null,2)}
${list.join("
")}`) + ${sxpr(stack[width ?? 2], depth ?? 3)}`)}) + list.sort((a,b) => vis(a)>vis(b) ? 1 : -1) + const q = (id,delta) => Object.entries(req.query) + .map(([k,v]) => k == id ? `${k}=${+v+delta}` : `${k}=${v}`) + .join('&') + const p = id => ` + ` + const m = id => ` − ` + const d = id => `${id} ${p(id)} ${m(id)}` + res.send(style('usage',key)+` +

${files.total()} uses in ${files.size()} files + ${files.tally().map(([k,v]) => `
${v}${k}`).join("\n")}
+

${d('width')} ${d('depth')}
+

${list.join("\n")}
`) }) app.get('/nesting', (req,res) => { - const {file,type,start,end} = req.query + const {file,type,key,start,end} = req.query const result = [] - const doit = (branch,stack) => { + visitor.walk(mods,(branch,stack) => { if(stack.at(-1)==file && branch.type==type && branch.start==start && branch.end==end) { - const file = stack.at(-1) const path = stack.slice(0,-1).map((n,i) => ` - - ${n.type}: + + ${n.type}: ${sxpr(n,3,null,stack[i-1])}`).reverse() const hit = stack[1] result.push(` - ${path.join("")}


\n -

${escape(JSON.stringify(hit,omit,2))}
`) +

${path.join("")}

+

${escape(JSON.stringify(hit,omit,2))}
`) } - } - visitor.wander(mods,doit) - res.send(style+`
${JSON.stringify(req.query,null,2)}
${result.join("
")}`) + }) + res.send(style('nesting',key)+`${result.join("
")}`) +}) + +app.get('/similar', (req,res) => { + const {file,type,key,start,end,nest} = req.query + let nested + visitor.walk(mods,(branch,stack) => { + if(stack.at(-1)==file && branch.type==type && branch.start==start && branch.end==end) + nested = stack[nest]}) + const norm = node => vis(`\n\n\n${sxpr(node,3,null)}`) + const source = (file,node) => mods.find(mod => mod.file == file).text.substring(+node.start,+node.end) + const want = norm(nested) + const result = [] + visitor.walk(mods,(branch,stack) => { + if(norm(branch) == want) + result.push(`
${escape(source(stack.at(-1),branch))}

`)}) + res.send(style('similar',key)+ + `

${want}


` + + result.join("\n") + ) }) @@ -106,6 +141,7 @@ function counter() { counts.set(item, counts.get(item)+1) else counts.set(item,1) + return item }, size() { return counts.size @@ -165,4 +201,16 @@ function elipsis(obj) { return `(${dots})` } +function vis(row) { + return row.split(/\n/)[3].trim() + .replaceAll(/<.*?>/g,'') + .replaceAll(/\.\.+/g,'..') +} + +function query(obj,adj={}) { + return Object.entries(obj) + .map(([k,v]) => k in adj ? `${k}=${adj[k](v)}` : `${k}=${v}`) + .join('&') +} + app.listen(1954) \ No newline at end of file