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