Beispiel #1
0
        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 SmokeTest() {
			LalrProcessor processor = new LalrProcessor(new Tokenizer(new StringReader("((100+5.0)/\r\n(4.5+.5))-\r\n12345.4e+1"), grammar), true);
			Assert.Equal(ParseMessage.Accept, processor.ParseAll());
			XPathDocument doc;
			using (TokenXmlReader reader = new TokenXmlReader(null, processor.CurrentToken)) {
				doc = new XPathDocument(reader);
			}
			Trace.Write("XML: " + doc.CreateNavigator().OuterXml);
		}
 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();
 }