xref: /netbsd/usr.bin/xlint/lint1/scan.l (revision 3a0e83fa)
1 %{
2 /* $NetBSD: scan.l,v 1.139 2023/07/13 08:40:38 rillig Exp $ */
3 
4 /*
5  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
6  * Copyright (c) 1994, 1995 Jochen Pohl
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  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by Jochen Pohl for
20  *	The NetBSD Project.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID)
38 __RCSID("$NetBSD: scan.l,v 1.139 2023/07/13 08:40:38 rillig Exp $");
39 #endif
40 
41 #include "lint1.h"
42 #include "cgram.h"
43 
44 %}
45 
46 
47 HEX	[0-9A-Fa-f]
48 EXP	([eE][+-]?[0-9]+)
49 PEXP	(p[+-]?[0-9A-Fa-f]+)
50 FSUF	([fFlL]?[i]?)
51 
52 %option nounput
53 
54 %%
55 
56 [_A-Za-z][_A-Za-z0-9]*		return lex_name(yytext, yyleng);
57 0[bB][01]+[lLuU]*		return lex_integer_constant(yytext, yyleng, 2);
58 0[0-7]*[lLuU]*			return lex_integer_constant(yytext, yyleng, 8);
59 [1-9][0-9]*[lLuU]*		return lex_integer_constant(yytext, yyleng, 10);
60 0[xX]{HEX}+[lLuU]*		return lex_integer_constant(yytext, yyleng, 16);
61 [0-9]+\.[0-9]*{EXP}?{FSUF}	|
62 [0-9]+{EXP}{FSUF}		|
63 0[xX]{HEX}+\.{HEX}*{PEXP}{FSUF}	|
64 0[xX]{HEX}+{PEXP}{FSUF}		|
65 \.[0-9]+{EXP}?{FSUF}		return lex_floating_constant(yytext, yyleng);
66 "="				return T_ASSIGN;
67 "*="				return lex_operator(T_OPASSIGN, MULASS);
68 "/="				return lex_operator(T_OPASSIGN, DIVASS);
69 "%="				return lex_operator(T_OPASSIGN, MODASS);
70 "+="				return lex_operator(T_OPASSIGN, ADDASS);
71 "-="				return lex_operator(T_OPASSIGN, SUBASS);
72 "<<="				return lex_operator(T_OPASSIGN, SHLASS);
73 ">>="				return lex_operator(T_OPASSIGN, SHRASS);
74 "&="				return lex_operator(T_OPASSIGN, ANDASS);
75 "^="				return lex_operator(T_OPASSIGN, XORASS);
76 "|="				return lex_operator(T_OPASSIGN, ORASS);
77 "||"				return T_LOGOR;
78 "&&"				return T_LOGAND;
79 "|"				return T_BITOR;
80 "&"				return T_AMPER;
81 "^"				return T_BITXOR;
82 "=="				return lex_operator(T_EQUALITY, EQ);
83 "!="				return lex_operator(T_EQUALITY, NE);
84 "<"				return lex_operator(T_RELATIONAL, LT);
85 ">"				return lex_operator(T_RELATIONAL, GT);
86 "<="				return lex_operator(T_RELATIONAL, LE);
87 ">="				return lex_operator(T_RELATIONAL, GE);
88 "<<"				return lex_operator(T_SHIFT, SHL);
89 ">>"				return lex_operator(T_SHIFT, SHR);
90 "++"				return lex_operator(T_INCDEC, INC);
91 "--"				return lex_operator(T_INCDEC, DEC);
92 "->"				return T_ARROW;
93 "."				return T_POINT;
94 "+"				return lex_operator(T_ADDITIVE, PLUS);
95 "-"				return lex_operator(T_ADDITIVE, MINUS);
96 "*"				return T_ASTERISK;
97 "/"				return lex_operator(T_MULTIPLICATIVE, DIV);
98 "%"				return lex_operator(T_MULTIPLICATIVE, MOD);
99 "!"				return T_LOGNOT;
100 "~"				return T_COMPLEMENT;
101 "\""				return lex_string();
102 "L\""				return lex_wide_string();
103 ";"				return T_SEMI;
104 "{"				return T_LBRACE;
105 "}"				return T_RBRACE;
106 ","				return T_COMMA;
107 ":"				return T_COLON;
108 "?"				return T_QUEST;
109 "["				return T_LBRACK;
110 "]"				return T_RBRACK;
111 "("				return T_LPAREN;
112 ")"				return T_RPAREN;
113 "..."				return T_ELLIPSIS;
114 "'"				return lex_character_constant();
115 "L'"				return lex_wide_character_constant();
116 ^#.*$				lex_directive(yytext);
117 \n				lex_next_line();
118 \t|" "|\f|\v			;
119 "/*"				lex_comment();
120 "//"				lex_slash_slash_comment();
121 .				lex_unknown_character(yytext[0]);
122 
123 %%
124 
125 /*
126  * In the above list of regular expressions, the tokens for character
127  * constants, string literals and comments are incomplete; they only match
128  * a prefix.  The remainder of these tokens is scanned by reading bytes
129  * directly from the input stream.
130  */
131 int
132 lex_input(void)
133 {
134 	return input();
135 }
136