]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Ecc/CodeFragmentCollector.py
BaseTools: remove uncalled function
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / CodeFragmentCollector.py
CommitLineData
30fdf114
LG
1## @file\r
2# preprocess source file\r
3#\r
231fdbb2 4# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
30fdf114 5#\r
40d841f6 6# This program and the accompanying materials\r
30fdf114
LG
7# are licensed and made available under the terms and conditions of the BSD License\r
8# which accompanies this distribution. The full text of the license may be found at\r
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14\r
15##\r
16# Import Modules\r
17#\r
18\r
19import re\r
1be2ed90 20import Common.LongFilePathOs as os\r
30fdf114
LG
21import sys\r
22\r
23import antlr3\r
24from CLexer import CLexer\r
25from CParser import CParser\r
26\r
27import FileProfile\r
28from CodeFragment import Comment\r
29from CodeFragment import PP_Directive\r
30from ParserWarning import Warning\r
31\r
32\r
33##define T_CHAR_SPACE ' '\r
34##define T_CHAR_NULL '\0'\r
35##define T_CHAR_CR '\r'\r
36##define T_CHAR_TAB '\t'\r
37##define T_CHAR_LF '\n'\r
38##define T_CHAR_SLASH '/'\r
39##define T_CHAR_BACKSLASH '\\'\r
40##define T_CHAR_DOUBLE_QUOTE '\"'\r
41##define T_CHAR_SINGLE_QUOTE '\''\r
42##define T_CHAR_STAR '*'\r
43##define T_CHAR_HASH '#'\r
44\r
45(T_CHAR_SPACE, T_CHAR_NULL, T_CHAR_CR, T_CHAR_TAB, T_CHAR_LF, T_CHAR_SLASH, \\r
46T_CHAR_BACKSLASH, T_CHAR_DOUBLE_QUOTE, T_CHAR_SINGLE_QUOTE, T_CHAR_STAR, T_CHAR_HASH) = \\r
47(' ', '\0', '\r', '\t', '\n', '/', '\\', '\"', '\'', '*', '#')\r
48\r
49SEPERATOR_TUPLE = ('=', '|', ',', '{', '}') \r
50\r
51(T_COMMENT_TWO_SLASH, T_COMMENT_SLASH_STAR) = (0, 1)\r
52\r
53(T_PP_INCLUDE, T_PP_DEFINE, T_PP_OTHERS) = (0, 1, 2)\r
54\r
55## The collector for source code fragments.\r
56#\r
57# PreprocessFile method should be called prior to ParseFile\r
58#\r
59# GetNext*** procedures mean these procedures will get next token first, then make judgement.\r
60# Get*** procedures mean these procedures will make judgement on current token only.\r
61# \r
62class CodeFragmentCollector:\r
63 ## The constructor\r
64 #\r
65 # @param self The object pointer\r
66 # @param FileName The file that to be parsed\r
67 #\r
68 def __init__(self, FileName):\r
69 self.Profile = FileProfile.FileProfile(FileName)\r
70 self.Profile.FileLinesList.append(T_CHAR_LF)\r
71 self.FileName = FileName\r
72 self.CurrentLineNumber = 1\r
73 self.CurrentOffsetWithinLine = 0\r
74\r
75 self.__Token = ""\r
76 self.__SkippedChars = ""\r
77\r
30fdf114
LG
78 ## __SkipWhiteSpace() method\r
79 #\r
80 # Skip white spaces from current char, return number of chars skipped\r
81 #\r
82 # @param self The object pointer\r
83 # @retval Count The number of chars skipped\r
84 #\r
85 def __SkipWhiteSpace(self):\r
86 Count = 0\r
87 while not self.__EndOfFile():\r
88 Count += 1\r
89 if self.__CurrentChar() in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_LF, T_CHAR_SPACE, T_CHAR_TAB):\r
90 self.__SkippedChars += str(self.__CurrentChar())\r
91 self.__GetOneChar()\r
92\r
93 else:\r
94 Count = Count - 1\r
95 return Count\r
96\r
97 ## __EndOfFile() method\r
98 #\r
99 # Judge current buffer pos is at file end\r
100 #\r
101 # @param self The object pointer\r
102 # @retval True Current File buffer position is at file end\r
103 # @retval False Current File buffer position is NOT at file end\r
104 #\r
105 def __EndOfFile(self):\r
106 NumberOfLines = len(self.Profile.FileLinesList)\r
107 SizeOfLastLine = NumberOfLines\r
108 if NumberOfLines > 0:\r
109 SizeOfLastLine = len(self.Profile.FileLinesList[-1])\r
110 \r
111 if self.CurrentLineNumber == NumberOfLines and self.CurrentOffsetWithinLine >= SizeOfLastLine - 1:\r
112 return True\r
113 elif self.CurrentLineNumber > NumberOfLines:\r
114 return True\r
115 else:\r
116 return False\r
117\r
118 ## __EndOfLine() method\r
119 #\r
120 # Judge current buffer pos is at line end\r
121 #\r
122 # @param self The object pointer\r
123 # @retval True Current File buffer position is at line end\r
124 # @retval False Current File buffer position is NOT at line end\r
125 #\r
126 def __EndOfLine(self):\r
127 SizeOfCurrentLine = len(self.Profile.FileLinesList[self.CurrentLineNumber - 1])\r
128 if self.CurrentOffsetWithinLine >= SizeOfCurrentLine - 1:\r
129 return True\r
130 else:\r
131 return False\r
132 \r
133 ## Rewind() method\r
134 #\r
135 # Reset file data buffer to the initial state\r
136 #\r
137 # @param self The object pointer\r
138 #\r
139 def Rewind(self):\r
140 self.CurrentLineNumber = 1\r
141 self.CurrentOffsetWithinLine = 0\r
142 \r
143 ## __UndoOneChar() method\r
144 #\r
145 # Go back one char in the file buffer\r
146 #\r
147 # @param self The object pointer\r
148 # @retval True Successfully go back one char\r
149 # @retval False Not able to go back one char as file beginning reached\r
150 # \r
151 def __UndoOneChar(self):\r
152 \r
153 if self.CurrentLineNumber == 1 and self.CurrentOffsetWithinLine == 0:\r
154 return False\r
155 elif self.CurrentOffsetWithinLine == 0:\r
156 self.CurrentLineNumber -= 1\r
157 self.CurrentOffsetWithinLine = len(self.__CurrentLine()) - 1\r
158 else:\r
159 self.CurrentOffsetWithinLine -= 1\r
160 return True\r
161 \r
162 ## __GetOneChar() method\r
163 #\r
164 # Move forward one char in the file buffer\r
165 #\r
166 # @param self The object pointer\r
167 # \r
168 def __GetOneChar(self):\r
169 if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r
170 self.CurrentLineNumber += 1\r
171 self.CurrentOffsetWithinLine = 0\r
172 else:\r
173 self.CurrentOffsetWithinLine += 1\r
174\r
175 ## __CurrentChar() method\r
176 #\r
177 # Get the char pointed to by the file buffer pointer\r
178 #\r
179 # @param self The object pointer\r
180 # @retval Char Current char\r
181 # \r
182 def __CurrentChar(self):\r
183 CurrentChar = self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine]\r
184# if CurrentChar > 255:\r
185# raise Warning("Non-Ascii char found At Line %d, offset %d" % (self.CurrentLineNumber, self.CurrentOffsetWithinLine), self.FileName, self.CurrentLineNumber)\r
186 return CurrentChar\r
187 \r
188 ## __NextChar() method\r
189 #\r
190 # Get the one char pass the char pointed to by the file buffer pointer\r
191 #\r
192 # @param self The object pointer\r
193 # @retval Char Next char\r
194 #\r
195 def __NextChar(self):\r
196 if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r
197 return self.Profile.FileLinesList[self.CurrentLineNumber][0]\r
198 else:\r
199 return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine + 1]\r
200 \r
201 ## __SetCurrentCharValue() method\r
202 #\r
203 # Modify the value of current char\r
204 #\r
205 # @param self The object pointer\r
206 # @param Value The new value of current char\r
207 #\r
208 def __SetCurrentCharValue(self, Value):\r
209 self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine] = Value\r
210 \r
211 ## __SetCharValue() method\r
212 #\r
213 # Modify the value of current char\r
214 #\r
215 # @param self The object pointer\r
216 # @param Value The new value of current char\r
217 #\r
218 def __SetCharValue(self, Line, Offset, Value):\r
219 self.Profile.FileLinesList[Line - 1][Offset] = Value\r
220 \r
221 ## __CurrentLine() method\r
222 #\r
223 # Get the list that contains current line contents\r
224 #\r
225 # @param self The object pointer\r
226 # @retval List current line contents\r
227 #\r
228 def __CurrentLine(self):\r
229 return self.Profile.FileLinesList[self.CurrentLineNumber - 1]\r
230 \r
231 ## __InsertComma() method\r
232 #\r
233 # Insert ',' to replace PP\r
234 #\r
235 # @param self The object pointer\r
236 # @retval List current line contents\r
237 #\r
238 def __InsertComma(self, Line):\r
239 \r
240 \r
241 if self.Profile.FileLinesList[Line - 1][0] != T_CHAR_HASH:\r
242 BeforeHashPart = str(self.Profile.FileLinesList[Line - 1]).split(T_CHAR_HASH)[0]\r
243 if BeforeHashPart.rstrip().endswith(T_CHAR_COMMA) or BeforeHashPart.rstrip().endswith(';'):\r
244 return\r
245 \r
246 if Line - 2 >= 0 and str(self.Profile.FileLinesList[Line - 2]).rstrip().endswith(','):\r
247 return\r
248 \r
249 if Line - 2 >= 0 and str(self.Profile.FileLinesList[Line - 2]).rstrip().endswith(';'):\r
250 return\r
251 \r
252 if str(self.Profile.FileLinesList[Line]).lstrip().startswith(',') or str(self.Profile.FileLinesList[Line]).lstrip().startswith(';'):\r
253 return\r
254 \r
255 self.Profile.FileLinesList[Line - 1].insert(self.CurrentOffsetWithinLine, ',')\r
256 \r
257 ## PreprocessFile() method\r
258 #\r
259 # Preprocess file contents, replace comments with spaces.\r
260 # In the end, rewind the file buffer pointer to the beginning\r
261 # BUGBUG: No !include statement processing contained in this procedure\r
262 # !include statement should be expanded at the same FileLinesList[CurrentLineNumber - 1]\r
263 #\r
264 # @param self The object pointer\r
265 # \r
266 def PreprocessFile(self):\r
267\r
268 self.Rewind()\r
269 InComment = False\r
270 DoubleSlashComment = False\r
271 HashComment = False\r
272 PPExtend = False\r
273 CommentObj = None\r
274 PPDirectiveObj = None\r
275 # HashComment in quoted string " " is ignored.\r
276 InString = False\r
277 InCharLiteral = False \r
278 \r
279 self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesListFromFile]\r
280 while not self.__EndOfFile():\r
281 \r
282 if not InComment and self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE:\r
283 InString = not InString\r
284 \r
285 if not InComment and self.__CurrentChar() == T_CHAR_SINGLE_QUOTE:\r
286 InCharLiteral = not InCharLiteral\r
287 # meet new line, then no longer in a comment for // and '#'\r
288 if self.__CurrentChar() == T_CHAR_LF:\r
4231a819 289 if HashComment and PPDirectiveObj is not None:\r
30fdf114
LG
290 if PPDirectiveObj.Content.rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r
291 PPDirectiveObj.Content += T_CHAR_LF\r
292 PPExtend = True\r
293 else:\r
294 PPExtend = False\r
295 \r
296 EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
297 \r
298 if InComment and DoubleSlashComment:\r
299 InComment = False\r
300 DoubleSlashComment = False\r
301 CommentObj.Content += T_CHAR_LF\r
302 CommentObj.EndPos = EndLinePos\r
303 FileProfile.CommentList.append(CommentObj)\r
304 CommentObj = None\r
305 if InComment and HashComment and not PPExtend:\r
306 InComment = False\r
307 HashComment = False\r
308 PPDirectiveObj.Content += T_CHAR_LF\r
309 PPDirectiveObj.EndPos = EndLinePos\r
310 FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
311 PPDirectiveObj = None\r
312 \r
313 if InString or InCharLiteral:\r
314 CurrentLine = "".join(self.__CurrentLine())\r
315 if CurrentLine.rstrip(T_CHAR_LF).rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r
316 SlashIndex = CurrentLine.rindex(T_CHAR_BACKSLASH)\r
317 self.__SetCharValue(self.CurrentLineNumber, SlashIndex, T_CHAR_SPACE)\r
318 \r
319 if InComment and not DoubleSlashComment and not HashComment:\r
320 CommentObj.Content += T_CHAR_LF\r
321 self.CurrentLineNumber += 1\r
322 self.CurrentOffsetWithinLine = 0 \r
323 # check for */ comment end\r
324 elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:\r
325 CommentObj.Content += self.__CurrentChar()\r
326# self.__SetCurrentCharValue(T_CHAR_SPACE)\r
327 self.__GetOneChar()\r
328 CommentObj.Content += self.__CurrentChar()\r
329# self.__SetCurrentCharValue(T_CHAR_SPACE)\r
330 CommentObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
331 FileProfile.CommentList.append(CommentObj)\r
332 CommentObj = None\r
333 self.__GetOneChar()\r
334 InComment = False\r
335 # set comments to spaces\r
336 elif InComment: \r
337 if HashComment:\r
338 # // follows hash PP directive\r
339 if self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r
340 InComment = False\r
341 HashComment = False\r
342 PPDirectiveObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine - 1)\r
343 FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
344 PPDirectiveObj = None\r
345 continue\r
346 else:\r
347 PPDirectiveObj.Content += self.__CurrentChar()\r
348 if PPExtend:\r
349 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
350 else:\r
351 CommentObj.Content += self.__CurrentChar()\r
352# self.__SetCurrentCharValue(T_CHAR_SPACE)\r
353 self.__GetOneChar()\r
354 # check for // comment\r
355 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r
356 InComment = True\r
357 DoubleSlashComment = True\r
358 CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_TWO_SLASH)\r
359 # check for '#' comment\r
360 elif self.__CurrentChar() == T_CHAR_HASH and not InString and not InCharLiteral:\r
361 InComment = True\r
362 HashComment = True \r
363 PPDirectiveObj = PP_Directive('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None)\r
364 # check for /* comment start\r
365 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR:\r
366 CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_SLASH_STAR)\r
367 CommentObj.Content += self.__CurrentChar()\r
368# self.__SetCurrentCharValue( T_CHAR_SPACE)\r
369 self.__GetOneChar()\r
370 CommentObj.Content += self.__CurrentChar()\r
371# self.__SetCurrentCharValue( T_CHAR_SPACE)\r
372 self.__GetOneChar()\r
373 InComment = True\r
374 else:\r
375 self.__GetOneChar()\r
376 \r
377 EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
378 \r
379 if InComment and DoubleSlashComment:\r
380 CommentObj.EndPos = EndLinePos\r
381 FileProfile.CommentList.append(CommentObj)\r
382 if InComment and HashComment and not PPExtend:\r
383 PPDirectiveObj.EndPos = EndLinePos\r
384 FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
385\r
386 self.Rewind()\r
387\r
388 def PreprocessFileWithClear(self):\r
389\r
390 self.Rewind()\r
391 InComment = False\r
392 DoubleSlashComment = False\r
393 HashComment = False\r
394 PPExtend = False\r
395 CommentObj = None\r
396 PPDirectiveObj = None\r
397 # HashComment in quoted string " " is ignored.\r
398 InString = False\r
399 InCharLiteral = False \r
400\r
401 self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesListFromFile]\r
402 while not self.__EndOfFile():\r
403 \r
404 if not InComment and self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE:\r
405 InString = not InString\r
406 \r
407 if not InComment and self.__CurrentChar() == T_CHAR_SINGLE_QUOTE:\r
408 InCharLiteral = not InCharLiteral\r
409 # meet new line, then no longer in a comment for // and '#'\r
410 if self.__CurrentChar() == T_CHAR_LF:\r
4231a819 411 if HashComment and PPDirectiveObj is not None:\r
30fdf114
LG
412 if PPDirectiveObj.Content.rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r
413 PPDirectiveObj.Content += T_CHAR_LF\r
414 PPExtend = True\r
415 else:\r
416 PPExtend = False\r
417 \r
418 EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
419 \r
420 if InComment and DoubleSlashComment:\r
421 InComment = False\r
422 DoubleSlashComment = False\r
423 CommentObj.Content += T_CHAR_LF\r
424 CommentObj.EndPos = EndLinePos\r
425 FileProfile.CommentList.append(CommentObj)\r
426 CommentObj = None\r
427 if InComment and HashComment and not PPExtend:\r
428 InComment = False\r
429 HashComment = False\r
430 PPDirectiveObj.Content += T_CHAR_LF\r
431 PPDirectiveObj.EndPos = EndLinePos\r
432 FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
433 PPDirectiveObj = None\r
434 \r
435 if InString or InCharLiteral:\r
436 CurrentLine = "".join(self.__CurrentLine())\r
437 if CurrentLine.rstrip(T_CHAR_LF).rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r
438 SlashIndex = CurrentLine.rindex(T_CHAR_BACKSLASH)\r
439 self.__SetCharValue(self.CurrentLineNumber, SlashIndex, T_CHAR_SPACE)\r
440 \r
441 if InComment and not DoubleSlashComment and not HashComment:\r
442 CommentObj.Content += T_CHAR_LF\r
443 self.CurrentLineNumber += 1\r
444 self.CurrentOffsetWithinLine = 0 \r
445 # check for */ comment end\r
446 elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:\r
447 CommentObj.Content += self.__CurrentChar()\r
448 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
449 self.__GetOneChar()\r
450 CommentObj.Content += self.__CurrentChar()\r
451 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
452 CommentObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
453 FileProfile.CommentList.append(CommentObj)\r
454 CommentObj = None\r
455 self.__GetOneChar()\r
456 InComment = False\r
457 # set comments to spaces\r
458 elif InComment: \r
459 if HashComment:\r
460 # // follows hash PP directive\r
461 if self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r
462 InComment = False\r
463 HashComment = False\r
464 PPDirectiveObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine - 1)\r
465 FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
466 PPDirectiveObj = None\r
467 continue\r
468 else:\r
469 PPDirectiveObj.Content += self.__CurrentChar()\r
470# if PPExtend:\r
471# self.__SetCurrentCharValue(T_CHAR_SPACE)\r
472 else:\r
473 CommentObj.Content += self.__CurrentChar()\r
474 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
475 self.__GetOneChar()\r
476 # check for // comment\r
477 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r
478 InComment = True\r
479 DoubleSlashComment = True\r
480 CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_TWO_SLASH)\r
481 # check for '#' comment\r
482 elif self.__CurrentChar() == T_CHAR_HASH and not InString and not InCharLiteral:\r
483 InComment = True\r
484 HashComment = True \r
485 PPDirectiveObj = PP_Directive('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None)\r
486 # check for /* comment start\r
487 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR:\r
488 CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_SLASH_STAR)\r
489 CommentObj.Content += self.__CurrentChar()\r
490 self.__SetCurrentCharValue( T_CHAR_SPACE)\r
491 self.__GetOneChar()\r
492 CommentObj.Content += self.__CurrentChar()\r
493 self.__SetCurrentCharValue( T_CHAR_SPACE)\r
494 self.__GetOneChar()\r
495 InComment = True\r
496 else:\r
497 self.__GetOneChar()\r
498\r
499 EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
500 \r
501 if InComment and DoubleSlashComment:\r
502 CommentObj.EndPos = EndLinePos\r
503 FileProfile.CommentList.append(CommentObj)\r
504 if InComment and HashComment and not PPExtend:\r
505 PPDirectiveObj.EndPos = EndLinePos\r
506 FileProfile.PPDirectiveList.append(PPDirectiveObj)\r
507 self.Rewind()\r
508\r
509 ## ParseFile() method\r
510 #\r
511 # Parse the file profile buffer to extract fd, fv ... information\r
512 # Exception will be raised if syntax error found\r
513 #\r
514 # @param self The object pointer\r
515 #\r
516 def ParseFile(self):\r
517 self.PreprocessFile()\r
518 # restore from ListOfList to ListOfString\r
519 self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r
520 FileStringContents = ''\r
521 for fileLine in self.Profile.FileLinesList:\r
522 FileStringContents += fileLine\r
523 cStream = antlr3.StringStream(FileStringContents)\r
524 lexer = CLexer(cStream)\r
525 tStream = antlr3.CommonTokenStream(lexer)\r
526 parser = CParser(tStream)\r
527 parser.translation_unit()\r
528 \r
529 def ParseFileWithClearedPPDirective(self):\r
530 self.PreprocessFileWithClear()\r
531 # restore from ListOfList to ListOfString\r
532 self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r
533 FileStringContents = ''\r
534 for fileLine in self.Profile.FileLinesList:\r
535 FileStringContents += fileLine\r
536 cStream = antlr3.StringStream(FileStringContents)\r
537 lexer = CLexer(cStream)\r
538 tStream = antlr3.CommonTokenStream(lexer)\r
539 parser = CParser(tStream)\r
540 parser.translation_unit()\r
541 \r
542 def CleanFileProfileBuffer(self):\r
543 FileProfile.CommentList = []\r
544 FileProfile.PPDirectiveList = []\r
545 FileProfile.PredicateExpressionList = []\r
546 FileProfile.FunctionDefinitionList = []\r
547 FileProfile.VariableDeclarationList = []\r
548 FileProfile.EnumerationDefinitionList = []\r
549 FileProfile.StructUnionDefinitionList = []\r
550 FileProfile.TypedefDefinitionList = []\r
551 FileProfile.FunctionCallingList = []\r
552 \r
553 def PrintFragments(self):\r
554 \r
555 print '################# ' + self.FileName + '#####################'\r
556 \r
557 print '/****************************************/'\r
558 print '/*************** COMMENTS ***************/'\r
559 print '/****************************************/'\r
560 for comment in FileProfile.CommentList:\r
561 print str(comment.StartPos) + comment.Content\r
562 \r
563 print '/****************************************/'\r
564 print '/********* PREPROCESS DIRECTIVES ********/'\r
565 print '/****************************************/'\r
566 for pp in FileProfile.PPDirectiveList:\r
567 print str(pp.StartPos) + pp.Content\r
568 \r
569 print '/****************************************/'\r
570 print '/********* VARIABLE DECLARATIONS ********/'\r
571 print '/****************************************/'\r
572 for var in FileProfile.VariableDeclarationList:\r
573 print str(var.StartPos) + var.Modifier + ' '+ var.Declarator\r
574 \r
575 print '/****************************************/'\r
576 print '/********* FUNCTION DEFINITIONS *********/'\r
577 print '/****************************************/'\r
578 for func in FileProfile.FunctionDefinitionList:\r
579 print str(func.StartPos) + func.Modifier + ' '+ func.Declarator + ' ' + str(func.NamePos)\r
580 \r
581 print '/****************************************/'\r
582 print '/************ ENUMERATIONS **************/'\r
583 print '/****************************************/'\r
584 for enum in FileProfile.EnumerationDefinitionList:\r
585 print str(enum.StartPos) + enum.Content\r
586 \r
587 print '/****************************************/'\r
588 print '/*********** STRUCTS/UNIONS *************/'\r
589 print '/****************************************/'\r
590 for su in FileProfile.StructUnionDefinitionList:\r
591 print str(su.StartPos) + su.Content\r
592 \r
593 print '/****************************************/'\r
594 print '/********* PREDICATE EXPRESSIONS ********/'\r
595 print '/****************************************/'\r
596 for predexp in FileProfile.PredicateExpressionList:\r
597 print str(predexp.StartPos) + predexp.Content\r
598 \r
599 print '/****************************************/' \r
600 print '/************** TYPEDEFS ****************/'\r
601 print '/****************************************/'\r
602 for typedef in FileProfile.TypedefDefinitionList:\r
603 print str(typedef.StartPos) + typedef.ToType\r
604 \r
605if __name__ == "__main__":\r
606 \r
607 collector = CodeFragmentCollector(sys.argv[1])\r
608 collector.PreprocessFile()\r
609 print "For Test."\r