forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddFunctionPrologue.py
116 lines (85 loc) · 2.86 KB
/
AddFunctionPrologue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Assuming all functions begin with ')' followed by '{', just find the matching brace and
# add a line with 'g_pVCR->SyncToken("<random string here>");'
import dlexer
import sys
class BlankStruct:
pass
def MatchParensBack( list, iStart ):
parenCount = -1
for i in range( 0, iStart ):
if list[iStart-i].id == __TOKEN_OPENPAREN:
parenCount += 1
elif list[iStart-i].id == __TOKEN_CLOSEPAREN:
parenCount -= 1
if parenCount == 0:
return iStart - i
return -1
if len( sys.argv ) >= 2:
# Setup the parser.
parser = dlexer.DLexer( 0 )
__TOKEN_NEWLINE = parser.AddToken( '\n' )
__TOKEN_WHITESPACE = parser.AddToken( '[ \\t\\f\\v]+' )
__TOKEN_OPENBRACE = parser.AddToken( '{' )
__TOKEN_CLOSEBRACE = parser.AddToken( '}' )
__TOKEN_OPENPAREN = parser.AddToken( '\(' )
__TOKEN_CLOSEPAREN = parser.AddToken( '\)' )
__TOKEN_COMMENT = parser.AddToken( r"\/\/.*" )
__TOKEN_CONST = parser.AddToken( "const" )
__TOKEN_IF = parser.AddToken( "if" )
__TOKEN_WHILE = parser.AddToken( "while" )
__TOKEN_FOR = parser.AddToken( "for" )
__TOKEN_SWITCH = parser.AddToken( "switch" )
validChars = r"\~\@\#\$\%\^\&\!\,\w\.-/\[\]\<\>\""
__TOKEN_IDENT = parser.AddToken( '[' + validChars + ']+' )
__TOKEN_OPERATOR = parser.AddToken( "\=|\+" )
__TOKEN_SCOPE_OPERATOR = parser.AddToken( "::" )
__TOKEN_IGNORE = parser.AddToken( r"\#|\;|\:|\||\?|\'|\\|\*|\-|\`" )
head = None
# First, read all the tokens into a list.
list = []
parser.BeginReadFile( sys.argv[1] )
while 1:
m = parser.GetToken()
if m:
list.append( m )
else:
break
# Make a list of all the non-whitespace ones.
nw = []
for token in list:
if token.id == __TOKEN_NEWLINE or token.id == __TOKEN_WHITESPACE:
token.iNonWhitespace = -2222
else:
token.iNonWhitespace = len( nw )
nw.append( token )
# Get ready to output sync tokens.
file = open( sys.argv[1], 'r' )
fileLines = file.readlines()
file.close()
curLine = 1
iCur = 0
file = open( sys.argv[1], 'w' )
# Now, search for the patterns we're interested in.
# Look for <ident>::<ident> '(' <idents...> ')' followed by a '{'. This would be a function.
for token in list:
file.write( token.val )
if token.id == __TOKEN_NEWLINE:
curLine += 1
if token.id == __TOKEN_OPENBRACE:
i = token.iNonWhitespace
if i >= 6:
if nw[i-1].id == __TOKEN_CLOSEPAREN:
pos = MatchParensBack( nw, i-2 )
if pos != -1:
if nw[pos-1].id == __TOKEN_IDENT:
#ADD PROLOGUE CODE HERE
#file.write( "\n\tg_pVCR->SyncToken( \"%d_%s\" ); // AUTO-GENERATED SYNC TOKEN\n" % (iCur, nw[pos-1].val) )
iCur += 1
# TEST CODE TO PRINT OUT FUNCTION NAMES
#if nw[pos-2].id == __TOKEN_SCOPE_OPERATOR:
# print "%d: %s::%s" % ( curLine, nw[pos-3].val, nw[pos-1].val )
#else:
# print "%d: %s" % ( curLine, nw[pos-1].val )
file.close()
else:
print "VCRMode_AddSyncTokens <filename>"