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