public static bool TryParse(string text, out SqlQuery sqlQuery)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

#if false
            AntlrInputStream  str    = new AntlrInputStream(text);
            sqlLexer          lexer  = new sqlLexer(str);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            sqlParser         parser = new sqlParser(tokens)
            {
                ErrorHandler = ThrowExceptionOnErrors.Singleton,
            };
            ErrorListener <IToken> listener = new ErrorListener <IToken>(parser, lexer, tokens);
            parser.AddErrorListener(listener);

            sqlParser.ProgramContext programContext;
            try
            {
                programContext = parser.program();
            }
            catch (Exception)
            {
                sqlQuery = default;
                return(false);
            }

            if (listener.hadError)
            {
                sqlQuery = default;
                return(false);
            }

            sqlQuery = (SqlQuery)CstToAstVisitor.Singleton.Visit(programContext);
            return(true);
#else
            sqlQuery = default;
            return(false);
#endif
        }
Exemple #2
0
            public static TryCatch <SqlQuery> Parse(string text)
            {
                if (text == null)
                {
                    throw new ArgumentNullException(nameof(text));
                }

                AntlrInputStream  str    = new AntlrInputStream(text);
                sqlLexer          lexer  = new sqlLexer(str);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                sqlParser         parser = new sqlParser(tokens)
                {
                    ErrorHandler = ThrowExceptionOnErrors.Singleton,
                };
                ErrorListener <IToken> listener = new ErrorListener <IToken>(parser, lexer, tokens);

                parser.AddErrorListener(listener);

                sqlParser.ProgramContext programContext;
                try
                {
                    programContext = parser.program();
                }
                catch (Exception ex)
                {
                    return(TryCatch <SqlQuery> .FromException(ex));
                }

                if (listener.parseException != null)
                {
                    return(TryCatch <SqlQuery> .FromException(listener.parseException));
                }

                SqlQuery sqlQuery = (SqlQuery)CstToAstVisitor.Singleton.Visit(programContext);

                return(TryCatch <SqlQuery> .FromResult(sqlQuery));
            }