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