Пример #1
0
        private static ParseResult IsTokenValid(ParseContext context, SqlGrammarRuleSequenceTerminal terminalReference, int level, int tokenOffset, ReservedWordScope scope)
        {
            var tokenIsValid = false;
            IList <StatementGrammarNode> nodes = null;

            var terminalId = terminalReference.Id;

            if (context.TokenBuffer.Count > tokenOffset)
            {
                var currentToken = context.TokenBuffer[tokenOffset];

                var terminal       = terminalReference.Terminal;
                var isReservedWord = false;
                if (String.IsNullOrEmpty(terminal.RegexValue))
                {
                    var tokenValue = currentToken.UpperInvariantValue;
                    tokenIsValid   = String.Equals(terminal.Value, tokenValue) || (terminal.AllowQuotedNotation && tokenValue.Length == terminal.Value.Length + 2 && tokenValue[0] == '"' && tokenValue[tokenValue.Length - 1] == '"' && String.Equals(tokenValue.Substring(1, tokenValue.Length - 2), terminal.Value));
                    isReservedWord = tokenIsValid && (scope == ReservedWordScope.Sql ? terminal.ReservedWord == ReservedWordType.Sql : terminal.ReservedWord > 0);
                }
                else
                {
                    tokenIsValid = terminal.RegexMatcher.IsMatch(currentToken.Value);
                    if (tokenIsValid && !terminalReference.AllowReservedWord)
                    {
                        var isNotReservedWord = !OracleGrammarDescription.ReservedWordsSql.Contains(currentToken.UpperInvariantValue);

                        if (isNotReservedWord && scope == ReservedWordScope.PlSqlBody)
                        {
                            var effectiveReservedWords = context.PlSqlStatementTokenIndex.Count == 0 || context.PlSqlStatementTokenIndex.Peek() == tokenOffset
                                                                ? OracleGrammarDescription.ReservedWordsPlSqlBody
                                                                : OracleGrammarDescription.ReservedWordsPlSql;

                            isNotReservedWord = !effectiveReservedWords.Contains(currentToken.UpperInvariantValue);
                        }

                        if (isNotReservedWord && scope == ReservedWordScope.PlSqlDeclaration)
                        {
                            isNotReservedWord = !OracleGrammarDescription.ReservedWordsPlSqlDeclaration.Contains(currentToken.UpperInvariantValue);
                        }

                        tokenIsValid &= isNotReservedWord;
                    }
                }

                if (tokenIsValid)
                {
                    var terminalNode =
                        new StatementGrammarNode(NodeType.Terminal, context.Statement, currentToken)
                    {
                        Id             = terminalId,
                        Level          = level,
                        IsRequired     = terminalReference.IsRequired,
                        IsReservedWord = isReservedWord
                    };

                    nodes = new[] { terminalNode };
                }
            }

            return
                (new ParseResult
            {
                NodeId = terminalId,
                Status = tokenIsValid ? ParseStatus.Success : ParseStatus.SequenceNotFound,
                Nodes = nodes
            });
        }
Пример #2
0
		private static ParseResult IsTokenValid(ParseContext context, SqlGrammarRuleSequenceTerminal terminalReference, int level, int tokenOffset, ReservedWordScope scope)
		{
			var tokenIsValid = false;
			IList<StatementGrammarNode> nodes = null;

			var terminalId = terminalReference.Id;
			if (context.TokenBuffer.Count > tokenOffset)
			{
				var currentToken = context.TokenBuffer[tokenOffset];

				var terminal = terminalReference.Terminal;
				var isReservedWord = false;
				if (String.IsNullOrEmpty(terminal.RegexValue))
				{
					var tokenValue = currentToken.UpperInvariantValue;
					tokenIsValid = String.Equals(terminal.Value, tokenValue) || (terminal.AllowQuotedNotation && tokenValue.Length == terminal.Value.Length + 2 && tokenValue[0] == '"' && tokenValue[tokenValue.Length - 1] == '"' && String.Equals(tokenValue.Substring(1, tokenValue.Length - 2), terminal.Value));
					isReservedWord = tokenIsValid && (scope == ReservedWordScope.Sql ? terminal.ReservedWord == ReservedWordType.Sql : terminal.ReservedWord > 0);
				}
				else
				{
					tokenIsValid = terminal.RegexMatcher.IsMatch(currentToken.Value);
					if (tokenIsValid && !terminalReference.AllowReservedWord)
					{
						var isNotReservedWord = !OracleGrammarDescription.ReservedWordsSql.Contains(currentToken.UpperInvariantValue);

						if (isNotReservedWord && scope == ReservedWordScope.PlSqlBody)
						{
							var effectiveReservedWords = context.PlSqlStatementTokenIndex.Count == 0 || context.PlSqlStatementTokenIndex.Peek() == tokenOffset
								? OracleGrammarDescription.ReservedWordsPlSqlBody
								: OracleGrammarDescription.ReservedWordsPlSql;

							isNotReservedWord = !effectiveReservedWords.Contains(currentToken.UpperInvariantValue);
						}

						if (isNotReservedWord && scope == ReservedWordScope.PlSqlDeclaration)
						{
							isNotReservedWord = !OracleGrammarDescription.ReservedWordsPlSqlDeclaration.Contains(currentToken.UpperInvariantValue);
						}

						tokenIsValid &= isNotReservedWord;
					}
				}

				if (tokenIsValid)
				{
					var terminalNode =
						new StatementGrammarNode(NodeType.Terminal, context.Statement, currentToken)
						{
							Id = terminalId,
							Level = level,
							IsRequired = terminalReference.IsRequired,
							IsReservedWord = isReservedWord
						};

					nodes = new[] { terminalNode };
				}
			}

			return
				new ParseResult
				{
					NodeId = terminalId,
					Status = tokenIsValid ? ParseStatus.Success : ParseStatus.SequenceNotFound,
					Nodes = nodes
				};
		}