nseel-yylex.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Nullsoft Expression Evaluator Library (NS-EEL)
  3. Copyright (C) 1999-2003 Nullsoft, Inc.
  4. nseel-yylex.c
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include "ns-eel-int.h"
  20. #define NBPW 16
  21. #define EOF (-1)
  22. #define ERROR 256 /* yacc's value */
  23. static int llset(compileContext *ctx);
  24. static int llinp(compileContext *ctx, char **exp);
  25. static int lexgetc(char **exp)
  26. {
  27. char c= **exp;
  28. if (c) (*exp)++;
  29. return( c != 0 ? c : -1);
  30. }
  31. static int tst__b(register int c, char tab[])
  32. {
  33. return (tab[(c >> 3) & 037] & (1 << (c & 07)) );
  34. }
  35. int nseel_gettoken(compileContext *ctx, char *lltb, int lltbsiz)
  36. {
  37. register char *lp, *tp, *ep;
  38. tp = lltb;
  39. ep = tp+lltbsiz-1;
  40. for (lp = ctx->llbuf; lp < ctx->llend && tp < ep;)
  41. *tp++ = *lp++;
  42. *tp = 0;
  43. return(tp-lltb);
  44. }
  45. int nseel_yylex(compileContext *ctx, char **exp)
  46. {
  47. register int c, st;
  48. int final, l, llk, i;
  49. register struct lextab *lp;
  50. char *cp;
  51. while (1)
  52. {
  53. llk = 0;
  54. if (llset(ctx)) return(0);
  55. st = 0;
  56. final = -1;
  57. lp = &nseel_lextab;
  58. do {
  59. if (lp->lllook && (l = lp->lllook[st])) {
  60. for (c=0; c<NBPW; c++)
  61. if (l&(1<<c))
  62. ctx->llsave[c] = ctx->llp1;
  63. llk++;
  64. }
  65. if ((i = lp->llfinal[st]) != -1) {
  66. final = i;
  67. ctx->llend = ctx->llp1;
  68. }
  69. if ((c = llinp(ctx,exp)) < 0)
  70. break;
  71. if ((cp = lp->llbrk) && llk==0 && tst__b(c, cp)) {
  72. ctx->llp1--;
  73. break;
  74. }
  75. } while ((st = (*lp->llmove)(lp, c, st)) != -1);
  76. if (ctx->llp2 < ctx->llp1)
  77. ctx->llp2 = ctx->llp1;
  78. if (final == -1) {
  79. ctx->llend = ctx->llp1;
  80. if (st == 0 && c < 0)
  81. return(0);
  82. if ((cp = lp->llill) && tst__b(c, cp)) {
  83. continue;
  84. }
  85. return(ERROR);
  86. }
  87. if (c = (final >> 11) & 037)
  88. ctx->llend = ctx->llsave[c-1];
  89. if ((c = (*lp->llactr)(ctx,final&03777)) >= 0)
  90. return(c);
  91. }
  92. }
  93. void nseel_llinit(compileContext *ctx)
  94. {
  95. ctx->llp1 = ctx->llp2 = ctx->llend = ctx->llbuf;
  96. ctx->llebuf = ctx->llbuf + sizeof(ctx->llbuf);
  97. ctx->lleof = ctx->yyline = 0;
  98. }
  99. static int llinp(compileContext *ctx, char **exp)
  100. {
  101. register c;
  102. register struct lextab *lp;
  103. register char *cp;
  104. lp = &nseel_lextab;
  105. cp = lp->llign; /* Ignore class */
  106. for (;;) {
  107. /*
  108. * Get the next character from the save buffer (if possible)
  109. * If the save buffer's empty, then return EOF or the next
  110. * input character. Ignore the character if it's in the
  111. * ignore class.
  112. */
  113. c = (ctx->llp1 < ctx->llp2) ? *ctx->llp1 & 0377 : (ctx->lleof) ? EOF : lexgetc(exp);
  114. if (c >= 0) { /* Got a character? */
  115. if (cp && tst__b(c, cp))
  116. continue; /* Ignore it */
  117. if (ctx->llp1 >= ctx->llebuf) { /* No, is there room? */
  118. return -1;
  119. }
  120. *ctx->llp1++ = c; /* Store in token buff */
  121. } else
  122. ctx->lleof = 1; /* Set EOF signal */
  123. return(c);
  124. }
  125. }
  126. static int llset(compileContext *ctx)
  127. /*
  128. * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  129. */
  130. {
  131. register char *lp1, *lp2;
  132. for (lp1 = ctx->llbuf, lp2 = ctx->llend; lp2 < ctx->llp2;)
  133. *lp1++ = *lp2++;
  134. ctx->llend = ctx->llp1 = ctx->llbuf;
  135. ctx->llp2 = lp1;
  136. return(ctx->lleof && lp1 == ctx->llbuf);
  137. }