about summary refs log tree commit diff stats
path: root/js/scripting-lang/lexer.js
blob: 3fb300ff7872c859cec4bd28e8aaa5da672e9865 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Lexer for the scripting language
// Supports both Node.js and browser environments

/**
 * Token types for the language
 * 
 * @description Defines all token types used by the lexer and parser.
 * Each token type represents a distinct syntactic element in the language.
 * 
 * The token types are organized into categories:
 * - Literals: NUMBER, STRING, TRUE, FALSE
 * - Operators: PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, etc.
 * - Keywords: WHEN, IS, THEN, FUNCTION, etc.
 * - Punctuation: LEFT_PAREN, RIGHT_PAREN, SEMICOLON, COMMA, etc.
 * - Special: IO_IN, IO_OUT, IO_ASSERT, FUNCTION_REF, COMPOSE
 * 
 * This enumeration provides a centralized definition of all possible
 * token types, ensuring consistency between lexer and parser. The token
 * types are designed to support the combinator-based architecture where
 * all operations are translated to function calls.
 */
export const TokenType = {
    NUMBER: 'NUMBER',
    PLUS: 'PLUS',
    MINUS: 'MINUS',
    MULTIPLY: 'MULTIPLY',
    DIVIDE: 'DIVIDE',
    IDENTIFIER: 'IDENTIFIER',
    ASSIGNMENT: 'ASSIGNMENT',
    ARROW: 'ARROW',
    CASE: 'CASE',
    OF: 'OF',
    WHEN: 'WHEN',
    IS: 'IS',
    THEN: 'THEN',
    WILDCARD: 'WILDCARD',
    FUNCTION: 'FUNCTION',
    LEFT_PAREN: 'LEFT_PAREN',
    RIGHT_PAREN: 'RIGHT_PAREN',
    LEFT_BRACE: 'LEFT_BRACE',
    RIGHT_BRACE: 'RIGHT_BRACE',
    LEFT_BRACKET: 'LEFT_BRACKET',
    RIGHT_BRACKET: 'RIGHT_BRACKET',
    SEMICOLON: 'SEMICOLON',
    COMMA: 'COMMA',
    DOT: 'DOT',
    STRING: 'STRING',
    TRUE: 'TRUE',
    FALSE: 'FALSE',
    AND: 'AND',
    OR: 'OR',
    XOR: 'XOR',
    NOT: 'NOT',
    EQUALS: 'EQUALS',
    LESS_THAN: 'LESS_THAN',
    GREATER_THAN: 'GREATER_THAN',
    LESS_EQUAL: 'LESS_EQUAL',
    GREATER_EQUAL: 'GREATER_EQUAL',
    NOT_EQUAL: 'NOT_EQUAL',
    MODULO: 'MODULO',
    POWER: 'POWER',
    IO_IN: 'IO_IN',
    IO_OUT: 'IO_OUT',
    IO_ASSERT: 'IO_ASSERT',
    FUNCTION_REF: 'FUNCTION_REF',
    COMPOSE: 'COMPOSE'
};

/**
 * Converts source code into tokens
 * 
 * @param {string} input - The source code to tokenize
 * @returns {Array.<Object>} Array of token objects with type, value, line, and column
 * @throws {Error} For unexpected characters or malformed tokens
 * 
 * @description The lexer performs lexical analysis by converting source code
 * into a stream of tokens. Each token represents a meaningful unit of the
 * language syntax, such as identifiers, literals, operators, and keywords.
 * 
 * The lexer implements a character-by-character scanning approach with
 * lookahead for multi-character tokens. It maintains line and column
 * information for accurate error reporting and debugging.
 * 
 * Key features:
 * - Handles whitespace and comments (single-line and multi-line)
 * - Recognizes all language constructs including operators, keywords, and literals
 * - Supports string literals with escape sequences
 * - Provides detailed position information for error reporting
 * - Cross-platform compatibility (Node.js, Bun, browser)
 * - Supports function composition with 'via' keyword
 * - Handles function references with '@' operator
 * 
 * The lexer is designed to be robust and provide clear error messages
 * for malformed input, making it easier to debug syntax errors in user code.
 * It supports the combinator-based architecture by recognizing all operators
 * and special tokens needed for function composition and application.
 */
