about summary refs log tree commit diff stats
path: root/js/baba-yaga/src/legacy/lexer.js
blob: 054dd0eb1d17cc1b0d0dc7106d64671f026737cc (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// lexer.js

import { LexError, ErrorHelpers } from '../core/error.js';

const tokenTypes = {
  IDENTIFIER: 'IDENTIFIER',
  TYPE: 'TYPE',
  NUMBER: 'NUMBER',
  STRING: 'STRING',
  ARROW: 'ARROW',
  COLON: 'COLON',
  SEMICOLON: 'SEMICOLON',
  COMMA: 'COMMA',
  KEYWORD: 'KEYWORD',
  OPERATOR: 'OPERATOR',
  LPAREN: 'LPAREN',
  RPAREN: 'RPAREN',
  DOT: 'DOT',
  LBRACKET: 'LBRACKET',
  RBRACKET: 'RBRACKET',
  LBRACE: 'LBRACE',
  RBRACE: 'RBRACE',
  EOF: 'EOF',
};

const keywords = ['when', 'is', 'then', 'if', 'Ok', 'Err', 'true', 'false', 'PI', 'INFINITY', 'and', 'or', 'xor'];

function createLexer(input) {
  let position = 0;
  let line = 1;
  let column = 1;

  function isWhitespace(char) {
    return /\s/.test(char);
  }

  function isDigit(char) {
    return /\d/.test(char);
  }

  function isLetter(char) {
    return /[a-zA-Z_0-9]/.test(char);
  }

  function readWhile(predicate) {
    let str = '';
    while (position < input.length && predicate(input[position])) {
      str += input[position];
      position++;
      column++;
    }
    return str;
  }

  function readString() {
    let str = '';
    const startLine = line;
    const startColumn = column;
    
    position++; // Skip the opening quote
    column++;
    
    while (position < input.length && input[position] !== '"') {
      const char = input[position];
      
      // Handle newlines in strings
      if (char === '\n') {
        line++;
        column = 1;
      } else {
        column++;
      }
      
      // Handle escape sequences
      if (char === '\\' && position + 1 < input.length) {
        const nextChar = input[position + 1];
        switch (nextChar) {
          case 'n':
            str += '\n';
            position += 2;
            column++;
            break;
          case 't':
            str += '\t';
            position += 2;
            column++;
            break;
          case 'r':
            str += '\r';
            position += 2;
            column++;
            break;
          case '\\':
            str += '\\';
            position += 2;
            column++;
            break;
          case '"':
            str += '"';
            position += 2;
            column++;
            break;
          default:
            str += char;
            position++;
        }
      } else {
        str += char;
        position++;
      }
    }
    
    // Check for unterminated string
    if (position >= input.length) {
      throw new LexError(
        'Unterminated string literal',
        { line: startLine, column: startColumn, length: str.length + 1 },
        input,
        [
          'Add closing quote " at the end of the string',
          'Check for unescaped quotes inside the string',
          'Use \\" to include quotes in strings'
        ]
      );
    }
    
    position++; // Skip the closing quote
    column++;
    return { type: tokenTypes.STRING, value: str, line: startLine, column: startColumn };
  }

  function readNumber() {
    let value = readWhile(isDigit);
    let isFloat = false;
    if (peekChar() === '.') {
      position++;
      column++;
      value += '.' + readWhile(isDigit);
      isFloat = true;
    }
    
    const numericValue = isFloat ? parseFloat(value) : parseInt(value, 10);
    return { 
      type: tokenTypes.NUMBER, 
      value: numericValue, 
      isFloat: isFloat,
      originalString: value,
      line, 
      column 
    };
  }

  function peekChar() {
    return input[position];
  }

  function shouldBeNegativeLiteral() {
    // Look at the previous non-whitespace token to decide
    let prevPos = position - 1;
    while (prevPos >= 0 && isWhitespace(input[prevPos])) {
      prevPos--;
    }
    
    if (prevPos < 0) {
      // At start of input - should be negative literal
      return true;
    }
    
    const prevChar = input[prevPos];
    
    // After opening parenthesis, comma, or operators - should be negative literal
    if (prevChar === '(' || prevChar === ',' || prevChar === '+' || 
        prevChar === '*' || prevChar === '/' || prevChar === '%' ||
        prevChar === '=' || prevChar === '>' || prevChar === '<' ||
        prevChar === ':' || prevChar === ';') {
      return true;
    }
    
    // After closing parenthesis - should be binary minus
    if (prevChar === ')') {
      return false;
    }
    
    // After numbers - this is tricky. In most cases it should be binary minus,
    // but in function call contexts it might be a negative literal.
    // Let's look ahead to see if this is likely a function call context.
    if (isDigit(prevChar)) {
      // Look ahead to see if we're in a function call context
      // If we see whitespace followed by another minus, it's probably a negative literal
      let lookAheadPos = position + 1;
      while (lookAheadPos < input.length && isWhitespace(input[lookAheadPos])) {
        lookAheadPos++;
      }
      if (lookAheadPos < input.length && input[lookAheadPos] === '-') {
        // This looks like a function call with consecutive negative arguments
        return true;
      }
      return false; // Default to binary minus
    }
    
    // After identifiers - could be either, but in most contexts it's a negative literal
    // (function calls, variable declarations, etc.)
    if (isLetter(prevChar)) {
      return true;
    }
    
    // Default to negative literal
    return true;
  }

  function readNegativeNumber() {
    // Consume the minus sign
    position++;
    column++;
    
    // Read the number part
    let value = '-' + readWhile(isDigit);
    let isFloat = false;
    
    if (peekChar() === '.') {
      position++;
      column++;
      value += '.' + readWhile(isDigit);
      isFloat = true;
    }
    
    const numericValue = isFloat ? parseFloat(value) : parseInt(value, 10);
    return { 
      type: tokenTypes.NUMBER, 
      value: numericValue, 
      isFloat: isFloat,
      originalString: value,
      line, 
      column 
    };
  }

  function nextToken() {
    if (position >= input.length) {
      return { type: tokenTypes.EOF, line, column };
    }

    let char = input[position];

    if (isWhitespace(char)) {
      if (char === '\n') {
        line++;
        column = 1;
      } else {
        column++;
      }
      position++;
      return nextToken();
    }

    if (char === '/' && input[position + 1] === '/') {
      while (position < input.length && input[position] !== '\n') {
        position++;
        column++;
      }
      return nextToken(); // Skip the comment and get the next real token
    }

    if (char === '(') {
      position++;
      column++;
      return { type: tokenTypes.LPAREN, value: '(', line, column };
    }

    if (char === ')') {
      position++;
      column++;
      return { type: tokenTypes.RPAREN, value: ')', line, column };
    }

    if (char === '[') {
      position++;
      column++;
      return { type: tokenTypes.LBRACKET, value: '[', line, column };
    }

    if (char === ']') {
      position++;
      column++;
      return { type: tokenTypes.RBRACKET, value: ']', line, column };
    }

    if (char === '{') {
      position++;
      column++;
      return { type: tokenTypes.LBRACE, value: '{', line, column };
    }

    if (char === '}') {
      position++;
      column++;
      return { type: tokenTypes.RBRACE, value: '}', line, column };
    }

    // Handle double dot operator for string concatenation (must come before single dot)
    if (char === '.' && input[position + 1] === '.') {
      position += 2;
      column += 2;
      return { type: tokenTypes.OPERATOR, value: '..', line, column };
    }

    if (char === '.') {
      position++;
      column++;
      return { type: tokenTypes.DOT, value: '.', line, column };
    }

    // Handle negative numbers based on context
    if (char === '-' && position + 1 < input.length && isDigit(input[position + 1])) {
      // Check if this should be a negative literal vs binary minus
      if (shouldBeNegativeLiteral()) {
        return readNegativeNumber();
      }
    }

    if (isDigit(char)) {
      return readNumber();
    }

    if (isLetter(char)) {
      const value = readWhile(isLetter);
      if (['Int', 'String', 'Result', 'Float', 'Number', 'List', 'Table', 'Bool'].includes(value)) {
        return { type: tokenTypes.TYPE, value, line, column };
      }
      if (keywords.includes(value)) {
        return { type: tokenTypes.KEYWORD, value, line, column };
      }
      return { type: tokenTypes.IDENTIFIER, value, line, column };
    }

    if (char === '"') {
      return readString();
    }

    if (char === ':') {
      position++;
      column++;
      return { type: tokenTypes.COLON, value: ':', line, column };
    }
    
    if (char === '-' && input[position + 1] === '>') {
      position += 2;
      column += 2;
      return { type: tokenTypes.ARROW, value: '->', line, column };
    }

    if (char === ';') {
      position++;
      column++;
      return { type: tokenTypes.SEMICOLON, value: ';', line, column };
    }
    
    // Handle >= and <=
    if (char === '>' && input[position + 1] === '=') {
      position += 2;
      column += 2;
      return { type: tokenTypes.OPERATOR, value: '>=', line, column };
    }
    if (char === '<' && input[position + 1] === '=') {
      position += 2;
      column += 2;
      return { type: tokenTypes.OPERATOR, value: '<=', line, column };
    }
    
    // Handle != (not equal)
    if (char === '!' && input[position + 1] === '=') {
      position += 2;
      column += 2;
      return { type: tokenTypes.OPERATOR, value: '!=', line, column };
    }
    
    if (char === ',') {
        position++;
        column++;
        return { type: tokenTypes.COMMA, value: ',', line, column };
    }
    
    if (['+', '-', '*', '/', '=', '>', '<', '%'].includes(char)) {
        position++;
        column++;
        return { type: tokenTypes.OPERATOR, value: char, line, column };
    }

    const suggestions = [];
    
    // Common character mistakes
    if (char === '"' || char === '"') {
      suggestions.push('Use straight quotes " instead of curly quotes');
    } else if (char === '–' || char === '—') {
      suggestions.push('Use regular minus - or arrow -> instead of em/en dash');
    } else if (/[^\x00-\x7F]/.test(char)) {
      suggestions.push('Use only ASCII characters in Baba Yaga code');
    } else {
      suggestions.push(`Character "${char}" is not valid in Baba Yaga syntax`);
    }
    
    throw new LexError(
      `Unexpected character: ${JSON.stringify(char)}`,
      { line, column, length: 1 },
      input,
      suggestions
    );
  }

  function allTokens() {
    const tokens = [];
    let token;
    do {
      token = nextToken();
      tokens.push(token);
    } while (token.type !== tokenTypes.EOF);
    return tokens;
  }

  return {
    allTokens,
  };
}

export { createLexer, tokenTypes };