2 # This file is used to define class objects of INF file [Protocols] section.
3 # It will consumed by InfParser.
5 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
13 from Library
.ParserValidate
import IsValidCVariableName
14 from Library
.CommentParsing
import ParseComment
15 from Library
.ExpressionValidate
import IsValidFeatureFlagExp
17 from Library
.Misc
import Sdict
19 from Object
.Parser
.InfMisc
import ErrorInInf
21 from Library
import DataType
as DT
22 from Logger
import StringTable
as ST
24 def ParseProtocolComment(CommentsList
, InfProtocolItemObj
):
31 for CommentItem
in CommentsList
:
36 CommentItemHelpText
= \
37 ParseComment(CommentItem
,
38 DT
.PROTOCOL_USAGE_TOKENS
,
39 DT
.PROTOCOL_NOTIFY_TOKENS
,
46 if CommentItemHelpText
is None:
47 CommentItemHelpText
= ''
48 if Count
== len(CommentsList
) and CommentItemUsage
== CommentItemNotify
== DT
.ITEM_UNDEFINED
:
49 CommentItemHelpText
= DT
.END_OF_LINE
51 if Count
== len(CommentsList
):
52 if BlockFlag
== 1 or BlockFlag
== 2:
53 if CommentItemUsage
== CommentItemNotify
== DT
.ITEM_UNDEFINED
:
60 if BlockFlag
== -1 or BlockFlag
== 1 or BlockFlag
== 2:
61 if CommentItemUsage
== CommentItemNotify
== DT
.ITEM_UNDEFINED
:
67 if BlockFlag
== 1 or BlockFlag
== 2:
73 # Combine two comment line if they are generic comment
75 if CommentItemUsage
== CommentItemNotify
== PreUsage
== PreNotify
== DT
.ITEM_UNDEFINED
:
76 CommentItemHelpText
= PreHelpText
+ DT
.END_OF_LINE
+ CommentItemHelpText
78 PreHelpText
= CommentItemHelpText
81 CommentItemIns
= InfProtocolItemCommentContent()
82 CommentItemIns
.SetUsageItem(CommentItemUsage
)
83 CommentItemIns
.SetNotify(CommentItemNotify
)
84 CommentItemIns
.SetHelpStringItem(CommentItemHelpText
)
85 CommentInsList
.append(CommentItemIns
)
94 # Add previous help string
96 CommentItemIns
= InfProtocolItemCommentContent()
97 CommentItemIns
.SetUsageItem(DT
.ITEM_UNDEFINED
)
98 CommentItemIns
.SetNotify(DT
.ITEM_UNDEFINED
)
99 if PreHelpText
== '' or PreHelpText
.endswith(DT
.END_OF_LINE
):
100 PreHelpText
+= DT
.END_OF_LINE
101 CommentItemIns
.SetHelpStringItem(PreHelpText
)
102 CommentInsList
.append(CommentItemIns
)
104 # Add Current help string
106 CommentItemIns
= InfProtocolItemCommentContent()
107 CommentItemIns
.SetUsageItem(CommentItemUsage
)
108 CommentItemIns
.SetNotify(CommentItemNotify
)
109 CommentItemIns
.SetHelpStringItem(CommentItemHelpText
)
110 CommentInsList
.append(CommentItemIns
)
118 PreUsage
= CommentItemUsage
119 PreNotify
= CommentItemNotify
120 PreHelpText
= CommentItemHelpText
122 InfProtocolItemObj
.SetCommentList(CommentInsList
)
124 return InfProtocolItemObj
126 class InfProtocolItemCommentContent():
129 # ## SOMETIMES_CONSUMES ## HelpString
135 self
.HelpStringItem
= ''
137 self
.CommentList
= []
139 def SetUsageItem(self
, UsageItem
):
140 self
.UsageItem
= UsageItem
141 def GetUsageItem(self
):
142 return self
.UsageItem
144 def SetNotify(self
, Notify
):
145 if Notify
!= DT
.ITEM_UNDEFINED
:
150 def SetHelpStringItem(self
, HelpStringItem
):
151 self
.HelpStringItem
= HelpStringItem
152 def GetHelpStringItem(self
):
153 return self
.HelpStringItem
155 class InfProtocolItem():
158 self
.FeatureFlagExp
= ''
159 self
.SupArchList
= []
160 self
.CommentList
= []
162 def SetName(self
, Name
):
167 def SetFeatureFlagExp(self
, FeatureFlagExp
):
168 self
.FeatureFlagExp
= FeatureFlagExp
169 def GetFeatureFlagExp(self
):
170 return self
.FeatureFlagExp
172 def SetSupArchList(self
, SupArchList
):
173 self
.SupArchList
= SupArchList
174 def GetSupArchList(self
):
175 return self
.SupArchList
177 def SetCommentList(self
, CommentList
):
178 self
.CommentList
= CommentList
179 def GetCommentList(self
):
180 return self
.CommentList
186 class InfProtocolObject():
188 self
.Protocols
= Sdict()
190 # Macro defined in this section should be only used in this section.
194 def SetProtocol(self
, ProtocolContent
, Arch
= None,):
196 for ArchItem
in Arch
:
200 if (ArchItem
== '' or ArchItem
is None):
202 __SupArchList
.append(ArchItem
)
204 for Item
in ProtocolContent
:
206 # Get Comment content of this protocol
210 CommentsList
= Item
[1]
211 CurrentLineOfItem
= Item
[2]
212 LineInfo
= (CurrentLineOfItem
[2], CurrentLineOfItem
[1], CurrentLineOfItem
[0])
214 InfProtocolItemObj
= InfProtocolItem()
215 if len(Item
) >= 1 and len(Item
) <= 2:
217 # Only CName contained
219 if not IsValidCVariableName(Item
[0]):
220 ErrorInInf(ST
.ERR_INF_PARSER_INVALID_CNAME
%(Item
[0]),
223 InfProtocolItemObj
.SetName(Item
[0])
225 ErrorInInf(ST
.ERR_INF_PARSER_CNAME_MISSING
,
229 # Contained CName and Feature Flag Express
230 # <statements> ::= <CName> ["|"
231 # <FeatureFlagExpress>]
232 # For Protocol Object
234 if Item
[1].strip() == '':
235 ErrorInInf(ST
.ERR_INF_PARSER_FEATURE_FLAG_EXP_MISSING
,
238 # Validate Feature Flag Express for Item[1]
240 FeatureFlagRtv
= IsValidFeatureFlagExp(Item
[1].strip())
241 if not FeatureFlagRtv
[0]:
242 ErrorInInf(ST
.ERR_INF_PARSER_FEATURE_FLAG_EXP_SYNTAX_INVLID
%(FeatureFlagRtv
[1]),
244 InfProtocolItemObj
.SetFeatureFlagExp(Item
[1])
246 if len(Item
) < 1 or len(Item
) > 2:
248 # Invalid format of Protocols statement
250 ErrorInInf(ST
.ERR_INF_PARSER_GUID_PPI_PROTOCOL_SECTION_CONTENT_ERROR
,
254 # Get/Set Usage and HelpString for Protocol entry
256 if CommentsList
is not None and len(CommentsList
) != 0:
257 InfProtocolItemObj
= ParseProtocolComment(CommentsList
, InfProtocolItemObj
)
259 CommentItemIns
= InfProtocolItemCommentContent()
260 CommentItemIns
.SetUsageItem(DT
.ITEM_UNDEFINED
)
261 CommentItemIns
.SetNotify(DT
.ITEM_UNDEFINED
)
262 InfProtocolItemObj
.SetCommentList([CommentItemIns
])
264 InfProtocolItemObj
.SetSupArchList(__SupArchList
)
267 # Determine protocol name duplicate. Follow below rule:
269 # A protocol must not be duplicated within a [Protocols] section.
270 # A protocol may appear in multiple architectural [Protocols]
271 # sections. A protocol listed in an architectural [Protocols]
272 # section must not be listed in the common architectural
273 # [Protocols] section.
275 # NOTE: This check will not report error now.
277 for Item
in self
.Protocols
:
278 if Item
.GetName() == InfProtocolItemObj
.GetName():
279 ItemSupArchList
= Item
.GetSupArchList()
280 for ItemArch
in ItemSupArchList
:
281 for ProtocolItemObjArch
in __SupArchList
:
282 if ItemArch
== ProtocolItemObjArch
:
284 # ST.ERR_INF_PARSER_ITEM_DUPLICATE
287 if ItemArch
.upper() == 'COMMON' or ProtocolItemObjArch
.upper() == 'COMMON':
289 # ST.ERR_INF_PARSER_ITEM_DUPLICATE_COMMON
293 if (InfProtocolItemObj
) in self
.Protocols
:
294 ProcotolList
= self
.Protocols
[InfProtocolItemObj
]
295 ProcotolList
.append(InfProtocolItemObj
)
296 self
.Protocols
[InfProtocolItemObj
] = ProcotolList
299 ProcotolList
.append(InfProtocolItemObj
)
300 self
.Protocols
[InfProtocolItemObj
] = ProcotolList
304 def GetProtocol(self
):
305 return self
.Protocols