slc-lex.l revision 178825
10Sstevel@tonic-gate%{
20Sstevel@tonic-gate/*
30Sstevel@tonic-gate * Copyright (c) 2004 Kungliga Tekniska H�gskolan
40Sstevel@tonic-gate * (Royal Institute of Technology, Stockholm, Sweden).
50Sstevel@tonic-gate * All rights reserved.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
80Sstevel@tonic-gate * modification, are permitted provided that the following conditions
90Sstevel@tonic-gate * are met:
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate *    notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
150Sstevel@tonic-gate *    notice, this list of conditions and the following disclaimer in the
160Sstevel@tonic-gate *    documentation and/or other materials provided with the distribution.
170Sstevel@tonic-gate *
180Sstevel@tonic-gate * 3. Neither the name of the Institute nor the names of its contributors
190Sstevel@tonic-gate *    may be used to endorse or promote products derived from this software
200Sstevel@tonic-gate *    without specific prior written permission.
210Sstevel@tonic-gate *
220Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
230Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
240Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
250Sstevel@tonic-gate * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
260Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
270Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
280Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
290Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
300Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
310Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
320Sstevel@tonic-gate * SUCH DAMAGE.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate/* $Id: slc-lex.l 15118 2005-05-10 22:19:01Z lha $ */
360Sstevel@tonic-gate
370Sstevel@tonic-gate#undef ECHO
380Sstevel@tonic-gate
390Sstevel@tonic-gate#include <stdio.h>
400Sstevel@tonic-gate#include <string.h>
410Sstevel@tonic-gate#include <stdarg.h>
420Sstevel@tonic-gate#include <stdlib.h>
430Sstevel@tonic-gate#include "slc.h"
440Sstevel@tonic-gate#include "slc-gram.h"
450Sstevel@tonic-gateunsigned lineno = 1;
460Sstevel@tonic-gate
470Sstevel@tonic-gatestatic void handle_comment(void);
480Sstevel@tonic-gatestatic char * handle_string(void);
490Sstevel@tonic-gate
500Sstevel@tonic-gate#define YY_NO_UNPUT
510Sstevel@tonic-gate
520Sstevel@tonic-gate#undef ECHO
530Sstevel@tonic-gate
540Sstevel@tonic-gate%}
550Sstevel@tonic-gate%%
560Sstevel@tonic-gate[A-Za-z][-A-Za-z0-9_]*	{
570Sstevel@tonic-gate			  yylval.string = strdup ((const char *)yytext);
580Sstevel@tonic-gate			  return LITERAL;
590Sstevel@tonic-gate			}
600Sstevel@tonic-gate"\""			{ yylval.string = handle_string(); return STRING; }
610Sstevel@tonic-gate\n			{ ++lineno; }
620Sstevel@tonic-gate\/\*			{ handle_comment(); }
630Sstevel@tonic-gate[={}]			{ return *yytext; }
640Sstevel@tonic-gate[ \t]			;
650Sstevel@tonic-gate%%
660Sstevel@tonic-gate
670Sstevel@tonic-gatevoid
680Sstevel@tonic-gateerror_message (const char *format, ...)
690Sstevel@tonic-gate{
700Sstevel@tonic-gate     va_list args;
710Sstevel@tonic-gate
720Sstevel@tonic-gate     va_start (args, format);
730Sstevel@tonic-gate     fprintf (stderr, "%s:%d: ", filename, lineno);
740Sstevel@tonic-gate     vfprintf (stderr, format, args);
750Sstevel@tonic-gate     va_end (args);
760Sstevel@tonic-gate     error_flag++;
770Sstevel@tonic-gate}
780Sstevel@tonic-gate
790Sstevel@tonic-gatevoid
802139Sjp161948yyerror (char *s)
810Sstevel@tonic-gate{
820Sstevel@tonic-gate    error_message("%s\n", s);
830Sstevel@tonic-gate}
840Sstevel@tonic-gate
850Sstevel@tonic-gatestatic void
860Sstevel@tonic-gatehandle_comment(void)
870Sstevel@tonic-gate{
880Sstevel@tonic-gate    int c;
890Sstevel@tonic-gate    int start_lineno = lineno;
900Sstevel@tonic-gate    int level = 1;
910Sstevel@tonic-gate    int seen_star = 0;
920Sstevel@tonic-gate    int seen_slash = 0;
930Sstevel@tonic-gate    while((c = input()) != EOF) {
940Sstevel@tonic-gate	if(c == '/') {
950Sstevel@tonic-gate	    if(seen_star) {
960Sstevel@tonic-gate		if(--level == 0)
970Sstevel@tonic-gate		    return;
980Sstevel@tonic-gate		seen_star = 0;
990Sstevel@tonic-gate		continue;
1000Sstevel@tonic-gate	    }
1010Sstevel@tonic-gate	    seen_slash = 1;
1020Sstevel@tonic-gate	    continue;
1030Sstevel@tonic-gate	}
1040Sstevel@tonic-gate	if(seen_star && c == '/') {
1050Sstevel@tonic-gate	    if(--level == 0)
1060Sstevel@tonic-gate		return;
1070Sstevel@tonic-gate	    seen_star = 0;
1080Sstevel@tonic-gate	    continue;
1090Sstevel@tonic-gate	}
1100Sstevel@tonic-gate	if(c == '*') {
1110Sstevel@tonic-gate	    if(seen_slash) {
1120Sstevel@tonic-gate		level++;
1130Sstevel@tonic-gate		seen_star = seen_slash = 0;
1140Sstevel@tonic-gate		continue;
1150Sstevel@tonic-gate	    }
1160Sstevel@tonic-gate	    seen_star = 1;
1170Sstevel@tonic-gate	    continue;
1180Sstevel@tonic-gate	}
1190Sstevel@tonic-gate	seen_star = seen_slash = 0;
1200Sstevel@tonic-gate	if(c == '\n') {
1210Sstevel@tonic-gate	    lineno++;
1220Sstevel@tonic-gate	    continue;
1230Sstevel@tonic-gate	}
1240Sstevel@tonic-gate    }
1250Sstevel@tonic-gate    if(c == EOF)
1260Sstevel@tonic-gate	error_message("unterminated comment, possibly started on line %d\n", start_lineno);
1270Sstevel@tonic-gate}
1280Sstevel@tonic-gate
1290Sstevel@tonic-gatestatic char *
1300Sstevel@tonic-gatehandle_string(void)
1310Sstevel@tonic-gate{
1320Sstevel@tonic-gate    char x[1024];
1330Sstevel@tonic-gate    int i = 0;
1340Sstevel@tonic-gate    int c;
1350Sstevel@tonic-gate    int quote = 0;
1360Sstevel@tonic-gate    while((c = input()) != EOF){
1370Sstevel@tonic-gate	if(quote) {
1380Sstevel@tonic-gate	    x[i++] = '\\';
1390Sstevel@tonic-gate	    x[i++] = c;
1400Sstevel@tonic-gate	    quote = 0;
1410Sstevel@tonic-gate	    continue;
1420Sstevel@tonic-gate	}
1430Sstevel@tonic-gate	if(c == '\n'){
1440Sstevel@tonic-gate	    error_message("unterminated string");
1450Sstevel@tonic-gate	    lineno++;
1460Sstevel@tonic-gate	    break;
1470Sstevel@tonic-gate	}
1480Sstevel@tonic-gate	if(c == '\\'){
1490Sstevel@tonic-gate	    quote++;
1500Sstevel@tonic-gate	    continue;
1510Sstevel@tonic-gate	}
1520Sstevel@tonic-gate	if(c == '\"')
1530Sstevel@tonic-gate	    break;
1540Sstevel@tonic-gate	x[i++] = c;
1550Sstevel@tonic-gate    }
1560Sstevel@tonic-gate    x[i] = '\0';
1570Sstevel@tonic-gate    return strdup(x);
1580Sstevel@tonic-gate}
1590Sstevel@tonic-gate
1600Sstevel@tonic-gateint
1610Sstevel@tonic-gateyywrap ()
1620Sstevel@tonic-gate{
1630Sstevel@tonic-gate     return 1;
1640Sstevel@tonic-gate}
1650Sstevel@tonic-gate