Change to header file name for parser. See log for `parse.h' for
[become] / src / lexer.l
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
79fae27d 3 * $Id: lexer.l,v 1.4 1999/05/04 16:17:26 mdw Exp $
c4f2d992 4 *
5 * Lexical analyser for `become.conf' files
6 *
c758e654 7 * (c) 1998 EBI
c4f2d992 8 */
9
03f996bd 10/*----- Licensing notice --------------------------------------------------*
c4f2d992 11 *
12 * This file is part of `become'
13 *
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
03f996bd 25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
c4f2d992 27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: lexer.l,v $
79fae27d 32 * Revision 1.4 1999/05/04 16:17:26 mdw
33 * Change to header file name for parser. See log for `parse.h' for
34 * details.
35 *
36 * Revision 1.3 1998/01/12 16:46:07 mdw
c758e654 37 * Fix copyright date.
38 *
03f996bd 39 * Revision 1.2 1997/08/04 10:24:23 mdw
40 * Sources placed under CVS control.
41 *
42 * Revision 1.1 1997/07/21 13:47:48 mdw
c4f2d992 43 * Initial revision
44 *
45 */
46
47/*----- Declarations section ----------------------------------------------*/
48
49/* --- Header files --- */
50
51%{
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55
56#include "become.h"
57#include "lexer.h"
79fae27d 58#include "parse.h"
c4f2d992 59#include "utils.h"
60%}
61
62/* --- Start conditions --- */
63
64%x s_KEYWORD
65%x s_NORMAL
66%x s_STRING
67
68/* --- A handy static buffer --- */
69
70 static char lex__buff[4096];
71 static char *lex__ptr;
72
73/* --- Line number --- */
74
75 int lex_line;
76
77%%
78
79 /*---- Main scanner definition -------------------------------------------*/
80
81 /* --- Comments --- */
82
83<INITIAL,s_KEYWORD,s_NORMAL>{
84 "#".*\n lex_line++;
85
86 /* --- Whitespace --- */
87
88 [ \t] /* munch */
89 \n lex_line++;
90}
91
92 /* --- Keywords --- */
93
94<INITIAL,s_KEYWORD>{
95 user BEGIN(s_NORMAL); return (USER);
96 command BEGIN(s_NORMAL); return (COMMAND);
97 host BEGIN(s_NORMAL); return (HOST);
98 allow BEGIN(s_NORMAL); return (ALLOW);
99 port BEGIN(s_NORMAL); return (PORT);
100 keyfile BEGIN(s_NORMAL); return (KEYFILE);
101 . BEGIN(s_NORMAL); return (BADTOKEN);
102}
103 /* --- Other sorts of tokens --- */
104
105<s_NORMAL>{
106 [0-9]* yylval.i = atoi(yytext); return (INT);
107 [a-zA-Z_][a-zA-Z_0-9]* yylval.s = yytext; return (WORD);
108 \" BEGIN(s_STRING); lex__ptr = lex__buff;
109 ";" BEGIN(s_KEYWORD); return (';');
110 "->" return (ARROW);
111 . return (yytext[0]);
112}
113
114 /* --- Strings and things --- *
115 *
116 * Be a little careful about buffer overflows here.
117 */
118
119<s_STRING>{
120 \\. {
121 if (lex__ptr >
122 lex__buff + sizeof(lex__buff) - 8) {
123 moan("string too long at line %i",
124 lex_line);
125 *lex__ptr++ = 0;
126 yylval.s = lex__buff;
127 BEGIN(s_NORMAL);
128 return (STRING);
129 }
130 *lex__ptr++ = yytext[1];
131 }
132 \" {
133 *lex__ptr++ = 0;
134 yylval.s = lex__buff;
135 BEGIN(s_NORMAL);
136 return (STRING);
137 }
138 \n |
139 <<EOF>> {
140 moan("missing `\"', inserted at line %i",
141 lex_line);
142 lex_line++;
143 *lex__ptr++ = 0;
144 yylval.s = lex__buff;
145 BEGIN(s_NORMAL);
146 return (STRING);
147 }
148 . {
149 if (lex__ptr >
150 lex__buff + sizeof(lex__buff) - 8) {
151 moan("string too long at line %i",
152 lex_line);
153 *lex__ptr++ = 0;
154 yylval.s = lex__buff;
155 BEGIN(s_NORMAL);
156 return (STRING);
157 }
158 *lex__ptr++ = yytext[0];
159 }
160}
161
162%%
163
164/*----- Support routines --------------------------------------------------*/
165
166/* --- @lexer_scan@ --- *
167 *
168 * Arguments: @FILE *fp@ = pointer to a stream object to scan
169 *
170 * Returns: ---
171 *
172 * Use: Initialises the scanner ready to parse from the given
173 * stream.
174 */
175
176void lexer_scan(FILE *fp)
177{
178 yyin = fp;
179 lex_line = 1;
180 BEGIN(INITIAL);
181}
182
183/*----- That's all, folks -------------------------------------------------*/