]> dreyeck.freedombox.rocks Git - idiomatic.git/blob - index.js
76c9158583a0ebe4c444264dd44b1d39dba6eae3
[idiomatic.git] / index.js
1 const express = require('express')
2 const acorn = require('acorn')
3 const fs = require('fs')
4 // const fs = require('node:fs/promises');
5 const visitor = require('./visitor.js')
6
7 const dir = '../wiki-client/lib'
8 const mods = []
9 fs.readdir(dir, async (err, files) => {
10 mods.push(... await Promise.all(files.map(load)))
11 })
12
13 async function load(file) {
14 return new Promise(resolve => {
15 fs.readFile(`${dir}/${file}`, "utf8", (err,text) => {
16 const tree = acorn.parse(text, {ecmaVersion: "latest"})
17 resolve({file,text,tree})
18 })
19 })
20 }
21
22
23 // P A G E S
24
25 const style = (title,here='') => `
26 <style>
27 body {font-family:sans-serif;}
28 a {text-decoration:none;}
29 td:first-child {text-align:right;}
30 .hi {background-color:pink;}
31 section {letter-spacing:.2rem; font-size:1.2rem;}
32 </style>
33 <section>— ${title} <span style="background-color:#ddd;">&nbsp;${escape(here)}&nbsp;</span> —</section>`
34 const app = express()
35
36 app.get('/index', async (req,res,next) => {
37 console.log(new Date().toLocaleTimeString(), 'index')
38 const reductions = counter()
39 const doit = branch => {reductions.count(branch.type)}
40 visitor.walk(mods,doit)
41 const result = `
42 <p>${reductions.size()} non-terminals
43 <br>${reductions.total()} reductions
44 <p><table>${reductions.tally()
45 .map(([k,v]) => `<tr><td>${v}<td>${link(k)}`)
46 .join("\n")}</table>`
47 res.send(style('index')+result);
48 next()
49 })
50
51 function link(key) {
52 if(key.match(/^Ident/)) return `<a href="/terminal?type=${key}&field=name">${key}</a>`
53 if(key.match(/^(As|B|L|U).*Ex/)) return `<a href="/terminal?type=${key}&field=operator">${key}</a>`
54 if(key.match(/^Lit/)) return `<a href="/terminal?type=${key}&field=value">${key}</a>`
55 return key
56 }
57
58 app.get('/terminal', (req,res) => {
59 const {type,field} = req.query
60 const lits = counter()
61 const doit = branch => {if(branch.type==type) lits.count(branch[field])}
62 visitor.walk(mods,doit)
63 const result = style('terminal',type)+`
64 <p>${lits.size()} uniques
65 <br>${lits.total()} total
66 <p><table>${lits.tally()
67 .map(([k,v]) => `<tr><td>${v}<td><a href="/usage?type=${type}&field=${field}&key=${encodeURIComponent(k)}&width=2&depth=3">${escape(k)}</a>`)
68 .join("\n")}</table>`
69 res.send(result)
70 })
71
72 app.get('/usage', (req,res) => {
73 const {type,field,key,width,depth} = req.query
74 const list = []
75 const files = counter()
76 const doit = (branch,stack) => {
77 if(branch.type==type && branch[field]==key)list.push(`
78 <tr><td><a href="/nesting/?file=${files.count(stack.at(-1))}&type=${type}&key=${key}&start=${branch.start}&end=${branch.end}">
79 ${stack.at(-1)}</a>
80 <td>${sxpr(stack[width ?? 2], depth ?? 3)}`)
81 }
82 visitor.walk(mods,doit)
83 list.sort((a,b) => vis(a)>vis(b) ? 1 : -1)
84 const q = (id,delta) => Object.entries(req.query)
85 .map(([k,v]) => k == id ? `${k}=${+v+delta}` : `${k}=${v}`)
86 .join('&')
87 const p = id => `<a href=/usage?${q(id,+1)} style="background-color:#ddd;">&nbsp;&plus;&nbsp;</a>`
88 const m = id => `<a href=/usage?${q(id,-1)} style="background-color:#ddd;">&nbsp;&minus;&nbsp;</a>`
89 const d = id => `<span title=${req.query[id]}>${id} ${p(id)} ${m(id)}</span>`
90 res.send(style('usage',key)+`
91 <p><details><summary>${files.total()} uses in ${files.size()} files</summary>
92 <table>${files.tally().map(([k,v]) => `<tr><td>${v}<td>${k}`).join("\n")}</table></details>
93 <p><section>— ${d('width')} ${d('depth')} —</section>
94 <p><table>${list.join("\n")}</table>`)
95 })
96
97 app.get('/nesting', (req,res) => {
98 const {file,type,key,start,end} = req.query
99 const result = []
100 const doit = (branch,stack) => {
101 if(stack.at(-1)==file && branch.type==type && branch.start==start && branch.end==end) {
102 const path = stack.slice(0,-1).map((n,i) => `
103 <tr>
104 <td><a title=${file} href=/similar?${query(req.query)}&nest=${i}>${n.type}</a>:
105 <td>${sxpr(n,3,null,stack[i-1])}`).reverse()
106 const hit = stack[1]
107 result.push(`
108 <p><table>${path.join("")}</table><br>
109 <p><pre>${escape(JSON.stringify(hit,omit,2))}</pre>`)
110 }
111 }
112 visitor.walk(mods,doit)
113 res.send(style('nesting',key)+`${result.join("<hr>")}`)
114 })
115
116 app.get('/similar', (req,res) => {
117 const {file,type,key,start,end,nest} = req.query
118 let nested
119 visitor.walk(mods,(branch,stack) => {
120 if(stack.at(-1)==file && branch.type==type && branch.start==start && branch.end==end)
121 nested = stack[nest]
122 })
123 const norm = node => vis(`\n\n\n${sxpr(node,3,null)}`)
124 const source = (file,node) => mods.find(mod => mod.file == file).text.substring(+node.start,+node.end)
125 const want = norm(nested)
126 const result = []
127 visitor.walk(mods,(branch,stack) => {
128 if(norm(branch) == want) result.push(`<pre>${escape(source(stack.at(-1),branch))}</pre><hr>`)
129 })
130 res.send(style('similar',key)+
131 `<p>${want}<hr>` +
132 result.join("\n")
133 )
134 })
135
136
137 // H E L P E R S
138
139 function counter() {
140 const counts = new Map()
141 return {
142 count(item) {
143 if(counts.has(item))
144 counts.set(item, counts.get(item)+1)
145 else
146 counts.set(item,1)
147 return item
148 },
149 size() {
150 return counts.size
151 },
152 total() {
153 return [...counts]
154 .reduce((sum,each) => sum + each[1], 0)
155 },
156 tally() {
157 return [...counts]
158 .sort((a,b) => a[1]==b[1] ? (a[0]>b[0] ? 1 : -1) : b[1]-a[1])
159 },
160 }
161 }
162
163 function escape(text) {
164 try {
165 return text
166 .replace(/&/g, '&amp;')
167 .replace(/</g, '&lt;')
168 .replace(/>/g, '&gt;')
169 .replace(/\*(.+?)\*/g, '<i>$1</i>')
170 } catch (e) {
171 return text
172 }
173 }
174
175 function sxpr(obj,deep,key,child) {
176 const hilite = obj===child ? 'class="hi"' : ''
177 const link = word => obj.type == 'Identifier' ? `<a href=/usage?type=Identifier&field=name&key=${word}>${word}</a>` : word
178 if (obj) {
179 if(deep) {
180 const fields = Object.entries(obj)
181 .filter(([k,v]) => !['start','end','raw','computed','optional','kind'].includes(k))
182 .map(([k,v]) =>
183 k=='type' ? abv(v) :
184 (typeof v == 'string') ? link(escape(v)) :
185 Array.isArray(v) ? `[${v.map(o => sxpr(o,deep-1,k,child)).join(" ")}]` :
186 sxpr(v, deep-1, k, child))
187 .join(" ")
188 return key ? `<span ${hilite} title=${key}>(${(fields)})</span>` : `(${(fields)})`
189 } else return elipsis(obj)
190 } else return `<span title=${obj}>.</span>`
191 }
192
193 function abv(type) {
194 return `<span title=${type}>${type.replaceAll(/[a-z]/g,'')}</span>`
195 }
196
197 function omit(k,v) {
198 return k=='type'?v:k=='start'||k=='end'?undefined:v
199 }
200
201 function elipsis(obj) {
202 const bytes = (obj.end||0)-(obj.start||0)
203 const dots = '..' + '.'.repeat(Math.floor(Math.log2(bytes||1)))
204 return `(<span title="${bytes} bytes">${dots}</span>)`
205 }
206
207 function vis(row) {
208 return row.split(/\n/)[3].trim()
209 .replaceAll(/<.*?>/g,'')
210 .replaceAll(/\.\.+/g,'..')
211 }
212
213
214 function query(obj,adj={}) {
215 return Object.entries(obj)
216 .map(([k,v]) => k in adj ? `${k}=${adj[k](v)}` : `${k}=${v}`)
217 .join('&')
218 }
219
220 app.listen(1954)