1 %{
2 /*        $NetBSD: aicasm_macro_scan.l,v 1.3 2005/12/11 12:22:18 christos Exp $ */
3 
4 /*
5  * Sub-Lexical Analyzer for macro invokation in
6  * the Aic7xxx SCSI Host adapter sequencer assembler.
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  * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_macro_scan.l,v 1.4 2002/09/27 03:23:02 gibbs Exp $
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 #ifdef __linux__
55 #include "../queue.h"
56 #else
57 #include <sys/queue.h>
58 #endif
59 
60 #include "aicasm.h"
61 #include "aicasm_symbol.h"
62 #include "aicasm_macro_gram.h"
63 
64 #define MAX_STR_CONST 4096
65 static char string_buf[MAX_STR_CONST];
66 static char *string_buf_ptr;
67 static int  parren_count;
68 static char buf[255];
69 %}
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 <ARGLIST>{SPACE}    ;
82 <ARGLIST>\(                   {
83                                         parren_count++;
84                                         if (parren_count == 1) {
85                                                   string_buf_ptr = string_buf;
86                                                   return ('(');
87                                         }
88                                         *string_buf_ptr++ = '(';
89                               }
90 <ARGLIST>\)                   {
91                                         if (parren_count == 1) {
92                                                   if (string_buf_ptr != string_buf) {
93                                                             /*
94                                                              * Return an argument and
95                                                              * rescan this parren so we
96                                                              * can return it as well.
97                                                              */
98                                                             *string_buf_ptr = '\0';
99                                                             mmlval.str = string_buf;
100                                                             string_buf_ptr = string_buf;
101                                                             unput(')');
102                                                             return T_ARG;
103                                                   }
104                                                   BEGIN INITIAL;
105                                                   return (')');
106                                         }
107                                         parren_count--;
108                                         *string_buf_ptr++ = ')';
109                               }
110 <ARGLIST>{MCARG}    {
111                                         char *yptr;
112 
113                                         yptr = yytext;
114                                         while (*yptr)
115                                                   *string_buf_ptr++ = *yptr++;
116                               }
117 <ARGLIST>\,                   {
118                                         if (string_buf_ptr != string_buf) {
119                                                   /*
120                                                    * Return an argument and
121                                                    * rescan this comma so we
122                                                    * can return it as well.
123                                                    */
124                                                   *string_buf_ptr = '\0';
125                                                   mmlval.str = string_buf;
126                                                   string_buf_ptr = string_buf;
127                                                   unput(',');
128                                                   return T_ARG;
129                                         }
130                                         return ',';
131                               }
132 {WORD}[(]           {
133                                         /* May be a symbol or a macro invocation. */
134                                         mmlval.sym = symtable_get(yytext);
135                                         if (mmlval.sym->type != MACRO) {
136                                                   stop("Expecting Macro Name",
137                                                        EX_DATAERR);
138                                         }
139                                         unput('(');
140                                         parren_count = 0;
141                                         BEGIN ARGLIST;
142                                         return T_SYMBOL;
143                               }
144 .                             {
145                                         snprintf(buf, sizeof(buf), "Invalid character "
146                                                    "'%c'", yytext[0]);
147                                         stop(buf, EX_DATAERR);
148                               }
149 %%
150 
151 int
152 mmwrap()
153 {
154           stop("EOF encountered in macro call", EX_DATAERR);
155 }
156