Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Common / EntitySql / CqlParserHelpers.cs / 1305376 / CqlParserHelpers.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- namespace System.Data.Common.EntitySql { using System; using System.Collections.Generic; using System.IO; using System.Globalization; using System.Data.Common.EntitySql.AST; ////// Represents the Cql Parser engine. Also, implements helpers and util routines. /// internal sealed partial class CqlParser { private Node _parsedTree; private CqlLexer _lexer; private string _query; private ParserOptions _parserOptions; private const string _internalYaccSyntaxErrorMessage = "syntax error"; ////// Contains inclusive count of method expressions. /// private uint _methodExprCounter; private Stack_methodExprCounterStack; private string _version = YYMAJOR.ToString(NumberFormatInfo.InvariantInfo) + '.' + YYMINOR.ToString(NumberFormatInfo.InvariantInfo); internal CqlParser( ParserOptions parserOptions, bool debug ) { // The common practice is to make the null check at the public surface, // however this method is a convergence zone from multiple public entry points and it makes sense to // check for null once, here. EntityUtil.CheckArgumentNull(parserOptions, "parserOptions"); _parserOptions = parserOptions; yydebug = debug; } /// /// Main entry point for parsing cql. /// /// query text ///Thrown when Syntatic rules are violated and the query cannot be accepted ///Abstract Syntax Tree internal Node Parse(string query) { // The common practice is to make the null check at the public surface, // however this method is a convergence zone from multiple public entry points and it makes sense to // check for null once, here. EntityUtil.CheckArgumentNull(query, "query"); if (String.IsNullOrEmpty(query) || query.Trim().Length == 0) { throw EntityUtil.Argument(System.Data.Entity.Strings.InvalidEmptyQueryTextArgument); } _query = query; _parsedTree = null; _parserOptions = _parserOptions.MakeReadOnly(); _methodExprCounter = 0; _methodExprCounterStack = new Stack(); internalParseEntryPoint(); return _parsedTree; } /// /// Returns query string /// internal string Query { get { return _query; } } #if EXTRA_ENTITYSQL_PARSER_DEBUGGING ////// Enables/Disables yacc debugging. /// internal bool EnableDebug { get { return yydebug; } set { yydebug = value; } } #endif ////// Returns ParserOptions used /// ///Once parse has been invoked, ParserOptions are frozen and cannot be changed. otherwise a EntityException exception will be thrown internal ParserOptions ParserOptions { get { return _parserOptions; } } ////// Internal entry point /// private void internalParseEntryPoint() { _lexer = new CqlLexer(Query, ParserOptions); #if EXTRA_ENTITYSQL_PARSER_DEBUGGING CqlLexer.Token tk = lexer.yylex(); while (null != tk) { Console.WriteLine("{0} := {1}", tk.TokenId, lexer.yytext()); tk = lexer.yylex(); } #endif yyparse(); } // // Conversion/Cast/Helpers // private static Node AstNode(object o) { return ((Node)o); } private static int AstNodePos( object o ) { return ((Node)o).ErrCtx.InputPosition; } private static CqlLexer.TerminalToken Terminal( object o ) { return ((CqlLexer.TerminalToken)o); } private static int TerminalPos( object o ) { return ((CqlLexer.TerminalToken)o).IPos; } private static NodeListToNodeList (object o) where T : Node { return ((NodeList )o); } private short yylex() { CqlLexer.Token token = null; token = _lexer.yylex(); if (null == token) { return 0; } _lexer.AdvanceIPos(); yylval = token.Value; return token.TokenId; } private void yyerror_stackoverflow() { yyerror(System.Data.Entity.Strings.StackOverflowInParser); } private void yyerror( string s ) { if (s.Equals(_internalYaccSyntaxErrorMessage, StringComparison.Ordinal)) { int errorPosition = _lexer.IPos; string syntaxContextInfo = null; string term = _lexer.YYText; if (!String.IsNullOrEmpty(term)) { syntaxContextInfo = System.Data.Entity.Strings.LocalizedTerm; ErrorContext errCtx = null; Node astNode = yylval as Node; if (null != astNode && (null != astNode.ErrCtx) && (!String.IsNullOrEmpty(astNode.ErrCtx.ErrorContextInfo))) { errCtx = astNode.ErrCtx; errorPosition = Math.Min(errorPosition, errorPosition - term.Length); } if ((yylval is CqlLexer.TerminalToken) && CqlLexer.IsReservedKeyword(term) && !(astNode is Identifier)) { syntaxContextInfo = System.Data.Entity.Strings.LocalizedKeyword; term = term.ToUpperInvariant(); errorPosition = Math.Min(errorPosition, errorPosition - term.Length); } else if (null != errCtx) { syntaxContextInfo = EntityRes.GetString(errCtx.ErrorContextInfo); } syntaxContextInfo = String.Format(CultureInfo.CurrentCulture, "{0} '{1}'", syntaxContextInfo, term); } throw EntityUtil.EntitySqlError(_query, System.Data.Entity.Strings.GenericSyntaxError, errorPosition, syntaxContextInfo, false /* loadErrorContextInfoFromResource */); } throw EntityUtil.EntitySqlError(_query, s, _lexer.IPos); } // // Error tracking helpers // private void SetErrCtx(Node astExpr, CqlLexer.TerminalToken tokenValue, string info) { SetErrCtx(astExpr, tokenValue.IPos, info); } private void SetErrCtx(Node astExpr, int inputPos, string info) { astExpr.ErrCtx.InputPosition = inputPos; astExpr.ErrCtx.ErrorContextInfo = info; astExpr.ErrCtx.CommandText = _query; } private void StartMethodExprCounting() { // Save the current counter value. _methodExprCounterStack.Push(_methodExprCounter); // Reset the counter for the current level. _methodExprCounter = 0; } private void IncrementMethodExprCount() { ++_methodExprCounter; } private uint EndMethodExprCounting() { // Save number of method expressions on the current level. uint count = _methodExprCounter; // Restore upper level counter and adjust it with the number of method expressions on the current level. _methodExprCounter += _methodExprCounterStack.Pop(); return count; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- namespace System.Data.Common.EntitySql { using System; using System.Collections.Generic; using System.IO; using System.Globalization; using System.Data.Common.EntitySql.AST; ////// Represents the Cql Parser engine. Also, implements helpers and util routines. /// internal sealed partial class CqlParser { private Node _parsedTree; private CqlLexer _lexer; private string _query; private ParserOptions _parserOptions; private const string _internalYaccSyntaxErrorMessage = "syntax error"; ////// Contains inclusive count of method expressions. /// private uint _methodExprCounter; private Stack_methodExprCounterStack; private string _version = YYMAJOR.ToString(NumberFormatInfo.InvariantInfo) + '.' + YYMINOR.ToString(NumberFormatInfo.InvariantInfo); internal CqlParser( ParserOptions parserOptions, bool debug ) { // The common practice is to make the null check at the public surface, // however this method is a convergence zone from multiple public entry points and it makes sense to // check for null once, here. EntityUtil.CheckArgumentNull(parserOptions, "parserOptions"); _parserOptions = parserOptions; yydebug = debug; } /// /// Main entry point for parsing cql. /// /// query text ///Thrown when Syntatic rules are violated and the query cannot be accepted ///Abstract Syntax Tree internal Node Parse(string query) { // The common practice is to make the null check at the public surface, // however this method is a convergence zone from multiple public entry points and it makes sense to // check for null once, here. EntityUtil.CheckArgumentNull(query, "query"); if (String.IsNullOrEmpty(query) || query.Trim().Length == 0) { throw EntityUtil.Argument(System.Data.Entity.Strings.InvalidEmptyQueryTextArgument); } _query = query; _parsedTree = null; _parserOptions = _parserOptions.MakeReadOnly(); _methodExprCounter = 0; _methodExprCounterStack = new Stack(); internalParseEntryPoint(); return _parsedTree; } /// /// Returns query string /// internal string Query { get { return _query; } } #if EXTRA_ENTITYSQL_PARSER_DEBUGGING ////// Enables/Disables yacc debugging. /// internal bool EnableDebug { get { return yydebug; } set { yydebug = value; } } #endif ////// Returns ParserOptions used /// ///Once parse has been invoked, ParserOptions are frozen and cannot be changed. otherwise a EntityException exception will be thrown internal ParserOptions ParserOptions { get { return _parserOptions; } } ////// Internal entry point /// private void internalParseEntryPoint() { _lexer = new CqlLexer(Query, ParserOptions); #if EXTRA_ENTITYSQL_PARSER_DEBUGGING CqlLexer.Token tk = lexer.yylex(); while (null != tk) { Console.WriteLine("{0} := {1}", tk.TokenId, lexer.yytext()); tk = lexer.yylex(); } #endif yyparse(); } // // Conversion/Cast/Helpers // private static Node AstNode(object o) { return ((Node)o); } private static int AstNodePos( object o ) { return ((Node)o).ErrCtx.InputPosition; } private static CqlLexer.TerminalToken Terminal( object o ) { return ((CqlLexer.TerminalToken)o); } private static int TerminalPos( object o ) { return ((CqlLexer.TerminalToken)o).IPos; } private static NodeListToNodeList (object o) where T : Node { return ((NodeList )o); } private short yylex() { CqlLexer.Token token = null; token = _lexer.yylex(); if (null == token) { return 0; } _lexer.AdvanceIPos(); yylval = token.Value; return token.TokenId; } private void yyerror_stackoverflow() { yyerror(System.Data.Entity.Strings.StackOverflowInParser); } private void yyerror( string s ) { if (s.Equals(_internalYaccSyntaxErrorMessage, StringComparison.Ordinal)) { int errorPosition = _lexer.IPos; string syntaxContextInfo = null; string term = _lexer.YYText; if (!String.IsNullOrEmpty(term)) { syntaxContextInfo = System.Data.Entity.Strings.LocalizedTerm; ErrorContext errCtx = null; Node astNode = yylval as Node; if (null != astNode && (null != astNode.ErrCtx) && (!String.IsNullOrEmpty(astNode.ErrCtx.ErrorContextInfo))) { errCtx = astNode.ErrCtx; errorPosition = Math.Min(errorPosition, errorPosition - term.Length); } if ((yylval is CqlLexer.TerminalToken) && CqlLexer.IsReservedKeyword(term) && !(astNode is Identifier)) { syntaxContextInfo = System.Data.Entity.Strings.LocalizedKeyword; term = term.ToUpperInvariant(); errorPosition = Math.Min(errorPosition, errorPosition - term.Length); } else if (null != errCtx) { syntaxContextInfo = EntityRes.GetString(errCtx.ErrorContextInfo); } syntaxContextInfo = String.Format(CultureInfo.CurrentCulture, "{0} '{1}'", syntaxContextInfo, term); } throw EntityUtil.EntitySqlError(_query, System.Data.Entity.Strings.GenericSyntaxError, errorPosition, syntaxContextInfo, false /* loadErrorContextInfoFromResource */); } throw EntityUtil.EntitySqlError(_query, s, _lexer.IPos); } // // Error tracking helpers // private void SetErrCtx(Node astExpr, CqlLexer.TerminalToken tokenValue, string info) { SetErrCtx(astExpr, tokenValue.IPos, info); } private void SetErrCtx(Node astExpr, int inputPos, string info) { astExpr.ErrCtx.InputPosition = inputPos; astExpr.ErrCtx.ErrorContextInfo = info; astExpr.ErrCtx.CommandText = _query; } private void StartMethodExprCounting() { // Save the current counter value. _methodExprCounterStack.Push(_methodExprCounter); // Reset the counter for the current level. _methodExprCounter = 0; } private void IncrementMethodExprCount() { ++_methodExprCounter; } private uint EndMethodExprCounting() { // Save number of method expressions on the current level. uint count = _methodExprCounter; // Restore upper level counter and adjust it with the number of method expressions on the current level. _methodExprCounter += _methodExprCounterStack.Pop(); return count; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebBrowserNavigatingEventHandler.cs
- WindowsPrincipal.cs
- ItemsPresenter.cs
- documentsequencetextcontainer.cs
- XmlILCommand.cs
- SizeAnimation.cs
- UnionCodeGroup.cs
- Regex.cs
- CqlLexerHelpers.cs
- RepeaterDesigner.cs
- ColumnWidthChangedEvent.cs
- ToolBarTray.cs
- RawStylusActions.cs
- ConfigurationSection.cs
- DrawingBrush.cs
- DataGridRow.cs
- BitArray.cs
- GeometryValueSerializer.cs
- ButtonBase.cs
- SubpageParaClient.cs
- JavaScriptSerializer.cs
- latinshape.cs
- NavigationService.cs
- WindowsAuthenticationEventArgs.cs
- ScriptIgnoreAttribute.cs
- NodeFunctions.cs
- PartialCachingAttribute.cs
- KoreanCalendar.cs
- ProxyHelper.cs
- DataDocumentXPathNavigator.cs
- TextTreeFixupNode.cs
- SqlConnectionStringBuilder.cs
- HostProtectionPermission.cs
- Rotation3D.cs
- XamlSerializationHelper.cs
- TransactionState.cs
- ColumnCollection.cs
- PasswordTextNavigator.cs
- SignedInfo.cs
- DBCommand.cs
- ConfigurationManagerHelper.cs
- FocusChangedEventArgs.cs
- SecurityTokenSerializer.cs
- SmtpAuthenticationManager.cs
- RenderingBiasValidation.cs
- ISFTagAndGuidCache.cs
- ValueConversionAttribute.cs
- CategoryNameCollection.cs
- TextParagraph.cs
- PropertyToken.cs
- DiscreteKeyFrames.cs
- SessionPageStateSection.cs
- SqlCachedBuffer.cs
- StatusBar.cs
- CqlBlock.cs
- DataGridViewCellStyleChangedEventArgs.cs
- DecimalKeyFrameCollection.cs
- DoubleAnimationUsingKeyFrames.cs
- TreeNodeBindingCollection.cs
- TableTextElementCollectionInternal.cs
- DataGridViewCheckBoxColumn.cs
- LineVisual.cs
- TemplateBuilder.cs
- AutoSizeComboBox.cs
- XamlToRtfWriter.cs
- log.cs
- TextFindEngine.cs
- XmlTextReaderImpl.cs
- ShadowGlyph.cs
- TrackingWorkflowEventArgs.cs
- WebBrowserBase.cs
- FontFamily.cs
- XmlReaderSettings.cs
- Visual.cs
- ScriptControlDescriptor.cs
- BitmapEffect.cs
- XmlLangPropertyAttribute.cs
- _LazyAsyncResult.cs
- ColorBlend.cs
- ContextActivityUtils.cs
- AttachInfo.cs
- ExpandCollapsePattern.cs
- DiscoveryDefaults.cs
- SecurityPermission.cs
- HScrollProperties.cs
- GeneralTransform3DTo2D.cs
- WebPartCatalogCloseVerb.cs
- Comparer.cs
- GPRECTF.cs
- Storyboard.cs
- SerializationStore.cs
- PackUriHelper.cs
- mactripleDES.cs
- ConditionalAttribute.cs
- ForceCopyBuildProvider.cs
- DbConnectionPoolGroup.cs
- LogicalExpr.cs
- AuthenticationModulesSection.cs
- GlobalAllocSafeHandle.cs
- AssemblyBuilder.cs