ICSharpCode.NRefactory.ParserFactory.CreateParser C# (CSharp) Method

CreateParser() public static method

public static CreateParser ( SupportedLanguage language, TextReader textReader ) : IParser
language SupportedLanguage
textReader TextReader
return IParser
		public static IParser CreateParser(SupportedLanguage language, TextReader textReader)
		{
			Parser.ILexer lexer = CreateLexer(language, textReader);
			switch (language) {
				case SupportedLanguage.CSharp:
					return new ICSharpCode.NRefactory.Parser.CSharp.Parser(lexer);
				case SupportedLanguage.VBNet:
					return new ICSharpCode.NRefactory.Parser.VB.Parser(lexer);
			}
			throw new System.NotSupportedException(language + " not supported.");
		}
		

Same methods

ParserFactory::CreateParser ( string fileName ) : IParser
ParserFactory::CreateParser ( string fileName, Encoding encoding ) : IParser

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
        /// members.
        /// </summary>
        public INode Parse(string code)
        {
            IParser parser = ParserFactory.CreateParser(language, new StringReader(code));

            parser.Parse();
            this.Errors      = parser.Errors;
            this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
            this.SnippetType = SnippetType.CompilationUnit;
            INode result = parser.CompilationUnit;

            if (this.Errors.Count > 0)
            {
                if (language == SupportedLanguage.CSharp)
                {
                    // SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
                    parser = ParserFactory.CreateParser(language, new StringReader(code + ";"));
                }
                else
                {
                    parser = ParserFactory.CreateParser(language, new StringReader(code));
                }
                Expression expression = parser.ParseExpression();
                if (expression != null && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors      = parser.Errors;
                    this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType = SnippetType.Expression;
                    result           = expression;
                }
            }
            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(language, new StringReader(code));
                BlockStatement block = parser.ParseBlock();
                if (block != null && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors      = parser.Errors;
                    this.Specials    = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType = SnippetType.Statements;
                    result           = block;
                }
            }
            if (this.Errors.Count > 0)
            {
                parser = ParserFactory.CreateParser(language, new StringReader(code));
                List <INode> members = parser.ParseTypeMembers();
                if (members != null && members.Count > 0 && parser.Errors.Count < this.Errors.Count)
                {
                    this.Errors          = parser.Errors;
                    this.Specials        = parser.Lexer.SpecialTracker.RetrieveSpecials();
                    this.SnippetType     = SnippetType.TypeMembers;
                    result               = new NodeListNode(members);
                    result.StartLocation = members[0].StartLocation;
                    result.EndLocation   = members[members.Count - 1].EndLocation;
                }
            }
            Debug.Assert(result is CompilationUnit || !result.StartLocation.IsEmpty);
            Debug.Assert(result is CompilationUnit || !result.EndLocation.IsEmpty);
            return(result);
        }