/crypto/heimdal/lib/sl/slc-lex.l

https://bitbucket.org/freebsd/freebsd-head/ · LEX · 160 lines · 112 code · 15 blank · 33 comment · 0 complexity · a9dceef63490f3d57378464ef988fa18 MD5 · raw file

  1. %{
  2. /*
  3. * Copyright (c) 2004 Kungliga Tekniska Hรถgskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * 3. Neither the name of the Institute nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. /* $Id$ */
  35. #undef ECHO
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdarg.h>
  39. #include <stdlib.h>
  40. #include "slc.h"
  41. #include "slc-gram.h"
  42. unsigned lineno = 1;
  43. static void handle_comment(void);
  44. static char * handle_string(void);
  45. #define YY_NO_UNPUT
  46. #undef ECHO
  47. %}
  48. %option nounput
  49. %%
  50. [A-Za-z][-A-Za-z0-9_]* {
  51. yylval.string = strdup ((const char *)yytext);
  52. return LITERAL;
  53. }
  54. "\"" { yylval.string = handle_string(); return STRING; }
  55. \n { ++lineno; }
  56. \/\* { handle_comment(); }
  57. [={}] { return *yytext; }
  58. [ \t] ;
  59. %%
  60. void
  61. error_message (const char *format, ...)
  62. {
  63. va_list args;
  64. va_start (args, format);
  65. fprintf (stderr, "%s:%d: ", filename, lineno);
  66. vfprintf (stderr, format, args);
  67. va_end (args);
  68. error_flag++;
  69. }
  70. void
  71. yyerror (char *s)
  72. {
  73. error_message("%s\n", s);
  74. }
  75. static void
  76. handle_comment(void)
  77. {
  78. int c;
  79. int start_lineno = lineno;
  80. int level = 1;
  81. int seen_star = 0;
  82. int seen_slash = 0;
  83. while((c = input()) != EOF) {
  84. if(c == '/') {
  85. if(seen_star) {
  86. if(--level == 0)
  87. return;
  88. seen_star = 0;
  89. continue;
  90. }
  91. seen_slash = 1;
  92. continue;
  93. } else if(c == '*') {
  94. if(seen_slash) {
  95. level++;
  96. seen_star = seen_slash = 0;
  97. continue;
  98. }
  99. seen_star = 1;
  100. continue;
  101. }
  102. seen_star = seen_slash = 0;
  103. if(c == '\n') {
  104. lineno++;
  105. continue;
  106. }
  107. }
  108. if(c == EOF)
  109. error_message("unterminated comment, possibly started on line %d\n", start_lineno);
  110. }
  111. static char *
  112. handle_string(void)
  113. {
  114. char x[1024];
  115. int i = 0;
  116. int c;
  117. int quote = 0;
  118. while((c = input()) != EOF){
  119. if(quote) {
  120. x[i++] = '\\';
  121. x[i++] = c;
  122. quote = 0;
  123. continue;
  124. }
  125. if(c == '\n'){
  126. error_message("unterminated string");
  127. lineno++;
  128. break;
  129. }
  130. if(c == '\\'){
  131. quote++;
  132. continue;
  133. }
  134. if(c == '\"')
  135. break;
  136. x[i++] = c;
  137. }
  138. x[i] = '\0';
  139. return strdup(x);
  140. }
  141. int
  142. yywrap ()
  143. {
  144. return 1;
  145. }