public void TestParser(string filename, StringBuilder output) { try { // Create a new file by changing CRLFs to LFs and generate a new steam // or the tokens generated by the lexer will always have off by one errors TestCommandHandler commandHandler = new TestCommandHandler(output); string input = File.ReadAllText(filename).Replace("\r\n", "\n"); var inputStream = GenerateStreamFromString(input); StreamReader streamReader = new StreamReader(inputStream); using (Parser parser = new Parser( commandHandler, new TestVariableResolver(output), streamReader, filename)) { commandHandler.SetParser(parser); parser.Parse(); } } catch (BatchParserException ex) { output.AppendLine(string.Format(CultureInfo.CurrentCulture, "[PARSER ERROR: code {0} at {1} - {2} in {3}, token text: {4}, message: {5}]", ex.ErrorCode, GetPositionString(ex.Begin), GetPositionString(ex.End), GetFilenameOnly(ex.Begin.Filename), ex.Text, ex.Message)); } }
public void VerifyThrowOnUnresolvedVariable() { string script = "print '$(NotDefined)'"; StringBuilder output = new StringBuilder(); TestCommandHandler handler = new TestCommandHandler(output); IVariableResolver resolver = new TestVariableResolver(new StringBuilder()); Parser p = new Parser( handler, resolver, new StringReader(script), "test"); p.ThrowOnUnresolvedVariable = true; handler.SetParser(p); Assert.Throws <BatchParserException>(() => p.Parse()); }
public void TestParser(string filename, StringBuilder output) { try { TestCommandHandler commandHandler = new TestCommandHandler(output); Parser parser = new Parser( commandHandler, new TestVariableResolver(output), new StreamReader(File.Open(filename, FileMode.Open)), filename); commandHandler.SetParser(parser); parser.Parse(); } catch (BatchParserException ex) { output.AppendLine(string.Format(CultureInfo.CurrentCulture, "[PARSER ERROR: code {0} at {1} - {2} in {3}, token text: {4}, message: {5}]", ex.ErrorCode, GetPositionString(ex.Begin), GetPositionString(ex.End), GetFilenameOnly(ex.Begin.Filename), ex.Text, ex.Message)); } }