aicasm_macro_scan.l 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. %{
  2. /*
  3. * Sub-Lexical Analyzer for macro invokation in
  4. * the Aic7xxx SCSI Host adapter sequencer assembler.
  5. *
  6. * Copyright (c) 2001 Adaptec Inc.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  16. * substantially similar to the "NO WARRANTY" disclaimer below
  17. * ("Disclaimer") and any redistribution must be conditioned upon
  18. * including a substantially similar Disclaimer requirement for further
  19. * binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  38. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_macro_scan.l#8 $
  42. *
  43. * $FreeBSD$
  44. */
  45. #include <sys/types.h>
  46. #include <inttypes.h>
  47. #include <limits.h>
  48. #include <regex.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <sysexits.h>
  52. #ifdef __linux__
  53. #include "../queue.h"
  54. #else
  55. #include <sys/queue.h>
  56. #endif
  57. #include "aicasm.h"
  58. #include "aicasm_symbol.h"
  59. #include "aicasm_macro_gram.h"
  60. #define MAX_STR_CONST 4096
  61. static char string_buf[MAX_STR_CONST];
  62. static char *string_buf_ptr;
  63. static int parren_count;
  64. static char buf[255];
  65. int mmlineno;
  66. %}
  67. WORD [A-Za-z_][-A-Za-z_0-9]*
  68. SPACE [ \t]+
  69. MCARG [^(), \t]+
  70. %x ARGLIST
  71. %%
  72. \n {
  73. ++mmlineno;
  74. }
  75. \r ;
  76. <ARGLIST>{SPACE} ;
  77. <ARGLIST>\( {
  78. parren_count++;
  79. if (parren_count == 1) {
  80. string_buf_ptr = string_buf;
  81. return ('(');
  82. }
  83. *string_buf_ptr++ = '(';
  84. }
  85. <ARGLIST>\) {
  86. if (parren_count == 1) {
  87. if (string_buf_ptr != string_buf) {
  88. /*
  89. * Return an argument and
  90. * rescan this parren so we
  91. * can return it as well.
  92. */
  93. *string_buf_ptr = '\0';
  94. mmlval.str = string_buf;
  95. string_buf_ptr = string_buf;
  96. unput(')');
  97. return T_ARG;
  98. }
  99. BEGIN INITIAL;
  100. return (')');
  101. }
  102. parren_count--;
  103. *string_buf_ptr++ = ')';
  104. }
  105. <ARGLIST>{MCARG} {
  106. char *yptr;
  107. yptr = mmtext;
  108. while (*yptr)
  109. *string_buf_ptr++ = *yptr++;
  110. }
  111. <ARGLIST>\, {
  112. if (string_buf_ptr != string_buf) {
  113. /*
  114. * Return an argument and
  115. * rescan this comma so we
  116. * can return it as well.
  117. */
  118. *string_buf_ptr = '\0';
  119. mmlval.str = string_buf;
  120. string_buf_ptr = string_buf;
  121. unput(',');
  122. return T_ARG;
  123. }
  124. return ',';
  125. }
  126. {WORD}[(] {
  127. /* May be a symbol or a macro invocation. */
  128. mmlval.sym = symtable_get(mmtext);
  129. if (mmlval.sym->type != MACRO) {
  130. stop("Expecting Macro Name",
  131. EX_DATAERR);
  132. }
  133. unput('(');
  134. parren_count = 0;
  135. BEGIN ARGLIST;
  136. return T_SYMBOL;
  137. }
  138. . {
  139. snprintf(buf, sizeof(buf), "Invalid character "
  140. "'%c'", mmtext[0]);
  141. stop(buf, EX_DATAERR);
  142. }
  143. %%
  144. int
  145. mmwrap()
  146. {
  147. stop("EOF encountered in macro call", EX_DATAERR);
  148. }