public INode ParseText(string text)
 {
     text = text.Trim();
     using (ICSharpCode.NRefactory.IParser parser = ICSharpCode.NRefactory.ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(text))) {
         if (text.EndsWith(";") || text.EndsWith("}"))
         {
             BlockStatement block = null;
             try {
                 block      = parser.ParseBlock();
                 LastErrors = parser.Errors;
             } catch (Exception) {
             }
             if (block != null)
             {
                 return(block);
             }
         }
         return(parser.ParseExpression());
     }
 }
Ejemplo n.º 2
0
        string GetFormattedText(PolicyContainer policyParent, string input)
        {
            hasErrors = false;
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor();

            SetFormatOptions(outputVisitor, policyParent);

            outputVisitor.OutputFormatter.IndentationLevel = startIndentLevel;
            using (ICSharpCode.NRefactory.IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(input))) {
                parser.Parse();
                hasErrors = parser.Errors.Count != 0;
                //				if (hasErrors)
                //					Console.WriteLine (parser.Errors.ErrorOutput);
                IList <ISpecial> specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
                if (parser.Errors.Count == 0)
                {
                    using (SpecialNodesInserter.Install(specials, outputVisitor)) {
                        parser.CompilationUnit.AcceptVisitor(outputVisitor, null);
                    }
                    return(outputVisitor.Text);
                }
            }
            //			Console.WriteLine ("trying to parse block.");
            using (ICSharpCode.NRefactory.IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(input))) {
                BlockStatement blockStatement = parser.ParseBlock();
                hasErrors = parser.Errors.Count != 0;
                //				if (hasErrors)
                //					Console.WriteLine (parser.Errors.ErrorOutput);
                IList <ISpecial> specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
                if (parser.Errors.Count == 0)
                {
                    StringBuilder result = new StringBuilder();
                    using (var inserter = SpecialNodesInserter.Install(specials, outputVisitor)) {
                        foreach (ICSharpCode.NRefactory.Ast.INode node in blockStatement.Children)
                        {
                            node.AcceptVisitor(outputVisitor, null);
                            //							result.AppendLine (outputVisitor.Text);
                        }
                        if (!outputVisitor.OutputFormatter.LastCharacterIsNewLine)
                        {
                            outputVisitor.OutputFormatter.NewLine();
                        }
                        inserter.Finish();
                        result.AppendLine(outputVisitor.Text);
                    }
                    return(result.ToString());
                }
            }
            //			Console.WriteLine ("trying to parse expression.");
            using (ICSharpCode.NRefactory.IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(input))) {
                Expression expression = parser.ParseExpression();
                hasErrors = parser.Errors.Count != 0;
                //				if (hasErrors)
                //					Console.WriteLine (parser.Errors.ErrorOutput);
                IList <ISpecial> specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
                if (parser.Errors.Count == 0)
                {
                    using (SpecialNodesInserter.Install(specials, outputVisitor)) {
                        expression.AcceptVisitor(outputVisitor, null);
                    }
                    return(outputVisitor.Text);
                }
            }
            return(input);
        }