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