private Expression<Func<bool>> BuildExpression(ExpressionToken token) { if (token is OperatorExpressionToken) { // have to wrap BinaryExpression in a Lambda so we can execute it. return Expression.Lambda<Func<bool>>(BuildOperatorExpression((OperatorExpressionToken)token)); } return BuildPermissionExpression((PermissionExpressionToken)token); }
public static IToken Parse(string text, int startPosition = 0, Action<IToken, int, string> callback = null) { if (string.IsNullOrWhiteSpace(text)) return new NullToken(); if (text.StartsWith("'") && text.IndexOf('\'', 1) <= 0) return new StringToken(text.Substring(1)); List<string> blocks = text.SplitIntoBlocks(new[] {'\'', '\'', '"', '"', '^', '^', '{', '}', '(', ')'}, true, StringUtils.DelimiterInclude.IncludeSeparately); string simplifed = ""; List<IToken> subResults = new List<IToken>(); int currentPosition = startPosition; for (int i = 0; i < blocks.Count; i += 3) { string start = blocks[i]; string entry = blocks[i + 1]; IToken subResult = null; switch (start) { case "\"": case "'": subResult = new StringToken(entry); callback?.Invoke(subResult, currentPosition, "'" + entry + "'"); break; case "^": subResult = Parse(entry); callback?.Invoke(subResult, currentPosition, "'" + entry + "'"); break; case "{": subResult = new ExpressionToken(null, new SubstitutionOperator(), Parse(entry)); callback?.Invoke(subResult, currentPosition, "{" + entry + "}"); break; case "(": subResult = Parse(entry, currentPosition + 1, callback); break; default: simplifed += entry; break; } if (subResult != null) { simplifed += $"█{subResults.Count}█"; subResults.Add(subResult); } if (callback != null) currentPosition += start.Length + entry.Length + blocks[i + 2].Length; } IToken result = ParseExpressionNoBrackets(simplifed, startPosition, callback); result = SubstituteValues(result, subResults); return result ?? new StringToken(text); }
/// <summary> /// Converts an expression token to a list of tokens if possible and required. /// </summary> /// <param name="expression">The expression to convert.</param> /// <returns>The original token by default.</returns> public override IToken EvaluateList(ExpressionToken expression) { IToken firstList = expression.First.EvaluateList(); IToken lastList = expression.Second.EvaluateList(); if (firstList == null || lastList == null) return expression; return AddToList(firstList, lastList); }
public override IToken Perform(IToken dataToken, TokenTreeList parameters, bool isFinal) { ListToken listToken = dataToken as ListToken; if (listToken != null && listToken.Tokens.Count > 0) { IToken current = listToken.Tokens[0]; for (int i = 1; i < listToken.Tokens.Count; ++i) { IToken token = listToken.Tokens[i]; current = new ExpressionToken(current, new PlusOperator(), token).Evaluate(parameters, isFinal); } return current; } return dataToken; }
private static void FormatFunction(IFormattedTextBlock formattedText, ExpressionToken token, int blockOffset, string block) { if (token.First is StringToken) { string name = token.First.Text; int index = block.IndexOf(name); index = block.IndexOf(':', index + name.Length); FormatItem(VttSection.Function, formattedText, blockOffset, block.Substring(0, index + 1)); FormatToken(formattedText, token.Second, blockOffset + index + 1, block.Substring(index + 1)); } else { FormatItem(VttSection.Function, formattedText, blockOffset, block); } }
private static IToken AddToken(IToken current, IToken separator, IToken toAdd, TokenTreeList parameters, bool isFinal) { ListToken currentList = toAdd as ListToken; if (currentList != null) { foreach (IToken tokenToAdd in currentList.Tokens) current = AddToken(current, separator, tokenToAdd, parameters, isFinal); } else if (current == null) { current = toAdd; } else { current = new ExpressionToken(current, new PlusOperator(), separator).Evaluate(parameters, isFinal); current = new ExpressionToken(current, new PlusOperator(), toAdd).Evaluate(parameters, isFinal); } return current; }
public IToken Parse(string text) { List<string> blocks = text.SplitIntoBlocks(new[] {'\'', '\'', '"', '"', '{', '}', '(', ')'}, true, StringUtils.DelimiterInclude.IncludeSeparately); string simplifed = ""; List<IToken> subResults = new List<IToken>(); for (int i = 0; i < blocks.Count / 3; i++) { int index = i * 3; string start = blocks[index]; string entry = blocks[index + 1]; IToken subResult; switch (start) { case "\"": case "'": subResult = new StringToken(entry); simplifed += $"█{subResults.Count}█"; subResults.Add(subResult); break; case "{": subResult = new ExpressionToken(null, new SubstitutionToken(), Parse(entry)); simplifed += $"█{subResults.Count}█"; subResults.Add(subResult); break; case "(": subResult = Parse(entry); simplifed += $"█{subResults.Count}█"; subResults.Add(subResult); break; default: simplifed += entry; break; } } IToken result = ParseExpressionNoBrackets(simplifed); result = SubstituteValues(result, subResults); return result ?? new StringToken(text); }
protected void VisitWhenNotMatchedThenInsertToken(MergeStatement statement , IList <string> tempUpdateAliases , string targetAlias , string targetColumnOn , string targetTable , string sourceAlias , string sourceTable , string sourceColumnOn) { string tempSourceAlias = "tmp_" + sourceAlias; ExpressionToken tempExpression = new ExpressionToken(); if (statement.WhenNotMatched.Count != 0) { int counter = 0; foreach (WhenMatchedToken item in statement.WhenNotMatched) { tempExpression = Sql.Name(sourceAlias, sourceColumnOn) .NotIn(Sql.Select.Output(targetColumnOn).From(targetTable)); if (item.AndCondition != null) { tempExpression = tempExpression.And(AddPrefixToExpressionToken((ExpressionToken)item.AndCondition, sourceAlias)); } CreateTableStatement createTempTable = Sql.CreateTemporaryTable(tempSourceAlias) .As(Sql.Select.From(Sql.NameAs(sourceTable, sourceAlias)).Where(tempExpression)); VisitStatement(createTempTable); State.WriteStatementTerminator(); InsertStatement insertStatement = Sql.Insert.Into(targetTable); if ((((WhenNotMatchedTokenThenInsertToken)item).Values.Count == 0) || (((WhenNotMatchedTokenThenInsertToken)item).Columns.Count == 0) || (((WhenNotMatchedTokenThenInsertToken)item).Columns.Count != ((WhenNotMatchedTokenThenInsertToken)item).Values.Count)) { insertStatement.Columns.Add(targetColumnOn); insertStatement.From(Sql.Select.From(tempSourceAlias).Output(sourceColumnOn)); } else { foreach (Name columnName in ((WhenNotMatchedTokenThenInsertToken)item).Columns) { insertStatement.Columns.Add(Sql.Name(columnName.LastPart)); } SelectStatement fromSelect = Sql.Select.From(tempSourceAlias); foreach (Token outputColumn in ((WhenNotMatchedTokenThenInsertToken)item).Values) { fromSelect.Output.Add(Sql.Name(((Name)outputColumn).LastPart)); } insertStatement.From(fromSelect); } VisitStatement(insertStatement); State.WriteStatementTerminator(); counter++; VisitStatement(Sql.DropTable(tempSourceAlias, true)); State.WriteStatementTerminator(); } } }
////////////////////////////////////////////////////////////////////////////////////////// public static Int32 GetPriority(ExpressionToken ExpressionToken) { return(GetPriority(ExpressionToken.TokenType, ExpressionToken.TokenChars)); }
public static Boolean EvaluateOnp( DynContext DynLanContext) { Boolean result = false; ExpressionState expState = DynLanContext.CurrentExpressionState; ExpressionContext expContext = DynLanContext.CurrentExpressionContext; // czy zakończyć i zapisać wynik if (expState.TokenIndex >= expState.Expression.OnpTokens.Count) { Object finResult = null; if (expState.ValueStack.Count > 0) { finResult = MyCollectionsExtenders.Pop(expState.ValueStack); } expState.ValueStack.Clear(); expState.Finished = true; expState.Result = InternalTypeConverter.ToOuter(finResult); MyCollectionsExtenders.Pop(expContext.Stack); if (expContext.Current != null) { expContext.Current.PushValue(InternalTypeConverter.ToOuter(finResult)); return(false); } else { expContext.Result = InternalTypeConverter.ToOuter(finResult); expContext.IsFinished = true; return(true); } } ExpressionToken token = expState.Expression.OnpTokens[expState.TokenIndex]; // wykonanie następnej operacji if (token.TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(token.TokenChars); Object value = operationValue == null ? null : InternalTypeConverter.ToInner(operationValue.Value); expState.PushValue(value); } else if (token.TokenType == TokenType.PROPERTY_NAME) { expState.PushValue(token.TokenName); } if (token.TokenType == TokenType.VARIABLE) { result = ObjectValueGetter.EvaluateValue( token.TokenName, DynLanContext); } else if (token.TokenType == TokenType.OPERATOR) { Object valueA = InternalTypeConverter.ToInner( MyCollectionsExtenders.Pop(expState.ValueStack)); Object valueB = InternalTypeConverter.ToInner( MyCollectionsExtenders.Pop(expState.ValueStack)); Object value = null; OperatorType operatorType = OperatorTypeHelper. GetOperationType(token.TokenChars); try { value = OperationHelper.Do( operatorType, valueB, valueA, DynLanContext.ForceDecimals); expState.PushValue(value); } catch { throw; } } expState.TokenIndex++; return(result); }
public static string GetRegex(this ExpressionToken tkn) { return(tkn.GetAttribute <ExpressionTokenAttribute>().Regex); }
/// <summary> /// Odszukanie ciągów QUEUE w wyrażeniu /// </summary> public void PrepareQueueExpressions( IList <ExpressionToken> Tokens, ParserSettings ParserSettings, ExpressionGroup ExpressionGroup) { ExpressionTokens outTokens = new ExpressionTokens(); ExpressionTokens queueTokens = null; Boolean queueStarted = false; Int32 startIndex = -1; Int32 endIndex = -1; for (var i = 0; i < Tokens.Count; i++) { ExpressionToken token = Tokens[i]; // rozpoczecie sekwencji queue if (token != null && queueStarted == false && (OnpOnpTokenHelper.IsPropertyOperatorToken(token) || OnpOnpTokenHelper.IsFunctionOperatorToken(token))) { ExpressionTokens sequence = new ExpressionTokens( GetNextTokensOnSameLevel(Tokens, i)); ExpressionTokens prevSequence = new ExpressionTokens( GetPrevTokensOnSameLevel(Tokens, i - 1)); if (queueTokens == null) { queueTokens = new ExpressionTokens(); } if (prevSequence == null) { throw new FormatException(); } queueTokens.AddRange(prevSequence); queueTokens.AddRange(sequence); queueStarted = true; startIndex = i - prevSequence.Count; i += sequence.Count - 1; } // zakończenie sekwencji queue else if ( token != null && queueStarted == true && ( token.TokenType == TokenType.BRACKET_END || token.TokenType == TokenType.OPERATOR && !OnpOnpTokenHelper.IsPropertyOperatorToken(token) && !OnpOnpTokenHelper.IsFunctionOperatorToken(token) )) { endIndex = i - 1; // zastapienie ciagu tokenków zmienną i podpiecie nowego wyrazenia do zmiennej if (queueTokens != null && queueTokens.Count > 0) { ExpressionToken newToken = CreateQueueExpression( queueTokens, ParserSettings, ExpressionGroup); for (var j = endIndex; j >= startIndex; j--) { Tokens.RemoveAt(j); i--; } Tokens.Insert(startIndex, newToken); } queueTokens = null; endIndex = -1; startIndex = -1; queueStarted = false; } // kontynuacja sekwencji queue else if (queueStarted == true) { ExpressionTokens sequence = new ExpressionTokens( GetNextTokensOnSameLevel(Tokens, i)); queueTokens.AddRange( sequence); i += sequence.Count - 1; } } // zastapienie ciagu tokenków zmienną i podpiecie nowego wyrazenia do zmiennej if (queueTokens != null && queueTokens.Count > 0) { endIndex = Tokens.Count - 1; ExpressionToken newToken = CreateQueueExpression( queueTokens, ParserSettings, ExpressionGroup); for (var j = endIndex; j >= startIndex; j--) { Tokens.RemoveAt(j); } Tokens.Insert(startIndex, newToken); } queueTokens = null; endIndex = -1; startIndex = -1; }
private static OperationResult Add(ValueToken left, ValueToken right) { if (left is ConstantToken && right is ConstantToken) { var token = new ConstantToken(((ConstantToken)left).NumericValue + ((ConstantToken)right).NumericValue); return(OperationResult.CreateSuccess(token)); } if (left is ConstantToken && right is VariableToken) { return(OperationResult.CreateSuccess( new ExpressionToken(new List <Token> { left, new BinaryOperationToken { Value = "+" }, right }) )); } if (left is ConstantToken && right is ExpressionToken) { var members = ((ExpressionToken)right).Members; var newExpression = new ExpressionToken(new List <Token>(members)); if (members.Any(x => x is ConstantToken)) { var c = newExpression.Members.Where(x => x is ConstantToken).FirstOrDefault() as ConstantToken; c.NumericValue += ((ConstantToken)left).NumericValue; } else { newExpression.Members.AddRange(new List <Token> { new BinaryOperationToken { Value = "+" }, left }); } return(OperationResult.CreateSuccess(newExpression)); } if (left is ConstantToken && right is FractionToken) { return(OperationResult.CreateSuccess( new ExpressionToken(new List <Token> { left, new BinaryOperationToken { Value = "+" }, right }) )); } if (left is VariableToken && right is ConstantToken) { return(OperationResult.CreateSuccess( new ExpressionToken(new List <Token> { left, new BinaryOperationToken { Value = "+" }, right }) )); } if (left is VariableToken && right is VariableToken) { if (((VariableToken)left).Variables.Except(((VariableToken)right).Variables).Count() == 0) { var token = new VariableToken(((VariableToken)left).Quotient + ((VariableToken)right).Quotient, ((VariableToken)left).Variables); return(OperationResult.CreateSuccess(token)); } else { return(OperationResult.CreateSuccess( new ExpressionToken(new List <Token> { left, new BinaryOperationToken { Value = "+" }, right }) )); } } if (left is VariableToken && right is ExpressionToken) { var members = ((ExpressionToken)right).Members; var newExpression = new ExpressionToken(new List <Token>(members)); if (members.Any(x => x is VariableToken v && v.Variables.Except(((VariableToken)left).Variables).Count() == 0)) { var v = newExpression.Members .Where(x => x is VariableToken v && v.Variables.Except(((VariableToken)left).Variables).Count() == 0) .FirstOrDefault() as VariableToken; v.Quotient += ((VariableToken)left).Quotient; } else { newExpression.Members.AddRange(new List <Token> { new BinaryOperationToken { Value = "+" }, left }); } return(OperationResult.CreateSuccess(newExpression)); }
public static Boolean EvaluateQueue( PainContext PainContext) { ExpressionState expState = PainContext.CurrentExpressionState; ExpressionContext expContext = PainContext.CurrentExpressionContext; // policzenie nastepnego parametru if (expState.AreParametersCalculating) { expState.ParameterIndex++; if (expState.ParameterIndex < expState.ParameterTokens.Count) { Boolean result = false; ExpressionToken parameterToken = expState.ParameterTokens[expState.ParameterIndex]; if (parameterToken.TokenType == TokenType.VARIABLE) { result = ObjectValueGetter.EvaluateValue( parameterToken.TokenName, PainContext); } else if (parameterToken.TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(parameterToken.TokenChars); Object value = ( operationValue == null ? null : operationValue.Value); expState.Parameters.Add(value); } else { throw new PainIncorrectExpressionFormatException(); } return(result); } } // czy zakończyć i zapisać wynik if (expState.TokenIndex >= expState.Expression.Tokens.Count) { Object finResult = null; if (expState.ValueStack.Count > 0) { finResult = expState.ValueStack.Pop(); } expState.ValueStack.Clear(); expState.Finished = true; expState.Result = InternalTypeConverter.ToOuter(finResult); expContext.Stack.Pop(); if (expContext.Current != null) { expContext.Current.PushValue(InternalTypeConverter.ToOuter(finResult)); return(false); } else { expContext.Result = InternalTypeConverter.ToOuter(finResult); expContext.IsFinished = true; return(true); } } Boolean isFirstToken = expState.TokenIndex == 0; ExpressionToken token = expState. Expression. Tokens[expState.TokenIndex]; ExpressionTokens sequence = new ExpressionTokens( new TokenizerQueue(). GetNextTokensOnSameLevel(expState.Expression.Tokens, expState.TokenIndex)); Int32 originalSequenceCount = sequence.Count; sequence.RemoveBrackets(); // wykonanie następnej operacji if (token.TokenType == TokenType.BRACKET_BEGIN) { IList <ExpressionToken> prevSequenceTokens = new TokenizerQueue(). GetPrevTokensOnSameLevel(expState.Expression.Tokens, expState.TokenIndex - 1); ExpressionTokens prev_sequence = prevSequenceTokens == null ? null : new ExpressionTokens(prevSequenceTokens); // jeśli poprzedni operator to @ if ( prev_sequence != null && prev_sequence.Count == 1 && OnpOnpTokenHelper.IsFunctionOperatorToken(prev_sequence[0])) { Boolean result = false; if (sequence.Count == 0 || expState.AreParametersCalculated) { Object obj = expState.ValueStack.Count == 1 ? new EmptyObject() : expState.ValueStack.Peek(1); Object methodObject = expState. ValueStack. Peek(0); result = EvaluatorForMethods.EvaluateMethod( obj, methodObject, expState.Parameters, PainContext); expState.CleanParametersState(); expState.TokenIndex += originalSequenceCount; } else { expState.ParameterTokens = GetParameterTokens(sequence); expState.ParameterIndex = -1; expState.Parameters = new List <Object>(); result = false; } return(result); } // jeśli poprzedni operator to . else if ( prev_sequence != null && prev_sequence.Count == 1 && OnpOnpTokenHelper.IsPropertyOperatorToken(prev_sequence[0])) { throw new PainIncorrectExpressionFormatException(); } // jeśli brak poprzedniego operatora else if ( prev_sequence == null && sequence.Count == 1) { Boolean result = false; if (sequence[0].TokenType == TokenType.VARIABLE) { result = ObjectValueGetter.EvaluateValue( sequence[0].TokenName, PainContext); } else if (sequence[0].TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(sequence[0].TokenChars); Object value = ( operationValue == null ? null : operationValue.Value); expState.PushValue(value); } else { throw new PainIncorrectExpressionFormatException(); } expState.TokenIndex += originalSequenceCount; // -1; return(result); } else { throw new PainInvalidExpressionException("Incorrect expression " + expState.Expression.Tokens.JoinToString(expState.TokenIndex) + "!"); } } else if (token.TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(token.TokenChars); Object value = ( operationValue == null ? null : operationValue.Value); if (isFirstToken) { expState.PushValue(value); } else { Object prevValue = expState.ValueStack.Peek(); Object method = ObjectValueGetter.GetValueFromObject( prevValue, UniConvert.ToString(value)); expState.PushValue(method); } expState.TokenIndex += originalSequenceCount; // -1 } else if (token.TokenType == TokenType.PROPERTY_NAME) { if (isFirstToken) { throw new PainIncorrectExpressionFormatException(); } else { Object prevValue = expState.ValueStack.Peek(); Object value = ObjectValueGetter.GetValueFromObject( prevValue, token.TokenName); expState.PushValue(value); } expState.TokenIndex += originalSequenceCount; // -1 } else if (token.TokenType == TokenType.VARIABLE) { Boolean result = false; ExpressionToken next_token = expState.TokenIndex + 1 < expState.Expression.Tokens.Count ? expState.Expression.Tokens[expState.TokenIndex + 1] : null; Object prevValue = null; if (isFirstToken) { prevValue = new EmptyObject(); /*result = ObjectValueGetter.EvaluateValueOrMethod( * new EMPTY_OBJECT(), * sequence[0].TokenName, * -1, * PainContext);*/ } else { prevValue = ( expState.ValueStack.Count == 0 ? null : expState.ValueStack.Peek()); } Int32 paramCount = -1; // jeśli następny operator to operator wołania funkcji @ to pobieramy z niego liczbę parametrów dla funkcji if (OnpOnpTokenHelper.IsFunctionOperatorToken(next_token) && next_token.TokenData != null && next_token.TokenData.FunctionParametersCount != null) { paramCount = next_token.TokenData.FunctionParametersCount.Value; } else { paramCount = -1; } result = ObjectValueGetter.EvaluateValueOrMethod( prevValue, sequence[0].TokenName, paramCount, PainContext); if (PainContext.CurrentExpressionState.ValueStack.Peek() == null) { ExpressionToken next_next_token = expState.TokenIndex + 2 < expState.Expression.Tokens.Count ? expState.Expression.Tokens[expState.TokenIndex + 2] : null; if (next_token.TokenType == TokenType.OPERATOR && OnpOnpTokenHelper.IsPropertyOperatorToken(next_token) && next_next_token != null) { } else { next_next_token = null; } if (paramCount < 0) { if (next_next_token != null) { ExpressionToken next_next_next_next_next_token = expState.TokenIndex + 5 < expState.Expression.Tokens.Count ? expState.Expression.Tokens[expState.TokenIndex + 5] : null; if (next_next_token.TokenName == "__EXT_SET" && next_next_next_next_next_token != null) { throw new PainMethodNotFoundException("Cannot find property " + next_next_next_next_next_token.TokenName + " in undefined object '" + sequence[0].TokenName + "'"); } else { throw new PainMethodNotFoundException("Cannot call method '" + next_next_token.TokenName + "' in undefined object '" + sequence[0].TokenName + "'"); } } else { throw new PainMethodNotFoundException("Undefined object '" + sequence[0].TokenName + "'"); } } else { throw new PainMethodNotFoundException("Undefined method '" + sequence[0].TokenName + "'!"); } } expState.TokenIndex += originalSequenceCount; // -1 return(result); } else if (token.TokenType == TokenType.OPERATOR) { if (!OnpOnpTokenHelper.IsFunctionOperatorToken(token) && !OnpOnpTokenHelper.IsPropertyOperatorToken(token)) { throw new PainIncorrectExpressionFormatException(); } expState.TokenIndex += originalSequenceCount; // -1 } if (expState.ValueStack.Peek() == null) { return(false); } return(false); }
private void ParseCreateRelationship(KeywordToken relationshipToken) { AddToken(relationshipToken); var code = Code; var word = code.PeekWordR(); var relind = DkDict.Dict.GetRelInd(word); if (relind != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, relind.Definition)); if (code.ReadNumber()) { AddToken(new NumberToken(Scope, code.Span, code.Text)); } } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } } DkDict.Table table = null; var done = false; while (!code.EndOfFile && !done) { if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "updates") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); } else if (word == "prompt" || word == "comment" || word == "image" || word == "description") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } } else if (word == "one" || word == "many") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); word = code.PeekWordR(); if (!string.IsNullOrEmpty(word)) { table = DkDict.Dict.GetTable(word); if (table != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, table.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } } } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } } } else if (word == "to") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), "to")); if (code.ReadExactWholeWord("one") || code.ReadExactWholeWord("many")) { AddToken(new KeywordToken(Scope, code.Span, code.Text)); word = code.PeekWordR(); if (!string.IsNullOrEmpty(word)) { table = DkDict.Dict.GetTable(word); if (table != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, table.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } } } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } } } } else if (word == "order") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), "order")); if (code.ReadExactWholeWord("by")) { AddToken(new KeywordToken(Scope, code.Span, "by")); if (code.ReadExactWholeWord("unique")) { AddToken(new KeywordToken(Scope, code.Span, "unique")); } while (!code.EndOfFile) { if (code.PeekExact('(') || code.PeekExact('{')) { break; } if (table != null && !string.IsNullOrEmpty(word = code.PeekWordR())) { var field = table.GetColumn(word); if (field != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, field.Definition)); } else { break; } } else { break; } } } } else if (word == "tag") { ParseTag(Scope, this, new KeywordToken(Scope, code.MovePeekedSpan(), "tag"), _createRelationshipEndTokens); } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } else if (code.ReadExact('(') || code.ReadExact('{')) { var brackets = new BracketsToken(Scope); brackets.AddOpen(code.Span); AddToken(brackets); while (!code.EndOfFile) { if (code.ReadExact(')') || code.ReadExact('}')) { brackets.AddClose(code.Span); done = true; break; } if (!TryParseColumnDefinition(Scope, brackets, relind != null ? relind.Definition : null, true)) { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } } else { var exp = ExpressionToken.TryParse(Scope, _createRelationshipEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } }
private static bool TryParseColumnDefinition(Scope scope, GroupToken parent, Definition parentDef, bool includeNameAndDataType) { string word; var code = scope.Code; ExpressionToken exp; if (includeNameAndDataType) { // Column name if (parentDef != null && parentDef.AllowsChild) { if (!string.IsNullOrEmpty(word = code.PeekWordR())) { var childDef = parentDef.GetChildDefinitions(word).FirstOrDefault(); if (childDef != null) { parent.AddToken(new IdentifierToken(scope, code.MovePeekedSpan(), code.Text, childDef)); } } else { return(false); } } else { return(false); } // Data type if ((exp = ExpressionToken.TryParse(scope, _columnEndTokens)) != null) { parent.AddToken(exp); } } // Column attributes while (!code.EndOfFile) { if (code.PeekExact(')') || code.PeekExact('}') || code.PeekExact(',')) { break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "prompt" || word == "comment" || word == "group" || word == "row" || word == "col" || word == "rows" || word == "cols" || word == "image") { parent.AddToken(new KeywordToken(scope, code.MovePeekedSpan(), code.Text)); if ((exp = ExpressionToken.TryParse(scope, _columnEndTokens)) != null) { parent.AddToken(exp); } continue; } if (word == "endgroup" || word == "form" || word == "formonly" || word == "zoom" || word == "tool") { parent.AddToken(new KeywordToken(scope, code.MovePeekedSpan(), code.Text)); continue; } if (word == "tag") { ParseTag(scope, parent, new KeywordToken(scope, code.MovePeekedSpan(), "tag"), _columnEndTokens); continue; } if ((exp = ExpressionToken.TryParse(scope, _columnEndTokens)) != null) { parent.AddToken(exp); } else { break; } } else { if ((exp = ExpressionToken.TryParse(scope, _columnEndTokens)) != null) { parent.AddToken(exp); } else { break; } } } if (code.ReadExact(',')) { parent.AddToken(new DelimiterToken(scope, code.Span)); } return(true); }
private void ParseAlterTable() { var code = Code; DkDict.Table table = null; ExpressionToken exp; var word = code.PeekWordR(); if (!string.IsNullOrEmpty(word) && (table = DkDict.Dict.GetTable(word)) != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, table.Definition)); } else { if ((exp = ExpressionToken.TryParse(Scope, _alterTableEndTokens)) != null) { AddToken(exp); } } ParseTableAttributes(_alterTableEndTokens); if (code.ReadExact(';')) { AddToken(new StatementEndToken(Scope, code.Span)); return; } if (code.ReadExactWholeWord("before") || code.ReadExactWholeWord("after")) { AddToken(new KeywordToken(Scope, code.Span, code.Text)); } if (code.ReadExactWholeWord("column")) { AddToken(new KeywordToken(Scope, code.Span, code.Text)); } if (table != null) { var field = table.GetColumn(code.PeekWordR()); if (field != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), code.Text, field.Definition)); } else { if ((exp = ExpressionToken.TryParse(Scope, _alterTableEndTokens)) != null) { AddToken(exp); } } } if (code.ReadExactWholeWord("add") || code.ReadExactWholeWord("alter") || code.ReadExactWholeWord("drop") || code.ReadExactWholeWord("move")) { AddToken(new KeywordToken(Scope, code.Span, code.Text)); } if (code.ReadExactWholeWord("column")) { AddToken(new KeywordToken(Scope, code.Span, code.Text)); } if (code.ReadExactWholeWord("sametype")) { AddToken(new KeywordToken(Scope, code.Span, code.Text)); } else { var dataType = DataType.TryParse(new DataType.ParseArgs { Code = code, Scope = Scope, TokenCreateCallback = token => { AddToken(token); }, VisibleModel = true, AllowTags = true }); } TryParseColumnDefinition(Scope, this, table != null ? table.Definition : null, false); if (code.ReadExact(';')) { AddToken(new StatementEndToken(Scope, code.Span)); } }
private void ParseTableAttributes(string[] endTokens) { var code = Code; string word; ExpressionToken exp; while (true) { if (code.PeekExact('(') || code.PeekExact('{')) { break; } if (!code.Peek()) { break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "updates" || word == "display" || word == "modal" || word == "nopick" || word == "pick") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), code.Text)); continue; } if (word == "database" || word == "snapshot" || word == "prompt" || word == "comment" || word == "image" || word == "description" || word == "updates") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), code.Text)); if ((exp = ExpressionToken.TryParse(Scope, endTokens)) != null) { AddToken(exp); } continue; } if (word == "tag") { ParseTag(Scope, this, new KeywordToken(Scope, code.MovePeekedSpan(), "tag"), endTokens); continue; } if ((exp = ExpressionToken.TryParse(Scope, endTokens)) != null) { AddToken(exp); } else { break; } } else { if ((exp = ExpressionToken.TryParse(Scope, endTokens)) != null) { AddToken(exp); } else { break; } } } }
private void ParseCreateTable(KeywordToken tableToken) { AddToken(tableToken); var code = Code; // Table name if (!code.ReadWord()) { return; } var table = DkDict.Dict.GetTable(code.Text); if (table != null) { AddToken(new IdentifierToken(Scope, code.Span, code.Text, table.Definition)); } else { AddToken(new UnknownToken(Scope, code.Span, code.Text)); } // Table number if (!code.ReadNumber()) { return; } AddToken(new NumberToken(Scope, code.Span, code.Text)); // Table number+1 if (Code.ReadNumber()) { AddToken(new NumberToken(Scope, code.Span, code.Text)); } ExpressionToken exp; // Attributes ParseTableAttributes(_createTableEndTokens); BracketsToken brackets = null; BracesToken braces = null; GroupToken parent = null; if (code.ReadExact('(')) { brackets = new BracketsToken(Scope); brackets.AddOpen(code.Span); AddToken(brackets); parent = brackets; } else if (code.ReadExact('{')) { braces = new BracesToken(Scope); braces.AddOpen(code.Span); AddToken(braces); parent = braces; } else { return; } // Columns while (!code.EndOfFile) { if (code.ReadExact(')') || code.ReadExact('}')) { if (brackets != null) { brackets.AddClose(code.Span); } else if (braces != null) { braces.AddClose(code.Span); } return; } if (code.ReadExact(',')) { parent.AddToken(new DelimiterToken(Scope, code.Span)); continue; } if (!TryParseColumnDefinition(Scope, parent, table != null ? table.Definition : null, true)) { if ((exp = ExpressionToken.TryParse(Scope, _columnEndTokens)) != null) { parent.AddToken(exp); } else { break; } } } }
private void ParseCreateWorkspace() { var code = Code; var word = code.PeekWordR(); if (!string.IsNullOrEmpty(word)) { AddToken(new UnknownToken(Scope, code.MovePeekedSpan(), word)); } else { return; } while (true) { if (code.PeekExact('(')) { break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "prompt" || word == "comment" || word == "description" || word == "image") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); var exp = ExpressionToken.TryParse(Scope, _createWorkspaceEndTokens); if (exp != null) { AddToken(exp); } } else if (word == "tag") { ParseTag(Scope, this, new KeywordToken(Scope, code.MovePeekedSpan(), "tag"), _createWorkspaceEndTokens); } else { var exp = ExpressionToken.TryParse(Scope, _createWorkspaceEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } else { var exp = ExpressionToken.TryParse(Scope, _createWorkspaceEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } if (code.ReadExact('(')) { var brackets = new BracketsToken(Scope); brackets.AddOpen(code.Span); AddToken(brackets); while (true) { if (code.ReadExact(')')) { brackets.AddClose(code.Span); break; } if (code.ReadExact(',')) { brackets.AddToken(new DelimiterToken(Scope, code.Span)); } if (code.ReadWord()) { brackets.AddToken(new UnknownToken(Scope, code.Span, code.Text)); DkDict.Table table = null; do { table = DkDict.Dict.GetTable(code.PeekWordR()); if (table != null) { brackets.AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), code.Text, table.Definition)); if (code.ReadExact('\\')) { brackets.AddToken(new OperatorToken(Scope, code.Span, code.Text)); } } }while (table != null); while (true) { if (code.ReadExact(',')) { brackets.AddToken(new DelimiterToken(Scope, code.Span)); break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "prompt" || word == "comment") { brackets.AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); var exp = ExpressionToken.TryParse(Scope, _createWorkspaceColumnEndTokens); if (exp != null) { brackets.AddToken(exp); } } else if (word == "preload") { brackets.AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); } else if (word == "tag") { ParseTag(Scope, brackets, new KeywordToken(Scope, code.MovePeekedSpan(), "tag"), _createWorkspaceColumnEndTokens); } else { var exp = ExpressionToken.TryParse(Scope, _createWorkspaceColumnEndTokens); if (exp != null) { brackets.AddToken(exp); } else { break; } } } else { var exp = ExpressionToken.TryParse(Scope, _createWorkspaceColumnEndTokens); if (exp != null) { brackets.AddToken(exp); } else { break; } } } } } } }
private void ParseCreateTimeRelationship() { var code = Code; var word = code.PeekWordR(); var relind = DkDict.Dict.GetRelInd(word); if (relind != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, relind.Definition)); } else { return; } if (code.ReadNumber()) { AddToken(new NumberToken(Scope, code.Span, code.Text)); } else { return; } DkDict.Table table = null; while (!code.EndOfFile) { if (code.PeekExact('(') || code.PeekExact('{')) { break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "prompt" || word == "comment" || word == "description") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), word)); var exp = ExpressionToken.TryParse(Scope, _createTimeRelationshipEndTokens); if (exp != null) { AddToken(exp); } } else if (word == "tag") { ParseTag(Scope, this, new KeywordToken(Scope, code.MovePeekedSpan(), word), _createTimeRelationshipEndTokens); } else if (word == "order") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), "order")); if (code.ReadExactWholeWord("by")) { AddToken(new KeywordToken(Scope, code.Span, "by")); while (!code.EndOfFile) { if (code.PeekExact('(') || code.PeekExact('{')) { break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { var field = table != null?table.GetColumn(word) : null; if (field != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, field.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createTimeRelationshipEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } } } } else if (word == "to") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), "to")); if ((table = DkDict.Dict.GetTable(word)) != null) { AddToken(new IdentifierToken(Scope, code.Span, word, table.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createTimeRelationshipEndTokens); if (exp != null) { AddToken(exp); } } } else if ((table = DkDict.Dict.GetTable(word)) != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, table.Definition)); if (code.ReadExactWholeWord("to")) { AddToken(new KeywordToken(Scope, code.Span, "to")); if ((table = DkDict.Dict.GetTable(word)) != null) { AddToken(new IdentifierToken(Scope, code.Span, word, table.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createTimeRelationshipEndTokens); if (exp != null) { AddToken(exp); } } } } else { var exp = ExpressionToken.TryParse(Scope, _createTimeRelationshipEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } else { var exp = ExpressionToken.TryParse(Scope, _createTimeRelationshipEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } if (code.PeekExact('(')) { AddToken(BracketsToken.Parse(Scope, expectedDataType: null)); } }
private void ParseCreateIndex() { var code = Code; DkDict.RelInd relind = null; DkDict.Table table = null; string word; if (code.ReadExactWholeWord("primary")) { AddToken(new KeywordToken(Scope, code.Span, "primary")); } if (code.ReadExactWholeWord("nopick")) { AddToken(new KeywordToken(Scope, code.Span, "nopick")); } if (code.ReadExactWholeWord("NOPICK")) { AddToken(new KeywordToken(Scope, code.Span, "NOPICK")); } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if ((relind = DkDict.Dict.GetRelInd(word)) != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, relind.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createIndexEndTokens); if (exp != null) { AddToken(exp); } } } else { var exp = ExpressionToken.TryParse(Scope, _createIndexEndTokens); if (exp != null) { AddToken(exp); } } if (code.ReadExactWholeWord("on")) { AddToken(new KeywordToken(Scope, code.Span, "on")); if (!string.IsNullOrEmpty(word = code.PeekWordR()) && (table = DkDict.Dict.GetTable(word)) != null) { AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, table.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createIndexEndTokens); if (exp != null) { AddToken(exp); } } } while (!code.EndOfFile) { if (code.PeekExact('(') || code.PeekExact('{')) { break; } if (!string.IsNullOrEmpty(word = code.PeekWordR())) { if (word == "description") { AddToken(new KeywordToken(Scope, code.MovePeekedSpan(), "description")); var exp = ExpressionToken.TryParse(Scope, _createIndexEndTokens); if (exp != null) { AddToken(exp); } } else if (word == "tag") { ParseTag(Scope, this, new KeywordToken(Scope, code.MovePeekedSpan(), "tag"), _createIndexEndTokens); } else { var exp = ExpressionToken.TryParse(Scope, _createIndexEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } else { var exp = ExpressionToken.TryParse(Scope, _createIndexEndTokens); if (exp != null) { AddToken(exp); } else { break; } } } if (code.ReadExact('(') || code.ReadExact('{')) { var brackets = new BracketsToken(Scope); brackets.AddOpen(code.Span); AddToken(brackets); DkDict.Column field = null; while (!code.EndOfFile) { if (code.ReadExact(')') || code.ReadExact('}')) { brackets.AddClose(code.Span); break; } if (code.ReadExact(',')) { brackets.AddToken(new DelimiterToken(Scope, code.Span)); } if (table != null && !string.IsNullOrEmpty(word = code.PeekWordR()) && (field = table.GetColumn(word)) != null) { brackets.AddToken(new IdentifierToken(Scope, code.MovePeekedSpan(), word, field.Definition)); } else { var exp = ExpressionToken.TryParse(Scope, _createIndexColumnEndTokens); if (exp != null) { brackets.AddToken(exp); } else { break; } } } } }
private static IToken SubstituteValues(IToken parsed, IReadOnlyList<IToken> subResults) { IToken result = parsed; StringToken stringToken = parsed as StringToken; if (stringToken != null) { if (stringToken.Value.Contains("█")) { result = null; string[] bits = stringToken.Value.Split('█'); for (int i = 0; i < bits.Length - 1; i += 2) { int position = int.Parse(bits[i + 1]); IToken substituted = subResults[position]; IToken partial = string.IsNullOrWhiteSpace(bits[i]) ? substituted : new ExpressionToken(new StringToken(bits[i]), new StringPlusOperator(), substituted); result = result == null ? partial : new ExpressionToken(result, new StringPlusOperator(), partial); } if (!string.IsNullOrWhiteSpace(bits[bits.Length - 1])) result = new ExpressionToken(result, new StringPlusOperator(), new StringToken(bits[bits.Length - 1])); } } else { ExpressionToken expression = parsed as ExpressionToken; if (expression != null) result = new ExpressionToken(SubstituteValues(expression.First, subResults), expression.Operator, SubstituteValues(expression.Second, subResults)); } return result; }
/// <summary> /// Processes each rule object by creating tokens and associated actions /// </summary> /// <param name="rootobject">An object containing tokens and actions to run on these tokens</param> private void ProcessObject(NamespaceRecommendations namespaceRecommendations) { var namespaces = namespaceRecommendations.NameSpaces; foreach (var @namespace in namespaces) { foreach (var recommendation in @namespace.Recommendations) { RecommendedActions recommendedActions = recommendation.RecommendedActions .Where(ra => ra.Preferred == "Yes" && ra.TargetFrameworks.Any(t => t.Name.Equals(_targetFramework))).FirstOrDefault(); //There are recommendations, but none of them are preferred if (recommendedActions == null && recommendation.RecommendedActions.Count > 0) { LogHelper.LogError("No preferred recommendation set for recommendation {0} with target framework {1}", recommendation.Value, _targetFramework); continue; } else if (recommendedActions != null) { if (recommendedActions.Actions != null && recommendedActions.Actions.Count > 0) { var targetCPUs = new List <string> { "x86", "x64", "ARM64" }; try { targetCPUs = recommendedActions.TargetFrameworks.FirstOrDefault(t => t.Name == _targetFramework)?.TargetCPU; } catch { LogHelper.LogError("Error parsing CPUs for target framework"); } var recommendationType = Enum.Parse(typeof(ActionTypes), recommendation.Type); switch (recommendationType) { case ActionTypes.Namespace: { var usingToken = new UsingDirectiveToken() { Key = recommendation.Value, Description = recommendedActions.Description, TargetCPU = targetCPUs }; var namespaceToken = new NamespaceToken() { Key = recommendation.Value, Description = recommendedActions.Description, TargetCPU = targetCPUs }; if (!_rootNodes.Usingdirectivetokens.Contains(usingToken)) { _rootNodes.Usingdirectivetokens.Add(usingToken); } if (!_rootNodes.NamespaceTokens.Contains(namespaceToken)) { _rootNodes.NamespaceTokens.Add(namespaceToken); } ParseActions(usingToken, recommendedActions.Actions); ParseActions(namespaceToken, recommendedActions.Actions); break; } case ActionTypes.Class: { if (recommendation.KeyType == CTA.Rules.Config.Constants.BaseClass || recommendation.KeyType == CTA.Rules.Config.Constants.ClassName) { var token = new ClassDeclarationToken() { Key = recommendation.Value, Description = recommendedActions.Description, TargetCPU = targetCPUs, FullKey = recommendation.Value, Namespace = @namespace.Name }; if (!_rootNodes.Classdeclarationtokens.Contains(token)) { _rootNodes.Classdeclarationtokens.Add(token); } ParseActions(token, recommendedActions.Actions); } else if (recommendation.KeyType == CTA.Rules.Config.Constants.Identifier) { var token = new IdentifierNameToken() { Key = recommendation.Value, Description = recommendedActions.Description, TargetCPU = targetCPUs, FullKey = recommendation.Value, Namespace = @namespace.Name }; if (!_rootNodes.Identifiernametokens.Contains(token)) { _rootNodes.Identifiernametokens.Add(token); } ParseActions(token, recommendedActions.Actions); } break; } case ActionTypes.Interface: { if (recommendation.KeyType == CTA.Rules.Config.Constants.BaseClass || recommendation.KeyType == CTA.Rules.Config.Constants.ClassName) { var token = new InterfaceDeclarationToken() { Key = recommendation.Value, Description = recommendedActions.Description, TargetCPU = targetCPUs, FullKey = recommendation.Value, Namespace = @namespace.Name }; if (!_rootNodes.InterfaceDeclarationTokens.Contains(token)) { _rootNodes.InterfaceDeclarationTokens.Add(token); } ParseActions(token, recommendedActions.Actions); } else if (recommendation.KeyType == CTA.Rules.Config.Constants.Identifier) { var token = new IdentifierNameToken() { Key = recommendation.Value, Description = recommendedActions.Description, TargetCPU = targetCPUs, FullKey = recommendation.Value, Namespace = @namespace.Name }; if (!_rootNodes.Identifiernametokens.Contains(token)) { _rootNodes.Identifiernametokens.Add(token); } ParseActions(token, recommendedActions.Actions); } break; } case ActionTypes.Method: { var token = new InvocationExpressionToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.Invocationexpressiontokens.Contains(token)) { _rootNodes.Invocationexpressiontokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.Expression: { var token = new ExpressionToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.Expressiontokens.Contains(token)) { _rootNodes.Expressiontokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.Attribute: { var token = new AttributeToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.Attributetokens.Contains(token)) { _rootNodes.Attributetokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.ObjectCreation: { var token = new ObjectCreationExpressionToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.ObjectCreationExpressionTokens.Contains(token)) { _rootNodes.ObjectCreationExpressionTokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.MethodDeclaration: { var token = new MethodDeclarationToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.MethodDeclarationTokens.Contains(token)) { _rootNodes.MethodDeclarationTokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.ElementAccess: { var token = new ElementAccessToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.ElementAccesstokens.Contains(token)) { _rootNodes.ElementAccesstokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.MemberAccess: { var token = new MemberAccessToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value, Type = recommendation.ContainingType }; if (!_rootNodes.MemberAccesstokens.Contains(token)) { _rootNodes.MemberAccesstokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } case ActionTypes.Project: { var token = new ProjectToken() { Key = recommendation.Name, Description = recommendedActions.Description, TargetCPU = targetCPUs, Namespace = @namespace.Name, FullKey = recommendation.Value }; if (!_rootNodes.ProjectTokens.Contains(token)) { _rootNodes.ProjectTokens.Add(token); } ParseActions(token, recommendedActions.Actions); break; } default: break; } } } } } }
private static void ValidateTokens( ExpressionTokens tokens) { // usunięcie operatorów na końcu for (int i = tokens.Count - 1; i >= 0; i--) { ExpressionToken currentToken = tokens[i]; if (currentToken.TokenType == TokenType.OPERATOR) { // tokens.RemoveAt(i); throw new DynLanIncorrectExpressionFormatException("Invalid operator " + currentToken.TokenName + " at the end of expression!"); } else { break; } } Int32 equalOperators = 0; Int32 beginBrackets = 0; Int32 endBrackets = 0; for (int i = 0; i < tokens.Count; i++) { ExpressionToken prevToken = i > 0 ? tokens[i - 1] : null; ExpressionToken currentToken = tokens[i]; ExpressionToken nextToken = i < tokens.Count - 1 ? tokens[i + 1] : null; Boolean isStringOrNumber = /* currentToken.TokenType == TokenType.FUNCTION_CALL ||*/ currentToken.TokenType == TokenType.VARIABLE || currentToken.TokenType == TokenType.VALUE || currentToken.TokenType == TokenType.PROPERTY_NAME; Boolean isOperator = currentToken.TokenType == TokenType.OPERATOR; // przecinki moga wystepowac tylko w nawiasach i przy wolaniu funkcji if (currentToken.TokenType == TokenType.SEPARATOR) { Boolean functionCallFound = false; Int32 innerBrackets = 0; for (var j = i - 1; j >= 0; j--) { ExpressionToken token = tokens[j]; if (token.TokenType == TokenType.BRACKET_END) { innerBrackets++; } else if (token.TokenType == TokenType.BRACKET_BEGIN) { innerBrackets--; } else if (token.TokenType == TokenType.OPERATOR && OnpOnpTokenHelper.IsFunctionOperatorToken(token)) { if (innerBrackets == -1) { functionCallFound = true; break; } } } if (!functionCallFound) { throw new DynLanIncorrectExpressionFormatException("Invalid function call"); } } if (currentToken.TokenType == TokenType.EQUAL_OPERATOR) { equalOperators++; } if (nextToken != null) { if (isOperator && (nextToken.TokenType == TokenType.OPERATOR || nextToken.TokenType == TokenType.BRACKET_END)) { throw new DynLanIncorrectExpressionFormatException("Incorrect symbol '" + nextToken.TokenName + "' after operator '" + currentToken.TokenName + "'"); } else if (currentToken.TokenType == TokenType.BRACKET_BEGIN && !OnpOnpTokenHelper.IsFunctionOperatorToken(prevToken) && (nextToken.TokenType == TokenType.OPERATOR || nextToken.TokenType == TokenType.BRACKET_END || nextToken.TokenType == TokenType.EQUAL_OPERATOR)) { throw new DynLanIncorrectExpressionFormatException("Incorrect symbol '" + nextToken.TokenName + "' after operator '('"); } else if ( currentToken.TokenType == TokenType.BRACKET_END && nextToken.TokenType != TokenType.OPERATOR && nextToken.TokenType != TokenType.EQUAL_OPERATOR && nextToken.TokenType != TokenType.BRACKET_END && nextToken.TokenType != TokenType.SEPARATOR) { throw new DynLanIncorrectExpressionFormatException("Incorrect symbol '" + nextToken.TokenName + "' after operator ')'"); } } if (currentToken.TokenType == TokenType.BRACKET_BEGIN) { beginBrackets++; } else if (currentToken.TokenType == TokenType.BRACKET_END) { endBrackets++; } } if (beginBrackets != endBrackets) { throw new DynLanIncorrectExpressionFormatException("Number of opening brackets is not equal with number of ending brackets"); } }
/// <summary> /// Zwraca kolejny token /// </summary> public static ExpressionToken GetNextToken(IList <Char> Chars, Int32 StartIndex, ExpressionToken PrevToken) { // szukanie operatórw porównania Char[] foundCompareOperator = StringHelper.StartsWith(Chars, DynLanuageSymbols.CompareOperators, StartIndex, false); if (foundCompareOperator != null) { return(new ExpressionToken(foundCompareOperator, TokenType.OPERATOR) { StartIndex = StartIndex, EndIndex = StartIndex + foundCompareOperator.Length - 1 }); } // szukanie nawiasu START Char[] foundBracketBegin = StringHelper.StartsWith(Chars, DynLanuageSymbols.BracketBegin, StartIndex, false); if (foundBracketBegin != null) { return(new ExpressionToken(foundBracketBegin, TokenType.BRACKET_BEGIN) { StartIndex = StartIndex, EndIndex = StartIndex + foundBracketBegin.Length - 1 }); } // szukanie nawiasu END Char[] foundBracketEnd = StringHelper.StartsWith(Chars, DynLanuageSymbols.BracketEnd, StartIndex, false); if (foundBracketEnd != null) { return(new ExpressionToken(foundBracketEnd, TokenType.BRACKET_END) { StartIndex = StartIndex, EndIndex = StartIndex + foundBracketEnd.Length - 1 }); } // szukanie indexera START Char[] foundIndexerBegin = StringHelper.StartsWith(Chars, DynLanuageSymbols.IndexerBegin, StartIndex, false); if (foundIndexerBegin != null) { return(new ExpressionToken(foundIndexerBegin, TokenType.INDEXER_BEGIN) { StartIndex = StartIndex, EndIndex = StartIndex + foundIndexerBegin.Length - 1 }); } // szukanie indexera END Char[] foundIndexerEnd = StringHelper.StartsWith(Chars, DynLanuageSymbols.IndexerEnd, StartIndex, false); if (foundIndexerEnd != null) { return(new ExpressionToken(foundIndexerEnd, TokenType.INDEXER_END) { StartIndex = StartIndex, EndIndex = StartIndex + foundIndexerEnd.Length - 1 }); } // szukanie operatora równości Char[] foundEqualOperator = StringHelper.StartsWith(Chars, DynLanuageSymbols.EqualOperator, StartIndex, false); if (foundEqualOperator != null) { return(new ExpressionToken(foundEqualOperator, TokenType.EQUAL_OPERATOR) { StartIndex = StartIndex, EndIndex = StartIndex + foundEqualOperator.Length - 1 }); } // szukanie operatora Char[] foundOperator = StringHelper.StartsWith(Chars, DynLanuageSymbols.Operators, StartIndex, false); if (foundOperator != null) { return(new ExpressionToken(foundOperator, TokenType.OPERATOR) { StartIndex = StartIndex, EndIndex = StartIndex + foundOperator.Length - 1 }); } // szukanie operatorów logicznych Char[] foundLogicalOperator = StringHelper.StartsWith(Chars, DynLanuageSymbols.LogicalOperators, StartIndex, true); if (foundLogicalOperator != null) { Char?prevChar = StartIndex > 0 ? (Char?)Chars[StartIndex - 1] : null; Char?nextChar = StartIndex + foundLogicalOperator.Length < Chars.Count ? (Char?)Chars[StartIndex + foundLogicalOperator.Length] : null; if (!(prevChar != null && Char.IsLetterOrDigit(prevChar.Value) && Char.IsLetterOrDigit(foundLogicalOperator[0])) && !(nextChar != null && Char.IsLetterOrDigit(nextChar.Value) && Char.IsLetterOrDigit(foundLogicalOperator[foundLogicalOperator.Length - 1]))) { return(new ExpressionToken(foundLogicalOperator, TokenType.OPERATOR) { StartIndex = StartIndex, EndIndex = StartIndex + foundLogicalOperator.Length - 1 }); } else { prevChar = prevChar; } } // szukanie białych znaków Char[] foundWhitespaces = StringHelper.StartsWith(Chars, DynLanuageSymbols.Whitespaces, StartIndex, false); if (foundWhitespaces != null) { return(new ExpressionToken(foundWhitespaces, TokenType.WHITESPACE) { StartIndex = StartIndex, EndIndex = StartIndex + foundWhitespaces.Length - 1 }); } // szukanie przecinka Char[] foundSeparator = StringHelper.StartsWith(Chars, DynLanuageSymbols.Separators, StartIndex, false); if (foundSeparator != null) { return(new ExpressionToken(foundSeparator, TokenType.SEPARATOR) { StartIndex = StartIndex, EndIndex = StartIndex + foundSeparator.Length - 1 }); } TokenType tokenType = TokenType.VARIABLE; Int32 nextIndex = -1; List <Char> chars = new List <Char>(); OnpOnpStringFindResult firstNext = StringHelper.FirstNextIndex( Chars, StartIndex, new[] { DynLanuageSymbols.IndexerBegin, DynLanuageSymbols.IndexerEnd, DynLanuageSymbols.BracketBegin, DynLanuageSymbols.BracketEnd, DynLanuageSymbols.OperatorsAndPropertyAndBrackets, DynLanuageSymbols.Separators }, true); if (firstNext == null || firstNext.Index < 0) { for (Int32 j = StartIndex; j < Chars.Count; j++) { chars.Add(Chars[j]); } } else { int len = firstNext.Index; for (Int32 j = StartIndex; j < len; j++) { chars.Add(Chars[j]); } nextIndex = firstNext.Index; } if (nextIndex < 0) { nextIndex = Chars.Count; } StringHelper.Trim(chars); if (chars.Count != 0) { if (StringHelper.IsValue(chars)) { tokenType = TokenType.VALUE; } //if (tokenType == TokenType.VARIABLE) // for (var i = 0; i < chars.Count; i++) // chars[i] = Char.ToUpper(chars[i]); return (new ExpressionToken(chars, tokenType) { TokenLength = (nextIndex - StartIndex), StartIndex = StartIndex, EndIndex = StartIndex + chars.Count - 1 }); } return(null); }
/// <summary> /// zastapienie ciagu tokenków zmienną i podpiecie nowego wyrazenia do zmiennej /// </summary> public void CorrectQueueExpression( Expression Expression, ParserSettings ParserSettings, ExpressionGroup ExpressionGroup) { if (Expression == null) { return; } if (Expression.Tokens == null || Expression.Tokens.Count <= 0) { return; } for (var i = 0; i < Expression.Tokens.Count; i++) { ExpressionToken token = Expression.Tokens[i]; ExpressionToken token_next = i + 1 < Expression.Tokens.Count ? Expression.Tokens[i + 1] : null; ExpressionToken token_prev = i - 1 >= 0 ? Expression.Tokens[i - 1] : null; ExpressionTokens functionCallTokens = new ExpressionTokens( GetNextTokensOnSameLevel(Expression.Tokens, i)); if (functionCallTokens.Count > 1) { // generowanie expressions dla wnętrz funkcji IList <ExpressionTokens> functionParameters = SplitTokensIntoFunctionParameters(functionCallTokens).ToList(); foreach (ExpressionTokens functionParameter in functionParameters) { // generowanie expression z wyrażenia z parametru if (functionParameter.Count > 1) { Expression functionExpression = new Expression(); functionExpression.Tokens = new ExpressionTokens(functionParameter); functionExpression.IsOnpExecution = true; ExpressionGroup.Expressions[functionExpression.ID] = functionExpression; new Tokenizer().PrepareExpressions( functionExpression, ParserSettings, ExpressionGroup); ExpressionToken functionParameterToken = new ExpressionToken( functionExpression.ID.ToCharArray(), TokenType.VARIABLE); Int32 index = Expression.Tokens. RemoveSequence(functionParameter); Expression.Tokens.Insert( index, functionParameterToken); i = index; } // gdy pojedyncze wyrażenie w fukncji else { Int32 index = Expression.Tokens. IndexOfSequence(functionParameter); i = index; } } // dla operatora @ ustalenie liczby parametrów if (token_prev != null && token_prev.TokenType == TokenType.OPERATOR && (OnpOnpTokenHelper.IsFunctionOperatorToken(token_prev))) { token_prev.TokenData = new OnpTokenData(); token_prev.TokenData.FunctionParametersCount = functionParameters.Count; } } else { // zamiana typu zmiennej na property_name jeśli nie jest to pierwsza zmienna if (i > 0 && (token.TokenType == TokenType.VARIABLE || token.TokenType == TokenType.VALUE)) { if (token_next == null || !OnpOnpTokenHelper.IsFunctionOperatorToken(token_next)) { token.TokenType = TokenType.PROPERTY_NAME; } } // usunięcie operatorów typu 'get property' ('.') /*else if (OnpOnpTokenHelper.IsPropertyOperatorToken(token) ) * { * queueTokens.RemoveAt(i); * i--; * }*/ } } }
////////////////////////////////////////////////////////////////// private static void ConvertMinusOperatorToMinusOne(ExpressionTokens Tokens) { for (var i = 0; i < Tokens.Count; i++) { ExpressionToken token = Tokens[i]; ExpressionToken nextToken = null; ExpressionToken prevToken = null; // nie potrzebne poniewaz usuwane sa biale znaki /* * for (var j = i + 1; j < Tokens.Count; j++) * if (Tokens[j].TokenType != TokenType.WHITESPACE) * { * nextToken = Tokens[j]; * break; * } * * for (var j = i - 1; j >= 0; j--) * if (Tokens[j].TokenType != TokenType.WHITESPACE) * { * prevToken = Tokens[j]; * break; * } */ nextToken = (i + 1 < Tokens.Count ? Tokens[i + 1] : null); prevToken = (i - 1 >= 0 ? Tokens[i - 1] : null); // zamiana minusa na -1 if (token.TokenType == TokenType.OPERATOR) { if (StringHelper.StrEquals(token.TokenChars, OperatorTypeHelper.op_minus, false)) { //if (!(nextToken.TokenType == TokenType.VALUE && StringHelper.IsDateTime(nextToken.TokenChars))) { Tokens.RemoveAt(i); Int32 index = i; if (prevToken != null && ( /*prevToken.TokenType == TokenType.FUNCTION_CALL ||*/ prevToken.TokenType == TokenType.BRACKET_END || prevToken.TokenType == TokenType.VALUE || prevToken.TokenType == TokenType.PROPERTY_NAME || prevToken.TokenType == TokenType.VARIABLE)) { ExpressionToken operator1 = new ExpressionToken(OperatorTypeHelper.op_plus, TokenType.OPERATOR); Tokens.Insert(index++, operator1); } ExpressionToken value1 = new ExpressionToken(OperatorTypeHelper.number_minus_one, TokenType.VALUE); Tokens.Insert(index++, value1); if (nextToken != null) { ExpressionToken operator2 = new ExpressionToken(OperatorTypeHelper.op_multiply, TokenType.OPERATOR); Tokens.Insert(index++, operator2); } } } } } }
/// <summary> /// The value to use if the evaluated parameter is an expression token. /// </summary> /// <param name="finalValue">The evaluated parameter.</param> /// <returns>The token to use.</returns> public override IToken ValueIfFinalValueIsExpression(ExpressionToken finalValue) { return finalValue.Operator is SubstitutionOperator ? new ListToken() : base.ValueIfFinalValueIsExpression(finalValue); }
//////////////////////////// private void CorrectSetExpression( Expression SetExpression, String ValueExpressionID, ParserSettings ParserSettings, ExpressionGroup ExpressionGroup) { if (SetExpression.Tokens.Count == 0) { return; } // jeśli tylko jeden symbol (podstawienie do zmiennej) else if (SetExpression.Tokens.Count == 1) { ExpressionToken firstToken = SetExpression.Tokens[0]; if (firstToken.TokenType != TokenType.VARIABLE) { throw new DynLanIncorrectExpressionFormatException("Incorrect setting value to type " + firstToken.TokenType + " (" + firstToken.TokenName + ")"); } if (firstToken.TokenName.Length == 0 || Char.IsDigit(firstToken.TokenName[0])) { throw new DynLanIncorrectExpressionFormatException("Incorrect name of token '" + firstToken.TokenName + "'"); } SetExpression.Tokens.Clear(); SetExpression.Tokens.AddRange( new[] { #if CASE_INSENSITIVE new ExpressionToken(MethodSetValue.Name.ToUpper(), TokenType.VARIABLE), #else new ExpressionToken(MethodSetValue.Name, TokenType.VARIABLE), #endif new ExpressionToken(OperatorTypeHelper.op_methodcall, TokenType.OPERATOR), new ExpressionToken('(', TokenType.BRACKET_BEGIN), new ExpressionToken("'" + firstToken.TokenName + "'", TokenType.VALUE), new ExpressionToken(',', TokenType.SEPARATOR), new ExpressionToken(ValueExpressionID, TokenType.VARIABLE), new ExpressionToken(')', TokenType.BRACKET_END) }); } // jeśli przypisanie wyniku do elementu listy lub dictionary else { Int32 lastIndex = SetExpression.Tokens.Count - 1; ExpressionTokens sequence2 = new ExpressionTokens( new TokenizerQueue(). GetPrevTokensOnSameLevel( SetExpression.Tokens, lastIndex)); ExpressionTokens sequence1 = new ExpressionTokens( new TokenizerQueue(). GetPrevTokensOnSameLevel( SetExpression.Tokens, lastIndex - sequence2.Count)); ExpressionTokens sequence0 = new ExpressionTokens( new TokenizerQueue(). GetPrevTokensOnSameLevel( SetExpression.Tokens, lastIndex - sequence2.Count - sequence1.Count)); // a.get@(b).c = d ========> a.get@(b).set2@('c',d) if (sequence2.Count == 1 && sequence1.Count == 1 && OnpOnpTokenHelper.IsPropertyOperatorToken(sequence1[0])) { ExpressionToken propertyOperatorToken = sequence1[0]; ExpressionToken propertyNameToken = sequence2[0]; SetExpression.Tokens.Remove(propertyNameToken); SetExpression.Tokens.AddRange( new[] { #if CASE_INSENSITIVE new ExpressionToken(ExtenderSetValue.Name.ToUpper().ToCharArray(), TokenType.VARIABLE), #else new ExpressionToken(ExtenderSetValue.Name.ToCharArray(), TokenType.VARIABLE), #endif new ExpressionToken(OperatorTypeHelper.op_methodcall, TokenType.OPERATOR), new ExpressionToken('(', TokenType.BRACKET_BEGIN), new ExpressionToken("'" + propertyNameToken.TokenName + "'", TokenType.VALUE), new ExpressionToken(',', TokenType.SEPARATOR), new ExpressionToken(ValueExpressionID, TokenType.VARIABLE), new ExpressionToken(')', TokenType.BRACKET_END) }); } // a.get@(b).get@(d) = e ========> a.get@(b).set2@(d,e) else if ( sequence2.Count > 1 && sequence1.Count == 1 && OnpOnpTokenHelper.IsFunctionOperatorToken(sequence1[0]) && sequence0.Count == 1 && (sequence0[0].TokenType == TokenType.PROPERTY_NAME || sequence0[0].TokenType == TokenType.VARIABLE)) { ExpressionToken functionNameToken = sequence0[0]; ExpressionToken functionOperatorToken = sequence1[0]; ExpressionTokens functionCallToken = sequence2; functionNameToken.Set( ExtenderCollectionSetter.NameChars, false); Int32 bracketBeginIndex = SetExpression. Tokens. IndexOf(functionCallToken[0]); SetExpression.Tokens.Insert( bracketBeginIndex + 1, new ExpressionToken(ValueExpressionID, TokenType.VARIABLE)); SetExpression.Tokens.Insert( bracketBeginIndex + 2, new ExpressionToken(',', TokenType.SEPARATOR)); } else { throw new DynLanIncorrectExpressionFormatException(); } } }
private ExpressionToken ParseExpression(ExpressionToken lhs, TokenPriority priority) { while (true) { if (lhs.Type == TokenType.MinusOperator) { lhs = ParseExpression(UpcomingToken, TokenPriority.UnaryMinus); if (lhs is ValueToken) { lhs = -lhs; } else { throw new ArgumentException($"Unary minus is valid only for ValueToken!{lhs} is not a ValueToken"); } NextToken(); break; } else if (lhs.Type == TokenType.NotOperator) { lhs = ParseExpression(UpcomingToken, TokenPriority.Not); if (lhs is ValueToken) { lhs = !lhs; } else { throw new ArgumentException($"Unary minus is valid only for ValueToken!{lhs} is not a ValueToken"); } NextToken(); break; } break; } while (UpcomingToken is OperatorToken && (UpcomingToken as OperatorToken).Priority >= priority) { OperatorToken op = UpcomingToken as OperatorToken; NextToken(); ExpressionToken rhs = UpcomingToken; NextToken(); while (UpcomingToken is OperatorToken && (UpcomingToken as OperatorToken).Priority > op.Priority) { rhs = ParseExpression(rhs, (UpcomingToken as OperatorToken).Priority); NextToken(); } switch (op.Type) { case TokenType.PlusOperator: lhs = lhs + rhs; break; case TokenType.MinusOperator: lhs = lhs - rhs; break; case TokenType.DivideOperator: lhs = lhs / rhs; break; case TokenType.MultiplyOperator: lhs = lhs * rhs; break; case TokenType.EqualOperator: lhs = ExpressionToken.AreTokensEqual(lhs, rhs); break; case TokenType.LessOperator: lhs = lhs < rhs; break; case TokenType.GreaterOperator: lhs = lhs > rhs; break; case TokenType.LessOrEqualOperator: lhs = lhs <= rhs; break; case TokenType.GreaterOrEqualOperator: lhs = lhs >= rhs; break; case TokenType.AndOperator: lhs = lhs & rhs; break; case TokenType.OrOperator: lhs = lhs | rhs; break; } } return(lhs); }
//////////////////////////// /// <summary> /// Ustala kolejnośc tokenów zgodną z ONP /// </summary> private ExpressionTokens TransformToOnp( IList <ExpressionToken> Tokens, ParserSettings ParserSettings) { ExpressionTokens onpTokens = new ExpressionTokens(); // defaul settings if (ParserSettings == null) { ParserSettings = new ParserSettings(); } // przygotowanie stosu Stack <ExpressionToken> _tokenStack = new Stack <ExpressionToken>(); // ONP for (int i = 0; i < Tokens.Count; i++) { ExpressionToken token = Tokens[i]; if (token.TokenType == TokenType.VALUE || token.TokenType == TokenType.PROPERTY_NAME || token.TokenType == TokenType.VARIABLE || /*token.TokenType == TokenType.FUNCTION_CALL ||*/ token.TokenType == TokenType.WHITESPACE) { onpTokens.Add(token); } else if (token.TokenType == TokenType.BRACKET_BEGIN) { _tokenStack.Push(token); } else if (token.TokenType == TokenType.OPERATOR) { while (_tokenStack.Count > 0) { var lV = _tokenStack.Peek(); if (lV.Priority >= token.Priority) { _tokenStack.Pop(); onpTokens.Add(lV); } else { break; } } _tokenStack.Push(token); } else if (token.TokenType == TokenType.BRACKET_END) { while (_tokenStack.Count > 0) { var lV = _tokenStack.Peek(); if (lV.TokenType == TokenType.BRACKET_BEGIN) { _tokenStack.Pop(); break; } else { _tokenStack.Pop(); onpTokens.Add(lV); } } } } while (_tokenStack.Count > 0) { onpTokens.Add(_tokenStack.Pop()); } return(onpTokens); }
public void IsMatchMethod(object value, ExpressionToken token, IsMatchResult isMatchResult) { if (isMatchResult.ReturnValue != null && isMatchResult.ToString() != string.Empty) isMatchResult.ReturnValue += " && "; //Custom attribute if searching by style string tokenAttribute = token.Attribute.Replace("parentElement", "parentNode").Replace("parentWindow", "ownerDocument.defaultView"); string tokenValue = token.Value; if (tokenAttribute.Equals("style", StringComparison.OrdinalIgnoreCase)) { if (tokenValue.Contains("#")) tokenValue = HexToDecimal(tokenValue); tokenAttribute += ".cssText"; } string parentString = ""; while (tokenAttribute.StartsWith("parentNode.")) { tokenAttribute = tokenAttribute.Remove(0, "parentNode.".Length); parentString += ".parentNode"; } tokenAttribute = AttributeNormalizer.Normalize(tokenAttribute); if (tokenValue == "") tokenValue = "[]*"; if (!string.IsNullOrEmpty(tokenValue)) { if (token.MatchType == MatchType.Contains) { if (token.ExpectedMatchCount != int.MinValue) { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim) != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim).length == {2}) || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim) != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim).length == {2}))", tokenAttribute, tokenValue, token.ExpectedMatchCount); } else { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim) != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim).length >= 0) || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim) != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim).length >= 0))", tokenAttribute, tokenValue); } } else { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim) != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim)[0].length == elems[a]" + parentString + ".getAttribute('{0}').toString().length) || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim) != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim)[0].length == elems[a]" + parentString + ".{0}.toString().length))", tokenAttribute, tokenValue, token.Value.Length); } } else { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString() != null && elems[a]" + parentString + ".getAttribute('{0}').toString() == '') || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString() != null && elems[a]" + parentString + ".{0}.toString() == '')) ", tokenAttribute, tokenValue, token.Value.Length); } isMatchResult.ContinueChecking = true; }
/// <summary> /// The value to use if the evaluated parameter is an expression token. /// </summary> /// <param name="finalValue">The evaluated parameter.</param> /// <returns>The token to use.</returns> public virtual IToken ValueIfFinalValueIsExpression(ExpressionToken finalValue) { return new NullToken(); }
private static void DetermineOperatorType(ListReader <ExpressionToken> reader, ExpressionToken tk) { tk.OperatorType = KnownOperators.TryMap(tk.Value); switch (tk.OperatorType) { case OperatorType.PreDecrement: case OperatorType.PreIncrement: // detect if it's really post ++ -- versions var prev = reader.Position > 1 ? reader.Peek(-2) : null; if (prev != null && prev.TokenType == ExpressionTokenType.Value) { if (tk.OperatorType == OperatorType.PreIncrement) { tk.OperatorType = OperatorType.PostIncrement; } else { tk.OperatorType = OperatorType.PostDecrement; } } break; case OperatorType.Addition: case OperatorType.Subtraction: // detect if unary + - prev = reader.Position > 1 ? reader.Peek(-2) : null; if (prev == null || (prev.TokenType == ExpressionTokenType.Operator && prev.OperatorType != OperatorType.PostDecrement && prev.OperatorType != OperatorType.PostIncrement)) { if (tk.OperatorType == OperatorType.Addition) { tk.OperatorType = OperatorType.UnaryPlus; } else { tk.OperatorType = OperatorType.UnaryMinus; } } break; case OperatorType.None: throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Operator {0} is not supported.", tk.Value)); } }
public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal) { if (_function == null) { if (first == null) throw new Exception($"Operation {Text} can not be unary."); IToken functionToken = first.Evaluate(parameters, isFinal); if (functionToken == null || functionToken is ListToken) throw new Exception($"First element of Operation {Text} is not unique."); string function = functionToken.Text; switch (function) { case AndFunction.ID: _function = new AndFunction(); break; case AggregateFunction.ID: _function = new AggregateFunction(); break; case CaseFunction.ID: _function = new CaseFunction(); break; case ComparisonFunction.ID: _function = new ComparisonFunction(); break; case ContainsFunction.ID: _function = new ContainsFunction(); break; case CountFunction.ID: _function = new CountFunction(); break; case DoubleFunction.ID: _function = new DoubleFunction(); break; case IfFunction.ID: _function = new IfFunction(); break; case IntFunction.ID: _function = new IntFunction(); break; case JoinFunction.ID: _function = new JoinFunction(); break; case OrFunction.ID: _function = new OrFunction(); break; case OverFunction.ID: _function = new OverFunction(); break; case RangeFunction.ID: _function = new RangeFunction(); break; case RegexFunction.ID: _function = new RegexFunction(); break; case ReverseFunction.ID: _function = new ReverseFunction(); break; case SplitFunction.ID: _function = new SplitFunction(); break; case SumFunction.ID: _function = new SumFunction(); break; case UserFunction.ID: _function = new UserFunction(); break; default: ListToken newList = new ListToken { new ExpressionToken(null, new SubstitutionOperator(), new StringToken(function)) }; ListToken oldList = last.Evaluate(parameters, isFinal) as ListToken; if (oldList != null) { foreach (IToken token in oldList.Tokens) newList.Add(token); } else { newList.Add(last); } ExpressionToken expression = new ExpressionToken(null, new FunctionOperator(new UserFunction()), newList); return expression.Evaluate(parameters, isFinal); } } IToken parameterList; if (isFinal && _function is UserFunction) { parameterList = PrepareUserFunction(last, parameters); } else if (_function.IsComparisonFunction) { parameterList = last.EvaluateList(); } else { parameterList = last.Evaluate(parameters, !_function.FinalCanBeExpression && isFinal); ExpressionToken expression = parameterList as ExpressionToken; if (expression != null) { if (isFinal) { IToken substitute = _function.ValueIfFinalValueIsExpression(expression); if (substitute is NullToken) return new ExpressionToken(null, new FunctionOperator(_function), parameterList); parameterList = substitute; } else { return new ExpressionToken(null, new FunctionOperator(_function), parameterList); } } } return _function.Perform(parameterList, parameters, isFinal); }
private static IToken PerformSubstitutionOperation(IReadOnlyList<string> tokens) { IToken token = null; if (!string.IsNullOrWhiteSpace(tokens[1])) { token = new StringToken(tokens[1]); } IToken result = new ExpressionToken(null, new SubstitutionToken(), token); if (!string.IsNullOrWhiteSpace(tokens[0])) { token = ParseExpressionNoBrackets(tokens[0].Trim()); ExpressionToken expression = token as ExpressionToken; if (expression?.NeedsSecond ?? false) result = expression.SetSecond(result); else result = new ExpressionToken(token, new StringPlusToken(), result); } if (!string.IsNullOrWhiteSpace(tokens[3])) { token = ParseExpressionNoBrackets(tokens[3].Trim()); result = new ExpressionToken(result, new StringPlusToken(), token); } return result; }
private static Boolean GotoCatch( DynLanContext DynLanContext, Exception exception) { while (true) { DynLanState currentState = DynLanContext. CurrentState; // reset dla kontekstu obliczeń, ponieważ przechodzimy do catch'a currentState.ExpressionContext = null; DynLanCodeLines lines = currentState.GetCurrentLines(); DynLanCodeLine currentLine = currentState.GetCurrentLine(); // poszukanie poprzedniego catch'a DynLanCodeLine prevCatch = DynLanCodeLinesExtender. PrevLineWithLessDepth(lines, currentLine, l => l.OperatorType == EOperatorType.CATCH); // poszukanie poprzedniego try'a DynLanCodeLine prevTry = DynLanCodeLinesExtender. PrevLineWithLessDepth(lines, currentLine, l => l.OperatorType == EOperatorType.TRY); if (exception is DynLanAbortException) { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } else if (prevTry == null) { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } // jeśli znalazł try'a i nie jesteśmy w catch'u else if (prevTry.Depth < currentLine.Depth && (prevCatch == null || lines.IndexOf(prevCatch) < lines.IndexOf(prevTry))) { DynLanCodeLine nextCatch = DynLanCodeLinesExtender.NextOnSameOrLower( lines, prevTry, i => i.OperatorType == EOperatorType.CATCH); if (nextCatch != null) { ExpressionToken variableForException = null; if (nextCatch.ExpressionGroup != null && nextCatch.ExpressionGroup.MainExpression != null && nextCatch.ExpressionGroup.MainExpression.Tokens != null && nextCatch.ExpressionGroup.MainExpression.Tokens.Count > 0) { #if !NET20 variableForException = nextCatch. ExpressionGroup.MainExpression.Tokens. FirstOrDefault(i => i.TokenType != TokenType.BRACKET_BEGIN); #else variableForException = Linq2.FirstOrDefault(nextCatch. ExpressionGroup.MainExpression.Tokens, i => i.TokenType != TokenType.BRACKET_BEGIN); #endif } currentState.CurrentLineID = nextCatch.ID; if (variableForException != null && !String.IsNullOrEmpty(variableForException.TokenName)) { currentState.Object[variableForException.TokenName] = exception; } break; } else { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } } else { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } } return(false); }
private static IToken PerformSubstitutionOperation(IReadOnlyList<string> tokens, int startPosition, Action<IToken, int, string> callback) { IToken token = null; string firstToken = tokens[1]; if (!string.IsNullOrWhiteSpace(firstToken)) token = new StringToken(firstToken); IToken result = new ExpressionToken(null, new SubstitutionOperator(), token); //if (token != null) // callback?.Invoke(result, startPosition + tokens[0].Length, firstToken); if (!string.IsNullOrWhiteSpace(tokens[0])) { token = ParseExpressionNoBrackets(tokens[0].Trim(), startPosition, null); ExpressionToken expression = token as ExpressionToken; if (expression?.NeedsSecond ?? false) result = expression.SetSecond(result); else result = new ExpressionToken(token, new StringPlusOperator(), result); } if (!string.IsNullOrWhiteSpace(tokens[4])) { token = ParseExpressionNoBrackets(tokens[4].Trim(), startPosition + tokens[0].Length + tokens[3].Length, null); result = new ExpressionToken(result, new StringPlusOperator(), token); } return result; }
private static void OptimizeTokens( ExpressionTokens tokens) { // usuwanie pustych nawiasów /*for (int i = 0; i < tokens.Count - 1; i++) * { * ExpressionToken currentToken = tokens[i]; * ExpressionToken nextToken = tokens[i + 1]; * * // usuwanie pustych nawiasów * if (currentToken.TokenType == TokenType.BRACKET_BEGIN && * nextToken.TokenType == TokenType.BRACKET_END) * { * tokens.Insert(i + 1, new ExpressionToken(OperatorTypeHelper.number_zero, TokenType.VALUE)); * i--; * } * }*/ // zamiana dwóch minusów na plus for (int i = 0; i < tokens.Count - 1; i++) { ExpressionToken currentToken = tokens[i]; ExpressionToken nextToken = tokens[i + 1]; Boolean isMinus = currentToken.TokenType == TokenType.OPERATOR && StringHelper.StartsWith(currentToken.TokenChars, DynLanuageSymbols.MinusChars, false); Boolean isNextMinus = nextToken.TokenType == TokenType.OPERATOR && StringHelper.StartsWith(nextToken.TokenChars, DynLanuageSymbols.MinusChars, false); // zamiana dwóch minusów na plus if (isMinus && isNextMinus) { currentToken.Set(DynLanuageSymbols.PlusChars); tokens.RemoveAt(i + 1); i--; } } // zamiana dwóch plusów na jeden plus /*for (int i = 0; i < tokens.Count - 1; i++) * { * ExpressionToken currentToken = tokens[i]; * ExpressionToken nextToken = tokens[i + 1]; * * Boolean isPlus = * currentToken.TokenType == TokenType.OPERATOR && * StringHelper.StartsWith(currentToken.TokenChars, DynLanuageSymbols.PlusChars, false); * * Boolean isNextPlus = * nextToken.TokenType == TokenType.OPERATOR && * StringHelper.StartsWith(nextToken.TokenChars, DynLanuageSymbols.PlusChars, false); * * // zamiana dwóch plusów na jeden plus * if (isPlus && isNextPlus) * { * tokens.RemoveAt(i + 1); * i--; * } * }*/ }
public static ArgumentsExpression Parse(ExpressionWalker ew, ExpressionToken left, ExpressionToken right, bool argsOptional, Type caller = null) { var args = new ArgumentsExpression(); ew.IsCurrentOrThrow(left); ew.NextOrThrow(); var exprs = new List <Expression>(); while (ew.Current.Token != right && ew.HasNext) { if (ew.Current.Token == ExpressionToken.Comma) { if (ew.HasBack && ew.PeakBack.Token == ExpressionToken.Comma) { exprs.Add(NullIdentity.Instance); } ew.NextOrThrow(); continue; } var expression = ParseExpression(ew, caller); if (ew.IsCurrent(ExpressionToken.Colon)) { //handle keyvalue item exprs.Add(KeyValueExpression.Parse(ew, expression, caller)); } else { exprs.Add(expression); } } if (exprs.Count == 0 && !argsOptional) { throw new UnexpectedTokenException($"Was expecting an expression between {left} and {right}"); } ew.Next(); args.Arguments = exprs.ToArray(); return(args); }
public RightOperatorExpression(Expression left, ExpressionToken op) { Left = left; Op = op; }
/// <summary> /// Converts an expression token to a list of tokens if possible and required. /// </summary> /// <param name="expression">The expression to convert.</param> /// <returns>The original token by default.</returns> public virtual IToken EvaluateList(ExpressionToken expression) { return expression; }
public static ForStatement Parse(Scope scope, KeywordToken forToken) { var ret = new ForStatement(scope); var code = scope.Code; ret.AddToken(forToken); if (!code.ReadExact('(')) { return(ret); } var brackets = new BracketsToken(scope); brackets.AddOpen(code.Span); ret.AddToken(brackets); // Initializer var exp = ExpressionToken.TryParse(scope, _conditionEndTokens); if (exp != null) { brackets.AddToken(exp); } if (!code.ReadExact(';')) { return(ret); } // Condition exp = ExpressionToken.TryParse(scope, _conditionEndTokens); if (exp != null) { brackets.AddToken(exp); } if (!code.ReadExact(';')) { return(ret); } // Increment exp = ExpressionToken.TryParse(scope, _conditionEndTokens); if (exp != null) { brackets.AddToken(exp); } if (!code.ReadExact(')')) { return(ret); } brackets.AddClose(code.Span); // Body var bodyScope = scope.Clone(); bodyScope.BreakOwner = ret; bodyScope.ContinueOwner = ret; if (!code.PeekExact('{')) { return(ret); } var body = BracesToken.Parse(bodyScope, null); ret.AddToken(body); return(ret); }
public void IsMatchMethod(object value, ExpressionToken token, IsMatchResult isMatchResult) { IHTMLElement elem = (IHTMLElement)value; string copyOfAttribute = token.Attribute; while (copyOfAttribute.StartsWith("parentElement.")) { if (elem.parentElement == null) { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; return; } elem = elem.parentElement; copyOfAttribute = copyOfAttribute.Remove(0, "parentElement.".Length); } if (!token.Attribute.Equals(copyOfAttribute)) //if we removed parentElement we should re-normalize. copyOfAttribute = AttributeNormalizer.Normalize(copyOfAttribute); if (copyOfAttribute.StartsWith("parentWindow")) { string attr; isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; try { attr = copyOfAttribute.Split('.')[1]; } catch (Exception ex) { //need to throw expression wrong format exception throw ex; } if (attr == "location") { if (token.IsMatch(((HTMLDocument)elem.document).parentWindow.location.href)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } } if (attr == "name") { if (token.IsMatch(((HTMLDocument)elem.document).parentWindow.name)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } } } else { if (elem != null && !(elem is IHTMLUnknownElement)) { string attr; if (copyOfAttribute.Equals("style", StringComparison.OrdinalIgnoreCase)) { //the border, padding, and margin attributes if specified inline get applied to all 4 sides: //EG: border-right, border-left, border-top, border-bottom //Assumption: use -right for the global search if (token.Value.ToLower().Contains("border:")) token.Value = token.Value.Replace("border:", "border-right:"); if (token.Value.ToLower().Contains("padding:")) token.Value = token.Value.Replace("padding:", "padding-right:"); if (token.Value.ToLower().Contains("margin:")) token.Value = token.Value.Replace("margin:", "margin-right:"); attr = elem.style.cssText; } else { object elemAttribute = elem.getAttribute(copyOfAttribute, 0); if (!(elem.getAttribute(token.Attribute, 0) is string) && !(elemAttribute is Boolean) && !(elemAttribute is int)) if (((IHTMLElement4)elem).getAttributeNode(copyOfAttribute) != null && ((IHTMLElement4)elem).getAttributeNode(copyOfAttribute).nodeValue != null) attr = ((IHTMLElement4)elem).getAttributeNode(copyOfAttribute).nodeValue.ToString(); else attr = elemAttribute != null ? elemAttribute.ToString() : string.Empty; else attr = elemAttribute.ToString(); } if (token.IsMatch(attr)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } else { if (token.IsPartOfStyle) { string origAttr = token.Attribute; string origValue = token.Value; if (token.Attribute == "style" && token.Value.Contains(":")) { token.Attribute = token.Value.Split(new char[] { ':' })[0]; token.Value = token.Value.Split(new char[] { ':' })[1]; } if (((IHTMLElement2)elem).currentStyle.getAttribute(token.Attribute, 0) != null) attr = ((IHTMLElement2)elem).currentStyle.getAttribute(token.Attribute, 0).ToString(); if (token.IsMatch(attr)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } else { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; } token.Attribute = origAttr; token.Value = origValue; } else { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; } } } else { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; } } }
internal OnStatement(OnFunctionPrimitive theToken, ExpressionToken exp, FunctionToken func) { this.token = theToken; this.exp = exp; this.func = func; }