Skip to content

Commit

Permalink
Remove the superficial private constructor and create a separate meth…
Browse files Browse the repository at this point in the history
…od to parse. Update tests.
  • Loading branch information
bclothier committed Jul 5, 2018
1 parent e7197bd commit 294ab90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
24 changes: 13 additions & 11 deletions Rubberduck.Parsing/VBA/VBACodeStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public enum ParserMode
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly CommonTokenStream _tokenStream;
private readonly VBAParser _parser;

public TokenStreamRewriter Rewriter => new TokenStreamRewriter(_tokenStream);
public IParseTree ParseTree { get; }
private readonly string _moduleName;
private readonly ParserMode _mode;

/// <summary>
/// Parse a given string representing a module code. The code passed in must be
Expand All @@ -38,19 +37,22 @@ public enum ParserMode
/// <param name="moduleName">For logging purpose, provide descritpive name for where the virtual module is used</param>
/// <param name="moduleCodeString">A string containing valid module body.</param>
/// <param name="mode">Indicates what parser mode to use. By default we use SLL fallbacking to LL. When mode is explicitly specified, only that mode will be used with no fallback.</param>
public VBACodeStringParser(string moduleName, string moduleCodeString, ParserMode mode = ParserMode.Default) :
this(moduleName,
moduleCodeString)
{
ParseTree = mode == ParserMode.Default ? ParseInternal(moduleName) : ParseInternal(mode);
}

private VBACodeStringParser(string moduleName, string moduleCodeString)
public VBACodeStringParser(string moduleName, string moduleCodeString, ParserMode mode = ParserMode.Default)
{
var tokenStreamProvider = new SimpleVBAModuleTokenStreamProvider();
_tokenStream = tokenStreamProvider.Tokens(moduleCodeString);
_parser = new VBAParser(_tokenStream);
_parser.AddErrorListener(new MainParseExceptionErrorListener(moduleName, ParsePass.CodePanePass));
_moduleName = moduleName;
_mode = mode;
}

public (IParseTree parseTree, TokenStreamRewriter rewriter) Parse()
{
var parseTree = _mode == ParserMode.Default ? ParseInternal(_moduleName) : ParseInternal(_mode);
var rewriter = new TokenStreamRewriter(_tokenStream);

return (parseTree, rewriter);
}

private IParseTree ParseInternal(string moduleName)
Expand Down
12 changes: 7 additions & 5 deletions RubberduckTests/Parsing/VBACodeStringParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void CanParse()
MsgBox ""hi""
End Sub";
var parser = new VBACodeStringParser("test", inputCode);
Assert.IsInstanceOf<IParseTree>(parser.ParseTree);
Assert.IsInstanceOf<IParseTree>(parser.Parse().parseTree);
}

[Test]
Expand All @@ -32,6 +32,7 @@ public void CannotParse()
Assert.Throws<MainParseSyntaxErrorException>(() =>
{
var parser = new VBACodeStringParser("test", inputCode);
parser.Parse();
});
}

Expand All @@ -44,6 +45,7 @@ public void CannotParse_CodeSnippet()
Assert.Throws<MainParseSyntaxErrorException>(() =>
{
var parser = new VBACodeStringParser("test", inputCode);
parser.Parse();
});
}

Expand All @@ -56,7 +58,7 @@ public void ParseTreeIsValid()
MsgBox ""hi""
End Sub";
var parser = new VBACodeStringParser("test", inputCode);
var tree = parser.ParseTree;
var tree = parser.Parse().parseTree;

Assert.AreEqual(inputCode + "<EOF>", tree.GetChild(0).GetText());
}
Expand All @@ -71,7 +73,7 @@ public void GetRewriter()
End Sub";
var parser = new VBACodeStringParser("test", inputCode);

Assert.IsInstanceOf<TokenStreamRewriter>(parser.Rewriter);
Assert.IsInstanceOf<TokenStreamRewriter>(parser.Parse().rewriter);
}

[Test]
Expand All @@ -84,7 +86,7 @@ public void Parse_ExplicitSll()
End Sub";
var parser = new VBACodeStringParser("test", inputCode, VBACodeStringParser.ParserMode.Sll);

Assert.IsInstanceOf<IParseTree>(parser.ParseTree);
Assert.IsInstanceOf<IParseTree>(parser.Parse().parseTree);
}

[Test]
Expand All @@ -97,7 +99,7 @@ public void Parse_ExplicitLl()
End Sub";
var parser = new VBACodeStringParser("test", inputCode, VBACodeStringParser.ParserMode.Ll);

Assert.IsInstanceOf<IParseTree>(parser.ParseTree);
Assert.IsInstanceOf<IParseTree>(parser.Parse().parseTree);
}
}
}

0 comments on commit 294ab90

Please sign in to comment.