ERR_EMPTY_EXPR = 'Empty expression is not allowed.'\r
ERR_IN_OPERAND = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'\r
\r
+__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
+_ReLabel = re.compile('LABEL\((\w+)\)')\r
+_ReOffset = re.compile('OFFSET_OF\((\w+)\)')\r
+\r
## SplitString\r
# Split string to list according double quote\r
# For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']\r
for i, ch in enumerate(String):\r
if ch == '(':\r
InParenthesis += 1\r
- if ch == ')':\r
+ elif ch == ')':\r
if InParenthesis:\r
InParenthesis -= 1\r
else:\r
raise BadExpression(ERR_STRING_TOKEN % Item)\r
- if ch == '"' and not InSingleQuote:\r
+ elif ch == '"' and not InSingleQuote:\r
if String[i-1] != '\\':\r
InDoubleQuote = not InDoubleQuote\r
- if ch == "'" and not InDoubleQuote:\r
+ elif ch == "'" and not InDoubleQuote:\r
if String[i-1] != '\\':\r
InSingleQuote = not InSingleQuote\r
- if ch == ',':\r
+ elif ch == ',':\r
if InParenthesis or InSingleQuote or InDoubleQuote:\r
Item += String[i]\r
continue\r
RetList.append(Item)\r
return RetList\r
\r
-def IsValidCString(Str):\r
- ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
- if not ValidString.match(Str):\r
- return False\r
- return True\r
+def IsValidCName(Str):\r
+ return True if __ValidString.match(Str) else False\r
\r
def BuildOptionValue(PcdValue, GuidDict):\r
- IsArray = False\r
if PcdValue.startswith('H'):\r
InputValue = PcdValue[1:]\r
elif PcdValue.startswith("L'") or PcdValue.startswith("'"):\r
else:\r
InputValue = PcdValue\r
if IsFieldValueAnArray(InputValue):\r
- IsArray = True\r
- if IsArray:\r
try:\r
PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True)\r
except:\r
NonLetterOpLst = ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']\r
\r
PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')\r
- HexPattern = re.compile(r'0[xX][0-9a-fA-F]+$')\r
- RegGuidPattern = re.compile(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')\r
\r
SymbolPattern = re.compile("("\r
"\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"\r
self._LiteralToken.endswith('}'):\r
return True\r
\r
- if self.HexPattern.match(self._LiteralToken):\r
+ if gHexPattern.match(self._LiteralToken):\r
Token = self._LiteralToken[2:]\r
if not Token:\r
self._LiteralToken = '0x0'\r
self._Token = ''\r
if Expr:\r
Ch = Expr[0]\r
- Match = self.RegGuidPattern.match(Expr)\r
+ Match = gGuidPattern.match(Expr)\r
if Match and not Expr[Match.end():Match.end()+1].isalnum() \\r
and Expr[Match.end():Match.end()+1] != '_':\r
self._Idx += Match.end()\r
PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1])\r
LabelDict = {}\r
NewPcdValueList = []\r
- ReLabel = re.compile('LABEL\((\w+)\)')\r
- ReOffset = re.compile('OFFSET_OF\((\w+)\)')\r
LabelOffset = 0\r
- for Index, Item in enumerate(PcdValueList):\r
+ for Item in PcdValueList:\r
# compute byte offset of every LABEL\r
- LabelList = ReLabel.findall(Item)\r
- Item = ReLabel.sub('', Item)\r
+ LabelList = _ReLabel.findall(Item)\r
+ Item = _ReLabel.sub('', Item)\r
Item = Item.strip()\r
if LabelList:\r
for Label in LabelList:\r
- if not IsValidCString(Label):\r
+ if not IsValidCName(Label):\r
raise BadExpression('%s is not a valid c variable name' % Label)\r
if Label not in LabelDict.keys():\r
LabelDict[Label] = str(LabelOffset)\r
except:\r
LabelOffset = LabelOffset + 1\r
\r
- for Index, Item in enumerate(PcdValueList):\r
+ for Item in PcdValueList:\r
# for LABEL parse\r
Item = Item.strip()\r
try:\r
- Item = ReLabel.sub('', Item)\r
+ Item = _ReLabel.sub('', Item)\r
except:\r
pass\r
try:\r
- OffsetList = ReOffset.findall(Item)\r
+ OffsetList = _ReOffset.findall(Item)\r
except:\r
pass\r
for Offset in OffsetList:\r
Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue\r
if ItemSize == 0:\r
ItemValue, ItemSize = ParseFieldValue(Item)\r
- if not (Item.startswith('"') or Item.startswith('L') or Item.startswith('{')) and ItemSize > 1:\r
+ if Item[0] not in ['"','L','{'] and ItemSize > 1:\r
raise BadExpression("Byte array number %s should less than 0xFF." % Item)\r
else:\r
ItemValue = ParseFieldValue(Item)[0]\r