def _TryBackSlash(self, ProcessedLine, ProcessedComments): CatLine = '' Comment = '' Line = ProcessedLine CommentList = ProcessedComments while not self._RawData.IsEndOfFile(): if Line == '': self._LoggerError(ST.ERR_DECPARSE_BACKSLASH_EMPTY) break if Comment: CommentList.append((Comment, self._RawData.LineIndex)) if Line[-1] != DT.TAB_SLASH: CatLine += Line break elif len(Line) < 2 or Line[-2] != ' ': self._LoggerError(ST.ERR_DECPARSE_BACKSLASH) else: CatLine += Line[:-1] Line, Comment = CleanString(self._RawData.GetNextLine()) # # Reach end of content # if self._RawData.IsEndOfFile(): if not CatLine: if ProcessedLine[-1] == DT.TAB_SLASH: self._LoggerError(ST.ERR_DECPARSE_BACKSLASH_EMPTY) CatLine = ProcessedLine else: if not Line or Line[-1] == DT.TAB_SLASH: self._LoggerError(ST.ERR_DECPARSE_BACKSLASH_EMPTY) CatLine += Line self._RawData.CurrentLine = self._ReplaceMacro(CatLine) return CatLine, CommentList
def _TryBackSlash(self, ProcessedLine, ProcessedComments): CatLine = '' Comment = '' Line = ProcessedLine CommentList = ProcessedComments while not self._RawData.IsEndOfFile(): if Line == '': self._LoggerError(ST.ERR_DECPARSE_BACKSLASH_EMPTY) break if Comment: CommentList.append((Comment, self._RawData.LineIndex)) if Line[-1] != DT.TAB_SLASH: CatLine += Line break elif len(Line) < 2 or Line[-2] != ' ': self._LoggerError(ST.ERR_DECPARSE_BACKSLASH) else: CatLine += Line[:-1] Line, Comment = CleanString(self._RawData.GetNextLine()) # # Reach end of content # if self._RawData.IsEndOfFile(): if not CatLine: if ProcessedLine[-1] == DT.TAB_SLASH: self._LoggerError(ST.ERR_DECPARSE_BACKSLASH_EMPTY) CatLine = ProcessedLine else: if not Line or Line[-1] == DT.TAB_SLASH: self._LoggerError(ST.ERR_DECPARSE_BACKSLASH_EMPTY) CatLine += Line # # All MACRO values defined by the DEFINE statements in any section # (except [Userextensions] sections for Intel) of the INF or DEC file # must be expanded before processing of the file. # __IsReplaceMacro = True Header = self._RawData.CurrentScope[ 0] if self._RawData.CurrentScope else None if Header and len(Header) > 2: if Header[0].upper() == 'USEREXTENSIONS' and not ( Header[1] == 'TianoCore' and Header[2] == '"ExtraFiles"'): __IsReplaceMacro = False if __IsReplaceMacro: self._RawData.CurrentLine = self._ReplaceMacro(CatLine) else: self._RawData.CurrentLine = CatLine return CatLine, CommentList
def ParseDecComment(self): while not self._RawData.IsEndOfFile(): Line, Comment = CleanString(self._RawData.GetNextLine()) # # Header must be pure comment # if Line != '': self._RawData.UndoNextLine() break if Comment: self._HeadComment.append((Comment, self._RawData.LineIndex)) # # Double '#' indicates end of header comments # if not Comment or Comment == DT.TAB_SPECIAL_COMMENT: break return
def ParseDecComment(self): IsFileHeader = False IsBinaryHeader = False FileHeaderLineIndex = -1 BinaryHeaderLineIndex = -1 while not self._RawData.IsEndOfFile(): Line, Comment = CleanString(self._RawData.GetNextLine()) # # Header must be pure comment # if Line != '': self._RawData.UndoNextLine() break if Comment and Comment.startswith(DT.TAB_SPECIAL_COMMENT) and Comment.find(DT.TAB_HEADER_COMMENT) > 0 \ and not Comment[2:Comment.find(DT.TAB_HEADER_COMMENT)].strip(): IsFileHeader = True IsBinaryHeader = False FileHeaderLineIndex = self._RawData.LineIndex # # Get license information before '@file' # if not IsFileHeader and not IsBinaryHeader and Comment and Comment.startswith(DT.TAB_COMMENT_SPLIT) and \ DT.TAB_BINARY_HEADER_COMMENT not in Comment: self._HeadComment.append((Comment, self._RawData.LineIndex)) if Comment and IsFileHeader and \ not(Comment.startswith(DT.TAB_SPECIAL_COMMENT) \ and Comment.find(DT.TAB_BINARY_HEADER_COMMENT) > 0): self._HeadComment.append((Comment, self._RawData.LineIndex)) # # Double '#' indicates end of header comments # if (not Comment or Comment == DT.TAB_SPECIAL_COMMENT) and IsFileHeader: IsFileHeader = False continue if Comment and Comment.startswith(DT.TAB_SPECIAL_COMMENT) \ and Comment.find(DT.TAB_BINARY_HEADER_COMMENT) > 0: IsBinaryHeader = True IsFileHeader = False BinaryHeaderLineIndex = self._RawData.LineIndex if Comment and IsBinaryHeader: self.BinaryHeadComment.append((Comment, self._RawData.LineIndex)) # # Double '#' indicates end of header comments # if (not Comment or Comment == DT.TAB_SPECIAL_COMMENT) and IsBinaryHeader: IsBinaryHeader = False break if FileHeaderLineIndex > -1 and not IsFileHeader and not IsBinaryHeader: break if FileHeaderLineIndex > BinaryHeaderLineIndex and FileHeaderLineIndex > -1 and BinaryHeaderLineIndex > -1: self._LoggerError(ST.ERR_BINARY_HEADER_ORDER) if FileHeaderLineIndex == -1: Logger.Error(TOOL_NAME, FORMAT_INVALID, ST.ERR_NO_SOURCE_HEADER, File=self._RawData.Filename) return
def Parse(self): HeadComments = [] TailComments = [] #====================================================================== # CurComments may pointer to HeadComments or TailComments #====================================================================== CurComments = HeadComments CurObj = None ItemNum = 0 FromBuf = False #====================================================================== # Used to report error information if empty section found #====================================================================== Index = self._RawData.LineIndex LineStr = self._RawData.CurrentLine while not self._RawData.IsEndOfFile() or self._RawData.NextLine: if self._RawData.NextLine: #============================================================== # Have processed line in buffer #============================================================== Line = self._RawData.NextLine HeadComments.extend(self._RawData.HeadComment) TailComments.extend(self._RawData.TailComment) self._RawData.ResetNext() Comment = '' FromBuf = True else: #============================================================== # No line in buffer, read next line #============================================================== Line, Comment = CleanString(self._RawData.GetNextLine()) FromBuf = False if Line: if not FromBuf and CurObj and TailComments: #========================================================== # Set tail comments to previous statement if not empty. #========================================================== CurObj.SetTailComment(CurObj.GetTailComment()+TailComments) if not FromBuf: del TailComments[:] CurComments = TailComments Comments = [] if Comment: Comments = [(Comment, self._RawData.LineIndex)] #============================================================== # Try if last char of line has backslash #============================================================== Line, Comments = self._TryBackSlash(Line, Comments) CurComments.extend(Comments) #============================================================== # Macro found #============================================================== if Line.startswith('DEFINE '): self._MacroParser(Line) del HeadComments[:] del TailComments[:] CurComments = HeadComments continue if self._StopCurrentParsing(Line): #========================================================== # This line does not belong to this parse, # Save it, can be used by next parse #========================================================== self._RawData.SetNext(Line, HeadComments, TailComments) break Obj = self._ParseItem() ItemNum += 1 if Obj: Obj.SetHeadComment(Obj.GetHeadComment()+HeadComments) Obj.SetTailComment(Obj.GetTailComment()+TailComments) del HeadComments[:] del TailComments[:] CurObj = Obj else: CurObj = None else: if id(CurComments) == id(TailComments): #========================================================== # Check if this comment belongs to tail comment #========================================================== if not self._TailCommentStrategy(Comment): CurComments = HeadComments if Comment: CurComments.append(((Comment, self._RawData.LineIndex))) else: del CurComments[:] if self._IsStatementRequired() and ItemNum == 0: Logger.Error( TOOL_NAME, FILE_PARSE_FAILURE, File=self._RawData.Filename, Line=Index, ExtraData=ST.ERR_DECPARSE_STATEMENT_EMPTY % LineStr )
def ParseDecComment(self): IsFileHeader = False IsBinaryHeader = False FileHeaderLineIndex = -1 BinaryHeaderLineIndex = -1 TokenSpaceGuidCName = '' # # Parse PCD error comment section # while not self._RawData.IsEndOfFile(): self._RawData.CurrentLine = self._RawData.GetNextLine() if self._RawData.CurrentLine.startswith(DT.TAB_COMMENT_SPLIT) and \ DT.TAB_SECTION_START in self._RawData.CurrentLine and \ DT.TAB_SECTION_END in self._RawData.CurrentLine: self._RawData.CurrentLine = self._RawData.CurrentLine.replace(DT.TAB_COMMENT_SPLIT, '').strip() if self._RawData.CurrentLine[0] == DT.TAB_SECTION_START and \ self._RawData.CurrentLine[-1] == DT.TAB_SECTION_END: RawSection = self._RawData.CurrentLine[1:-1].strip() if RawSection.upper().startswith(DT.TAB_PCD_ERROR.upper()+'.'): TokenSpaceGuidCName = RawSection.split(DT.TAB_PCD_ERROR+'.')[1].strip() continue if TokenSpaceGuidCName and self._RawData.CurrentLine.startswith(DT.TAB_COMMENT_SPLIT): self._RawData.CurrentLine = self._RawData.CurrentLine.replace(DT.TAB_COMMENT_SPLIT, '').strip() if self._RawData.CurrentLine != '': if DT.TAB_VALUE_SPLIT not in self._RawData.CurrentLine: self._LoggerError(ST.ERR_DECPARSE_PCDERRORMSG_MISS_VALUE_SPLIT) PcdErrorNumber, PcdErrorMsg = GetSplitValueList(self._RawData.CurrentLine, DT.TAB_VALUE_SPLIT, 1) PcdErrorNumber = ParsePcdErrorCode(PcdErrorNumber, self._RawData.Filename, self._RawData.LineIndex) if not PcdErrorMsg.strip(): self._LoggerError(ST.ERR_DECPARSE_PCD_MISS_ERRORMSG) self.PcdErrorCommentDict[(TokenSpaceGuidCName, PcdErrorNumber)] = PcdErrorMsg.strip() else: TokenSpaceGuidCName = '' self._RawData.LineIndex = 0 self._RawData.CurrentLine = '' self._RawData.NextLine = '' while not self._RawData.IsEndOfFile(): Line, Comment = CleanString(self._RawData.GetNextLine()) # # Header must be pure comment # if Line != '': self._RawData.UndoNextLine() break if Comment and Comment.startswith(DT.TAB_SPECIAL_COMMENT) and Comment.find(DT.TAB_HEADER_COMMENT) > 0 \ and not Comment[2:Comment.find(DT.TAB_HEADER_COMMENT)].strip(): IsFileHeader = True IsBinaryHeader = False FileHeaderLineIndex = self._RawData.LineIndex # # Get license information before '@file' # if not IsFileHeader and not IsBinaryHeader and Comment and Comment.startswith(DT.TAB_COMMENT_SPLIT) and \ DT.TAB_BINARY_HEADER_COMMENT not in Comment: self._HeadComment.append((Comment, self._RawData.LineIndex)) if Comment and IsFileHeader and \ not(Comment.startswith(DT.TAB_SPECIAL_COMMENT) \ and Comment.find(DT.TAB_BINARY_HEADER_COMMENT) > 0): self._HeadComment.append((Comment, self._RawData.LineIndex)) # # Double '#' indicates end of header comments # if (not Comment or Comment == DT.TAB_SPECIAL_COMMENT) and IsFileHeader: IsFileHeader = False continue if Comment and Comment.startswith(DT.TAB_SPECIAL_COMMENT) \ and Comment.find(DT.TAB_BINARY_HEADER_COMMENT) > 0: IsBinaryHeader = True IsFileHeader = False BinaryHeaderLineIndex = self._RawData.LineIndex if Comment and IsBinaryHeader: self.BinaryHeadComment.append((Comment, self._RawData.LineIndex)) # # Double '#' indicates end of header comments # if (not Comment or Comment == DT.TAB_SPECIAL_COMMENT) and IsBinaryHeader: IsBinaryHeader = False break if FileHeaderLineIndex > -1 and not IsFileHeader and not IsBinaryHeader: break if FileHeaderLineIndex > BinaryHeaderLineIndex and FileHeaderLineIndex > -1 and BinaryHeaderLineIndex > -1: self._LoggerError(ST.ERR_BINARY_HEADER_ORDER) if FileHeaderLineIndex == -1: # self._LoggerError(ST.ERR_NO_SOURCE_HEADER) Logger.Error(TOOL_NAME, FORMAT_INVALID, ST.ERR_NO_SOURCE_HEADER, File=self._RawData.Filename) return