Match compiler: Python syntax simplification

Output in build/ stayed the same
This commit is contained in:
Thomas Jarosch 2012-12-31 15:01:12 +01:00
parent d366b00eca
commit 311630ac1c
1 changed files with 26 additions and 26 deletions

View File

@ -48,24 +48,24 @@ def compilePattern(matchStrs, pattern, nr, varid):
if varid: if varid:
arg2 = ', const unsigned int varid' arg2 = ', const unsigned int varid'
ret = '// ' + pattern + '\n' ret = '// ' + pattern + '\n'
ret = ret + 'static bool match' + str(nr) + '(const Token *tok'+arg2+') {\n' ret += 'static bool match' + str(nr) + '(const Token *tok'+arg2+') {\n'
if varid: if varid:
# if varid is provided, check that it's non-zero # if varid is provided, check that it's non-zero
ret = ret + ' if (varid==0U)\n' ret += ' if (varid==0U)\n'
ret = ret + ' throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");\n' ret += ' throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");\n'
tokens = pattern.split(' ') tokens = pattern.split(' ')
gotoNextToken = '' gotoNextToken = ''
for tok in tokens: for tok in tokens:
if tok == '': if tok == '':
continue continue
ret = ret + gotoNextToken ret += gotoNextToken
gotoNextToken = ' tok = tok->next();\n' gotoNextToken = ' tok = tok->next();\n'
# [abc] # [abc]
if (len(tok) > 2) and (tok[0] == '[') and (tok[-1] == ']'): if (len(tok) > 2) and (tok[0] == '[') and (tok[-1] == ']'):
ret = ret + ' if (!tok || tok->str().size()!=1U || !strchr("'+tok[1:-1]+'", tok->str()[0]))\n' ret += ' if (!tok || tok->str().size()!=1U || !strchr("'+tok[1:-1]+'", tok->str()[0]))\n'
ret = ret + ' return false;\n' ret += ' return false;\n'
# a|b|c # a|b|c
elif tok.find('|') > 0: elif tok.find('|') > 0:
@ -73,11 +73,11 @@ def compilePattern(matchStrs, pattern, nr, varid):
logicalOp = None logicalOp = None
neg = None neg = None
if "" in tokens2: if "" in tokens2:
ret = ret + ' if (tok && (' ret += ' if (tok && ('
logicalOp = ' || ' logicalOp = ' || '
neg = '' neg = ''
else: else:
ret = ret + ' if (!tok || !(' ret += ' if (!tok || !('
logicalOp = ' || ' logicalOp = ' || '
neg = '' neg = ''
first = True first = True
@ -85,28 +85,28 @@ def compilePattern(matchStrs, pattern, nr, varid):
if tok2 == '': if tok2 == '':
continue continue
if not first: if not first:
ret = ret + logicalOp ret += logicalOp
first = False first = False
ret = ret + neg + compileCmd(tok2, matchStrs) ret += neg + compileCmd(tok2, matchStrs)
if "" in tokens2: if "" in tokens2:
ret = ret + '))\n' ret += '))\n'
ret = ret + ' tok = tok->next();\n' ret += ' tok = tok->next();\n'
gotoNextToken = '' gotoNextToken = ''
else: else:
ret = ret + '))\n' ret += '))\n'
ret = ret + ' return false;\n' ret += ' return false;\n'
# !!a # !!a
elif tok[0:2]=="!!": elif tok[0:2]=="!!":
ret = ret + ' if (tok && tok->str() == ' + insertMatchStr(matchStrs, tok[2:]) + ')/* ' + tok[2:] + ' */\n' ret += ' if (tok && tok->str() == ' + insertMatchStr(matchStrs, tok[2:]) + ')/* ' + tok[2:] + ' */\n'
ret = ret + ' return false;\n' ret += ' return false;\n'
gotoNextToken = ' tok = tok ? tok->next() : NULL;\n' gotoNextToken = ' tok = tok ? tok->next() : NULL;\n'
else: else:
ret = ret + ' if (!tok || !' + compileCmd(tok, matchStrs) + ')\n' ret += ' if (!tok || !' + compileCmd(tok, matchStrs) + ')\n'
ret = ret + ' return false;\n' ret += ' return false;\n'
ret = ret + ' return true;\n}\n' ret += ' return true;\n}\n'
return ret return ret
@ -119,17 +119,17 @@ def parseMatch(line, pos1):
while pos < len(line): while pos < len(line):
if inString: if inString:
if line[pos] == '\\': if line[pos] == '\\':
pos = pos + 1 pos += 1
elif line[pos] == '"': elif line[pos] == '"':
inString = False inString = False
elif line[pos] == '"': elif line[pos] == '"':
inString = True inString = True
elif line[pos] == '(': elif line[pos] == '(':
parlevel = parlevel + 1 parlevel += 1
if parlevel == 1: if parlevel == 1:
argstart = pos + 1 argstart = pos + 1
elif line[pos] == ')': elif line[pos] == ')':
parlevel = parlevel - 1 parlevel -= 1
if parlevel == 0: if parlevel == 0:
ret = [] ret = []
ret.append(line[pos1:pos+1]) ret.append(line[pos1:pos+1])
@ -140,7 +140,7 @@ def parseMatch(line, pos1):
elif line[pos] == ',' and parlevel == 1: elif line[pos] == ',' and parlevel == 1:
args.append(line[argstart:pos]) args.append(line[argstart:pos])
argstart = pos + 1 argstart = pos + 1
pos = pos + 1 pos += 1
return None return None
@ -208,8 +208,8 @@ def convertFile(srcname, destname):
if arg3: if arg3:
a3 = ',' + arg3 a3 = ',' + arg3
line = line[:pos1]+'match'+str(patternNumber)+'('+arg1+a3+')'+line[pos1+len(g0):] line = line[:pos1]+'match'+str(patternNumber)+'('+arg1+a3+')'+line[pos1+len(g0):]
matchfunctions = matchfunctions + compilePattern(matchStrs, arg2, patternNumber, arg3) matchfunctions += compilePattern(matchStrs, arg2, patternNumber, arg3)
patternNumber = patternNumber + 1 patternNumber += 1
# Replace plain old C-string comparison with C++ strings # Replace plain old C-string comparison with C++ strings
while True: while True:
@ -228,7 +228,7 @@ def convertFile(srcname, destname):
text = line[startPos+1:endPos-1] text = line[startPos+1:endPos-1]
line = line[:startPos] + insertMatchStr(matchStrs, text) + line[endPos:] line = line[:startPos] + insertMatchStr(matchStrs, text) + line[endPos:]
code = code + line code += line
# Compute string list # Compute string list
stringList = '' stringList = ''