1
0

nseel-yylex.c 4.8 KB

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