NodeJS 为什么我得到一个致命的JavaScript错误,而从lisp代码生成令牌

wqsoz72f  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(106)

我知道某个声明超出了内存限制,但我不明白它是什么
对于上下文,该程序接受lisp代码作为输入并将其转换为标记

// regex tests
const LETTERS = /[-_a-zA-Z0-9]/;
const WHITESPACE = /\s/;
const NUMBERS = /\d/;
const RESERVEDWORDS = ['write-line', ';','write',....]; // ... is other reserved words that i did not list out

// lexing
module.exports = function tokenizer(input) {
    const tokenList = [];
    let i = 0;
    while (i < input.length) {
        console.log(tokenList[i]);
        let char = input[i];
        if (char === '(' || char === ')') {
            tokenList.push(
                {
                    type: 'paren',
                    value: char 
                }
            );
            i++;
            continue;
        }
        
        if (LETTERS.test(char)) {
            let value = '';
            while (LETTERS.test(char)) {
                value += char;
                char = input[++i];
            }
            console.log(value);
            if (RESERVEDWORDS.includes(value)) {
                tokenList.push(
                    {
                        type: 'keyword',
                        value 
                    }
                )
            }
            else {
                tokenList.push(
                    {
                        type: 'identifier',
                        value
                    }
                );
            }
            continue;
        }

        if (WHITESPACE.test(char)) {
            i++;
            continue;
        }

        if (NUMBERS.test(char)) {
            let value = '';
            while (NUMBERS.test(char)) {
                value += char;
                char = input[++i];
            }

            tokenList.push(
                {
                    type: 'number',
                    value
                }
            );
            continue;
        }
        /*
            .
            .
            .
             more token conversion condition checks
            .
            .
            .
        */

        throw new TypeError(`NameError: ${char}`);
    }
    return tokenList;
}

我试过弄清楚这是正则表达式的问题还是代码的其他部分的问题,但都无济于事。令牌转换成功,但我得到了一个Fatal error in , line 0 Fatal JavaScript invalid size error 169220804,我不知道为什么。

hm2xizp9

hm2xizp91#

// regex tests
const LETTERS = /[-_a-zA-Z0-9]/;
const WHITESPACE = /\s/;
const NUMBERS = /\d/;
const RESERVEDWORDS = ['write-line', ';', 'write']; // ... is other reserved words that i did not list out

// lexing
function tokenizer(input) {
  const tokenList = [];
  let i = 0;
  while (i < input.length) {
    console.log(tokenList[i]);
    let char = input[i];
    if (char === '(' || char === ')') {
      tokenList.push({
        type: 'paren',
        value: char
      });
      i++;
      continue;
    }

    if (char && LETTERS.test(char)) {
      let value = '';
      while (char && LETTERS.test(char)) {
        value += char;
        char = input[++i];
      }
      if (RESERVEDWORDS.includes(value)) {
        tokenList.push({
          type: 'keyword',
          value
        })
      } else {
        tokenList.push({
          type: 'identifier',
          value
        });
      }
      continue;
    }

    if (char && WHITESPACE.test(char)) {
      i++;
      continue;
    }

    if (char && NUMBERS.test(char)) {
      let value = '';
      while (char && NUMBERS.test(char)) {
        value += char;
        char = input[++i];
      }

      tokenList.push({
        type: 'number',
        value
      });
      continue;
    }
    /*
        .
        .
        .
         more token conversion condition checks
        .
        .
        .
    */

    throw new TypeError(`NameError: ${char}`);
  }
  return tokenList;
}

console.log(tokenizer('test'));

你看到的错误是当你尝试一个对平台来说太高的数组索引时。你得到这个错误的原因是因为像input[++i]这样的行。i可能会变得非常大,以至于会导致脚本崩溃。
现在,它变得如此之大的原因是:

while (LETTERS.test(char)) {
  value += char;
  char = input[++i];
}

当i大于input的长度时,char变成undefined。test()方法总是将参数强制为string。undefined变成“undefined”,与LETTERS匹配,因此它无限循环,直到错误停止它。
我只是在您的代码中添加了检查,以确保char持有一个值,并且函数正确返回。

相关问题