]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/Parser/InfGuidPpiProtocolSectionParser.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Parser / InfGuidPpiProtocolSectionParser.py
CommitLineData
4234283c 1## @file\r
f7496d71 2# This file contained the parser for [Guids], [Ppis], [Protocols] sections in INF file\r
4234283c 3#\r
f7496d71 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
2e351cbe 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
4234283c
LG
7#\r
8'''\r
9InfGuidPpiProtocolSectionParser\r
10'''\r
11##\r
12# Import Modules\r
13#\r
14\r
15import Logger.Log as Logger\r
16from Logger import StringTable as ST\r
17from Logger.ToolError import FORMAT_INVALID\r
18from Parser.InfParserMisc import InfExpandMacro\r
19from Library import DataType as DT\r
20from Library import GlobalData\r
21from Library.Parsing import MacroParser\r
22from Library.Misc import GetSplitValueList\r
23from Library.ParserValidate import IsValidIdString\r
24from Library.ParserValidate import IsValidUserId\r
25from Library.ParserValidate import IsValidArch\r
26from Parser.InfParserMisc import InfParserSectionRoot\r
27\r
28class InfGuidPpiProtocolSectionParser(InfParserSectionRoot):\r
29 ## InfGuidParser\r
30 #\r
31 #\r
32 def InfGuidParser(self, SectionString, InfSectionObject, FileName):\r
33 #\r
f7496d71 34 # Macro defined in this section\r
4234283c
LG
35 #\r
36 SectionMacros = {}\r
37 ValueList = []\r
38 GuidList = []\r
39 CommentsList = []\r
40 CurrentLineVar = None\r
41 #\r
42 # Parse section content\r
43 #\r
44 for Line in SectionString:\r
45 LineContent = Line[0]\r
46 LineNo = Line[1]\r
47\r
48 if LineContent.strip() == '':\r
49 CommentsList = []\r
50 continue\r
51\r
52 if LineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):\r
53 CommentsList.append(Line)\r
54 continue\r
55 else:\r
56 #\r
57 # Encounter a GUID entry\r
58 #\r
59 if LineContent.find(DT.TAB_COMMENT_SPLIT) > -1:\r
60 CommentsList.append((\r
61 LineContent[LineContent.find(DT.TAB_COMMENT_SPLIT):],\r
62 LineNo))\r
63 LineContent = \\r
64 LineContent[:LineContent.find(DT.TAB_COMMENT_SPLIT)]\r
65\r
66 if LineContent != '':\r
67 #\r
68 # Find Macro\r
69 #\r
70 Name, Value = MacroParser((LineContent, LineNo),\r
71 FileName,\r
72 DT.MODEL_EFI_GUID,\r
73 self.FileLocalMacros)\r
4231a819 74 if Name is not None:\r
4234283c
LG
75 SectionMacros[Name] = Value\r
76 CommentsList = []\r
77 ValueList = []\r
78 continue\r
79\r
80 TokenList = GetSplitValueList(LineContent, DT.TAB_VALUE_SPLIT, 1)\r
81 ValueList[0:len(TokenList)] = TokenList\r
82\r
83 #\r
84 # Replace with Local section Macro and [Defines] section Macro.\r
f7496d71 85 #\r
4234283c
LG
86 ValueList = [InfExpandMacro(Value, (FileName, LineContent, LineNo),\r
87 self.FileLocalMacros, SectionMacros, True)\r
88 for Value in ValueList]\r
89\r
90 CurrentLineVar = (LineContent, LineNo, FileName)\r
91\r
92\r
93 if len(ValueList) >= 1:\r
94 GuidList.append((ValueList, CommentsList, CurrentLineVar))\r
95 CommentsList = []\r
96 ValueList = []\r
97 continue\r
98\r
99 #\r
100 # Current section archs\r
f7496d71 101 #\r
4234283c
LG
102 ArchList = []\r
103 LineIndex = -1\r
104 for Item in self.LastSectionHeaderContent:\r
105 LineIndex = Item[3]\r
106 if Item[1] not in ArchList:\r
107 ArchList.append(Item[1])\r
108\r
109 if not InfSectionObject.SetGuid(GuidList, Arch=ArchList):\r
110 Logger.Error('InfParser',\r
111 FORMAT_INVALID,\r
112 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[Guid]"),\r
113 File=FileName,\r
114 Line=LineIndex)\r
115\r
116 ## InfPpiParser\r
117 #\r
118 #\r
119 def InfPpiParser(self, SectionString, InfSectionObject, FileName):\r
120 #\r
f7496d71 121 # Macro defined in this section\r
4234283c
LG
122 #\r
123 SectionMacros = {}\r
124 ValueList = []\r
125 PpiList = []\r
126 CommentsList = []\r
127 CurrentLineVar = None\r
128 #\r
129 # Parse section content\r
130 #\r
131 for Line in SectionString:\r
132 LineContent = Line[0]\r
133 LineNo = Line[1]\r
134\r
135 if LineContent.strip() == '':\r
136 CommentsList = []\r
137 continue\r
138\r
139 if LineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):\r
140 CommentsList.append(Line)\r
141 continue\r
142 else:\r
143 #\r
144 # Encounter a PPI entry\r
145 #\r
146 if LineContent.find(DT.TAB_COMMENT_SPLIT) > -1:\r
147 CommentsList.append((\r
148 LineContent[LineContent.find(DT.TAB_COMMENT_SPLIT):],\r
149 LineNo))\r
150 LineContent = \\r
151 LineContent[:LineContent.find(DT.TAB_COMMENT_SPLIT)]\r
152\r
153 if LineContent != '':\r
154 #\r
155 # Find Macro\r
156 #\r
157 Name, Value = MacroParser((LineContent, LineNo),\r
158 FileName,\r
159 DT.MODEL_EFI_PPI,\r
160 self.FileLocalMacros)\r
4231a819 161 if Name is not None:\r
4234283c
LG
162 SectionMacros[Name] = Value\r
163 ValueList = []\r
164 CommentsList = []\r
165 continue\r
166\r
167 TokenList = GetSplitValueList(LineContent, DT.TAB_VALUE_SPLIT, 1)\r
168 ValueList[0:len(TokenList)] = TokenList\r
169\r
170 #\r
171 # Replace with Local section Macro and [Defines] section Macro.\r
f7496d71 172 #\r
4234283c
LG
173 ValueList = [InfExpandMacro(Value, (FileName, LineContent, LineNo), self.FileLocalMacros, SectionMacros)\r
174 for Value in ValueList]\r
175\r
176 CurrentLineVar = (LineContent, LineNo, FileName)\r
177\r
178 if len(ValueList) >= 1:\r
179 PpiList.append((ValueList, CommentsList, CurrentLineVar))\r
180 ValueList = []\r
181 CommentsList = []\r
182 continue\r
183\r
184 #\r
185 # Current section archs\r
f7496d71 186 #\r
4234283c
LG
187 ArchList = []\r
188 LineIndex = -1\r
189 for Item in self.LastSectionHeaderContent:\r
190 LineIndex = Item[3]\r
191 if Item[1] not in ArchList:\r
192 ArchList.append(Item[1])\r
193\r
194 if not InfSectionObject.SetPpi(PpiList, Arch=ArchList):\r
195 Logger.Error('InfParser',\r
196 FORMAT_INVALID,\r
197 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[Ppis]"),\r
198 File=FileName,\r
199 Line=LineIndex)\r
200\r
201 ## InfUserExtensionParser\r
202 #\r
f7496d71 203 #\r
4234283c
LG
204 def InfUserExtensionParser(self, SectionString, InfSectionObject, FileName):\r
205\r
206 UserExtensionContent = ''\r
207\r
208 #\r
209 # Parse section content\r
210 #\r
211 for Line in SectionString:\r
212 LineContent = Line[0]\r
4234283c 213\r
c03f5b2c
HC
214# Comment the code to support user extension without any statement just the section header in []\r
215# if LineContent.strip() == '':\r
216# continue\r
4234283c
LG
217\r
218 UserExtensionContent += LineContent + DT.END_OF_LINE\r
4234283c
LG
219 continue\r
220\r
221 #\r
222 # Current section UserId, IdString\r
f7496d71 223 #\r
4234283c
LG
224 IdContentList = []\r
225 LastItem = ''\r
226 SectionLineNo = None\r
227 for Item in self.LastSectionHeaderContent:\r
228 UserId = Item[1]\r
229 IdString = Item[2]\r
230 Arch = Item[3]\r
231 SectionLineNo = Item[4]\r
232 if not IsValidArch(Arch):\r
233 Logger.Error(\r
234 'InfParser',\r
235 FORMAT_INVALID,\r
236 ST.ERR_INF_PARSER_DEFINE_FROMAT_INVALID % (Arch),\r
237 File=GlobalData.gINF_MODULE_NAME,\r
238 Line=SectionLineNo,\r
239 ExtraData=None)\r
240\r
241 if (UserId, IdString, Arch) not in IdContentList:\r
242 #\r
243 # To check the UserId and IdString valid or not.\r
244 #\r
245 if not IsValidUserId(UserId):\r
246 Logger.Error('InfParser',\r
247 FORMAT_INVALID,\r
248 ST.ERR_INF_PARSER_UE_SECTION_USER_ID_ERROR % (Item[1]),\r
249 File=GlobalData.gINF_MODULE_NAME,\r
250 Line=SectionLineNo,\r
251 ExtraData=None)\r
252\r
253 if not IsValidIdString(IdString):\r
254 Logger.Error('InfParser',\r
255 FORMAT_INVALID,\r
256 ST.ERR_INF_PARSER_UE_SECTION_ID_STRING_ERROR % (IdString),\r
257 File=GlobalData.gINF_MODULE_NAME, Line=SectionLineNo,\r
258 ExtraData=None)\r
259 IdContentList.append((UserId, IdString, Arch))\r
260 else:\r
261 #\r
f7496d71 262 # Each UserExtensions section header must have a unique set\r
4234283c 263 # of UserId, IdString and Arch values.\r
f7496d71
LG
264 # This means that the same UserId can be used in more than one\r
265 # section header, provided the IdString or Arch values are\r
266 # different. The same IdString values can be used in more than\r
267 # one section header if the UserId or Arch values are\r
268 # different. The same UserId and the same IdString can be used\r
269 # in a section header if the Arch values are different in each\r
4234283c
LG
270 # of the section headers.\r
271 #\r
272 Logger.Error('InfParser',\r
273 FORMAT_INVALID,\r
274 ST.ERR_INF_PARSER_UE_SECTION_DUPLICATE_ERROR % (\r
275 IdString),\r
276 File=GlobalData.gINF_MODULE_NAME,\r
277 Line=SectionLineNo,\r
278 ExtraData=None)\r
279 LastItem = Item\r
280\r
281 if not InfSectionObject.SetUserExtension(UserExtensionContent,\r
282 IdContent=IdContentList,\r
283 LineNo=SectionLineNo):\r
284 Logger.Error\\r
285 ('InfParser', FORMAT_INVALID, \\r
286 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[UserExtension]"), \\r
287 File=FileName, Line=LastItem[4])\r
288\r
289 def InfProtocolParser(self, SectionString, InfSectionObject, FileName):\r
290 #\r
f7496d71 291 # Macro defined in this section\r
4234283c
LG
292 #\r
293 SectionMacros = {}\r
294 ValueList = []\r
295 ProtocolList = []\r
296 CommentsList = []\r
297 CurrentLineVar = None\r
298 #\r
299 # Parse section content\r
300 #\r
301 for Line in SectionString:\r
302 LineContent = Line[0]\r
303 LineNo = Line[1]\r
304\r
305 if LineContent.strip() == '':\r
306 CommentsList = []\r
307 continue\r
308\r
309 if LineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):\r
310 CommentsList.append(Line)\r
311 continue\r
312 else:\r
313 #\r
314 # Encounter a Protocol entry\r
315 #\r
316 if LineContent.find(DT.TAB_COMMENT_SPLIT) > -1:\r
317 CommentsList.append((\r
318 LineContent[LineContent.find(DT.TAB_COMMENT_SPLIT):],\r
319 LineNo))\r
320 LineContent = \\r
321 LineContent[:LineContent.find(DT.TAB_COMMENT_SPLIT)]\r
322\r
323 if LineContent != '':\r
324 #\r
325 # Find Macro\r
326 #\r
327 Name, Value = MacroParser((LineContent, LineNo),\r
328 FileName,\r
329 DT.MODEL_EFI_PROTOCOL,\r
330 self.FileLocalMacros)\r
4231a819 331 if Name is not None:\r
4234283c
LG
332 SectionMacros[Name] = Value\r
333 ValueList = []\r
334 CommentsList = []\r
335 continue\r
336\r
337 TokenList = GetSplitValueList(LineContent, DT.TAB_VALUE_SPLIT, 1)\r
338 ValueList[0:len(TokenList)] = TokenList\r
339\r
340 #\r
341 # Replace with Local section Macro and [Defines] section Macro.\r
f7496d71 342 #\r
4234283c
LG
343 ValueList = [InfExpandMacro(Value, (FileName, LineContent, LineNo), self.FileLocalMacros, SectionMacros)\r
344 for Value in ValueList]\r
345\r
346 CurrentLineVar = (LineContent, LineNo, FileName)\r
347\r
348 if len(ValueList) >= 1:\r
349 ProtocolList.append((ValueList, CommentsList, CurrentLineVar))\r
350 ValueList = []\r
351 CommentsList = []\r
352 continue\r
353\r
354 #\r
355 # Current section archs\r
f7496d71 356 #\r
4234283c
LG
357 ArchList = []\r
358 LineIndex = -1\r
359 for Item in self.LastSectionHeaderContent:\r
360 LineIndex = Item[3]\r
361 if Item[1] not in ArchList:\r
362 ArchList.append(Item[1])\r
363\r
364 if not InfSectionObject.SetProtocol(ProtocolList, Arch=ArchList):\r
365 Logger.Error\\r
366 ('InfParser', FORMAT_INVALID, \\r
367 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[Protocol]"), \\r
368 File=FileName, Line=LineIndex)\r