@fraczak/k 6.1.0 → 6.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,105 +0,0 @@
1
- import http from 'http';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { fileURLToPath } from 'url';
5
- import codes from '../codes.mjs';
6
- import { parse } from './typeParser.mjs';
7
-
8
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
- const REGISTRY_FILE = path.join(__dirname, 'registry.json');
10
- const PORT = 3001;
11
-
12
- // Load existing registry if it exists
13
- if (fs.existsSync(REGISTRY_FILE)) {
14
- console.log('Loading existing registry...');
15
- try {
16
- const data = fs.readFileSync(REGISTRY_FILE, 'utf8');
17
- const json = JSON.parse(data);
18
- codes.load(json);
19
- console.log(`Loaded ${Object.keys(json).length} types.`);
20
- } catch (err) {
21
- console.error('Error loading registry:', err);
22
- }
23
- } else {
24
- console.log('No existing registry found. Starting fresh.');
25
- }
26
-
27
- const server = http.createServer((req, res) => {
28
- // Helper to send JSON response
29
- const sendJSON = (data, status = 200) => {
30
- res.writeHead(status, { 'Content-Type': 'application/json' });
31
- res.end(JSON.stringify(data));
32
- };
33
-
34
- // Helper to send Error
35
- const sendError = (message, status = 400) => {
36
- res.writeHead(status, { 'Content-Type': 'application/json' });
37
- res.end(JSON.stringify({ error: message }));
38
- };
39
-
40
- // Route: POST
41
- if (req.method === 'POST') {
42
- let body = '';
43
- req.on('data', chunk => {
44
- body += chunk.toString();
45
- });
46
- req.on('end', () => {
47
- try {
48
- // 1. Parse the input string using the Jison parser
49
- // The parser expects a string of type definitions
50
- console.log("Received body:", body);
51
- const definitions = parse(body);
52
- console.log("Parsed definitions:", JSON.stringify(definitions, null, 2));
53
-
54
- // 2. Register the parsed codes
55
- // codes.register returns the representatives map { temp_name: canonical_hash }
56
- const representatives = codes.register(definitions);
57
-
58
- // Filter out generated names (_gen_...) and canonical names (@...)
59
- const filteredRepresentatives = Object.fromEntries(
60
- Object.entries(representatives).filter(([key, value]) =>
61
- !key.startsWith('_gen_') && !key.startsWith('@')
62
- )
63
- );
64
-
65
- // 3. Save to disk
66
- const dump = codes.dump();
67
- fs.writeFileSync(REGISTRY_FILE, JSON.stringify(dump, null, 2));
68
-
69
- // 4. Return the mapping
70
- sendJSON(filteredRepresentatives);
71
-
72
- } catch (err) {
73
- console.error(err);
74
- sendError(`Registration failed: ${err.message}`);
75
- }
76
- });
77
- }
78
- // Route: GET
79
- else if (req.method === 'GET') {
80
- const typeId = req.url.split('/').pop();
81
- console.log("Requested type ID:", typeId);
82
- if (!typeId) {
83
- sendError('Missing type ID');
84
- return;
85
- }
86
- // Decode URI component in case @ is encoded
87
- const decodedId = decodeURIComponent(typeId);
88
- console.log("Decoded type ID:", decodedId);
89
-
90
- const typeDef = codes.find(decodedId);
91
- if (typeDef.code === 'undefined') {
92
- sendError('Type not found', 404);
93
- } else {
94
- sendJSON(typeDef);
95
- }
96
- }
97
- // 404 Not Found
98
- else {
99
- sendError('Not Found', 404);
100
- }
101
- });
102
-
103
- server.listen(PORT, () => {
104
- console.log(`Type Registry running at http://localhost:${PORT}/`);
105
- });
@@ -1,132 +0,0 @@
1
- %{
2
-
3
- let defs = {};
4
- let genCount = 0;
5
- let genNameId;
6
-
7
- function genName() {
8
- return `_gen_${genCount++}`;
9
- }
10
-
11
- function fromEscString(escString) {
12
- const isSingleQuoted = escString.startsWith("'");
13
- let str = escString.substring(1, escString.length - 1);
14
- if (isSingleQuoted) {
15
- str = str.replace(/\\'/g, "'");
16
- } else {
17
- str = str.replace(/\\"/g, '"');
18
- }
19
- str = str.replace(/\\\\/g, '\\');
20
- str = str.replace(/\\\//g, '/');
21
- str = str.replace(/\\b/g, '\b');
22
- str = str.replace(/\\f/g, '\f');
23
- str = str.replace(/\\n/g, '\n');
24
- str = str.replace(/\\r/g, '\r');
25
- str = str.replace(/\\t/g, '\t');
26
- str = str.replace(/\\u([\dA-F]{4})/gi, (match, p1) => String.fromCharCode(parseInt(p1, 16)));
27
-
28
- return str;
29
- }
30
-
31
- %}
32
-
33
- %lex
34
-
35
- %%
36
- \s+ /* blanks */
37
- "<" return 'LA';
38
- ">" return 'RA';
39
- "{" return 'LC';
40
- "}" return 'RC';
41
- "=" return 'EQ';
42
- "," return 'COMMA';
43
- ";" return 'SC';
44
- "@" return 'AT';
45
- "$" return 'DOLLAR';
46
- \"([^"\\]|\\(.|\n))*\"|\'([^'\\]|\\(.|\n))*\' return 'STRING';
47
- [a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]* return 'NAME';
48
- <<EOF>> return 'EOF';
49
- . return 'INVALID';
50
-
51
- /lex
52
-
53
- %token NAME STRING
54
- %token LA RA LC RC EQ COMMA SC AT DOLLAR
55
- %token EOF
56
-
57
- %start expressions
58
-
59
- %%
60
-
61
- expressions
62
- : initialize_defs definitions EOF
63
- { return defs; }
64
- ;
65
-
66
- initialize_defs
67
- : /* empty */
68
- { defs = {}; genCount = 0; }
69
- ;
70
-
71
- definitions
72
- : definition
73
- | definitions definition
74
- ;
75
-
76
- definition
77
- : DOLLAR name EQ LA field_list RA SC
78
- { defs[$2] = { code: 'union', union: $5 }; }
79
- | DOLLAR name EQ LC field_list RC SC
80
- { defs[$2] = { code: 'product', product: $5 }; }
81
- ;
82
-
83
- type_expr
84
- : LA field_list RA
85
- {
86
- genNameId = genName();
87
- defs[genNameId] = { code: 'union', union: $2 };
88
- $$ = genNameId;
89
- }
90
- | LC field_list RC
91
- {
92
- genNameId = genName();
93
- defs[genNameId] = { code: 'product', product: $2 };
94
- $$ = genNameId;
95
- }
96
- | ref
97
- { $$ = $1; }
98
- ;
99
-
100
- ref
101
- : name
102
- { $$ = $1; }
103
- | AT name
104
- { $$ = '@' + $2; }
105
- ;
106
-
107
- field_list
108
- : /* empty */
109
- { $$ = {}; }
110
- | field
111
- { $$ = {}; $$[$1.label] = $1.type; }
112
- | field_list COMMA field
113
- { $$ = $1; $$[$3.label] = $3.type; }
114
- ;
115
-
116
- field
117
- : type_expr label
118
- { $$ = { type: $1, label: $2 }; }
119
- ;
120
-
121
- label
122
- : name
123
- { $$ = $1; }
124
- | str
125
- { $$ = $1; }
126
- ;
127
-
128
- name: NAME { $$ = yytext; };
129
- str: STRING { $$ = fromEscString(yytext); };
130
-
131
- %%
132
-