export function lexer(input) {
    const tokens = [];
    let current = 0;
    let line = 1;
    let column = 1;

    while (current < input.length) {
        let char = input[current];

        // Skip whitespace
        if (/\s/.test(char)) {
            if (char === '\n') {
                line++;
                column = 1;
            } else {
                column++;
            }
            current++;
            continue;
        }

        // Skip comments (single line and multi-line)
        if (char === '/' && input[current + 1] === '/') {
            while (current < input.length && input[current] !== '\n') {
                current++;
                column++;
            }
            continue;
        }
        
        // Skip multi-line comments /* ... */
        if (char === '/' && input[current + 1] === '*') {
            current += 2; // Skip /*
            column += 2;
            while (current < input.length - 1 && !(input[current] === '*' && input[current + 1] === '/')) {
                if (input[current] === '\n') {
                    line++;
                    column = 1;
                } else {
                    column++;
                }
                current++;
            }
            if (current < input.length - 1) {
                current += 2; // Skip */
                column += 2;
            }
            continue;
        }

        // IO operations (..in, ..out, ..assert)
        if (char === '.' && input[current + 1] === '.') {
            current += 2; // Skip both dots
            column += 2;
            
            // Read the IO operation name
            let operation = '';
            while (current < input.length && /[a-zA-Z]/.test(input[current])) {
                operation += input[current];
                current++;
                column++;
            }
            
            // Determine the IO operation type
            switch (operation) {
                case 'in':
                    tokens.push({ type: TokenType.IO_IN, line, column: column - operation.length - 2 });
                    break;
                case 'out':
                    tokens.push({ type: TokenType.IO_OUT, line, column: column - operation.length - 2 });
                    break;
                case 'assert':
                    tokens.push({ type: TokenType.IO_ASSERT, line, column: column - operation.length - 2 });
                    break;
                default:
                    throw new Error(`Unknown IO operation: ..${operation} at line ${line}, column ${column - operation.length - 2}`);
            }
            continue;
        }
        
        // Function references (@function)
        if (char === '@') {
            current++; // Skip '@'
            column++;
            
            // Read the function name
            let functionName = '';
            while (current < input.length && /[a-zA-Z0-9_]/.test(input[current])) {
                functionName += input[current];
                current++;
                column++;
            }
            
            if (functionName === '') {
                throw new Error(`Invalid function reference at line ${line}, column ${column - 1}`);
            }
            
            tokens.push({ type: TokenType.FUNCTION_REF, name: functionName, line, column: column - functionName.length - 1 });
            continue;
        }

        // Numbers
        if (/[0-9]/.test(char)) {
            let value = '';
            while (current < input.length && /[0-9.]/.test(input[current])) {
                value += input[current];
                current++;
                column++;
            }
            tokens.push({ type: TokenType.NUMBER, value: parseFloat(value), line, column: column - value.length });
            continue;
        }

        // Identifiers and keywords
        if (/[a-zA-Z_]/.test(char)) {
            let value = '';
            const startColumn = column;
            while (current < input.length && /[a-zA-Z0-9_]/.test(input[current])) {
                value += input[current];
                current++;
                column++;
            }

            // Check for keywords
            switch (value) {
                case 'true':
                    tokens.push({ type: TokenType.TRUE, value: true, line, column: startColumn });
                    break;
                case 'false':
                    tokens.push({ type: TokenType.FALSE, value: false, line, column: startColumn });
                    break;
                case 'and':
                    tokens.push({ type: TokenType.AND, line, column: startColumn });
                    break;
                case 'or':
                    tokens.push({ type: TokenType.OR, line, column: startColumn });
                    break;
                case 'xor':
                    tokens.push({ type: TokenType.XOR, line, column: startColumn });
                    break;
                case 'not':
                    tokens.push({ type: TokenType.NOT, line, column: startColumn });
                    break;
                case 'case':
                    tokens.push({ type: TokenType.CASE, line, column: startColumn });
                    break;
                case 'of':
                    tokens.push({ type: TokenType.OF, line, column: startColumn });
                    break;
                case 'when':
                    tokens.push({ type: TokenType.WHEN, line, column: startColumn });
                    break;
                case 'is':
                    tokens.push({ type: TokenType.IS, line, column: startColumn });
                    break;
                case 'then':
                    tokens.push({ type: TokenType.THEN, line, column: startColumn });
                    break;
                case 'function':
                    tokens.push({ type: TokenType.FUNCTION, line, column: startColumn });
                    break;
                case 'via':
                    tokens.push({ type: TokenType.COMPOSE, line, column: startColumn });
                    break;
                case '_':
                    tokens.push({ type: TokenType.WILDCARD, line, column: startColumn });
                    break;
                default:
                    tokens.push({ type: TokenType.IDENTIFIER, value, line, column: startColumn });
            }
            continue;
        }

        // Strings
        if (char === '"') {
            let value = '';
            current++;
            column++;
            while (current < input.length && input[current] !== '"') {
                if (input[current] === '\\') {
                    current++;
                    column++;
                    if (current < input.length) {
                        switch (input[current]) {
                            case 'n': value += '\n'; break;
                            case 't': value += '\t'; break;
                            case 'r': value += '\r'; break;
                            case '\\': value += '\\'; break;
                            case '"': value += '"'; break;
                            default: value += input[current];
                        }
                    }
                } else {
                    value += input[current];
                }
                current++;
                column++;
            }
            if (current < input.length) {
                current++;
                column++;
            }
            tokens.push({ type: TokenType.STRING, value, line, column: column - value.length - 2 });
            continue;
        }

        // Operators and punctuation
        switch (char) {
            case '+':
                tokens.push({ type: TokenType.PLUS, line, column });
                break;
            case '-':
                if (input[current + 1] === '>') {
                    tokens.push({ type: TokenType.ARROW, line, column });
                    current++;
                    column++;
                } else {
                    tokens.push({ type: TokenType.MINUS, line, column });
                }
                break;
            case '*':
                tokens.push({ type: TokenType.MULTIPLY, line, column });
                break;
            case '/':
                tokens.push({ type: TokenType.DIVIDE, line, column });
                break;
            case '%':
                tokens.push({ type: TokenType.MODULO, line, column });
                break;
            case '^':
                tokens.push({ type: TokenType.POWER, line, column });
                break;
            case '(':
                tokens.push({ type: TokenType.LEFT_PAREN, line, column });
                break;
            case ')':
                tokens.push({ type: TokenType.RIGHT_PAREN, line, column });
                break;
            case '{':
                tokens.push({ type: TokenType.LEFT_BRACE, line, column });
                break;
            case '}':
                tokens.push({ type: TokenType.RIGHT_BRACE, line, column });
                break;
            case '[':
                tokens.push({ type: TokenType.LEFT_BRACKET, line, column });
                break;
            case ']':
                tokens.push({ type: TokenType.RIGHT_BRACKET, line, column });
                break;
            case ';':
                tokens.push({ type: TokenType.SEMICOLON, line, column });
                break;
            case ',':
                tokens.push({ type: TokenType.COMMA, line, column });
                break;
            case '.':
                tokens.push({ type: TokenType.DOT, line, column });
                break;
            case ':':
                tokens.push({ type: TokenType.ASSIGNMENT, line, column });
                break;

            case '=':
                if (input[current + 1] === '=') {
                    tokens.push({ type: TokenType.EQUALS, line, column });
                    current++;
                    column++;
                } else {
                    // Single = is used for equality comparison in assertions
                    tokens.push({ type: TokenType.EQUALS, line, column });
                }
                break;
            case '<':
                if (input[current + 1] === '=') {
                    tokens.push({ type: TokenType.LESS_EQUAL, line, column });
                    current++;
                    column++;
                } else {
                    tokens.push({ type: TokenType.LESS_THAN, line, column });
                }
                break;
            case '>':
                if (input[current + 1] === '=') {
                    tokens.push({ type: TokenType.GREATER_EQUAL, line, column });
                    current++;
                    column++;
                } else {
                    tokens.push({ type: TokenType.GREATER_THAN, line, column });
                }
                break;
            case '!':
                if (input[current + 1] === '=') {
                    tokens.push({ type: TokenType.NOT_EQUAL, line, column });
                    current++;
                    column++;
                } else {
                    throw new Error(`Unexpected character: ${char} at line ${line}, column ${column}`);
                }
                break;
            default:
                throw new Error(`Unexpected character: ${char} at line ${line}, column ${column}`);
        }

        current++;
        column++;
    }

    return tokens;
}