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