1/*	$NetBSD: slc-lex.l,v 1.1.1.2 2011/04/14 14:09:33 elric Exp $	*/
2
3%{
4/*
5 * Copyright (c) 2004 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
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 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of the Institute nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/* Id */
38
39#undef ECHO
40
41#include <stdio.h>
42#include <string.h>
43#include <stdarg.h>
44#include <stdlib.h>
45#include "slc.h"
46#include "slc-gram.h"
47unsigned lineno = 1;
48
49static void handle_comment(void);
50static char * handle_string(void);
51
52#define YY_NO_UNPUT
53
54#undef ECHO
55
56%}
57
58%option nounput
59
60%%
61[A-Za-z][-A-Za-z0-9_]*	{
62			  yylval.string = strdup ((const char *)yytext);
63			  return LITERAL;
64			}
65"\""			{ yylval.string = handle_string(); return STRING; }
66\n			{ ++lineno; }
67\/\*			{ handle_comment(); }
68[={}]			{ return *yytext; }
69[ \t]			;
70%%
71
72void
73error_message (const char *format, ...)
74{
75     va_list args;
76
77     va_start (args, format);
78     fprintf (stderr, "%s:%d: ", filename, lineno);
79     vfprintf (stderr, format, args);
80     va_end (args);
81     error_flag++;
82}
83
84void
85yyerror (char *s)
86{
87    error_message("%s\n", s);
88}
89
90static void
91handle_comment(void)
92{
93    int c;
94    int start_lineno = lineno;
95    int level = 1;
96    int seen_star = 0;
97    int seen_slash = 0;
98    while((c = input()) != EOF) {
99	if(c == '/') {
100	    if(seen_star) {
101		if(--level == 0)
102		    return;
103		seen_star = 0;
104		continue;
105	    }
106	    seen_slash = 1;
107	    continue;
108	} else if(c == '*') {
109	    if(seen_slash) {
110		level++;
111		seen_star = seen_slash = 0;
112		continue;
113	    }
114	    seen_star = 1;
115	    continue;
116	}
117	seen_star = seen_slash = 0;
118	if(c == '\n') {
119	    lineno++;
120	    continue;
121	}
122    }
123    if(c == EOF)
124	error_message("unterminated comment, possibly started on line %d\n", start_lineno);
125}
126
127static char *
128handle_string(void)
129{
130    char x[1024];
131    int i = 0;
132    int c;
133    int quote = 0;
134    while((c = input()) != EOF){
135	if(quote) {
136	    x[i++] = '\\';
137	    x[i++] = c;
138	    quote = 0;
139	    continue;
140	}
141	if(c == '\n'){
142	    error_message("unterminated string");
143	    lineno++;
144	    break;
145	}
146	if(c == '\\'){
147	    quote++;
148	    continue;
149	}
150	if(c == '\"')
151	    break;
152	x[i++] = c;
153    }
154    x[i] = '\0';
155    return strdup(x);
156}
157
158int
159yywrap ()
160{
161     return 1;
162}
163