195376Sgibbs%{
2139749Simp/*-
395376Sgibbs * Sub-Lexical Analyzer for macro invokation in
495376Sgibbs * the Aic7xxx SCSI Host adapter sequencer assembler.
595376Sgibbs *
695376Sgibbs * Copyright (c) 2001 Adaptec Inc.
795376Sgibbs * All rights reserved.
895376Sgibbs *
995376Sgibbs * Redistribution and use in source and binary forms, with or without
1095376Sgibbs * modification, are permitted provided that the following conditions
1195376Sgibbs * are met:
1295376Sgibbs * 1. Redistributions of source code must retain the above copyright
1395376Sgibbs *    notice, this list of conditions, and the following disclaimer,
1495376Sgibbs *    without modification.
1595376Sgibbs * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1695376Sgibbs *    substantially similar to the "NO WARRANTY" disclaimer below
1795376Sgibbs *    ("Disclaimer") and any redistribution must be conditioned upon
1895376Sgibbs *    including a substantially similar Disclaimer requirement for further
1995376Sgibbs *    binary redistribution.
2095376Sgibbs * 3. Neither the names of the above-listed copyright holders nor the names
2195376Sgibbs *    of any contributors may be used to endorse or promote products derived
2295376Sgibbs *    from this software without specific prior written permission.
2395376Sgibbs *
2495376Sgibbs * Alternatively, this software may be distributed under the terms of the
2595376Sgibbs * GNU General Public License ("GPL") version 2 as published by the Free
2695376Sgibbs * Software Foundation.
2795376Sgibbs *
2895376Sgibbs * NO WARRANTY
2995376Sgibbs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3095376Sgibbs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3195376Sgibbs * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3295376Sgibbs * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3395376Sgibbs * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3495376Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3595376Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3695376Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3795376Sgibbs * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3895376Sgibbs * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3995376Sgibbs * POSSIBILITY OF SUCH DAMAGES.
4095376Sgibbs *
41123577Sgibbs * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_macro_scan.l#8 $
4295376Sgibbs *
4395376Sgibbs * $FreeBSD$
4495376Sgibbs */
4595376Sgibbs
4695376Sgibbs#include <sys/types.h>
4795376Sgibbs
48104028Sgibbs#include <inttypes.h>
4995376Sgibbs#include <limits.h>
5095376Sgibbs#include <regex.h>
5195376Sgibbs#include <stdio.h>
5295376Sgibbs#include <string.h>
5395376Sgibbs#include <sysexits.h>
5495376Sgibbs#include <sys/queue.h>
5595376Sgibbs
5695376Sgibbs#include "aicasm.h"
5795376Sgibbs#include "aicasm_symbol.h"
5895376Sgibbs#include "aicasm_macro_gram.h"
5995376Sgibbs
6095376Sgibbs#define MAX_STR_CONST 4096
6195376Sgibbsstatic char string_buf[MAX_STR_CONST];
6295376Sgibbsstatic char *string_buf_ptr;
6395376Sgibbsstatic int  parren_count;
64193244Sdelphijstatic char msgbuf[255];
65193244Sdelphij
66193244Sdelphijextern int mmlex(void);
6795376Sgibbs%}
6895376Sgibbs
69229101Sdim%option noinput
70229101Sdim
7195376SgibbsWORD		[A-Za-z_][-A-Za-z_0-9]*
7295376SgibbsSPACE		[ \t]+
7395376SgibbsMCARG		[^(), \t]+
7495376Sgibbs
7595376Sgibbs%x ARGLIST
7695376Sgibbs
7795376Sgibbs%%
7895376Sgibbs\n			{
7995376Sgibbs				++yylineno;
8095376Sgibbs			}
81123577Sgibbs\r			;
8295376Sgibbs<ARGLIST>{SPACE}	;
8395376Sgibbs<ARGLIST>\(		{
8495376Sgibbs				parren_count++;
8595376Sgibbs				if (parren_count == 1) {
8695376Sgibbs					string_buf_ptr = string_buf;
8795376Sgibbs					return ('(');
8895376Sgibbs				}
8995376Sgibbs				*string_buf_ptr++ = '(';
9095376Sgibbs			}
9195376Sgibbs<ARGLIST>\)		{
9295376Sgibbs				if (parren_count == 1) {
9395376Sgibbs					if (string_buf_ptr != string_buf) {
9495376Sgibbs						/*
9595376Sgibbs						 * Return an argument and
9695376Sgibbs						 * rescan this parren so we
9795376Sgibbs						 * can return it as well.
9895376Sgibbs						 */
9995376Sgibbs						*string_buf_ptr = '\0';
10095376Sgibbs						mmlval.str = string_buf;
10195376Sgibbs						string_buf_ptr = string_buf;
10295376Sgibbs						unput(')');
10395376Sgibbs						return T_ARG;
10495376Sgibbs					}
10595376Sgibbs					BEGIN INITIAL;
10695376Sgibbs					return (')');
10795376Sgibbs				}
10895376Sgibbs				parren_count--;
10995376Sgibbs				*string_buf_ptr++ = ')';
11095376Sgibbs			}
11195376Sgibbs<ARGLIST>{MCARG}	{
11295376Sgibbs				char *yptr;
11395376Sgibbs
11495376Sgibbs				yptr = mmtext;
11595376Sgibbs				while (*yptr)
11695376Sgibbs					*string_buf_ptr++ = *yptr++;
11795376Sgibbs			}
11895376Sgibbs<ARGLIST>\,		{
11995376Sgibbs				if (string_buf_ptr != string_buf) {
12095376Sgibbs					/*
12195376Sgibbs					 * Return an argument and
12295376Sgibbs					 * rescan this comma so we
12395376Sgibbs					 * can return it as well.
12495376Sgibbs					 */
12595376Sgibbs					*string_buf_ptr = '\0';
12695376Sgibbs					mmlval.str = string_buf;
12795376Sgibbs					string_buf_ptr = string_buf;
12895376Sgibbs					unput(',');
12995376Sgibbs					return T_ARG;
13095376Sgibbs				}
13195376Sgibbs				return ',';
13295376Sgibbs			}
13395376Sgibbs{WORD}[(]		{
13495376Sgibbs				/* May be a symbol or a macro invocation. */
13595376Sgibbs				mmlval.sym = symtable_get(mmtext);
13695376Sgibbs				if (mmlval.sym->type != MACRO) {
13795376Sgibbs					stop("Expecting Macro Name",
13895376Sgibbs					     EX_DATAERR);
13995376Sgibbs				}
14095376Sgibbs				unput('(');
14195376Sgibbs				parren_count = 0;
14295376Sgibbs				BEGIN ARGLIST;
14395376Sgibbs				return T_SYMBOL;
14495376Sgibbs			}
14595376Sgibbs.			{
146193244Sdelphij				snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
14795376Sgibbs					 "'%c'", mmtext[0]);
148193244Sdelphij				stop(msgbuf, EX_DATAERR);
14995376Sgibbs			}
15095376Sgibbs%%
15195376Sgibbs
15295376Sgibbsint
153201261Sedmmwrap(void)
15495376Sgibbs{
15595376Sgibbs	stop("EOF encountered in macro call", EX_DATAERR);
156193244Sdelphij	return (1);
15795376Sgibbs}
158