xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/dtcompilerparser.l (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1783af925Schristos %{
2783af925Schristos /******************************************************************************
3783af925Schristos  *
4783af925Schristos  * Module Name: dtcompilerparser.l - Flex input file for table compiler lexer
5783af925Schristos  *
6783af925Schristos  *****************************************************************************/
7783af925Schristos 
8783af925Schristos /*
9*046a2985Schristos  * Copyright (C) 2000 - 2023, Intel Corp.
10783af925Schristos  * All rights reserved.
11783af925Schristos  *
12783af925Schristos  * Redistribution and use in source and binary forms, with or without
13783af925Schristos  * modification, are permitted provided that the following conditions
14783af925Schristos  * are met:
15783af925Schristos  * 1. Redistributions of source code must retain the above copyright
16783af925Schristos  *    notice, this list of conditions, and the following disclaimer,
17783af925Schristos  *    without modification.
18783af925Schristos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19783af925Schristos  *    substantially similar to the "NO WARRANTY" disclaimer below
20783af925Schristos  *    ("Disclaimer") and any redistribution must be conditioned upon
21783af925Schristos  *    including a substantially similar Disclaimer requirement for further
22783af925Schristos  *    binary redistribution.
23783af925Schristos  * 3. Neither the names of the above-listed copyright holders nor the names
24783af925Schristos  *    of any contributors may be used to endorse or promote products derived
25783af925Schristos  *    from this software without specific prior written permission.
26783af925Schristos  *
27783af925Schristos  * Alternatively, this software may be distributed under the terms of the
28783af925Schristos  * GNU General Public License ("GPL") version 2 as published by the Free
29783af925Schristos  * Software Foundation.
30783af925Schristos  *
31783af925Schristos  * NO WARRANTY
32783af925Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33783af925Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3446a330b4Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35783af925Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36783af925Schristos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37783af925Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38783af925Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39783af925Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40783af925Schristos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41783af925Schristos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42783af925Schristos  * POSSIBILITY OF SUCH DAMAGES.
43783af925Schristos  */
44783af925Schristos 
45783af925Schristos #include "aslcompiler.h"
46783af925Schristos #include "dtcompilerparser.y.h"
47783af925Schristos 
48783af925Schristos 
49783af925Schristos #define _COMPONENT          ACPI_COMPILER
50783af925Schristos         ACPI_MODULE_NAME    ("dtcompilerscanner")
51783af925Schristos 
52783af925Schristos /* handle locations */
53783af925Schristos 
54783af925Schristos int DtCompilerParsercolumn = 1;
5532aedd46Schristos int DtLabelByteOffset = 0;
56783af925Schristos int DtCompilerParserByteOffset = 0;
57783af925Schristos 
5832aedd46Schristos UINT32 DtTokenFirstLine = 0;
5932aedd46Schristos UINT32 DtTokenFirstColumn = 0;
6032aedd46Schristos 
61783af925Schristos #define YY_USER_ACTION \
6232aedd46Schristos             DtTokenFirstLine = DtCompilerParserlineno; \
6332aedd46Schristos             DtTokenFirstColumn = DtCompilerParsercolumn; \
64783af925Schristos             DtCompilerParsercolumn += DtCompilerParserleng; \
65783af925Schristos             DtCompilerParserByteOffset += DtCompilerParserleng; \
66783af925Schristos             DbgPrint (ASL_PARSE_OUTPUT,\
6732aedd46Schristos                 "user action occurred. DtCompilerParserlloc.first_line: %u\n",\
6832aedd46Schristos                 DtTokenFirstLine);
69783af925Schristos %}
70783af925Schristos 
71783af925Schristos %option nounput noinput yylineno
72783af925Schristos 
73783af925Schristos     /* Indicates a state used for parsing multiline C comments */
74783af925Schristos %x ML_COMMENT
75783af925Schristos %x DATA_STATE
76783af925Schristos 
77783af925Schristos WhiteSpace      [ \t\v\r]+
78783af925Schristos NewLines        [\n]+
79783af925Schristos 
80783af925Schristos     /* Avoid ", \n, and [] as a part of label name. These are not valid characters of a label name */
81783af925Schristos LabelName       [^ ":\n\[\]]([^":\n\[\]]*[^" :\n\[\]])?
82783af925Schristos 
83783af925Schristos     /* Avoid ", \n, \\, and [] as a part of data. These are not valid characters of data */
84783af925Schristos Data            [^ \\":\n\[\]]([^":\n\[\]\\]*[^" :\n\[\]\\])?
85783af925Schristos 
86783af925Schristos Text            [^ ":\n][^":\n]*
87783af925Schristos Comment         \[[^\n\[\]]*\]
88783af925Schristos CommentField    {LabelName}{WhiteSpace}*:{WhiteSpace}{Comment}?$
89783af925Schristos 
90783af925Schristos 
91783af925Schristos %%
92783af925Schristos 
93783af925Schristos <DATA_STATE>{WhiteSpace}"\\\n" {
94783af925Schristos         DbgPrint(ASL_PARSE_OUTPUT,"Continuation matched\n");
95783af925Schristos         return (DT_PARSEOP_LINE_CONTINUATION);
96783af925Schristos     }
97783af925Schristos 
98783af925Schristos ":" {
99783af925Schristos         DbgPrint(ASL_PARSE_OUTPUT, ": Matched\n");
100783af925Schristos         BEGIN (DATA_STATE);
101783af925Schristos         return (':');
102783af925Schristos     }
103783af925Schristos 
104783af925Schristos <INITIAL,DATA_STATE>{WhiteSpace} { DbgPrint(ASL_PARSE_OUTPUT,"Whitespace matched\n"); }
105783af925Schristos 
106783af925Schristos <INITIAL,DATA_STATE>{Comment}    { DbgPrint(ASL_PARSE_OUTPUT,"Comment matched\n"); }
107783af925Schristos 
108783af925Schristos "/*"                     { BEGIN (ML_COMMENT); }
109783af925Schristos <ML_COMMENT>"*/"         { BEGIN (INITIAL); }
110783af925Schristos <ML_COMMENT>"*/\n"       { BEGIN (INITIAL); }
111783af925Schristos <ML_COMMENT>([^*]|\n)+|. /* Ignore */
112783af925Schristos "//".*                   /* Ignore */
113783af925Schristos 
114783af925Schristos 
115783af925Schristos <DATA_STATE>{Data} {
116783af925Schristos       char *s;
117783af925Schristos       int size = strlen (DtCompilerParsertext);
118783af925Schristos       s=UtLocalCacheCalloc (size + 1);
119783af925Schristos       AcpiUtSafeStrncpy (s, DtCompilerParsertext, size + 1);
120783af925Schristos       DtCompilerParserlval.s = s;
121783af925Schristos       DbgPrint (ASL_PARSE_OUTPUT, "Data: %s\n", s);
122783af925Schristos       return (DT_PARSEOP_DATA);
123783af925Schristos }
124783af925Schristos 
125783af925Schristos {CommentField}  /* ignore */
126783af925Schristos 
127783af925Schristos {LabelName} {
128783af925Schristos     char *s;
129783af925Schristos     int size = strlen (DtCompilerParsertext);
130783af925Schristos     s=UtLocalCacheCalloc (size + 1);
131783af925Schristos     AcpiUtSafeStrncpy (s, DtCompilerParsertext, size + 1);
13232aedd46Schristos     DtCompilerParserlval.u = (DT_TABLE_UNIT *) UtLocalCacheCalloc (sizeof (DT_TABLE_UNIT));
13332aedd46Schristos     DtCompilerParserlval.u->Value = s;
13432aedd46Schristos     DtCompilerParserlval.u->Line = DtCompilerParserlineno;
13532aedd46Schristos     DtCompilerParserlval.u->Column = DtCompilerParsercolumn;
13632aedd46Schristos     DtLabelByteOffset = DtCompilerParserByteOffset;
137783af925Schristos     DbgPrint (ASL_PARSE_OUTPUT, "Label: %s\n", s);
138783af925Schristos     return (DT_PARSEOP_LABEL);
139783af925Schristos }
140783af925Schristos 
141783af925Schristos 
142783af925Schristos <DATA_STATE>\"{Text}?\" { // remove outer quotes from the string, they are unnecessary
143783af925Schristos     char *s;
144783af925Schristos     int size = strlen (DtCompilerParsertext);
145783af925Schristos     s=UtLocalCacheCalloc (size - 1);
146783af925Schristos     AcpiUtSafeStrncpy (s, DtCompilerParsertext + 1, size - 1);
147783af925Schristos     DtCompilerParserlval.s = s;
148783af925Schristos     DbgPrint (ASL_PARSE_OUTPUT, "String Data: %s\n", s);
149783af925Schristos     BEGIN (INITIAL);
150783af925Schristos     return (DT_PARSEOP_STRING_DATA);
151783af925Schristos }
152783af925Schristos 
153783af925Schristos 
154783af925Schristos <INITIAL,DATA_STATE>{NewLines} {
155783af925Schristos     DbgPrint(ASL_PARSE_OUTPUT,
156783af925Schristos         "Newline matched (data state). Current line number: %u\n",DtCompilerParserlineno);
157783af925Schristos     BEGIN (INITIAL); DtCompilerParsercolumn = 1;
158783af925Schristos }
159783af925Schristos 
160783af925Schristos 
161783af925Schristos %%
162783af925Schristos 
163783af925Schristos 
164783af925Schristos /*
165783af925Schristos  * Local support functions
166783af925Schristos  */
167783af925Schristos 
168783af925Schristos void
169783af925Schristos DtCompilerInitLexer (
170783af925Schristos     FILE                *inFile)
171783af925Schristos {
172783af925Schristos     yyin = inFile;
173783af925Schristos }
174783af925Schristos 
175783af925Schristos void
176783af925Schristos DtCompilerTerminateLexer (
177783af925Schristos     void)
178783af925Schristos {
179783af925Schristos     /*
180783af925Schristos      * Flex/Bison increments the lineno for the EOF so decrement by 1 to get
181783af925Schristos      * the correct number of lines.
182783af925Schristos      */
183783af925Schristos     AslGbl_CurrentLineNumber = DtCompilerParserlineno - 1;
184783af925Schristos     AslGbl_InputByteCount = DtCompilerParserByteOffset;
185783af925Schristos }
186