11da177e4SLinus Torvalds %{ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Sub-Lexical Analyzer for macro invokation in 41da177e4SLinus Torvalds * the Aic7xxx SCSI Host adapter sequencer assembler. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (c) 2001 Adaptec Inc. 71da177e4SLinus Torvalds * All rights reserved. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or without 101da177e4SLinus Torvalds * modification, are permitted provided that the following conditions 111da177e4SLinus Torvalds * are met: 121da177e4SLinus Torvalds * 1. Redistributions of source code must retain the above copyright 131da177e4SLinus Torvalds * notice, this list of conditions, and the following disclaimer, 141da177e4SLinus Torvalds * without modification. 151da177e4SLinus Torvalds * 2. Redistributions in binary form must reproduce at minimum a disclaimer 161da177e4SLinus Torvalds * substantially similar to the "NO WARRANTY" disclaimer below 171da177e4SLinus Torvalds * ("Disclaimer") and any redistribution must be conditioned upon 181da177e4SLinus Torvalds * including a substantially similar Disclaimer requirement for further 191da177e4SLinus Torvalds * binary redistribution. 201da177e4SLinus Torvalds * 3. Neither the names of the above-listed copyright holders nor the names 211da177e4SLinus Torvalds * of any contributors may be used to endorse or promote products derived 221da177e4SLinus Torvalds * from this software without specific prior written permission. 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * Alternatively, this software may be distributed under the terms of the 251da177e4SLinus Torvalds * GNU General Public License ("GPL") version 2 as published by the Free 261da177e4SLinus Torvalds * Software Foundation. 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * NO WARRANTY 291da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 301da177e4SLinus Torvalds * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 311da177e4SLinus Torvalds * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 321da177e4SLinus Torvalds * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 331da177e4SLinus Torvalds * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 341da177e4SLinus Torvalds * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 351da177e4SLinus Torvalds * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 361da177e4SLinus Torvalds * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 371da177e4SLinus Torvalds * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 381da177e4SLinus Torvalds * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 391da177e4SLinus Torvalds * POSSIBILITY OF SUCH DAMAGES. 401da177e4SLinus Torvalds * 411da177e4SLinus Torvalds * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_macro_scan.l#8 $ 421da177e4SLinus Torvalds * 431da177e4SLinus Torvalds * $FreeBSD$ 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <sys/types.h> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <inttypes.h> 491da177e4SLinus Torvalds #include <limits.h> 501da177e4SLinus Torvalds #include <regex.h> 511da177e4SLinus Torvalds #include <stdio.h> 521da177e4SLinus Torvalds #include <string.h> 531da177e4SLinus Torvalds #include <sysexits.h> 541da177e4SLinus Torvalds #include "../queue.h" 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include "aicasm.h" 571da177e4SLinus Torvalds #include "aicasm_symbol.h" 581da177e4SLinus Torvalds #include "aicasm_macro_gram.h" 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #define MAX_STR_CONST 4096 611da177e4SLinus Torvalds static char string_buf[MAX_STR_CONST]; 621da177e4SLinus Torvalds static char *string_buf_ptr; 631da177e4SLinus Torvalds static int parren_count; 641da177e4SLinus Torvalds static char buf[255]; 65*fc2adcfdSHannes Reinecke int mmlineno; 661da177e4SLinus Torvalds %} 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds WORD [A-Za-z_][-A-Za-z_0-9]* 691da177e4SLinus Torvalds SPACE [ \t]+ 701da177e4SLinus Torvalds MCARG [^(), \t]+ 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds %x ARGLIST 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds %% 751da177e4SLinus Torvalds \n { 76*fc2adcfdSHannes Reinecke ++mmlineno; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds \r ; 791da177e4SLinus Torvalds <ARGLIST>{SPACE} ; 801da177e4SLinus Torvalds <ARGLIST>\( { 811da177e4SLinus Torvalds parren_count++; 821da177e4SLinus Torvalds if (parren_count == 1) { 831da177e4SLinus Torvalds string_buf_ptr = string_buf; 841da177e4SLinus Torvalds return ('('); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds *string_buf_ptr++ = '('; 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds <ARGLIST>\) { 891da177e4SLinus Torvalds if (parren_count == 1) { 901da177e4SLinus Torvalds if (string_buf_ptr != string_buf) { 911da177e4SLinus Torvalds /* 921da177e4SLinus Torvalds * Return an argument and 931da177e4SLinus Torvalds * rescan this parren so we 941da177e4SLinus Torvalds * can return it as well. 951da177e4SLinus Torvalds */ 961da177e4SLinus Torvalds *string_buf_ptr = '\0'; 971da177e4SLinus Torvalds mmlval.str = string_buf; 981da177e4SLinus Torvalds string_buf_ptr = string_buf; 991da177e4SLinus Torvalds unput(')'); 1001da177e4SLinus Torvalds return T_ARG; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds BEGIN INITIAL; 1031da177e4SLinus Torvalds return (')'); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds parren_count--; 1061da177e4SLinus Torvalds *string_buf_ptr++ = ')'; 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds <ARGLIST>{MCARG} { 1091da177e4SLinus Torvalds char *yptr; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds yptr = mmtext; 1121da177e4SLinus Torvalds while (*yptr) 1131da177e4SLinus Torvalds *string_buf_ptr++ = *yptr++; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds <ARGLIST>\, { 1161da177e4SLinus Torvalds if (string_buf_ptr != string_buf) { 1171da177e4SLinus Torvalds /* 1181da177e4SLinus Torvalds * Return an argument and 1191da177e4SLinus Torvalds * rescan this comma so we 1201da177e4SLinus Torvalds * can return it as well. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds *string_buf_ptr = '\0'; 1231da177e4SLinus Torvalds mmlval.str = string_buf; 1241da177e4SLinus Torvalds string_buf_ptr = string_buf; 1251da177e4SLinus Torvalds unput(','); 1261da177e4SLinus Torvalds return T_ARG; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds return ','; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds {WORD}[(] { 1311da177e4SLinus Torvalds /* May be a symbol or a macro invocation. */ 1321da177e4SLinus Torvalds mmlval.sym = symtable_get(mmtext); 1331da177e4SLinus Torvalds if (mmlval.sym->type != MACRO) { 1341da177e4SLinus Torvalds stop("Expecting Macro Name", 1351da177e4SLinus Torvalds EX_DATAERR); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds unput('('); 1381da177e4SLinus Torvalds parren_count = 0; 1391da177e4SLinus Torvalds BEGIN ARGLIST; 1401da177e4SLinus Torvalds return T_SYMBOL; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds . { 1431da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Invalid character " 1441da177e4SLinus Torvalds "'%c'", mmtext[0]); 1451da177e4SLinus Torvalds stop(buf, EX_DATAERR); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds %% 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds int 1501da177e4SLinus Torvalds mmwrap() 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds stop("EOF encountered in macro call", EX_DATAERR); 1531da177e4SLinus Torvalds } 154