Minor typing cleanups for aicasm.
[dragonfly.git] / sys / dev / disk / aic7xxx / aicasm / aicasm_macro_scan.l
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#7 $
42  *
43  * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_macro_scan.l,v 1.1.2.3 2002/09/27 15:46:28 gibbs Exp $
44  * $DragonFly: src/sys/dev/disk/aic7xxx/aicasm/aicasm_macro_scan.l,v 1.4 2006/04/22 16:15:26 dillon Exp $
45  */
46
47 #include <sys/types.h>
48
49 #include <inttypes.h>
50 #include <limits.h>
51 #include <regex.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <sysexits.h>
55 #ifdef __linux__
56 #include "../queue.h"
57 #else
58 #include <sys/queue.h>
59 #endif
60
61 #include "aicasm.h"
62 #include "aicasm_symbol.h"
63 #include "aicasm_macro_gram.h"
64
65 #define MAX_STR_CONST 4096
66 static char string_buf[MAX_STR_CONST];
67 static char *string_buf_ptr;
68 static int  parren_count;
69 static char msgbuf[255];
70
71 extern int mmlex(void);
72 %}
73
74 WORD            [A-Za-z_][-A-Za-z_0-9]*
75 SPACE           [ \t]+
76 MCARG           [^(), \t]+
77
78 %x ARGLIST
79
80 %%
81 \n                      {
82                                 ++yylineno;
83                         }
84 <ARGLIST>{SPACE}        ;
85 <ARGLIST>\(             {
86                                 parren_count++;
87                                 if (parren_count == 1) {
88                                         string_buf_ptr = string_buf;
89                                         return ('(');
90                                 }
91                                 *string_buf_ptr++ = '(';
92                         }
93 <ARGLIST>\)             {
94                                 if (parren_count == 1) {
95                                         if (string_buf_ptr != string_buf) {
96                                                 /*
97                                                  * Return an argument and
98                                                  * rescan this parren so we
99                                                  * can return it as well.
100                                                  */
101                                                 *string_buf_ptr = '\0';
102                                                 mmlval.str = string_buf;
103                                                 string_buf_ptr = string_buf;
104                                                 unput(')');
105                                                 return T_ARG;
106                                         }
107                                         BEGIN INITIAL;
108                                         return (')');
109                                 }
110                                 parren_count--;
111                                 *string_buf_ptr++ = ')';
112                         }
113 <ARGLIST>{MCARG}        {
114                                 char *yptr;
115
116                                 yptr = mmtext;
117                                 while (*yptr)
118                                         *string_buf_ptr++ = *yptr++;
119                         }
120 <ARGLIST>\,             {
121                                 if (string_buf_ptr != string_buf) {
122                                         /*
123                                          * Return an argument and
124                                          * rescan this comma so we
125                                          * can return it as well.
126                                          */
127                                         *string_buf_ptr = '\0';
128                                         mmlval.str = string_buf;
129                                         string_buf_ptr = string_buf;
130                                         unput(',');
131                                         return T_ARG;
132                                 }
133                                 return ',';
134                         }
135 {WORD}[(]               {
136                                 /* May be a symbol or a macro invocation. */
137                                 mmlval.sym = symtable_get(mmtext);
138                                 if (mmlval.sym->type != MACRO) {
139                                         stop("Expecting Macro Name",
140                                              EX_DATAERR);
141                                 }
142                                 unput('(');
143                                 parren_count = 0;
144                                 BEGIN ARGLIST;
145                                 return T_SYMBOL;
146                         }
147 .                       { 
148                                 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
149                                          "'%c'", mmtext[0]);
150                                 stop(msgbuf, EX_DATAERR);
151                         }
152 %%
153
154 int
155 mmwrap(void)
156 {
157         stop("EOF encountered in macro call", EX_DATAERR);
158         return (1);
159 }