我正在尝试将旧的 C++ 词法分析器 ( source ) 移植到 JavaScript,并且由于我对 C/C++ 的不理解而有点挣扎。
我有一个参数c
,正如我目前看到的那样,它可以是我正在解析的输入文件块上的位置索引 ( *yy_cp
),也可以是存储在该地址的实际(包括 nul)字符。我需要c
用作查找表中的索引。词法分析器这样做:
/* Promotes a possibly negative, possibly signed char to an
* unsigned integer for use as an array index. If the signed char
* is negative, we want to instead treat it as an 8-bit unsigned
* char, hence the double cast.
*/
#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c)
并这样称呼它:
register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
它将存储yy_ec
包含 256 个条目(我假设扩展 ASCII)的查找表的值,在yy_c
. 查找的位置是由生成的YY_SC_TO_UI
,这就是我将它移植到 JavaScript 时丢失的地方。YY_SC_TO_UI
必须返回一个 0-255 之间的数字,所以我只取我所拥有的并且:
"[c]".charCodeAt(0)
或者还有什么我需要注意在JS中处理“可能的负数,可能的签名字符”吗?
谢谢。