Fix copyright date.
[become] / src / lexer.l
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
c758e654 3 * $Id: lexer.l,v 1.3 1998/01/12 16:46:07 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 $
c758e654 32 * Revision 1.3 1998/01/12 16:46:07 mdw
33 * Fix copyright date.
34 *
03f996bd 35 * Revision 1.2 1997/08/04 10:24:23 mdw
36 * Sources placed under CVS control.
37 *
38 * Revision 1.1 1997/07/21 13:47:48 mdw
c4f2d992 39 * Initial revision
40 *
41 */
42
43/*----- Declarations section ----------------------------------------------*/
44
45/* --- Header files --- */
46
47%{
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51
52#include "become.h"
53#include "lexer.h"
54#include "parser.h"
55#include "utils.h"
56%}
57
58/* --- Start conditions --- */
59
60%x s_KEYWORD
61%x s_NORMAL
62%x s_STRING
63
64/* --- A handy static buffer --- */
65
66 static char lex__buff[4096];
67 static char *lex__ptr;
68
69/* --- Line number --- */
70
71 int lex_line;
72
73%%
74
75 /*---- Main scanner definition -------------------------------------------*/
76
77 /* --- Comments --- */
78
79<INITIAL,s_KEYWORD,s_NORMAL>{
80 "#".*\n lex_line++;
81
82 /* --- Whitespace --- */
83
84 [ \t] /* munch */
85 \n lex_line++;
86}
87
88 /* --- Keywords --- */
89
90<INITIAL,s_KEYWORD>{
91 user BEGIN(s_NORMAL); return (USER);
92 command BEGIN(s_NORMAL); return (COMMAND);
93 host BEGIN(s_NORMAL); return (HOST);
94 allow BEGIN(s_NORMAL); return (ALLOW);
95 port BEGIN(s_NORMAL); return (PORT);
96 keyfile BEGIN(s_NORMAL); return (KEYFILE);
97 . BEGIN(s_NORMAL); return (BADTOKEN);
98}
99 /* --- Other sorts of tokens --- */
100
101<s_NORMAL>{
102 [0-9]* yylval.i = atoi(yytext); return (INT);
103 [a-zA-Z_][a-zA-Z_0-9]* yylval.s = yytext; return (WORD);
104 \" BEGIN(s_STRING); lex__ptr = lex__buff;
105 ";" BEGIN(s_KEYWORD); return (';');
106 "->" return (ARROW);
107 . return (yytext[0]);
108}
109
110 /* --- Strings and things --- *
111 *
112 * Be a little careful about buffer overflows here.
113 */
114
115<s_STRING>{
116 \\. {
117 if (lex__ptr >
118 lex__buff + sizeof(lex__buff) - 8) {
119 moan("string too long at line %i",
120 lex_line);
121 *lex__ptr++ = 0;
122 yylval.s = lex__buff;
123 BEGIN(s_NORMAL);
124 return (STRING);
125 }
126 *lex__ptr++ = yytext[1];
127 }
128 \" {
129 *lex__ptr++ = 0;
130 yylval.s = lex__buff;
131 BEGIN(s_NORMAL);
132 return (STRING);
133 }
134 \n |
135 <<EOF>> {
136 moan("missing `\"', inserted at line %i",
137 lex_line);
138 lex_line++;
139 *lex__ptr++ = 0;
140 yylval.s = lex__buff;
141 BEGIN(s_NORMAL);
142 return (STRING);
143 }
144 . {
145 if (lex__ptr >
146 lex__buff + sizeof(lex__buff) - 8) {
147 moan("string too long at line %i",
148 lex_line);
149 *lex__ptr++ = 0;
150 yylval.s = lex__buff;
151 BEGIN(s_NORMAL);
152 return (STRING);
153 }
154 *lex__ptr++ = yytext[0];
155 }
156}
157
158%%
159
160/*----- Support routines --------------------------------------------------*/
161
162/* --- @lexer_scan@ --- *
163 *
164 * Arguments: @FILE *fp@ = pointer to a stream object to scan
165 *
166 * Returns: ---
167 *
168 * Use: Initialises the scanner ready to parse from the given
169 * stream.
170 */
171
172void lexer_scan(FILE *fp)
173{
174 yyin = fp;
175 lex_line = 1;
176 BEGIN(INITIAL);
177}
178
179/*----- That's all, folks -------------------------------------------------*/