Couple of things I would do to the AttributeParser abstract factory:
Bring the creation of the
Regexout of theParsemethod since it is the same each call.The
if..elseeach with areturnin it is a bit wordy. I like the ternary operator just for these instances.Same advice in (1) goes for the
Regexes in theParseFileHeader,ParseDeclarationsandParseMembersmethods of theCodeFileParserclass.In the
ParseDeclarationsmethod, I'd reverse theif (isDeclarationSection) { ... }intoif (!isDeclarationSection) { continuelcontinue; }as I'm a proponent of "fail fast".ParseMembersis one ginormous method. I can't quite tell exactly everything it does. Or, more to the point, it does seem to do everything. I see a lot ofif..elses around that look like good break-up areas into other methods - perhapsParseProperty,ParseLoop,ParseConditional,ParseMethod, etc.I'm going to noodle on the other parts of the code and update this answer in a bit.
The refinagled code:
public class AttributeParser : IAttributeParser
{
private const string Syntax = @"^Attribute\s(?<Member>[a-zA-Z]+\.)?(?<Name>VB_\w+)\s=\s(?<Value>.*)$";
private static readonly Regex regex = new Regex(Syntax, RegexOptions.Compiled);
public IAttribute Parse(string instruction)
{
if (!regex.IsMatch(instruction))
{
return null;
}
var match = regex.Match(instruction);
var member = match.Groups["Member"].Value;
var name = match.Groups["Name"].Value;
var value = match.Groups["Value"].Value;
return string.IsNullOrEmpty(member)
? new Attribute(name, value)
: new MemberAttribute(name, value, member);
}
}