## @file
# preprocess source file
#
# Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
#
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
##
# Import Modules
#
import re
import os
import sys
import antlr3
import FileProfile
##define T_CHAR_SPACE ' '
##define T_CHAR_NULL '\0'
##define T_CHAR_CR '\r'
##define T_CHAR_TAB '\t'
##define T_CHAR_LF '\n'
##define T_CHAR_SLASH '/'
##define T_CHAR_BACKSLASH '\\'
##define T_CHAR_DOUBLE_QUOTE '\"'
##define T_CHAR_SINGLE_QUOTE '\''
##define T_CHAR_STAR '*'
##define T_CHAR_HASH '#'
(' ', '\0', '\r', '\t', '\n', '/', '\\', '\"', '\'', '*', '#')
## The collector for source code fragments.
#
# PreprocessFile method should be called prior to ParseFile
#
# GetNext*** procedures mean these procedures will get next token first, then make judgement.
# Get*** procedures mean these procedures will make judgement on current token only.
#
class CodeFragmentCollector:
## The constructor
#
# @param self The object pointer
# @param FileName The file that to be parsed
#
## __IsWhiteSpace() method
#
# Whether char at current FileBufferPos is whitespace
#
# @param self The object pointer
# @param Char The char to test
# @retval True The char is a kind of white space
# @retval False The char is NOT a kind of white space
#
return True
else:
return False
## __SkipWhiteSpace() method
#
# Skip white spaces from current char, return number of chars skipped
#
# @param self The object pointer
# @retval Count The number of chars skipped
#
Count = 0
while not self.__EndOfFile():
Count += 1
else:
return Count
## __EndOfFile() method
#
# Judge current buffer pos is at file end
#
# @param self The object pointer
# @retval True Current File buffer position is at file end
# @retval False Current File buffer position is NOT at file end
#
if NumberOfLines > 0:
return True
return True
else:
return False
## __EndOfLine() method
#
# Judge current buffer pos is at line end
#
# @param self The object pointer
# @retval True Current File buffer position is at line end
# @retval False Current File buffer position is NOT at line end
#
return True
else:
return False
## Rewind() method
#
# Reset file data buffer to the initial state
#
# @param self The object pointer
#
## __UndoOneChar() method
#
# Go back one char in the file buffer
#
# @param self The object pointer
# @retval True Successfully go back one char
# @retval False Not able to go back one char as file beginning reached
#
return False
else:
return True
## __GetOneChar() method
#
# Move forward one char in the file buffer
#
# @param self The object pointer
#
else:
## __CurrentChar() method
#
# Get the char pointed to by the file buffer pointer
#
# @param self The object pointer
# @retval Char Current char
#
# if CurrentChar > 255:
# raise Warning("Non-Ascii char found At Line %d, offset %d" % (self.CurrentLineNumber, self.CurrentOffsetWithinLine), self.FileName, self.CurrentLineNumber)
return CurrentChar
## __NextChar() method
#
# Get the one char pass the char pointed to by the file buffer pointer
#
# @param self The object pointer
# @retval Char Next char
#
else:
## __SetCurrentCharValue() method
#
# Modify the value of current char
#
# @param self The object pointer
# @param Value The new value of current char
#
## __SetCharValue() method
#
# Modify the value of current char
#
# @param self The object pointer
# @param Value The new value of current char
#
## __CurrentLine() method
#
# Get the list that contains current line contents
#
# @param self The object pointer
# @retval List current line contents
#
## __InsertComma() method
#
# Insert ',' to replace PP
#
# @param self The object pointer
# @retval List current line contents
#
return
return
return
if str(self.Profile.FileLinesList[Line]).lstrip().startswith(',') or str(self.Profile.FileLinesList[Line]).lstrip().startswith(';'):
return
## PreprocessFile() method
#
# Preprocess file contents, replace comments with spaces.
# In the end, rewind the file buffer pointer to the beginning
# BUGBUG: No !include statement processing contained in this procedure
# !include statement should be expanded at the same FileLinesList[CurrentLineNumber - 1]
#
# @param self The object pointer
#
CommentObj = None
PPDirectiveObj = None
# HashComment in quoted string " " is ignored.
while not self.__EndOfFile():
InCharLiteral = not InCharLiteral
# meet new line, then no longer in a comment for // and '#'
if HashComment and PPDirectiveObj != None:
else:
if InComment and DoubleSlashComment:
CommentObj = None
PPDirectiveObj = None
if InString or InCharLiteral:
# check for */ comment end
elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:
# self.__SetCurrentCharValue(T_CHAR_SPACE)
# self.__SetCurrentCharValue(T_CHAR_SPACE)
CommentObj = None
# set comments to spaces
elif InComment:
if HashComment:
# // follows hash PP directive
PPDirectiveObj = None
continue
else:
if PPExtend:
else:
# self.__SetCurrentCharValue(T_CHAR_SPACE)
# check for // comment
CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_TWO_SLASH)
# check for '#' comment
# check for /* comment start
CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_SLASH_STAR)
# self.__SetCurrentCharValue( T_CHAR_SPACE)
# self.__SetCurrentCharValue( T_CHAR_SPACE)
else:
if InComment and DoubleSlashComment:
CommentObj = None
PPDirectiveObj = None
# HashComment in quoted string " " is ignored.
while not self.__EndOfFile():
InCharLiteral = not InCharLiteral
# meet new line, then no longer in a comment for // and '#'
if HashComment and PPDirectiveObj != None:
else:
if InComment and DoubleSlashComment:
CommentObj = None
PPDirectiveObj = None
if InString or InCharLiteral:
# check for */ comment end
elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:
CommentObj = None
# set comments to spaces
elif InComment:
if HashComment:
# // follows hash PP directive
PPDirectiveObj = None
continue
else:
# if PPExtend:
# self.__SetCurrentCharValue(T_CHAR_SPACE)
else:
# check for // comment
CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_TWO_SLASH)
# check for '#' comment
# check for /* comment start
CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_SLASH_STAR)
else:
if InComment and DoubleSlashComment:
## ParseFile() method
#
# Parse the file profile buffer to extract fd, fv ... information
# Exception will be raised if syntax error found
#
# @param self The object pointer
#
# restore from ListOfList to ListOfString
FileStringContents = ''
# restore from ListOfList to ListOfString
FileStringContents = ''
FileProfile.CommentList = []
print '/****************************************/'
print '/*************** COMMENTS ***************/'
print '/****************************************/'
print '/****************************************/'
print '/********* PREPROCESS DIRECTIVES ********/'
print '/****************************************/'
print '/****************************************/'
print '/********* VARIABLE DECLARATIONS ********/'
print '/****************************************/'
print '/****************************************/'
print '/********* FUNCTION DEFINITIONS *********/'
print '/****************************************/'
print '/****************************************/'
print '/************ ENUMERATIONS **************/'
print '/****************************************/'
print '/****************************************/'
print '/****************************************/'
print '/****************************************/'
print '/********* PREDICATE EXPRESSIONS ********/'
print '/****************************************/'
print '/****************************************/'
print '/************** TYPEDEFS ****************/'
print '/****************************************/'
if __name__ == "__main__":
print "For Test."