public void BadNumbers() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset( "45DD" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } { p.Reset( "45.member" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } { p.Reset( ".45.member" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } { p.Reset( "45.01member" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } { p.Reset( ".45.member" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } { p.Reset( ".45.01member" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } { p.Reset( "45.01e23member" ); Assert.That( p.IsErrorOrEndOfInput, Is.True ); Assert.That( p.ErrorCode, Is.EqualTo( JSTokenizerError.ErrorNumberIdentifierStartsImmediately ) ); } }
public void ArraySupport() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset( "a[9]" ); Assert.That( p.IsErrorOrEndOfInput, Is.False ); Expr e = a.Analyse( p ); Assert.That( e is AccessorIndexerExpr ); AccessorIndexerExpr ac = e as AccessorIndexerExpr; IsConstant( ac.Index, 9 ); } { p.Reset( "array['Hello World!']" ); Assert.That( p.IsErrorOrEndOfInput, Is.False ); Expr e = a.Analyse( p ); Assert.That( e is AccessorIndexerExpr ); AccessorIndexerExpr ac = e as AccessorIndexerExpr; IsConstant( ac.Index, "Hello World!" ); } }
public static Expr AnalyseString( string s, Config configuration = null) { ExprAnalyser a = new ExprAnalyser( configuration ); return a.Analyse( new JSTokenizer( s ) ); }
public void SimpleExpression() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset( "value" ); Assert.That( p.IsErrorOrEndOfInput, Is.False ); Expr e = a.Analyse( p ); Assert.That( e is AccessorMemberExpr ); AccessorMemberExpr ac = e as AccessorMemberExpr; Assert.That( ac.IsUnbound == true ); } { p.Reset( "!" ); Expr e = a.Analyse( p ); Assert.That( e is UnaryExpr ); UnaryExpr u = e as UnaryExpr; Assert.That( u.TokenType == JSTokenizerToken.Not ); Assert.That( u.Expression is SyntaxErrorExpr ); Assert.That( SyntaxErrorCollector.Collect( e, null ).Count == 1 ); } { p.Reset( "!value" ); Expr e = a.Analyse( p ); Assert.That( e is UnaryExpr ); UnaryExpr u = e as UnaryExpr; Assert.That( u.TokenType == JSTokenizerToken.Not ); Assert.That( u.Expression is AccessorExpr ); Assert.That( SyntaxErrorCollector.Collect( e, Util.ActionVoid ).Count == 0 ); } { p.Reset( " 0.12e43 && ~b " ); Expr e = a.Analyse( p ); Assert.That( e is BinaryExpr ); BinaryExpr and = e as BinaryExpr; Assert.That( and.BinaryOperatorToken == JSTokenizerToken.And ); IsConstant( and.Left, 0.12e43 ); Assert.That( and.Right is UnaryExpr ); UnaryExpr u = and.Right as UnaryExpr; Assert.That( u.TokenType == JSTokenizerToken.BitwiseNot ); Assert.That( u.Expression is AccessorExpr ); Assert.That( SyntaxErrorCollector.Collect( e, Util.ActionVoid ).Count == 0 ); } { p.Reset( @"!a||~""x""" ); Expr e = a.Analyse( p ); Assert.That( e is BinaryExpr ); BinaryExpr or = e as BinaryExpr; Assert.That( or.BinaryOperatorToken == JSTokenizerToken.Or ); Assert.That( or.Left is UnaryExpr ); Assert.That( or.Right is UnaryExpr ); UnaryExpr u = or.Right as UnaryExpr; Assert.That( u.TokenType == JSTokenizerToken.BitwiseNot ); IsConstant( u.Expression, "x" ); Assert.That( SyntaxErrorCollector.Collect( e, Util.ActionVoid ).Count == 0 ); } { p.Reset( "(3)" ); Expr e = a.Analyse( p ); IsConstant( e, 3 ); } { p.Reset( "(3+typeof 'x')" ); Expr e = a.Analyse( p ); Assert.That( e is BinaryExpr ); BinaryExpr b = e as BinaryExpr; IsConstant( b.Left, 3 ); Assert.That( b.Right is UnaryExpr ); UnaryExpr u = b.Right as UnaryExpr; Assert.That( u.TokenType == JSTokenizerToken.TypeOf ); IsConstant( u.Expression, "x" ); Assert.That( SyntaxErrorCollector.Collect( e, Util.ActionVoid ).Count == 0 ); } { p.Reset( "1 ? 2 : 3" ); Expr e = a.Analyse( p ); Assert.That( e is IfExpr ); IfExpr i = e as IfExpr; Assert.That( i.IsTernaryOperator == true ); IsConstant( i.Condition, 1 ); IsConstant( i.WhenTrue, 2 ); IsConstant( i.WhenFalse, 3 ); } }
public void EmptyParsing() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset( "" ); Assert.That( p.IsEndOfInput ); Expr e = a.Analyse( p ); Assert.That( e is SyntaxErrorExpr ); } { p.Reset( " \r\n \n \r \n \t " ); Assert.That( p.IsEndOfInput ); Expr e = a.Analyse( p ); Assert.That( e is SyntaxErrorExpr ); } }
public static Expr AnalyseString(string s, Config configuration = null) { ExprAnalyser a = new ExprAnalyser(configuration); return(a.Analyse(new JSTokenizer(s))); }
/// <summary> /// Executes a string by first calling <see cref="ExprAnalyser.AnalyseString"/>. /// </summary> /// <param name="s">The string to execute.</param> /// <returns>A result that may be pending...</returns> public Result Execute(string s) { return(Execute(ExprAnalyser.AnalyseString(s))); }
/// <summary> /// Simple static helper to evaluate a string (typically a pure expression without side effects). /// </summary> /// <param name="s">The string to evaluate.</param> /// <param name="ctx">The <see cref="GlobalContext"/>. When null, a new default GlobalContext is used.</param> /// <returns>The result of the evaluation.</returns> public static RuntimeObj Evaluate(string s, GlobalContext ctx = null) { return(Evaluate(ExprAnalyser.AnalyseString(s))); }