سامانه صدور فاکتور و حسابداری
test: compare function tokens while preserving SQL literals and removing comments
davoodmehraban89 committed
Oct 16, 2026 at 14:14 UTC
4ba5e1ad3d51a8882a2e49e7b029a3b18b8fb882
1 file changed
+54
scripts/phase1a-lexical-compare.mjs
new
+54
@@ -0,0 +1,54 @@
1
+#!/usr/bin/env node
2
+// Canonical lexical comparison of PL/pgSQL / SQL function bodies.
3
+// Strip only token-separating whitespace and SQL comments; PRESERVE exact
4
+// text inside single/double quotes and dollar-quoted bodies.
5
+import fs from 'node:fs';
6
+export function lex(source){
7
+ const tokens=[]; let i=0;
8
+ while(i<source.length){
9
+ const c=source[i],next=source[i+1];
10
+ if(/\s/u.test(c)){i++;continue}
11
+ if(c==='-'&&next==='-'){i+=2;while(i<source.length&&source[i]!=='\n')i++;continue}
12
+ if(c==='/'&&next==='*'){i+=2;let depth=1;while(i<source.length&&depth){if(source[i]==='/'&&source[i+1]==='*'){depth++;i+=2}else if(source[i]==='*'&&source[i+1]==='/'){depth--;i+=2}else i++}if(depth)throw Error('unclosed SQL comment');continue}
13
+ if(c==="'"){
14
+ const start=i++;const esc=(start>0&&/[eE]/.test(source[start-1])&&(start===1||!/[a-zA-Z_0-9]/.test(source[start-2])));
15
+ let closed=false;
16
+ while(i<source.length){
17
+ if(esc&&source[i]==='\\'){i+=2;continue}
18
+ if(source[i]==="'"){if(source[i+1]==="'"){i+=2;continue}i++;closed=true;break}
19
+ i++;
20
+ }
21
+ if(!closed)throw Error('unclosed SQL string');tokens.push(source.slice(start,i));continue
22
+ }
23
+ if(c==='"'){const start=i++;let closed=false;while(i<source.length){if(source[i]==='"'){if(source[i+1]==='"'){i+=2;continue}i++;closed=true;break}i++}if(!closed)throw Error('unclosed SQL identifier');tokens.push(source.slice(start,i));continue}
24
+ if(c==='$'){
25
+ const tag=source.slice(i).match(/^\$[a-zA-Z_0-9]*\$/)?.[0];
26
+ if(tag){const end=source.indexOf(tag,i+tag.length);if(end<0)throw Error('unclosed dollar quote');tokens.push(source.slice(i,end+tag.length));i=end+tag.length;continue}
27
+ }
28
+ if(/[a-zA-Z_0-9\u0080-\uffff]/u.test(c)){const start=i++;while(i<source.length&&/[a-zA-Z_0-9\u0080-\uffff]/u.test(source[i]))i++;tokens.push(source.slice(start,i));continue}
29
+ tokens.push(c);i++;
30
+ }
31
+ return tokens;
32
+}
33
+export function fingerprint(source){
34
+ const tokens=lex(source);let a=0xcbf29ce484222325n,b=0x84222325cbf29ce4n;const mask=(1n<<64n)-1n;
35
+ for(const t of tokens){for(let i=0;i<t.length;i++){const x=BigInt(t.charCodeAt(i));a=((a^x)*0x100000001b3n)&mask;b=((b^x)*0x100000001b3n)&mask}a=((a^255n)*0x100000001b3n)&mask;b=((b^255n)*0x100000001b3n)&mask}
36
+ return {token_count:tokens.length,lexical_hash:a.toString(16).padStart(16,'0')+b.toString(16).padStart(16,'0')};
37
+}
38
+if(process.argv[1]?.endsWith('phase1a-lexical-compare.mjs')&&process.argv[2]){
39
+ const baseline=JSON.parse(fs.readFileSync(process.argv[2],'utf8')).functions;
40
+ const l=JSON.parse(fs.readFileSync(process.argv[3],'utf8'));
41
+ const local=Array.isArray(l)?l:l.functions;
42
+ const map=new Map(local.map(x=>[x.key,fingerprint(x.source)]));
43
+ const missing=[],mismatch=[],same=[];
44
+ for(const p of baseline){const got=map.get(p.key);
45
+ if(!got){missing.push(p.key);continue}
46
+ if(got.lexical_hash===p.lexical_hash&&got.token_count===p.token_count)same.push(p.key);
47
+ else mismatch.push({key:p.key,expected_count:p.token_count,got_count:got.token_count});
48
+ }
49
+ const extra=local.filter(x=>!baseline.some(p=>p.key===x.key)).map(x=>x.key);
50
+ const result={production:baseline.length,local:local.length,exactLexicalTokenMatch:same.length,bodyMismatch:mismatch,productionOnly:missing,localOnly:extra,disclaimer:'Lexical equality ignores only SQL comments and whitespace outside SQL literals. This is not a functional RLS test.'};
51
+ fs.writeFileSync('phase1a-lexical-diff.json',JSON.stringify(result,null,2)+'\n');
52
+ console.log(JSON.stringify(result));
53
+ if(mismatch.length||missing.length||extra.length)process.exitCode=1;
54
+}