public void ObjectsCanBeNested() { var code = AnnotatedText.Parse( @" object Hello { object World { def Invoke() = println(""Hello World!"") } }" ); var compilation = Compile(code.Text); Assert.Collection( compilation.RootSymbol.Types, hello => { Assert.Equal("Hello", hello.Name); Assert.Collection( hello.Types, world => { Assert.Equal("World", world.Name); } ); } ); }
private static void AssertIsMatch(string sourceWithMarkers, string expectedStackTrace) { var annotatedSource = AnnotatedText.Parse(sourceWithMarkers); var syntaxTree = CSharpSyntaxTree.ParseText(annotatedSource.Text); var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var options = new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true); var compilation = CSharpCompilation.Create("dummy", new[] { syntaxTree }, new[] { mscorlib }, options); var runtimeStackTrace = RunAndGetStackTrace(compilation); WriteStackTraceAndMethods(compilation, runtimeStackTrace, out var producedStackTrace, out var methods); Assert.Equal(AnnotatedText.NormalizeCode(expectedStackTrace), producedStackTrace); var root = syntaxTree.GetRoot(); var markedNodes = annotatedSource.Spans.Select(s => root.FindNode(s)).Reverse().ToArray(); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.Equal(markedNodes.Length, methods.Length); for (var i = 0; i < markedNodes.Length; i++) { var expectedSymbol = semanticModel.GetDeclaredSymbol(markedNodes[i]); Assert.NotNull(expectedSymbol); var actualSymbol = methods[i]; Assert.Equal(expectedSymbol, methods[i]); } }
public void CanLexComment(LineCommentTriviaData lineCommentTrivia) { var text = AnnotatedText .Parse( $@" {lineCommentTrivia.Text} 1 " ) .Text; var tokens = SyntaxTree.ParseTokens(text); Assert.Collection( tokens, token1 => { Assert.NotNull(token1); Assert.Collection( token1.LeadingTrivia, trivia => { Assert.Equal(SyntaxKind.LineCommentTrivia, trivia.Kind); Assert.Equal(lineCommentTrivia.Text, trivia.Text); }, trivia => { } ); Assert.Equal(SyntaxKind.NumberToken, token1.Kind); Assert.Equal("1", token1.Text); } ); }
public void DottedNamespaceSpecifiedForObject() { var code = AnnotatedText.Parse( @" namespace Hello.Namespace object Hello { def main() = println(""Hello World"") }" ); var compilation = TestHelpers.Compile(code.Text); var Hello = compilation.RootSymbol.LookupNamespace("Hello"); Assert.NotNull(Hello); var Namespace = Hello !.LookupNamespace("Namespace"); Assert.NotNull(Namespace); Assert.Collection( Namespace !.Types, symbol => { Assert.Equal("Hello", symbol.Name); Assert.Collection(symbol.Methods, ns => Assert.Equal("main", ns.Name)); } ); }
public static void AnnotatedText_ParsesCombination() { var text = @" This| {is} a {replacement:change}. At| this {:brand new }{span}. ".NormalizeCode(); var annotatedText = AnnotatedText.Parse(text); var expectedText = @" This is a replacement. At this span. ".NormalizeCode(); var expectedSpans = new[] { new TextSpan(4, 0), new TextSpan(5, 2), new TextSpan(25, 0), new TextSpan(31, 4) }; var expectedChanges = new[] { new TextChange(new TextSpan(10, 11), "change"), new TextChange(new TextSpan(31, 0), "brand new ") }; Assert.Equal(expectedText, annotatedText.Text); Assert.Equal(expectedSpans, annotatedText.Spans); Assert.Equal(expectedChanges, annotatedText.Changes); }
public void CanBindOverload() { var code = AnnotatedText.Parse( @" object Hello { def a(b: any) = b def a(b: string) = b def main() = println(a(""Hello World"")) }" ); var compilation = Compile(code.Text); Assert.Collection( compilation.RootSymbol.Types, symbol => { Assert.Equal("Hello", symbol.Name); Assert.Collection( symbol.Methods, a => { var p = Assert.Single(a.Parameters); Assert.Equal("b", p.Name); }, a => { var p = Assert.Single(a.Parameters); Assert.Equal("b", p.Name); }, main => { } ); } ); }
public void ParseForExpressionWithLineBreaks() { var text = AnnotatedText.Parse( @" for (x <- 0 to 5) x " ); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); //└──CompilationUnit // ├──ExpressionStatement // │ ├──ForExpression // │ │ ├──ForKeyword // │ │ ├──OpenParenToken // │ │ ├──NameExpression // │ │ │ └──IdentifierToken // │ │ ├──LessThanDashToken // │ │ ├──LiteralExpression // │ │ │ └──NumberToken 0 // │ │ ├──ToKeyword // │ │ ├──LiteralExpression // │ │ │ └──NumberToken 5 // │ │ ├──CloseParenToken // │ │ └──NameExpression // │ │ └──IdentifierToken // │ └──NewLineToken // └──EndOfInputToken e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.GlobalStatement); e.AssertNode(SyntaxKind.ExpressionStatement); e.AssertNode(SyntaxKind.ForExpression); e.AssertToken(SyntaxKind.ForKeyword, "for"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.OpenParenToken, "("); e.AssertToken(SyntaxKind.IdentifierToken, "x"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.LessThanDashToken, "<-"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.NumberToken, "0"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.ToKeyword, "to"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.NumberToken, "5"); e.AssertToken(SyntaxKind.CloseParenToken, ")"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.IdentifierName); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.IdentifierToken, "x"); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }
public static void AnnotatedText_DetectsColonOutsideOfChange() { var text = "This : is an error."; var exception = Assert.Throws <FormatException>(() => AnnotatedText.Parse(text)); Assert.Equal("Character ':' is unexpected at position 5.", exception.Message); }
public static void AnnotatedText_DetectsMissingBrace_ForChange() { var text = "Here {is: a brace missing."; var exception = Assert.Throws <FormatException>(() => AnnotatedText.Parse(text)); Assert.Equal("Missing '}' at position 26.", exception.Message); }
public void ParseNestedNonUnitBlockExpression() { var text = AnnotatedText.Parse( @" { val x = 5 5 }" ); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); //└──ExpressionStatement // ├──BlockExpression // │ ├──OpenBraceToken // │ ├──BlockExpression // │ │ ├──OpenBraceToken // │ │ ├──UnitExpression // │ │ │ ├──CloseBraceToken // │ │ │ └──CloseBraceToken // │ │ └──CloseBraceToken // │ └──CloseBraceToken // └──NewLineToken e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.GlobalStatement); e.AssertNode(SyntaxKind.ExpressionStatement); e.AssertNode(SyntaxKind.BlockExpression); e.AssertToken(SyntaxKind.OpenBraceToken, "{"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.VariableDeclarationStatement); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.ValKeyword, "val"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.IdentifierToken, "x"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.Initializer); e.AssertToken(SyntaxKind.EqualsToken, "="); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.NumberToken, "5"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.NumberToken, "5"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertToken(SyntaxKind.CloseBraceToken, "}"); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }
public void ParseUsingDirectives() { var text = AnnotatedText.Parse( @" using System using System.Collections.Immutable using System.Diagnostics " ); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.UsingDirective); e.AssertToken(SyntaxKind.UsingKeyword, "using"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "System"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.UsingDirective); e.AssertToken(SyntaxKind.UsingKeyword, "using"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia); e.AssertNode(SyntaxKind.QualifiedName); e.AssertNode(SyntaxKind.QualifiedName); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "System"); e.AssertToken(SyntaxKind.DotToken, "."); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "Collections"); e.AssertToken(SyntaxKind.DotToken, "."); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "Immutable"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.UsingDirective); e.AssertToken(SyntaxKind.UsingKeyword, "using"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia); e.AssertNode(SyntaxKind.QualifiedName); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "System"); e.AssertToken(SyntaxKind.DotToken, "."); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "Diagnostics"); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }
public async Task <(AnnotatedText, Dictionary <string, WordInfo>)> AnnotateText(string text) { var annotatedParagraphs = await AnnotateTextByParagraphs(text); var texts = annotatedParagraphs.Select(p => p.Item1); AnnotatedText resultText = MergeAnnotatedParagraphs(texts); var dictionaries = annotatedParagraphs.Select(p => p.Item2); Dictionary <string, WordInfo> dictionary = MergeDictionaries(dictionaries); return(resultText, dictionary); }
public override ConstraintResult ApplyTo <TActual>(TActual actual) { var theText = actual as string; var annotatedText = AnnotatedText.Parse(theText); var syntaxTree = SyntaxTree.Parse(annotatedText.Text); var compilation = new Compilation(syntaxTree); var result = compilation.Evaluate(new Dictionary <VariableSymbol, object>()); var diagnostics = AnnotatedText.UnindentLines((string)Expected); var annotationsCount = annotatedText.Spans.Length; var actualDiagsCount = result.Diagnostics.Length; if (annotationsCount < actualDiagsCount) { throw new Exception($"Too few markers '[]'. Found {annotationsCount}, expected {actualDiagsCount}"); } if (annotationsCount > actualDiagsCount) { throw new Exception($"Too many markers '[]'. Found {annotationsCount}, expected {actualDiagsCount}"); } if (annotationsCount != diagnostics.Length) { throw new Exception($"Inconsistent number of '[]' and expected diagnostics. Found {diagnostics.Length} diagnostics, expected {annotationsCount} based on number of '[]'"); } for (var i = 0; i < diagnostics.Length; i++) { var expectedDiagnostic = diagnostics[i]; var actualDiagnostic = result.Diagnostics[i].Message; if (!actualDiagnostic.Equals(expectedDiagnostic)) { Description = $"{expectedDiagnostic}"; return(new ConstraintResult(this, actualDiagnostic, false)); } var expectedSpan = annotatedText.Spans[i]; var actualSpan = result.Diagnostics[i].Span; if (!expectedSpan.Equals(actualSpan)) { Description = $"{expectedSpan}"; return(new ConstraintResult(this, actualSpan, false)); } } return(new ConstraintResult(this, actual, true)); }
private static AnnotatedText MergeAnnotatedParagraphs(IEnumerable <AnnotatedText> texts) { var sentences = texts.SelectMany(t => t.Sentences).ToArray(); var orderedSentences = Enumerable.Range(0, sentences.Length) .Zip(sentences, (i, s) => new Sentence { Index = i, Tokens = s.Tokens }); var resultText = new AnnotatedText { Sentences = orderedSentences.ToList() }; return(resultText); }
public static void AnnotatedText_ParsesSpans_WhenNested() { var text = "Root { span { with a nested } }"; var annotatedText = AnnotatedText.Parse(text); var expectedText = "Root span with a nested "; var expectedSpans = new[] { new TextSpan(5, 22), new TextSpan(11, 15) }; Assert.Equal(expectedText, annotatedText.Text); Assert.Equal(expectedSpans, annotatedText.Spans); Assert.Empty(annotatedText.Changes); }
public static void AnnotatedText_ParsesPositions() { var text = "Lorem | ip|sum"; var annotatedText = AnnotatedText.Parse(text); var expectedText = "Lorem ipsum"; var expectedPositions = new[] { new TextSpan(6, 0), new TextSpan(9, 0) }; Assert.Equal(expectedText, annotatedText.Text); Assert.Equal(expectedPositions, annotatedText.Spans); Assert.Empty(annotatedText.Changes); }
public void ParseUnaryExpressionAfterLineBreak() { var text = AnnotatedText.Parse( @" { val x = 3 -x } " ); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.GlobalStatement); e.AssertNode(SyntaxKind.ExpressionStatement); e.AssertNode(SyntaxKind.BlockExpression); e.AssertToken(SyntaxKind.OpenBraceToken, "{"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.VariableDeclarationStatement); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.ValKeyword, "val"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.IdentifierToken, "x"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.Initializer); e.AssertToken(SyntaxKind.EqualsToken, "="); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.NumberToken, "3"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.UnaryExpression); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.DashToken, "-"); e.AssertNode(SyntaxKind.IdentifierName); e.AssertToken(SyntaxKind.IdentifierToken, "x"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertToken(SyntaxKind.CloseBraceToken, "}"); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }
public static void AnnotatedText_ParsesSpans() { var text = "L{or}em{} ip{sum}"; var annotatedText = AnnotatedText.Parse(text); var expectedText = "Lorem ipsum"; var expectedSpans = new[] { new TextSpan(1, 2), new TextSpan(5, 0), new TextSpan(8, 3) }; Assert.Equal(expectedText, annotatedText.Text); Assert.Equal(expectedSpans, annotatedText.Spans); Assert.Empty(annotatedText.Changes); }
public static void AnnotatedText_ParsesChanges() { var text = "L{or:a}em{: test} ip{sum:}"; var annotatedText = AnnotatedText.Parse(text); var expectedText = "Lorem ipsum"; var expectedChanges = new[] { new TextChange(new TextSpan(1, 2), "a"), new TextChange(new TextSpan(5, 0), " test"), new TextChange(new TextSpan(8, 3), string.Empty) }; Assert.Equal(expectedText, annotatedText.Text); Assert.Empty(annotatedText.Spans); Assert.Equal(expectedChanges, annotatedText.Changes); }
public static void AssertDiagnostics( string diagnosticText, AnnotatedText annotatedText, ImmutableArray <Diagnostic> diagnostics ) { var expectedDiagnosticMessages = diagnosticText.UnindentLines(); Assert.True( annotatedText.Spans.Length == expectedDiagnosticMessages.Length, "Test invalid, must have equal number of diagnostics as text spans" ); var expectedDiagnostics = expectedDiagnosticMessages .Zip(annotatedText.Spans) .Select(tuple => new { Span = tuple.Second, Message = tuple.First }) .OrderBy(diagnostic => diagnostic.Span.Start) .ToArray(); var actualDiagnostics = diagnostics .OrderBy(diagnostic => diagnostic.Location?.Span.Start ?? -1) .ToArray(); for (var i = 0; i < expectedDiagnosticMessages.Length; i++) { Assert.True( i < expectedDiagnostics.Length, $"Expected at least {i + 1} expected diagnostics only found {expectedDiagnostics.Length}" ); Assert.True( i < actualDiagnostics.Length, $"Expected at least {i + 1} actual diagnostics only found {actualDiagnostics.Length}" ); var expectedMessage = expectedDiagnostics[i].Message; var actualMessage = actualDiagnostics[i].Message; Assert.Equal(expectedMessage, actualMessage); var expectedSpan = expectedDiagnostics[i].Span; var actualSpan = actualDiagnostics[i].Span; Assert.Equal(expectedSpan, actualSpan); } Assert.Equal(expectedDiagnosticMessages.Length, expectedDiagnostics.Length); }
public void ObjectNameIsCorrect() { var code = AnnotatedText.Parse( @" object Hello { def main() = println(""Hello World"") }" ); var compilation = Compile(code.Text); Assert.Collection( compilation.RootSymbol.Types, symbol => { Assert.Equal("Hello", symbol.Name); } ); }
public static void AssertHasDiagnostics( string text, string diagnosticText, [CallerMemberName] string?testName = null ) { var annotatedText = AnnotatedText.Parse(text); var syntaxTree = SyntaxTree.Parse(annotatedText.Text); using var scriptHost = new ScriptHost(AssembliesWithStdLib, testName ?? "test"); var result = scriptHost.Execute(syntaxTree); var diagnostics = result.Diagnostics; AssertDiagnostics(diagnosticText, annotatedText, diagnostics); }
public void ParseZeroString() { var text = AnnotatedText.Parse("\"\0\""); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.GlobalStatement); e.AssertNode(SyntaxKind.ExpressionStatement); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.StringToken, "\"\0\""); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }
public void ParseBinaryExpressionWithLineBreakInsideGroup() { var text = AnnotatedText.Parse( @" (false || (false || true)) " ); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.GlobalStatement); e.AssertNode(SyntaxKind.ExpressionStatement); e.AssertNode(SyntaxKind.GroupExpression); e.AssertToken(SyntaxKind.OpenParenToken, "("); e.AssertNode(SyntaxKind.BinaryExpression); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.FalseKeyword, "false"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.PipePipeToken, "||"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.GroupExpression); e.AssertToken(SyntaxKind.OpenParenToken, "("); e.AssertNode(SyntaxKind.BinaryExpression); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.FalseKeyword, "false"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.PipePipeToken, "||"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.TrueKeyword, "true"); e.AssertToken(SyntaxKind.CloseParenToken, ")"); e.AssertToken(SyntaxKind.CloseParenToken, ")"); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }
public void ObjectHasMethod() { var code = AnnotatedText.Parse( @" object Hello { def world() = println(""Hello World"") }" ); var compilation = Compile(code.Text); Assert.Collection( compilation.RootSymbol.Types, symbol => Assert.Collection( symbol.Methods, methodSymbol => Assert.Equal("world", methodSymbol.Name) ) ); }
private void LoweringShouldPreserveSideEffectOrderInCallExpressions() { string SideEffectBlock(int i) => AnnotatedText .Parse( $@"{{ println(""{i}"") ""V{i}"" }}" ) .Text; var source = AnnotatedText .Parse( $@" {{ println(""first"") println(concat({SideEffectBlock(0)}, sideEffect(""1""), {SideEffectBlock(2)})) println(""last"") }} def sideEffect(a: string): string = {{ println(a) ""V"" + a }} def concat(a: string, b: string, c: string): string = a + b + c " ) .Text; var tree = SyntaxTree.Parse(source); Assert.Empty(tree.Diagnostics); using var scriptHost = BuildScriptHostTestLib(); scriptHost.Execute(tree); var expectedOutput = BuildExpectedOutput("first", "0", "1", "2", "V0V1V2", "last"); AssertEvaluation("getOutput()", expectedOutput, scriptHost); }
private static AssertingEnumerator ForText(string textWithOptionalMarker, Func <string, SyntaxTree> factory) { var annotatedText = AnnotatedText.Parse(textWithOptionalMarker); if (annotatedText.Spans.Length > 1 || annotatedText.Changes.Length > 0) { throw new InvalidOperationException("This method only supports text with zero or one span."); } var syntaxTree = factory(annotatedText.Text); var span = annotatedText.Spans.Any() ? annotatedText.Spans.Single() : syntaxTree.Root.Root.Span; var enumerator = syntaxTree.Root.Root .DescendantNodesAndTokensAndSelf(true) .Where(n => span.Contains(n.Span)) .GetEnumerator(); return(new AssertingEnumerator(enumerator)); }
public void NamespaceSpecifiedForObject() { var code = AnnotatedText.Parse( @" namespace HelloNamespace object Hello { def main() = println(""Hello World"") }" ); var compilation = TestHelpers.Compile(code.Text); var HelloNamespace = compilation.RootSymbol.LookupNamespace("HelloNamespace"); Assert.NotNull(HelloNamespace); Assert.Equal("HelloNamespace", HelloNamespace !.Name); Assert.Collection(HelloNamespace.Members, symbol => Assert.Equal("Hello", symbol.Name)); }
public void CanUseConsole() { var code = AnnotatedText.Parse( @" using System object Hello { def print(value: any) = Console.Write(value) }" ); var compilation = Compile(code.Text); Assert.Collection( compilation.RootSymbol.Types, symbol => Assert.Collection( symbol.Methods, methodSymbol => Assert.Equal("unit", methodSymbol.ReturnType.Symbol.Name) ) ); }
public void ParseWhileExpression() { var text = AnnotatedText.Parse( @" { while ( true ) 1 }" ); var tree = SyntaxTree.Parse(text.Text); Assert.Empty(tree.Diagnostics); var expression = tree.Root; using var e = new AssertingEnumerator(expression); e.AssertNode(SyntaxKind.CompilationUnit); e.AssertNode(SyntaxKind.GlobalStatement); e.AssertNode(SyntaxKind.ExpressionStatement); e.AssertNode(SyntaxKind.BlockExpression); e.AssertToken(SyntaxKind.OpenBraceToken, "{"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertNode(SyntaxKind.WhileExpression); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.WhileKeyword, "while"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.OpenParenToken, "("); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.TrueKeyword, "true"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertToken(SyntaxKind.CloseParenToken, ")"); e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " "); e.AssertNode(SyntaxKind.LiteralExpression); e.AssertToken(SyntaxKind.NumberToken, "1"); e.AssertTrivia(SyntaxKind.EndOfLineTrivia); e.AssertToken(SyntaxKind.CloseBraceToken, "}"); e.AssertToken(SyntaxKind.EndOfInputToken, ""); }