public ProcessResult TryProcess(TextReader input, XmlWriter output, XmlResolver resolver) { if (input == null) { throw new ArgumentNullException("input"); } if (output == null) { throw new ArgumentNullException("output"); } LalrProcessor parser = new LalrProcessor(new Tokenizer(input, grammar)); ParseMessage message = parser.Parse(); while (CompiledGrammar.CanContinueParsing(message)) { message = parser.Parse(); } LineInfo position = default(LineInfo); if (parser.CurrentToken != null) { position = parser.CurrentToken.Position; } switch (message) { case ParseMessage.Accept: break; case ParseMessage.LexicalError: return(new ProcessResult(position, string.Format("Lexical error: {0}", parser.CurrentToken))); case ParseMessage.SyntaxError: StringBuilder result = new StringBuilder("Syntax error:"); foreach (Symbol expectedSymbol in parser.GetExpectedTokens()) { result.Append(' '); result.Append(expectedSymbol.Name); } result.Append(" expected"); return(new ProcessResult(position, result.ToString())); case ParseMessage.CommentError: return(new ProcessResult(position, "End of block comment not found")); default: // includes InternalError return(new ProcessResult(position, "Internal error")); } StringBuilder xsltError = new StringBuilder(); XsltArgumentList arguments = new XsltArgumentList(); arguments.XsltMessageEncountered += ((sender, args) => xsltError.Append(args.Message)); using (XmlReader reader = new TokenXmlReader(nametable, parser.CurrentToken)) { transform.Transform(reader, arguments, output, resolver); } if (xsltError.Length > 0) { return(new ProcessResult(default(LineInfo), xsltError.ToString())); } return(new ProcessResult()); }
internal static bool TryParse(string ruleString, out Reduction ruleToken) { using (StringReader reader = new StringReader(ruleString)) { Tokenizer tokenizer = new Tokenizer(reader, ruleGrammar); LalrProcessor processor = new LalrProcessor(tokenizer); ParseMessage message; do { message = processor.Parse(); if (message == ParseMessage.Accept) { ruleToken = (Reduction)processor.CurrentToken; return(true); } } while (CompiledGrammar.CanContinueParsing(message)); } ruleToken = null; return(false); }
public ProcessResult TryProcess(TextReader input, XmlWriter output, XmlResolver resolver) { if (input == null) { throw new ArgumentNullException("input"); } if (output == null) { throw new ArgumentNullException("output"); } LalrProcessor parser = new LalrProcessor(new Tokenizer(input, grammar)); ParseMessage message = parser.Parse(); while (CompiledGrammar.CanContinueParsing(message)) { message = parser.Parse(); } LineInfo position = default(LineInfo); if (parser.CurrentToken != null) { position = parser.CurrentToken.Position; } switch (message) { case ParseMessage.Accept: break; case ParseMessage.LexicalError: return new ProcessResult(position, string.Format("Lexical error: {0}", parser.CurrentToken)); case ParseMessage.SyntaxError: StringBuilder result = new StringBuilder("Syntax error:"); foreach (Symbol expectedSymbol in parser.GetExpectedTokens()) { result.Append(' '); result.Append(expectedSymbol.Name); } result.Append(" expected"); return new ProcessResult(position, result.ToString()); case ParseMessage.CommentError: return new ProcessResult(position, "End of block comment not found"); default: // includes InternalError return new ProcessResult(position, "Internal error"); } StringBuilder xsltError = new StringBuilder(); XsltArgumentList arguments = new XsltArgumentList(); arguments.XsltMessageEncountered += ((sender, args) => xsltError.Append(args.Message)); using (XmlReader reader = new TokenXmlReader(nametable, parser.CurrentToken)) { transform.Transform(reader, arguments, output, resolver); } if (xsltError.Length > 0) { return new ProcessResult(default(LineInfo), xsltError.ToString()); } return new ProcessResult(); }
public void ParseTreeWithTrim() { using (TestStringReader reader = CgtTokenizerTest.GetReader()) { Tokenizer tokenizer = new Tokenizer(reader, grammar); LalrProcessor processor = new LalrProcessor(tokenizer, true); Assert.Equal(true, processor.Trim); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.BlockRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.BlockRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.TokenRead, processor.Parse()); Assert.Equal(typeof(TextToken), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(Reduction), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(Reduction), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Reduction, processor.Parse()); Assert.Equal(typeof(Reduction), processor.CurrentToken.GetType()); Assert.Equal(ParseMessage.Accept, processor.Parse()); Assert.Equal("Expression", processor.CurrentToken.Symbol.Name); Assert.Equal(27, CountTokens(processor.CurrentToken)); } }
internal static bool TryParse(string ruleString, out Reduction ruleToken) { using (StringReader reader = new StringReader(ruleString)) { Tokenizer tokenizer = new Tokenizer(reader, ruleGrammar); LalrProcessor processor = new LalrProcessor(tokenizer); ParseMessage message; do { message = processor.Parse(); if (message == ParseMessage.Accept) { ruleToken = (Reduction)processor.CurrentToken; return true; } } while (CompiledGrammar.CanContinueParsing(message)); } ruleToken = null; return false; }