歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Lua的中文支持,修改了其中的語法分析器

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

日期:2017/3/1 10:06:45   编辑:Linux編程

相信各種大神喜歡利用中文來處理策劃,為了方便各種策劃童鞋來進行游戲策劃,鑒於大家都是中國人,英語的程度和對游戲解釋的程度都不如自己的母語,所以本人從各處學習找到了在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. } while (lislalnum(ls->current) || ls->current > 0x80);
  101. ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  102. luaZ_bufflen(ls->buff));
  103. seminfo->ts = ts;
  104. if (isreserved(ts)) /* reserved word? */
  105. return ts->tsv.extra - 1 + FIRST_RESERVED;
  106. else {
  107. return TK_NAME;
  108. }
  109. }
  110. else { /* single-char tokens (+ - / ...) */
  111. int c = ls->current;
  112. next(ls);
  113. return c;
  114. }
  115. }
  116. }
  117. }
  118. }

希望大家喜歡! 附上一張截圖

Copyright © Linux教程網 All Rights Reserved