xref: /freebsd/usr.bin/mkesdb/lex.l (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /* $NetBSD: lex.l,v 1.3 2006/02/09 22:03:15 dogcow Exp $	*/
2 
3 %{
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * Copyright (c)2003 Citrus Project,
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/endian.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 
37 #include <assert.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "ldef.h"
45 #include "yacc.h"
46 
47 #define YY_DECL int yylex(void)
48 
49 int linenumber = 1;
50 %}
51 %option	noinput
52 %option	nounput
53 
54 %x	COMMENT
55 
56 %%
57 
58 [ \t]+	{ }
59 #.*[\n]|"//".*[\n]|[\n]	{ linenumber++; return (R_LN); }
60 
61 "/*"		{ BEGIN COMMENT; }
62 <COMMENT>"*/"	{ BEGIN 0; }
63 <COMMENT>[\n]	{ linenumber++; }
64 <COMMENT>.	{ }
65 <COMMENT><<EOF>>	{
66 		yyerror("unexpected file end (unterminated comment)\n");
67 		exit(1);
68 	}
69 
70 ([1-9][0-9]*)|(0[0-9]*)|(0[xX][0-9A-Fa-f]+)	{
71 		yylval.i_value = strtoul(yytext, NULL, 0);
72 		return (L_IMM);
73 	}
74 
75 "NAME"		{ return (R_NAME); }
76 "ENCODING"	{ return (R_ENCODING); }
77 "VARIABLE"	{ return (R_VARIABLE); }
78 "DEFCSID"	{ return (R_DEFCSID); }
79 "INVALID"	{ return (R_INVALID); }
80 
81 \"([^\"\n]*(\\\")?)*\"|\'([^\'\n]*(\\\')?)*\'	{
82 		size_t len;
83 
84 		len = strlen(yytext);
85 		yylval.s_value = malloc(len - 1);
86 		strlcpy(yylval.s_value, yytext + 1, len - 1);
87 		return (L_STRING);
88 	}
89 [^ =/\-0-9\t\n][^ \t\n]*	{
90 		yylval.s_value = strdup(yytext);
91 		return (L_STRING);
92 	}
93 
94 %%
95 
96 #ifndef yywrap
97 int
98 yywrap(void)
99 {
100 
101 	return (1);
102 }
103 #endif
104