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