Skip to main content
2 of 4
added more regex stuff
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54

Couple of things I would do to the AttributeParser abstract factory:

  1. Bring the creation of the Regex out of the Parse method since it is the same each call.

  2. The if..else each with a return in it is a bit wordy. I like the ternary operator just for these instances.

  3. Same advice in (1) goes for the Regexes in the ParseFileHeader, ParseDeclarations and ParseMembers methods of the CodeFileParser class.

  4. 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);
    }
}
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54