public void Does_parse_linq_examples() { var it = new JsIdentifier("it"); "it < 5".ParseJsExpression(out var expr); Assert.That(expr, Is.EqualTo(new JsBinaryExpression(it, JsLessThan.Operator, new JsLiteral(5)))); "it.UnitsInStock > 0 and it.UnitPrice > 3".ParseJsExpression(out expr); Assert.That(expr, Is.EqualTo( new JsLogicalExpression( new JsBinaryExpression( new JsMemberExpression(new JsIdentifier("it"), new JsIdentifier("UnitsInStock")), JsGreaterThan.Operator, new JsLiteral(0) ), JsAnd.Operator, new JsBinaryExpression( new JsMemberExpression(new JsIdentifier("it"), new JsIdentifier("UnitPrice")), JsGreaterThan.Operator, new JsLiteral(3) ) ) )); }
/// <summary> /// Gets the key. /// </summary> /// <param name="token">The token.</param> /// <returns>System.String.</returns> /// <exception cref="ServiceStack.Script.SyntaxErrorException">Invalid Key. Expected a Literal or Identifier but was {token.DebugToken()}</exception> public static string GetKey(JsToken token) { return(token switch { JsLiteral literalKey => literalKey.Value.ToString(), JsIdentifier identifierKey => identifierKey.Name, JsMemberExpression { Property : JsIdentifier prop } => prop.Name,
public void Does_parse_not_unary_expression() { var it = new JsIdentifier("it"); "!it".ParseJsExpression(out var expr); Assert.That(expr, Is.EqualTo(new JsUnaryExpression(JsNot.Operator, it))); "!contains(items, it)".ParseJsExpression(out expr); Assert.That(expr, Is.EqualTo(new JsUnaryExpression(JsNot.Operator, new JsCallExpression( new JsIdentifier("contains"), new JsIdentifier("items"), new JsIdentifier("it") )))); }
/// <summary> /// Equalses the specified other. /// </summary> /// <param name="other">The other.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> protected bool Equals(JsIdentifier other) => string.Equals(Name, other.Name);
/// <summary> /// Initializes a new instance of the <see cref="JsArrowFunctionExpression"/> class. /// </summary> /// <param name="param">The parameter.</param> /// <param name="body">The body.</param> public JsArrowFunctionExpression(JsIdentifier param, JsToken body) : this(new[] { param }, body) { }
public void VisitIdentifier(JsIdentifier identifier) { }
internal static StringSegment ParseIdentifier(this StringSegment literal, out JsToken token) { var i = 0; literal = literal.AdvancePastWhitespace(); if (literal.IsNullOrEmpty()) { throw new SyntaxErrorException("Expected identifier before end"); } var c = literal.GetChar(i); if (!c.IsValidVarNameChar()) { throw new SyntaxErrorException($"Expected start of identifier but was '{c}'"); } i++; while (i < literal.Length) { c = literal.GetChar(i); if (IsValidVarNameChar(c)) { i++; } else { break; } } var identifier = literal.Subsegment(0, i).TrimEnd(); literal = literal.Advance(i); if (identifier.Equals("true")) { token = JsLiteral.True; } else if (identifier.Equals("false")) { token = JsLiteral.False; } else if (identifier.Equals("null")) { token = JsNull.Value; } else if (identifier.Equals("and")) { token = JsAnd.Operator; } else if (identifier.Equals("or")) { token = JsOr.Operator; } else { token = new JsIdentifier(identifier); } return(literal); }
/// <summary> /// Initializes a new instance of the <see cref="JsDeclaration"/> class. /// </summary> /// <param name="id">The identifier.</param> /// <param name="init">The initialize.</param> public JsDeclaration(JsIdentifier id, JsToken init) { Id = id; Init = init; }