another intermediate check-in while tying together new function prototype parser with header_parser

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@94 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mkarlesky
2009-05-14 13:47:56 +00:00
parent 500c30a5ce
commit 17631c66f8
2 changed files with 30 additions and 21 deletions
+15 -21
View File
@@ -1,7 +1,7 @@
class CMockHeaderParser
attr_accessor :src_lines, :prototypes, :c_attributes
attr_reader :src_lines, :prototypes, :c_attributes
def initialize(parser, source, cfg)
@src_lines = []
@@ -13,20 +13,22 @@ class CMockHeaderParser
end
def parse
mod = {:includes => nil, :functions => []}
hash = {:functions => []}
# build prototype list
extract_prototypes
# parse all prototyes into hashes of components and add to array
@prototypes.each {|prototype| mod[:functions] << parse_prototype(prototype)} if (@prototypes.length > 0)
return mod
@prototypes.each do |prototype|
hash[:functions] << parse_prototype(prototype) if (@prototypes.length > 0)
end
return hash
end
private
def import_source(source)
# look for any edge cases of typedef'd void;
# void must be void for cmock AndReturn calls to process properly.
# to a certain extent, these replacements assume we're chewing on pre-processed header files
# void must be void for cmock _ExpectAndReturn calls to process properly.
# to a certain extent, this action assumes we're chewing on pre-processed header files
void_types = source.scan(/typedef\s+(\(\s*)?void(\s*\))?\s+([\w\d]+)\s*;/)
void_types.each {|type| source.gsub!(/#{type}/, 'void')} if void_types.size > 0
@@ -35,29 +37,21 @@ class CMockHeaderParser
source.gsub!(/\/\/.*$/, '') # remove line comments
source.gsub!(/#.*/, '') # remove preprocessor statements
source.gsub!(/typedef.*/, '') # remove typedef statements
source.gsub!(/^\s+/, '') # remove excessive white space
source.gsub!(/\s+$/, '') # remove excessive white space
source.gsub!(/\s+/, ' ') # remove excessive white space
source.gsub!(/\s*=\s*['"a-zA-Z0-9_\.]+\s*/, '') # remove default value statements from argument lists
source.gsub!(/^\s+/, '') # remove extra white space
source.gsub!(/\s+$/, '') # remove extra white space
source.gsub!(/\s+/, ' ') # remove extra white space
@src_lines = source.split(/\s*;\s*/) # split source at end of statements (removing extra white space)
@src_lines.delete_if {|line| line.strip.length == 0} # remove blank lines
end
def c_attributes=(value)
@c_attributes = value
@attribute_match = Regexp.compile(%|(#{@c_attributes.join('|')}\s+)*|)
end
def extract_prototypes
# build array of function prototypes
@src_lines.each do |line|
# build array of function prototypes
if (line =~ @prototype_parse_matcher)
# (remove any default parameter statements from argument lists while scanning)
line.gsub!(/=\s*[a-zA-Z0-9_\.]+\s*/, '')
@prototypes << line
end
@prototypes << line if (line =~ @prototype_parse_matcher)
end
return @prototypes
end
def parse_prototype(prototype)
+15
View File
@@ -134,6 +134,21 @@ class CMockHeaderParserTest < Test::Unit::TestCase
assert_equal(expected, @parser.src_lines)
end
should "strip default values from function parameter lists" do
source =
"void Foo(int a = 57, float b=37.52, char c= 'd', char* e=\"junk\");\n"
@parser = CMockHeaderParser.new(@prototype_parser, source, @config)
expected =
[
"void Foo(int a, float b, char c, char* e)"
]
assert_equal(expected, @parser.src_lines)
end
should "raise upon prototype parsing failure" do
source =