xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/dtcompilerparser.l (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1 %{
2 /******************************************************************************
3  *
4  * Module Name: dtcompilerparser.l - Flex input file for table compiler lexer
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2023, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #include "aslcompiler.h"
46 #include "dtcompilerparser.y.h"
47 
48 
49 #define _COMPONENT          ACPI_COMPILER
50         ACPI_MODULE_NAME    ("dtcompilerscanner")
51 
52 /* handle locations */
53 
54 int DtCompilerParsercolumn = 1;
55 int DtLabelByteOffset = 0;
56 int DtCompilerParserByteOffset = 0;
57 
58 UINT32 DtTokenFirstLine = 0;
59 UINT32 DtTokenFirstColumn = 0;
60 
61 #define YY_USER_ACTION \
62             DtTokenFirstLine = DtCompilerParserlineno; \
63             DtTokenFirstColumn = DtCompilerParsercolumn; \
64             DtCompilerParsercolumn += DtCompilerParserleng; \
65             DtCompilerParserByteOffset += DtCompilerParserleng; \
66             DbgPrint (ASL_PARSE_OUTPUT,\
67                 "user action occurred. DtCompilerParserlloc.first_line: %u\n",\
68                 DtTokenFirstLine);
69 %}
70 
71 %option nounput noinput yylineno
72 
73     /* Indicates a state used for parsing multiline C comments */
74 %x ML_COMMENT
75 %x DATA_STATE
76 
77 WhiteSpace      [ \t\v\r]+
78 NewLines        [\n]+
79 
80     /* Avoid ", \n, and [] as a part of label name. These are not valid characters of a label name */
81 LabelName       [^ ":\n\[\]]([^":\n\[\]]*[^" :\n\[\]])?
82 
83     /* Avoid ", \n, \\, and [] as a part of data. These are not valid characters of data */
84 Data            [^ \\":\n\[\]]([^":\n\[\]\\]*[^" :\n\[\]\\])?
85 
86 Text            [^ ":\n][^":\n]*
87 Comment         \[[^\n\[\]]*\]
88 CommentField    {LabelName}{WhiteSpace}*:{WhiteSpace}{Comment}?$
89 
90 
91 %%
92 
93 <DATA_STATE>{WhiteSpace}"\\\n" {
94         DbgPrint(ASL_PARSE_OUTPUT,"Continuation matched\n");
95         return (DT_PARSEOP_LINE_CONTINUATION);
96     }
97 
98 ":" {
99         DbgPrint(ASL_PARSE_OUTPUT, ": Matched\n");
100         BEGIN (DATA_STATE);
101         return (':');
102     }
103 
104 <INITIAL,DATA_STATE>{WhiteSpace} { DbgPrint(ASL_PARSE_OUTPUT,"Whitespace matched\n"); }
105 
106 <INITIAL,DATA_STATE>{Comment}    { DbgPrint(ASL_PARSE_OUTPUT,"Comment matched\n"); }
107 
108 "/*"                     { BEGIN (ML_COMMENT); }
109 <ML_COMMENT>"*/"         { BEGIN (INITIAL); }
110 <ML_COMMENT>"*/\n"       { BEGIN (INITIAL); }
111 <ML_COMMENT>([^*]|\n)+|. /* Ignore */
112 "//".*                   /* Ignore */
113 
114 
115 <DATA_STATE>{Data} {
116       char *s;
117       int size = strlen (DtCompilerParsertext);
118       s=UtLocalCacheCalloc (size + 1);
119       AcpiUtSafeStrncpy (s, DtCompilerParsertext, size + 1);
120       DtCompilerParserlval.s = s;
121       DbgPrint (ASL_PARSE_OUTPUT, "Data: %s\n", s);
122       return (DT_PARSEOP_DATA);
123 }
124 
125 {CommentField}  /* ignore */
126 
127 {LabelName} {
128     char *s;
129     int size = strlen (DtCompilerParsertext);
130     s=UtLocalCacheCalloc (size + 1);
131     AcpiUtSafeStrncpy (s, DtCompilerParsertext, size + 1);
132     DtCompilerParserlval.u = (DT_TABLE_UNIT *) UtLocalCacheCalloc (sizeof (DT_TABLE_UNIT));
133     DtCompilerParserlval.u->Value = s;
134     DtCompilerParserlval.u->Line = DtCompilerParserlineno;
135     DtCompilerParserlval.u->Column = DtCompilerParsercolumn;
136     DtLabelByteOffset = DtCompilerParserByteOffset;
137     DbgPrint (ASL_PARSE_OUTPUT, "Label: %s\n", s);
138     return (DT_PARSEOP_LABEL);
139 }
140 
141 
142 <DATA_STATE>\"{Text}?\" { // remove outer quotes from the string, they are unnecessary
143     char *s;
144     int size = strlen (DtCompilerParsertext);
145     s=UtLocalCacheCalloc (size - 1);
146     AcpiUtSafeStrncpy (s, DtCompilerParsertext + 1, size - 1);
147     DtCompilerParserlval.s = s;
148     DbgPrint (ASL_PARSE_OUTPUT, "String Data: %s\n", s);
149     BEGIN (INITIAL);
150     return (DT_PARSEOP_STRING_DATA);
151 }
152 
153 
154 <INITIAL,DATA_STATE>{NewLines} {
155     DbgPrint(ASL_PARSE_OUTPUT,
156         "Newline matched (data state). Current line number: %u\n",DtCompilerParserlineno);
157     BEGIN (INITIAL); DtCompilerParsercolumn = 1;
158 }
159 
160 
161 %%
162 
163 
164 /*
165  * Local support functions
166  */
167 
168 void
169 DtCompilerInitLexer (
170     FILE                *inFile)
171 {
172     yyin = inFile;
173 }
174 
175 void
176 DtCompilerTerminateLexer (
177     void)
178 {
179     /*
180      * Flex/Bison increments the lineno for the EOF so decrement by 1 to get
181      * the correct number of lines.
182      */
183     AslGbl_CurrentLineNumber = DtCompilerParserlineno - 1;
184     AslGbl_InputByteCount = DtCompilerParserByteOffset;
185 }
186