1. 找到 LuaPlus 工程下的 Lua Source Files 下的 llex.c;
2. 在该文件中找到下面所列函数;
static int llex (LexState *ls, SemInfo *seminfo)
3. 这是 Lua 的语法分析模块的关键函数,它由一长段 switch 构成,在一串 case 后找到 default 分支
4. 在该分支下找到如下代码段,这是一段完整的 if 分支,该代码段负责将符合条件的字符组合识别为 identifier (函数名或变量名)或 keyword (关键字)
else if (isalpha(ls->current) || ls->current == ‘_‘) { 02. /* identifier or reserved word */ 03. TString *ts; 04. if (ls->current == ‘L‘) { 05. next(ls); 06. if (ls->current == ‘"‘ || ls->current == ‘/‘‘) { 07. read_wstring(ls, ls->current, seminfo); 08. return TK_WSTRING; 09. } 10. save(ls, ‘L‘); 11. } 12. /* identifier or reserved word */ 13. do { 14. save_and_next(ls); 15. } while (isalnum(ls->current) || ls->current == ‘_‘); 16. ts = luaX_newstring(ls, luaZ_buffer(ls->buff), 17. luaZ_bufflen(ls->buff)); 18. if (ts->tsv.reserved > 0) /* reserved word? */ 19. return ts->tsv.reserved - 1 + FIRST_RESERVED; 20. else { 21. seminfo->ts = ts; 22. return TK_NAME; 23. } 24. }
5. 将该段替换为下面这一段即可,其作用在于加入了对中文ASCII字符的识别
else if (isalpha(ls->current) || ls->current == ‘_‘ || ls->current > 0x80) { 02. /* identifier or reserved word */ 03. TString *ts; 04. if (ls->current == ‘L‘) { 05. next(ls); 06. if (ls->current == ‘"‘ || ls->current == ‘/‘‘) { 07. read_wstring(ls, ls->current, seminfo); 08. return TK_WSTRING; 09. } 10. save(ls, ‘L‘); 11. } 12. /* identifier or reserved word */ 13. do { 14. if(ls->current > 0x80) 15. { 16. save_and_next(ls); 17. save_and_next(ls); 18. } 19. else 20. save_and_next(ls); 21. } while (isalnum(ls->current) || ls->current == ‘_‘ || ls->current > 0x80); 22. ts = luaX_newstring(ls, luaZ_buffer(ls->buff), 23. luaZ_bufflen(ls->buff)); 24. if (ts->tsv.reserved > 0) /* reserved word? */ 25. return ts->tsv.reserved - 1 + FIRST_RESERVED; 26. else { 27. seminfo->ts = ts; 28. return TK_NAME; 29. } 30. }
时间: 2024-09-29 17:02:28