slc-lex.l revision 178826
14Srgrimes%{ 2509Srgrimes/* 32823Sjkh * Copyright (c) 2004 Kungliga Tekniska H�gskolan 44Srgrimes * (Royal Institute of Technology, Stockholm, Sweden). 5509Srgrimes * All rights reserved. 6509Srgrimes * 74Srgrimes * Redistribution and use in source and binary forms, with or without 84Srgrimes * modification, are permitted provided that the following conditions 94Srgrimes * are met: 104Srgrimes * 114Srgrimes * 1. Redistributions of source code must retain the above copyright 124Srgrimes * notice, this list of conditions and the following disclaimer. 134Srgrimes * 144Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 154Srgrimes * notice, this list of conditions and the following disclaimer in the 164Srgrimes * documentation and/or other materials provided with the distribution. 174Srgrimes * 184Srgrimes * 3. Neither the name of the Institute nor the names of its contributors 194Srgrimes * may be used to endorse or promote products derived from this software 204Srgrimes * without specific prior written permission. 214Srgrimes * 224Srgrimes * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 234Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 244Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 254Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 264Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27509Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 284Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 292056Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 302056Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 312056Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 324Srgrimes * SUCH DAMAGE. 332056Swollman */ 342056Swollman 354Srgrimes/* $Id: slc-lex.l 15118 2005-05-10 22:19:01Z lha $ */ 362112Swollman 37798Swollman#undef ECHO 382408Sbde 392408Sbde#include <stdio.h> 40798Swollman#include <string.h> 41798Swollman#include <stdarg.h> 42798Swollman#include <stdlib.h> 43798Swollman#include "slc.h" 44712Swollman#include "slc-gram.h" 454Srgrimesunsigned lineno = 1; 462408Sbde 474Srgrimesstatic void handle_comment(void); 48712Swollmanstatic char * handle_string(void); 49974Sdg 504Srgrimes#define YY_NO_UNPUT 514Srgrimes 524Srgrimes#undef ECHO 532408Sbde 542408Sbde%} 554Srgrimes%% 564Srgrimes[A-Za-z][-A-Za-z0-9_]* { 574Srgrimes yylval.string = strdup ((const char *)yytext); 582408Sbde return LITERAL; 592408Sbde } 602408Sbde"\"" { yylval.string = handle_string(); return STRING; } 612408Sbde\n { ++lineno; } 622408Sbde\/\* { handle_comment(); } 632408Sbde[={}] { return *yytext; } 641549Srgrimes[ \t] ; 654Srgrimes%% 661549Srgrimes 674Srgrimesvoid 681891Sdgerror_message (const char *format, ...) 694Srgrimes{ 70509Srgrimes va_list args; 71509Srgrimes 72509Srgrimes va_start (args, format); 734Srgrimes fprintf (stderr, "%s:%d: ", filename, lineno); 744Srgrimes vfprintf (stderr, format, args); 75509Srgrimes va_end (args); 764Srgrimes error_flag++; 774Srgrimes} 784Srgrimes 794Srgrimesvoid 804Srgrimesyyerror (char *s) 814Srgrimes{ 824Srgrimes error_message("%s\n", s); 834Srgrimes} 844Srgrimes 852232Sjkhstatic void 862232Sjkhhandle_comment(void) 872232Sjkh{ 882232Sjkh int c; 892232Sjkh int start_lineno = lineno; 902232Sjkh int level = 1; 912232Sjkh int seen_star = 0; 922239Sjkh int seen_slash = 0; 932239Sjkh while((c = input()) != EOF) { 942232Sjkh if(c == '/') { 952232Sjkh if(seen_star) { 962232Sjkh if(--level == 0) 972232Sjkh return; 982239Sjkh seen_star = 0; 991549Srgrimes continue; 1002232Sjkh } 1012514Sjkh seen_slash = 1; 1022232Sjkh continue; 1032232Sjkh } 1042232Sjkh if(seen_star && c == '/') { 1052232Sjkh if(--level == 0) 1062232Sjkh return; 1074Srgrimes seen_star = 0; 1081569Srgrimes continue; 109715Swollman } 1104Srgrimes if(c == '*') { 111715Swollman if(seen_slash) { 112715Swollman level++; 113715Swollman seen_star = seen_slash = 0; 114715Swollman continue; 115715Swollman } 1164Srgrimes seen_star = 1; 1174Srgrimes continue; 1184Srgrimes } 1194Srgrimes seen_star = seen_slash = 0; 1204Srgrimes if(c == '\n') { 1212464Sbde lineno++; 1222408Sbde continue; 1234Srgrimes } 1242408Sbde } 1252408Sbde if(c == EOF) 1262408Sbde error_message("unterminated comment, possibly started on line %d\n", start_lineno); 1272408Sbde} 128757Sdg 1294Srgrimesstatic char * 1304Srgrimeshandle_string(void) 1314Srgrimes{ 1324Srgrimes char x[1024]; 1334Srgrimes int i = 0; 1344Srgrimes int c; 1354Srgrimes int quote = 0; 1364Srgrimes while((c = input()) != EOF){ 1374Srgrimes if(quote) { 1382408Sbde x[i++] = '\\'; 1392408Sbde x[i++] = c; 1402408Sbde quote = 0; 141649Snate continue; 142715Swollman } 143715Swollman if(c == '\n'){ 144715Swollman error_message("unterminated string"); 145715Swollman lineno++; 146715Swollman break; 147715Swollman } 148715Swollman if(c == '\\'){ 1494Srgrimes quote++; 150715Swollman continue; 151715Swollman } 152715Swollman if(c == '\"') 1534Srgrimes break; 1541030Snate x[i++] = c; 1554Srgrimes } 1562408Sbde x[i] = '\0'; 1571549Srgrimes return strdup(x); 1582408Sbde} 1592408Sbde 1602408Sbdeint 1612408Sbdeyywrap () 1622408Sbde{ 1634Srgrimes return 1; 1644Srgrimes} 1654Srgrimes