Lua的中文支持,修改了其中的语法分析器


相信各种大神喜欢利用中文来处理策划,为了方便各种策划童鞋来进行游戏策划,鉴于大家都是中国人,英语的程度和对游戏解释的程度都不如自己的母语,所以本人从各处学习找到了在Lua最新版本中文的支持。

  1. static int llex (LexState *ls, SemInfo *seminfo) { 
  2.   luaZ_resetbuffer(ls->buff); 
  3.   for (;;) { 
  4.     switch (ls->current) { 
  5.       case '\n'case '\r': {  /* line breaks */ 
  6.         inclinenumber(ls); 
  7.         break
  8.       } 
  9.       case ' 'case '\f'case '\t'case '\v': {  /* spaces */ 
  10.         next(ls); 
  11.         break
  12.       } 
  13.       case '-': {  /* '-' or '--' (comment) */ 
  14.         next(ls); 
  15.         if (ls->current != '-'return '-'
  16.         /* else is a comment */ 
  17.         next(ls); 
  18.         if (ls->current == '[') {  /* long comment? */ 
  19.           int sep = skip_sep(ls); 
  20.           luaZ_resetbuffer(ls->buff);  /* `skip_sep' may dirty the buffer */ 
  21.           if (sep >= 0) { 
  22.             read_long_string(ls, NULL, sep);  /* skip long comment */ 
  23.             luaZ_resetbuffer(ls->buff);  /* previous call may dirty the buff. */ 
  24.             break
  25.           } 
  26.         } 
  27.         /* else short comment */ 
  28.         while (!currIsNewline(ls) && ls->current != EOZ) 
  29.           next(ls);  /* skip until end of line (or end of file) */ 
  30.         break
  31.       } 
  32.       case '[': {  /* long string or simply '[' */ 
  33.         int sep = skip_sep(ls); 
  34.         if (sep >= 0) { 
  35.           read_long_string(ls, seminfo, sep); 
  36.           return TK_STRING; 
  37.         } 
  38.         else if (sep == -1) return '['
  39.         else lexerror(ls, "invalid long string delimiter", TK_STRING); 
  40.       } 
  41.       case '=': { 
  42.         next(ls); 
  43.         if (ls->current != '='return '='
  44.         else { next(ls); return TK_EQ; } 
  45.       } 
  46.       case '<': { 
  47.         next(ls); 
  48.         if (ls->current != '='return '<'
  49.         else { next(ls); return TK_LE; } 
  50.       } 
  51.       case '>': { 
  52.         next(ls); 
  53.         if (ls->current != '='return '>'
  54.         else { next(ls); return TK_GE; } 
  55.       } 
  56.       case '~': { 
  57.         next(ls); 
  58.         if (ls->current != '='return '~'
  59.         else { next(ls); return TK_NE; } 
  60.       } 
  61.       case ':': { 
  62.         next(ls); 
  63.         if (ls->current != ':'return ':'
  64.         else { next(ls); return TK_DBCOLON; } 
  65.       } 
  66.       case '"'case '\'': {  /* short literal strings */ 
  67.         read_string(ls, ls->current, seminfo); 
  68.         return TK_STRING; 
  69.       } 
  70.       case '.': {  /* '.', '..', '...', or number */ 
  71.         save_and_next(ls); 
  72.         if (check_next(ls, ".")) { 
  73.           if (check_next(ls, ".")) 
  74.             return TK_DOTS;   /* '...' */ 
  75.           else return TK_CONCAT;   /* '..' */ 
  76.         } 
  77.         else if (!lisdigit(ls->current)) return '.'
  78.         /* else go through */ 
  79.       } 
  80.       case '0'case '1'case '2'case '3'case '4'
  81.       case '5'case '6'case '7'case '8'case '9': { 
  82.         read_numeral(ls, seminfo); 
  83.         return TK_NUMBER; 
  84.       } 
  85.       case EOZ: { 
  86.         return TK_EOS; 
  87.       } 
  88.       default: { 
  89.         if (lislalpha(ls->current) || (ls->current > 0x80)) {  /* identifier or reserved word? */ 
  90.           TString *ts; 
  91.            do   
  92.            { 
  93.                if(ls->current > 0x80)   
  94.                {   
  95.                     save_and_next(ls);   
  96.                     save_and_next(ls);   
  97.                 }   
  98.                 else   
  99.                     save_and_next(ls); 
  100.                  
  101.             } while (lislalnum(ls->current) || ls->current > 0x80);   
  102.  
  103.  
  104.           ts = luaX_newstring(ls, luaZ_buffer(ls->buff), 
  105.                                   luaZ_bufflen(ls->buff)); 
  106.           seminfo->ts = ts; 
  107.           if (isreserved(ts))  /* reserved word? */ 
  108.             return ts->tsv.extra - 1 + FIRST_RESERVED; 
  109.           else { 
  110.             return TK_NAME; 
  111.           } 
  112.         } 
  113.         else {  /* single-char tokens (+ - / ...) */ 
  114.           int c = ls->current; 
  115.           next(ls); 
  116.           return c; 
  117.         } 
  118.       } 
  119.     } 
  120.   } 
  121. }

大笑大笑大笑吐舌头希望大家喜欢! 附上一张截图

相关内容