X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FAutoGen%2FUniClassObject.py;h=54b6fb22a08ad6881b78f9d5ec7fb8250e430176;hb=a253d217ee3477fde46cadba0dfe364f9a826694;hp=a01381be5e6498a681d3f8a044ab4845fa1850c7;hpb=47fea6afd74af76c7e2a2b03d319b7ac035ac26a;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/AutoGen/UniClassObject.py b/BaseTools/Source/Python/AutoGen/UniClassObject.py index a01381be5e..54b6fb22a0 100644 --- a/BaseTools/Source/Python/AutoGen/UniClassObject.py +++ b/BaseTools/Source/Python/AutoGen/UniClassObject.py @@ -4,7 +4,7 @@ # # Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
# -# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
# 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 @@ -24,6 +24,7 @@ from Common.BuildToolError import * from Common.String import GetLineNo from Common.Misc import PathClass from Common.LongFilePathSupport import LongFilePath +from Common.GlobalData import * ## # Static definitions # @@ -117,13 +118,11 @@ LangConvTable = {'eng':'en', 'fra':'fr', \ # @retval LangName: Valid lanugage code in RFC 4646 format or None # def GetLanguageCode(LangName, IsCompatibleMode, File): - global LangConvTable - length = len(LangName) if IsCompatibleMode: if length == 3 and LangName.isalpha(): TempLangName = LangConvTable.get(LangName.lower()) - if TempLangName != None: + if TempLangName is not None: return TempLangName return LangName else: @@ -135,7 +134,7 @@ def GetLanguageCode(LangName, IsCompatibleMode, File): if LangName.isalpha(): return LangName elif length == 3: - if LangName.isalpha() and LangConvTable.get(LangName.lower()) == None: + if LangName.isalpha() and LangConvTable.get(LangName.lower()) is None: return LangName elif length == 5: if LangName[0:2].isalpha() and LangName[2] == '-': @@ -143,7 +142,7 @@ def GetLanguageCode(LangName, IsCompatibleMode, File): elif length >= 6: if LangName[0:2].isalpha() and LangName[2] == '-': return LangName - if LangName[0:3].isalpha() and LangConvTable.get(LangName.lower()) == None and LangName[3] == '-': + if LangName[0:3].isalpha() and LangConvTable.get(LangName.lower()) is None and LangName[3] == '-': return LangName EdkLogger.error("Unicode File Parser", FORMAT_INVALID, "Invalid RFC 4646 language code : %s" % LangName, File) @@ -194,14 +193,14 @@ class StringDefClassObject(object): self.UseOtherLangDef = UseOtherLangDef self.Length = 0 - if Name != None: + if Name is not None: self.StringName = Name self.StringNameByteList = UniToHexList(Name) - if Value != None: + if Value is not None: self.StringValue = Value + u'\x00' # Add a NULL at string tail self.StringValueByteList = UniToHexList(self.StringValue) self.Length = len(self.StringValueByteList) - if Token != None: + if Token is not None: self.Token = Token def __str__(self): @@ -212,11 +211,24 @@ class StringDefClassObject(object): repr(self.UseOtherLangDef) def UpdateValue(self, Value = None): - if Value != None: + if Value is not None: self.StringValue = Value + u'\x00' # Add a NULL at string tail self.StringValueByteList = UniToHexList(self.StringValue) self.Length = len(self.StringValueByteList) +def StripComments(Line): + Comment = u'//' + CommentPos = Line.find(Comment) + while CommentPos >= 0: + # if there are non matched quotes before the comment header + # then we are in the middle of a string + # but we need to ignore the escaped quotes and backslashes. + if ((Line.count(u'"', 0, CommentPos) - Line.count(u'\\"', 0, CommentPos)) & 1) == 1: + CommentPos = Line.find (Comment, CommentPos + 1) + else: + return Line[:CommentPos].strip() + return Line.strip() + ## UniFileClassObject # # A structure for .uni file definition @@ -241,7 +253,7 @@ class UniFileClassObject(object): Lang = distutils.util.split_quoted((Line.split(u"//")[0])) if len(Lang) != 3: try: - FileIn = self.OpenUniFile(LongFilePath(File.Path)) + FileIn = UniFileClassObject.OpenUniFile(LongFilePath(File.Path)) except UnicodeError, X: EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File); except: @@ -285,7 +297,8 @@ class UniFileClassObject(object): self.OrderedStringDict[LangName][Item.StringName] = len(self.OrderedStringList[LangName]) - 1 return True - def OpenUniFile(self, FileName): + @staticmethod + def OpenUniFile(FileName): # # Read file # @@ -304,14 +317,15 @@ class UniFileClassObject(object): FileIn.startswith(codecs.BOM_UTF16_LE)): Encoding = 'utf-16' - self.VerifyUcs2Data(FileIn, FileName, Encoding) + UniFileClassObject.VerifyUcs2Data(FileIn, FileName, Encoding) UniFile = StringIO.StringIO(FileIn) Info = codecs.lookup(Encoding) (Reader, Writer) = (Info.streamreader, Info.streamwriter) return codecs.StreamReaderWriter(UniFile, Reader, Writer) - def VerifyUcs2Data(self, FileIn, FileName, Encoding): + @staticmethod + def VerifyUcs2Data(FileIn, FileName, Encoding): Ucs2Info = codecs.lookup('ucs-2') # # Convert to unicode @@ -348,11 +362,11 @@ class UniFileClassObject(object): Value = '' Name = Item.split()[1] - # Check the string name is the upper character + # Check the string name if Name != '': - MatchString = re.match('[A-Z0-9_]+', Name, re.UNICODE) - if MatchString == None or MatchString.end(0) != len(Name): - EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid lower case character.' % (Name, self.File)) + MatchString = gIdentifierPattern.match(Name) + if MatchString is None: + EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid character.' % (Name, self.File)) LanguageList = Item.split(u'#language ') for IndexI in range(len(LanguageList)): if IndexI == 0: @@ -370,20 +384,6 @@ class UniFileClassObject(object): FileName = Item[Item.find(u'#include ') + len(u'#include ') :Item.find(u' ', len(u'#include '))][1:-1] self.LoadUniFile(FileName) - def StripComments(self, Line): - Comment = u'//' - CommentPos = Line.find(Comment) - while CommentPos >= 0: - # if there are non matched quotes before the comment header - # then we are in the middle of a string - # but we need to ignore the escaped quotes and backslashes. - if ((Line.count(u'"', 0, CommentPos) - Line.count(u'\\"', 0, CommentPos)) & 1) == 1: - CommentPos = Line.find (Comment, CommentPos + 1) - else: - return Line[:CommentPos].strip() - return Line.strip() - - # # Pre-process before parse .uni file # @@ -392,7 +392,7 @@ class UniFileClassObject(object): EdkLogger.error("Unicode File Parser", FILE_NOT_FOUND, ExtraData=File.Path) try: - FileIn = self.OpenUniFile(LongFilePath(File.Path)) + FileIn = UniFileClassObject.OpenUniFile(LongFilePath(File.Path)) except UnicodeError, X: EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File.Path); except: @@ -405,7 +405,7 @@ class UniFileClassObject(object): for Line in FileIn: Line = Line.strip() Line = Line.replace(u'\\\\', BACK_SLASH_PLACEHOLDER) - Line = self.StripComments(Line) + Line = StripComments(Line) # # Ignore empty line @@ -432,9 +432,19 @@ class UniFileClassObject(object): Line = Line.replace(u"\\'", u"'") Line = Line.replace(BACK_SLASH_PLACEHOLDER, u'\\') -# if Line.find(u'\\x'): -# hex = Line[Line.find(u'\\x') + 2 : Line.find(u'\\x') + 6] -# hex = "u'\\u" + hex + "'" + StartPos = Line.find(u'\\x') + while (StartPos != -1): + EndPos = Line.find(u'\\', StartPos + 1, StartPos + 7) + if EndPos != -1 and EndPos - StartPos == 6 : + if g4HexChar.match(Line[StartPos + 2 : EndPos], re.UNICODE): + EndStr = Line[EndPos: ] + UniStr = ('\u' + (Line[StartPos + 2 : EndPos])).decode('unicode_escape') + if EndStr.startswith(u'\\x') and len(EndStr) >= 7: + if EndStr[6] == u'\\' and g4HexChar.match(EndStr[2 : 6], re.UNICODE): + Line = Line[0 : StartPos] + UniStr + EndStr + else: + Line = Line[0 : StartPos] + UniStr + EndStr[1:] + StartPos = Line.find(u'\\x', StartPos + 1) IncList = gIncludePattern.findall(Line) if len(IncList) == 1: @@ -455,7 +465,7 @@ class UniFileClassObject(object): # Load a .uni file # def LoadUniFile(self, File = None): - if File == None: + if File is None: EdkLogger.error("Unicode File Parser", PARSER_ERROR, 'No unicode file is given') self.File = File # @@ -508,11 +518,11 @@ class UniFileClassObject(object): break # Value = Value.replace(u'\r\n', u'') Language = GetLanguageCode(Language, self.IsCompatibleMode, self.File) - # Check the string name is the upper character + # Check the string name if not self.IsCompatibleMode and Name != '': - MatchString = re.match('[A-Z0-9_]+', Name, re.UNICODE) - if MatchString == None or MatchString.end(0) != len(Name): - EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid lower case character.' % (Name, self.File)) + MatchString = gIdentifierPattern.match(Name) + if MatchString is None: + EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid character.' % (Name, self.File)) self.AddStringToList(Name, Language, Value) continue @@ -567,7 +577,7 @@ class UniFileClassObject(object): IsAdded = True if Name in self.OrderedStringDict[Language]: IsAdded = False - if Value != None: + if Value is not None: ItemIndexInList = self.OrderedStringDict[Language][Name] Item = self.OrderedStringList[Language][ItemIndexInList] Item.UpdateValue(Value)