aicasm_macro_scan.l revision 213765
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#8 $ 42 * 43 * $FreeBSD: head/sys/dev/aic7xxx/aicasm/aicasm_macro_scan.l 213765 2010-10-13 10:33:01Z rpaulo $ 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 YY_NO_INPUT 65#define MAX_STR_CONST 4096 66static char string_buf[MAX_STR_CONST]; 67static char *string_buf_ptr; 68static int parren_count; 69static char msgbuf[255]; 70 71extern int mmlex(void); 72%} 73 74WORD [A-Za-z_][-A-Za-z_0-9]* 75SPACE [ \t]+ 76MCARG [^(), \t]+ 77 78%x ARGLIST 79 80%% 81\n { 82 ++yylineno; 83 } 84\r ; 85<ARGLIST>{SPACE} ; 86<ARGLIST>\( { 87 parren_count++; 88 if (parren_count == 1) { 89 string_buf_ptr = string_buf; 90 return ('('); 91 } 92 *string_buf_ptr++ = '('; 93 } 94<ARGLIST>\) { 95 if (parren_count == 1) { 96 if (string_buf_ptr != string_buf) { 97 /* 98 * Return an argument and 99 * rescan this parren so we 100 * can return it as well. 101 */ 102 *string_buf_ptr = '\0'; 103 mmlval.str = string_buf; 104 string_buf_ptr = string_buf; 105 unput(')'); 106 return T_ARG; 107 } 108 BEGIN INITIAL; 109 return (')'); 110 } 111 parren_count--; 112 *string_buf_ptr++ = ')'; 113 } 114<ARGLIST>{MCARG} { 115 char *yptr; 116 117 yptr = mmtext; 118 while (*yptr) 119 *string_buf_ptr++ = *yptr++; 120 } 121<ARGLIST>\, { 122 if (string_buf_ptr != string_buf) { 123 /* 124 * Return an argument and 125 * rescan this comma so we 126 * can return it as well. 127 */ 128 *string_buf_ptr = '\0'; 129 mmlval.str = string_buf; 130 string_buf_ptr = string_buf; 131 unput(','); 132 return T_ARG; 133 } 134 return ','; 135 } 136{WORD}[(] { 137 /* May be a symbol or a macro invocation. */ 138 mmlval.sym = symtable_get(mmtext); 139 if (mmlval.sym->type != MACRO) { 140 stop("Expecting Macro Name", 141 EX_DATAERR); 142 } 143 unput('('); 144 parren_count = 0; 145 BEGIN ARGLIST; 146 return T_SYMBOL; 147 } 148. { 149 snprintf(msgbuf, sizeof(msgbuf), "Invalid character " 150 "'%c'", mmtext[0]); 151 stop(msgbuf, EX_DATAERR); 152 } 153%% 154 155int 156mmwrap(void) 157{ 158 stop("EOF encountered in macro call", EX_DATAERR); 159 return (1); 160} 161