public void GetStringConstant() { var expr = new StringExpression("foo"); Assert.AreEqual("foo", expr.Value); Assert.AreSame(StringType.Instance, expr.Type); Assert.AreSame(expr, expr.Reduce()); }
public void StringExpressionProducesStringThroughConstructor() { string[] strings = new [] { "one", "two", "three" }; StringExpression expression; foreach (var s in strings) { expression = new StringExpression(s); Assert.AreEqual("\"" + s + "\";", expression.ToString()); } expression = new StringExpression(); foreach (var s in strings) { expression.Value = s; Assert.AreEqual(s, expression.Value); } }
private Expression ReadString() { Token start = Consume(TokenKind.StringStart); StringExpression exp = new StringExpression(start.Line, start.Column); while (true) { Token tok = Current; if (tok.Kind == TokenKind.StringEnd) { Consume(); break; } if (tok.Kind == TokenKind.EOF) throw new ParseException("Unexpected end of file", tok.Line, tok.Column); if (tok.Kind == TokenKind.StringText) { Consume(); exp.AddExpression(new StringLiteralExpression(tok.Line, tok.Column, tok.Text)); } else if (tok.Kind == TokenKind.ExpStart) exp.AddExpression(ReadExpression()); else throw new ParseException("Unexpected token in string: " + tok.Kind, tok.Line, tok.Column); } return exp.Count == 1 ? exp[0] : exp; }
private AphidObject InterpretStringExpression(StringExpression expression) { return CallInitFunction(new AphidObject(StringParser.Parse(expression.Value))); }
public void StringExpressionProducesEmptyString() { var s = new StringExpression(); Assert.AreEqual("\"\";", s.ToString()); }
int IExpressionVisitor <int> .VisitString(StringExpression expression) { _Writer.WriteLiteral(expression.Value); return(0); }
public override SearchParameterQueryGeneratorContext VisitString(StringExpression expression, SearchParameterQueryGeneratorContext context) { return(expression.AcceptVisitor(_componentHandlers[(int)expression.ComponentIndex], context)); }
public virtual void Accept(StringExpression stringConst) { }
protected override string VisitStringExpression(StringExpression exp, out object resultObj) { resultObj = null; return(exp.ToString()); }
public override SearchParameterQueryGeneratorContext VisitString(StringExpression expression, SearchParameterQueryGeneratorContext context) { VisitSimpleString(expression, context, V1.Resource.ResourceId, expression.Value); return(context); }
protected virtual Expression VisitString(StringExpression node) { return(node); }
public override SearchParameterQueryGeneratorContext VisitString(StringExpression expression, SearchParameterQueryGeneratorContext context) { return(VisitSimpleString(expression, context, VLatest.TokenText.Text, expression.Value)); }
/// <summary> /// evaluates expression. /// This method is used by TemplateManager extensibility. /// </summary> public object EvalExpression(Expression exp) { currentExpression = exp; try { if (exp is StringLiteral) { return(((StringLiteral)exp).Content); } else if (exp is Name) { return(GetValue(((Name)exp).Id)); } else if (exp is FieldAccess) { FieldAccess fa = (FieldAccess)exp; object obj = EvalExpression(fa.Exp); string propertyName = fa.Field == "channelid"?"catalogid":fa.Field;//因为obj里面没有channelid,而对应的是catalgoid return(EvalProperty(obj, propertyName)); } else if (exp is MethodCall) { MethodCall ma = (MethodCall)exp; object obj = EvalExpression(ma.CallObject); string methodName = ma.Name; return(EvalMethodCall(obj, methodName, EvalArguments(ma.Args))); } else if (exp is IntLiteral) { return(((IntLiteral)exp).Value); } else if (exp is DoubleLiteral) { return(((DoubleLiteral)exp).Value); } else if (exp is FCall) { FCall fcall = (FCall)exp; if (!functions.ContainsKey(fcall.Name)) { string msg = string.Format("函数 '{0}' 未定义.", fcall.Name); throw new TemplateRuntimeException(msg, exp.Line, exp.Col); } TemplateFunction func = functions[fcall.Name]; object[] values = EvalArguments(fcall.Args); return(func(values)); } else if (exp is StringExpression) { StringExpression stringExp = (StringExpression)exp; StringBuilder sb = new StringBuilder(); foreach (Expression ex in stringExp.Expressions) { sb.Append(EvalExpression(ex)); } return(sb.ToString()); } else if (exp is BinaryExpression) { return(EvalBinaryExpression(exp as BinaryExpression)); } else if (exp is ArrayAccess) { return(EvalArrayAccess(exp as ArrayAccess)); } else { throw new TemplateRuntimeException("Invalid expression type: " + exp.GetType().Name, exp.Line, exp.Col); } } catch (TemplateRuntimeException ex) { DisplayError(ex); return(null); } catch (Exception ex) { DisplayError(new TemplateRuntimeException(ex.Message, currentExpression.Line, currentExpression.Col)); return(null); } }
public override void ConstructStringExpression(StringExpression node) { this.Write(node.StartDelimiter); this.Write(node.EscapedValue); this.Write(node.EndDelimiter); }
public void InvalidSyntax(string input) { StringExpression parser = new StringExpression(input, new TokenFactory()); Assert.Throws <ArgumentException>(() => parser.Parse()); }
private IExpression ParseExpression() { List<IExpression> arguments; List<KeyValuePair<IExpression, IExpression>> elements; IExpression expression; int index; IExpression key; decimal number; IExpression value; switch (this.lexer.Current.Type) { case LexemType.BracketBegin: elements = new List<KeyValuePair<IExpression, IExpression>> (); index = 0; for (this.lexer.Next (LexerMode.BLOCK); this.lexer.Current.Type != LexemType.BracketEnd; ) { key = this.ParseExpression (); if (this.lexer.Current.Type == LexemType.Colon) { this.lexer.Next (LexerMode.BLOCK); value = this.ParseExpression (); } else { value = key; key = new NumberExpression (index++); } elements.Add (new KeyValuePair<IExpression, IExpression> (key, value)); if (this.lexer.Current.Type == LexemType.Comma) this.lexer.Next (LexerMode.BLOCK); } this.lexer.Next (LexerMode.BLOCK); expression = new ArrayExpression (elements); break; case LexemType.Number: expression = new NumberExpression (decimal.TryParse (this.lexer.Current.Content, out number) ? number : 0); this.lexer.Next (LexerMode.BLOCK); break; case LexemType.String: expression = new StringExpression (this.lexer.Current.Content); this.lexer.Next (LexerMode.BLOCK); break; case LexemType.Literal: expression = this.ParseName (); break; default: throw new UnexpectedException (this.lexer, "expression"); } while (true) { switch (this.lexer.Current.Type) { case LexemType.BracketBegin: this.lexer.Next (LexerMode.BLOCK); value = this.ParseExpression (); if (this.lexer.Current.Type != LexemType.BracketEnd) throw new UnexpectedException (this.lexer, "array index end (']')"); this.lexer.Next (LexerMode.BLOCK); expression = new AccessExpression (expression, value); break; case LexemType.Dot: this.lexer.Next (LexerMode.BLOCK); if (this.lexer.Current.Type != LexemType.Literal) throw new UnexpectedException (this.lexer, "field name"); expression = new AccessExpression (expression, new StringExpression (this.lexer.Current.Content)); this.lexer.Next (LexerMode.BLOCK); break; case LexemType.ParenthesisBegin: arguments = new List<IExpression> (); for (this.lexer.Next (LexerMode.BLOCK); this.lexer.Current.Type != LexemType.ParenthesisEnd; ) { arguments.Add (this.ParseExpression ()); if (this.lexer.Current.Type == LexemType.Comma) this.lexer.Next (LexerMode.BLOCK); } this.lexer.Next (LexerMode.BLOCK); expression = new CallExpression (expression, arguments); break; default: return expression; } } }
private BoundExpression Bind(StringExpression stringExpression) { var str = stringExpression.String; return(new BoundStringExpression(str)); }
/// <summary> /// Replaces the specified expr. /// </summary> /// <param name="expr">The expr.</param> /// <param name="find">The find.</param> /// <param name="replace">The replace.</param> /// <returns></returns> public static StringExpression Replace(this StringExpression expr, string find, StringExpression replace) { if (string.IsNullOrEmpty(find)) { throw new ArgumentNullException("find"); } if (ReferenceEquals(replace, null)) { throw new ArgumentNullException("replace"); } var newExpr = (StringExpression)expr.Clone(); newExpr.Sql = "REPLACE(" + newExpr.Sql + ", ?, ?)"; newExpr.ChildExpressions.Add(new StringParameterExpression(find, expr.IsUnicode)); newExpr.ChildExpressions.Add(replace); return(newExpr); }
public abstract void ConstructStringExpression(StringExpression node);
/// <summary> /// Replaces the specified expr. /// </summary> /// <param name="expr">The expr.</param> /// <param name="find">The find.</param> /// <param name="replace">The replace.</param> /// <returns></returns> public static StringExpression Replace(this StringExpression expr, StringExpression find, StringExpression replace) { if (ReferenceEquals(find, null)) { throw new ArgumentNullException("find"); } if (ReferenceEquals(replace, null)) { throw new ArgumentNullException("replace"); } var newExpr = (StringExpression)expr.Clone(); newExpr.Sql = "REPLACE(" + newExpr.Sql + ", ?, ?)"; newExpr.ChildExpressions.Add(find); newExpr.ChildExpressions.Add(replace); return(newExpr); }
public void Visit(StringExpression strExpr) { sa.Print("\"" + strExpr.String + "\""); }
/// <summary> /// To ASCII code. /// </summary> /// <param name="expr">The expr.</param> /// <returns></returns> public static Int32Expression ToAscii(this StringExpression expr) { return(new Int32Expression("ASCII(" + expr.Sql + ")", ((StringExpression)expr.Clone()).ChildExpressions)); }
public void ParseOrAndExpressionTest1() { Action <string> testExpression = delegate(string condition) { IConditionExpression expression = ConditionExpressionParser.Instance.Parse( condition); Assert.IsNotNull(expression, "Expected an expression instance."); BinaryOperatorExpression operatorExpression = expression as BinaryOperatorExpression; Assert.IsNotNull(operatorExpression, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.Or, operatorExpression.Operator, "Unexpected operator."); // // Or left // BinaryOperatorExpression andExpression = operatorExpression.Left as BinaryOperatorExpression; Assert.IsNotNull(andExpression, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.And, andExpression.Operator, "Unexpected operator."); // // And Left // BinaryOperatorExpression testExpression1 = andExpression.Left as BinaryOperatorExpression; Assert.IsNotNull(testExpression1, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.Equal, testExpression1.Operator, "Unexpected operator."); ElementAttributeExpression test1AttributeExpression = testExpression1.Left as ElementAttributeExpression; Assert.IsNotNull(test1AttributeExpression, "Unexpected left node type."); Assert.AreEqual(ElementAttributeType.Name, test1AttributeExpression.ElementAttribute, "Attribute expression was not parsed correctly."); StringExpression test1StringExpression = testExpression1.Right as StringExpression; Assert.IsNotNull(test1StringExpression, "Unexpected right node type."); Assert.AreEqual("Test 1", test1StringExpression.Text, "String expression was not parsed correctly."); // // And Right // BinaryOperatorExpression testExpression2 = andExpression.Right as BinaryOperatorExpression; Assert.IsNotNull(testExpression2, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.Equal, testExpression2.Operator, "Unexpected operator."); ElementAttributeExpression test2AttributeExpression = testExpression2.Left as ElementAttributeExpression; Assert.IsNotNull(test2AttributeExpression, "Unexpected left node type."); Assert.AreEqual(ElementAttributeType.Name, test2AttributeExpression.ElementAttribute, "Attribute expression was not parsed correctly."); StringExpression test2StringExpression = testExpression2.Right as StringExpression; Assert.IsNotNull(test2StringExpression, "Unexpected right node type."); Assert.AreEqual("Test 2", test2StringExpression.Text, "String expression was not parsed correctly."); // // Or right // BinaryOperatorExpression testExpression3 = operatorExpression.Right as BinaryOperatorExpression; Assert.IsNotNull(testExpression3, "Expected an operator expression."); Assert.AreEqual(BinaryExpressionOperator.Equal, testExpression3.Operator, "Unexpected operator."); ElementAttributeExpression test3AttributeExpression = testExpression3.Left as ElementAttributeExpression; Assert.IsNotNull(test3AttributeExpression, "Unexpected left node type."); Assert.AreEqual(ElementAttributeType.Name, test3AttributeExpression.ElementAttribute, "Attribute expression was not parsed correctly."); StringExpression test3StringExpression = testExpression3.Right as StringExpression; Assert.IsNotNull(test3StringExpression, "Unexpected right node type."); Assert.AreEqual("Test 3", test3StringExpression.Text, "String expression was not parsed correctly."); }; string expressionText; expressionText = "(($(Name) == 'Test 1') And ($(Name) == 'Test 2')) Or ($(Name) == 'Test 3')"; testExpression(expressionText); expressionText = "$(Name) == 'Test 1' And $(Name) == 'Test 2' Or $(Name) == 'Test 3'"; testExpression(expressionText); }
/// <summary> /// To unicode. /// </summary> /// <param name="expr">The expr.</param> /// <returns></returns> public static Int32Expression ToUnicode(this StringExpression expr) { return(new Int32Expression("UNICODE(" + expr.Sql + ")", ((StringExpression)expr.Clone()).ChildExpressions)); }
/// <summary> /// Gets the length of current string expression. /// </summary> /// <param name="expr">The expr.</param> /// <returns></returns> public static Int32Expression GetLength(this StringExpression expr) { return(new Int32Expression("LEN(" + expr.Sql + ")", ((StringExpression)expr.Clone()).ChildExpressions)); }
public void visit(StringExpression that) { that.Type = new StringType(that.Position); }
void PrintExpression(StringExpression e, int d) { Say("StringExpression("); Say(e.Value); Say(")"); }
public int Visit(StringExpression expression) { MondValue.String(expression.Value).Serialize(_writer); return(0); }
public void MatchStringConstant() { var expr = new StringExpression("foo"); var expr1 = new StringExpression("foo"); var expr2 = new StringExpression("bar"); var expr3 = new IntegerExpression(42); Assert.IsTrue(expr.Match(expr, null)); Assert.IsTrue(expr.Match(expr1, null)); Assert.IsFalse(expr.Match(null, null)); Assert.IsFalse(expr.Match(expr2, null)); Assert.IsFalse(expr.Match(expr3, null)); }
protected SearchParameterQueryGeneratorContext VisitSimpleString(StringExpression expression, SearchParameterQueryGeneratorContext context, StringColumn column, string value) { context.StringBuilder.Append(column, context.TableAlias).Append(expression.ComponentIndex + 1); bool needsEscaping = false; switch (expression.StringOperator) { case StringOperator.Contains: needsEscaping = TryEscapeValueForLike(ref value); SqlParameter containsParameter = context.Parameters.AddParameter(column, $"%{value}%"); context.StringBuilder.Append(" LIKE ").Append(containsParameter.ParameterName); break; case StringOperator.EndsWith: needsEscaping = TryEscapeValueForLike(ref value); SqlParameter endWithParameter = context.Parameters.AddParameter(column, $"%{value}"); context.StringBuilder.Append(" LIKE ").Append(endWithParameter.ParameterName); break; case StringOperator.Equals: SqlParameter equalsParameter = context.Parameters.AddParameter(column, value); context.StringBuilder.Append(" = ").Append(equalsParameter.ParameterName); break; case StringOperator.NotContains: context.StringBuilder.Append(" NOT "); goto case StringOperator.Contains; case StringOperator.NotEndsWith: context.StringBuilder.Append(" NOT "); goto case StringOperator.EndsWith; case StringOperator.NotStartsWith: context.StringBuilder.Append(" NOT "); goto case StringOperator.StartsWith; case StringOperator.StartsWith: needsEscaping = TryEscapeValueForLike(ref value); SqlParameter startsWithParameter = context.Parameters.AddParameter(column, $"{value}%"); context.StringBuilder.Append(" LIKE ").Append(startsWithParameter.ParameterName); break; default: throw new ArgumentOutOfRangeException(expression.StringOperator.ToString()); } if (needsEscaping) { context.StringBuilder.Append(" ESCAPE '!'"); } if (column.IsAcentSensitive == null || column.IsCaseSensitive == null || column.IsAcentSensitive == expression.IgnoreCase || column.IsCaseSensitive == expression.IgnoreCase) { context.StringBuilder.Append(" COLLATE ").Append(expression.IgnoreCase ? DefaultCaseInsensitiveCollation : DefaultCaseSensitiveCollation); } return(context); }
void StringExpression(out Expression exp) { Expect(4); exp = new StringExpression(GetPragma(t), t.val, GetScope(), errors); SetEndPragma(exp); }
public void ExitFilename(BASICParser.FilenameContext context) { currentFilename = currentStringExpression.Pop(); }
private StringExpression ParseStringExpression() { var exp = new StringExpression(_currentToken.Lexeme); NextToken(); return exp; }
private void visitExpression(Expression expression) { if (expression is Name) { WriteLine("Name: " + ((Name)expression).Id); } else if (expression is FCall) { FCall fcall = (FCall)expression; WriteLine("FCall: " + fcall.Name); WriteLine("Parameters: "); foreach (Expression exp in fcall.Args) { visitExpression(exp); } } else if (expression is FieldAccess) { FieldAccess fa = (FieldAccess)expression; WriteLine("FieldAccess: " + fa.Exp + "." + fa.Field); } else if (expression is StringLiteral) { StringLiteral literal = (StringLiteral)expression; if (literal.Content.Length > 50) { WriteLine("String: " + literal.Content.Substring(0, 50) + "..."); } else { WriteLine("String: " + literal.Content); } } else if (expression is StringExpression) { StringExpression sexp = (StringExpression)expression; WriteLine("StringExpression"); foreach (Expression exp in sexp.Expressions) { visitExpression(exp); } } else if (expression is BinaryExpression) { BinaryExpression sexp = (BinaryExpression)expression; WriteLine("BinaryExpression"); visitExpression(sexp.Lhs); WriteLine("Operator " + sexp.Operator.ToString()); visitExpression(sexp.Rhs); } else { WriteLine("Expression: " + expression.GetType().ToString()); } }