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