X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FEcc%2FCodeFragmentCollector.py;h=b4f421342f60d0188a3ea8e559671f3feaf7f21a;hp=d95faeef6aecdd51313f7e1bc0f3051ebc2748ac;hb=f7496d717357b9af78414d19679b073403812340;hpb=30fdf1140b8d1ce93f3821d986fa165552023440 diff --git a/BaseTools/Source/Python/Ecc/CodeFragmentCollector.py b/BaseTools/Source/Python/Ecc/CodeFragmentCollector.py index d95faeef6a..b4f421342f 100644 --- a/BaseTools/Source/Python/Ecc/CodeFragmentCollector.py +++ b/BaseTools/Source/Python/Ecc/CodeFragmentCollector.py @@ -1,9 +1,9 @@ ## @file # preprocess source file # -# Copyright (c) 2007, Intel Corporation +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
# -# All rights reserved. This program and the accompanying materials +# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at # http://opensource.org/licenses/bsd-license.php @@ -16,8 +16,9 @@ # Import Modules # +from __future__ import print_function import re -import os +import Common.LongFilePathOs as os import sys import antlr3 @@ -46,7 +47,7 @@ from ParserWarning import Warning T_CHAR_BACKSLASH, T_CHAR_DOUBLE_QUOTE, T_CHAR_SINGLE_QUOTE, T_CHAR_STAR, T_CHAR_HASH) = \ (' ', '\0', '\r', '\t', '\n', '/', '\\', '\"', '\'', '*', '#') -SEPERATOR_TUPLE = ('=', '|', ',', '{', '}') +SEPERATOR_TUPLE = ('=', '|', ',', '{', '}') (T_COMMENT_TWO_SLASH, T_COMMENT_SLASH_STAR) = (0, 1) @@ -58,7 +59,7 @@ SEPERATOR_TUPLE = ('=', '|', ',', '{', '}') # # GetNext*** procedures mean these procedures will get next token first, then make judgement. # Get*** procedures mean these procedures will make judgement on current token only. -# +# class CodeFragmentCollector: ## The constructor # @@ -75,40 +76,6 @@ class CodeFragmentCollector: self.__Token = "" self.__SkippedChars = "" - ## __IsWhiteSpace() method - # - # Whether char at current FileBufferPos is whitespace - # - # @param self The object pointer - # @param Char The char to test - # @retval True The char is a kind of white space - # @retval False The char is NOT a kind of white space - # - def __IsWhiteSpace(self, Char): - if Char in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_SPACE, T_CHAR_TAB, T_CHAR_LF): - return True - else: - return False - - ## __SkipWhiteSpace() method - # - # Skip white spaces from current char, return number of chars skipped - # - # @param self The object pointer - # @retval Count The number of chars skipped - # - def __SkipWhiteSpace(self): - Count = 0 - while not self.__EndOfFile(): - Count += 1 - if self.__CurrentChar() in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_LF, T_CHAR_SPACE, T_CHAR_TAB): - self.__SkippedChars += str(self.__CurrentChar()) - self.__GetOneChar() - - else: - Count = Count - 1 - return Count - ## __EndOfFile() method # # Judge current buffer pos is at file end @@ -122,7 +89,7 @@ class CodeFragmentCollector: SizeOfLastLine = NumberOfLines if NumberOfLines > 0: SizeOfLastLine = len(self.Profile.FileLinesList[-1]) - + if self.CurrentLineNumber == NumberOfLines and self.CurrentOffsetWithinLine >= SizeOfLastLine - 1: return True elif self.CurrentLineNumber > NumberOfLines: @@ -144,7 +111,7 @@ class CodeFragmentCollector: return True else: return False - + ## Rewind() method # # Reset file data buffer to the initial state @@ -154,7 +121,7 @@ class CodeFragmentCollector: def Rewind(self): self.CurrentLineNumber = 1 self.CurrentOffsetWithinLine = 0 - + ## __UndoOneChar() method # # Go back one char in the file buffer @@ -162,9 +129,9 @@ class CodeFragmentCollector: # @param self The object pointer # @retval True Successfully go back one char # @retval False Not able to go back one char as file beginning reached - # + # def __UndoOneChar(self): - + if self.CurrentLineNumber == 1 and self.CurrentOffsetWithinLine == 0: return False elif self.CurrentOffsetWithinLine == 0: @@ -173,13 +140,13 @@ class CodeFragmentCollector: else: self.CurrentOffsetWithinLine -= 1 return True - + ## __GetOneChar() method # # Move forward one char in the file buffer # # @param self The object pointer - # + # def __GetOneChar(self): if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1: self.CurrentLineNumber += 1 @@ -193,13 +160,13 @@ class CodeFragmentCollector: # # @param self The object pointer # @retval Char Current char - # + # def __CurrentChar(self): CurrentChar = self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine] # if CurrentChar > 255: # raise Warning("Non-Ascii char found At Line %d, offset %d" % (self.CurrentLineNumber, self.CurrentOffsetWithinLine), self.FileName, self.CurrentLineNumber) return CurrentChar - + ## __NextChar() method # # Get the one char pass the char pointed to by the file buffer pointer @@ -212,7 +179,7 @@ class CodeFragmentCollector: return self.Profile.FileLinesList[self.CurrentLineNumber][0] else: return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine + 1] - + ## __SetCurrentCharValue() method # # Modify the value of current char @@ -222,7 +189,7 @@ class CodeFragmentCollector: # def __SetCurrentCharValue(self, Value): self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine] = Value - + ## __SetCharValue() method # # Modify the value of current char @@ -232,7 +199,7 @@ class CodeFragmentCollector: # def __SetCharValue(self, Line, Offset, Value): self.Profile.FileLinesList[Line - 1][Offset] = Value - + ## __CurrentLine() method # # Get the list that contains current line contents @@ -242,7 +209,7 @@ class CodeFragmentCollector: # def __CurrentLine(self): return self.Profile.FileLinesList[self.CurrentLineNumber - 1] - + ## __InsertComma() method # # Insert ',' to replace PP @@ -251,24 +218,24 @@ class CodeFragmentCollector: # @retval List current line contents # def __InsertComma(self, Line): - - + + if self.Profile.FileLinesList[Line - 1][0] != T_CHAR_HASH: BeforeHashPart = str(self.Profile.FileLinesList[Line - 1]).split(T_CHAR_HASH)[0] if BeforeHashPart.rstrip().endswith(T_CHAR_COMMA) or BeforeHashPart.rstrip().endswith(';'): return - + if Line - 2 >= 0 and str(self.Profile.FileLinesList[Line - 2]).rstrip().endswith(','): return - + if Line - 2 >= 0 and str(self.Profile.FileLinesList[Line - 2]).rstrip().endswith(';'): return - + if str(self.Profile.FileLinesList[Line]).lstrip().startswith(',') or str(self.Profile.FileLinesList[Line]).lstrip().startswith(';'): return - + self.Profile.FileLinesList[Line - 1].insert(self.CurrentOffsetWithinLine, ',') - + ## PreprocessFile() method # # Preprocess file contents, replace comments with spaces. @@ -277,7 +244,7 @@ class CodeFragmentCollector: # !include statement should be expanded at the same FileLinesList[CurrentLineNumber - 1] # # @param self The object pointer - # + # def PreprocessFile(self): self.Rewind() @@ -289,27 +256,27 @@ class CodeFragmentCollector: PPDirectiveObj = None # HashComment in quoted string " " is ignored. InString = False - InCharLiteral = False - + InCharLiteral = False + self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesListFromFile] while not self.__EndOfFile(): - + if not InComment and self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE: InString = not InString - + if not InComment and self.__CurrentChar() == T_CHAR_SINGLE_QUOTE: InCharLiteral = not InCharLiteral # meet new line, then no longer in a comment for // and '#' if self.__CurrentChar() == T_CHAR_LF: - if HashComment and PPDirectiveObj != None: + if HashComment and PPDirectiveObj is not None: if PPDirectiveObj.Content.rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH): PPDirectiveObj.Content += T_CHAR_LF PPExtend = True else: PPExtend = False - + EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine) - + if InComment and DoubleSlashComment: InComment = False DoubleSlashComment = False @@ -324,17 +291,17 @@ class CodeFragmentCollector: PPDirectiveObj.EndPos = EndLinePos FileProfile.PPDirectiveList.append(PPDirectiveObj) PPDirectiveObj = None - + if InString or InCharLiteral: CurrentLine = "".join(self.__CurrentLine()) if CurrentLine.rstrip(T_CHAR_LF).rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH): SlashIndex = CurrentLine.rindex(T_CHAR_BACKSLASH) self.__SetCharValue(self.CurrentLineNumber, SlashIndex, T_CHAR_SPACE) - + if InComment and not DoubleSlashComment and not HashComment: CommentObj.Content += T_CHAR_LF self.CurrentLineNumber += 1 - self.CurrentOffsetWithinLine = 0 + self.CurrentOffsetWithinLine = 0 # check for */ comment end elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH: CommentObj.Content += self.__CurrentChar() @@ -348,7 +315,7 @@ class CodeFragmentCollector: self.__GetOneChar() InComment = False # set comments to spaces - elif InComment: + elif InComment: if HashComment: # // follows hash PP directive if self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH: @@ -374,7 +341,7 @@ class CodeFragmentCollector: # check for '#' comment elif self.__CurrentChar() == T_CHAR_HASH and not InString and not InCharLiteral: InComment = True - HashComment = True + HashComment = True PPDirectiveObj = PP_Directive('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None) # check for /* comment start elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR: @@ -388,9 +355,9 @@ class CodeFragmentCollector: InComment = True else: self.__GetOneChar() - + EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine) - + if InComment and DoubleSlashComment: CommentObj.EndPos = EndLinePos FileProfile.CommentList.append(CommentObj) @@ -411,27 +378,27 @@ class CodeFragmentCollector: PPDirectiveObj = None # HashComment in quoted string " " is ignored. InString = False - InCharLiteral = False + InCharLiteral = False self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesListFromFile] while not self.__EndOfFile(): - + if not InComment and self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE: InString = not InString - + if not InComment and self.__CurrentChar() == T_CHAR_SINGLE_QUOTE: InCharLiteral = not InCharLiteral # meet new line, then no longer in a comment for // and '#' if self.__CurrentChar() == T_CHAR_LF: - if HashComment and PPDirectiveObj != None: + if HashComment and PPDirectiveObj is not None: if PPDirectiveObj.Content.rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH): PPDirectiveObj.Content += T_CHAR_LF PPExtend = True else: PPExtend = False - + EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine) - + if InComment and DoubleSlashComment: InComment = False DoubleSlashComment = False @@ -446,17 +413,17 @@ class CodeFragmentCollector: PPDirectiveObj.EndPos = EndLinePos FileProfile.PPDirectiveList.append(PPDirectiveObj) PPDirectiveObj = None - + if InString or InCharLiteral: CurrentLine = "".join(self.__CurrentLine()) if CurrentLine.rstrip(T_CHAR_LF).rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH): SlashIndex = CurrentLine.rindex(T_CHAR_BACKSLASH) self.__SetCharValue(self.CurrentLineNumber, SlashIndex, T_CHAR_SPACE) - + if InComment and not DoubleSlashComment and not HashComment: CommentObj.Content += T_CHAR_LF self.CurrentLineNumber += 1 - self.CurrentOffsetWithinLine = 0 + self.CurrentOffsetWithinLine = 0 # check for */ comment end elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH: CommentObj.Content += self.__CurrentChar() @@ -470,7 +437,7 @@ class CodeFragmentCollector: self.__GetOneChar() InComment = False # set comments to spaces - elif InComment: + elif InComment: if HashComment: # // follows hash PP directive if self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH: @@ -496,7 +463,7 @@ class CodeFragmentCollector: # check for '#' comment elif self.__CurrentChar() == T_CHAR_HASH and not InString and not InCharLiteral: InComment = True - HashComment = True + HashComment = True PPDirectiveObj = PP_Directive('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None) # check for /* comment start elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR: @@ -512,7 +479,7 @@ class CodeFragmentCollector: self.__GetOneChar() EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine) - + if InComment and DoubleSlashComment: CommentObj.EndPos = EndLinePos FileProfile.CommentList.append(CommentObj) @@ -540,7 +507,7 @@ class CodeFragmentCollector: tStream = antlr3.CommonTokenStream(lexer) parser = CParser(tStream) parser.translation_unit() - + def ParseFileWithClearedPPDirective(self): self.PreprocessFileWithClear() # restore from ListOfList to ListOfString @@ -553,7 +520,7 @@ class CodeFragmentCollector: tStream = antlr3.CommonTokenStream(lexer) parser = CParser(tStream) parser.translation_unit() - + def CleanFileProfileBuffer(self): FileProfile.CommentList = [] FileProfile.PPDirectiveList = [] @@ -564,61 +531,61 @@ class CodeFragmentCollector: FileProfile.StructUnionDefinitionList = [] FileProfile.TypedefDefinitionList = [] FileProfile.FunctionCallingList = [] - + def PrintFragments(self): - - print '################# ' + self.FileName + '#####################' - - print '/****************************************/' - print '/*************** COMMENTS ***************/' - print '/****************************************/' + + print('################# ' + self.FileName + '#####################') + + print('/****************************************/') + print('/*************** COMMENTS ***************/') + print('/****************************************/') for comment in FileProfile.CommentList: - print str(comment.StartPos) + comment.Content - - print '/****************************************/' - print '/********* PREPROCESS DIRECTIVES ********/' - print '/****************************************/' + print(str(comment.StartPos) + comment.Content) + + print('/****************************************/') + print('/********* PREPROCESS DIRECTIVES ********/') + print('/****************************************/') for pp in FileProfile.PPDirectiveList: - print str(pp.StartPos) + pp.Content - - print '/****************************************/' - print '/********* VARIABLE DECLARATIONS ********/' - print '/****************************************/' + print(str(pp.StartPos) + pp.Content) + + print('/****************************************/') + print('/********* VARIABLE DECLARATIONS ********/') + print('/****************************************/') for var in FileProfile.VariableDeclarationList: - print str(var.StartPos) + var.Modifier + ' '+ var.Declarator - - print '/****************************************/' - print '/********* FUNCTION DEFINITIONS *********/' - print '/****************************************/' + print(str(var.StartPos) + var.Modifier + ' '+ var.Declarator) + + print('/****************************************/') + print('/********* FUNCTION DEFINITIONS *********/') + print('/****************************************/') for func in FileProfile.FunctionDefinitionList: - print str(func.StartPos) + func.Modifier + ' '+ func.Declarator + ' ' + str(func.NamePos) - - print '/****************************************/' - print '/************ ENUMERATIONS **************/' - print '/****************************************/' + print(str(func.StartPos) + func.Modifier + ' '+ func.Declarator + ' ' + str(func.NamePos)) + + print('/****************************************/') + print('/************ ENUMERATIONS **************/') + print('/****************************************/') for enum in FileProfile.EnumerationDefinitionList: - print str(enum.StartPos) + enum.Content - - print '/****************************************/' - print '/*********** STRUCTS/UNIONS *************/' - print '/****************************************/' + print(str(enum.StartPos) + enum.Content) + + print('/****************************************/') + print('/*********** STRUCTS/UNIONS *************/') + print('/****************************************/') for su in FileProfile.StructUnionDefinitionList: - print str(su.StartPos) + su.Content - - print '/****************************************/' - print '/********* PREDICATE EXPRESSIONS ********/' - print '/****************************************/' + print(str(su.StartPos) + su.Content) + + print('/****************************************/') + print('/********* PREDICATE EXPRESSIONS ********/') + print('/****************************************/') for predexp in FileProfile.PredicateExpressionList: - print str(predexp.StartPos) + predexp.Content - - print '/****************************************/' - print '/************** TYPEDEFS ****************/' - print '/****************************************/' + print(str(predexp.StartPos) + predexp.Content) + + print('/****************************************/') + print('/************** TYPEDEFS ****************/') + print('/****************************************/') for typedef in FileProfile.TypedefDefinitionList: - print str(typedef.StartPos) + typedef.ToType - + print(str(typedef.StartPos) + typedef.ToType) + if __name__ == "__main__": - + collector = CodeFragmentCollector(sys.argv[1]) collector.PreprocessFile() - print "For Test." + print("For Test.")