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