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