void ICompletionSource.AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets) { return; var keywords = Keywords.Get(); var snapshot = this.Buffer.CurrentSnapshot; var trackSpan = this.FindTokenSpanAtPosition(session.GetTriggerPoint(this.Buffer), session); var preSpan = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start, trackSpan.GetStartPoint(snapshot).Position)); var tokens = new PSharpLexer().Tokenize(preSpan.GetText()); var parser = new PSharpParser(); parser.ParseTokens(tokens); this.RefineAvailableKeywords(parser.GetExpectedTokenTypes(), keywords); this.CompletionList = new List<Completion>(); foreach (var keyword in keywords) { this.CompletionList.Add(new Completion(keyword.Key, keyword.Key, keyword.Value.Item1, null, null)); } if (keywords.Count == 0) { return; } completionSets.Add(new CompletionSet( "Tokens", "Tokens", trackSpan, this.CompletionList, null)); }
public Task<bool> HasSuggestedActionsAsync(ISuggestedActionCategorySet requestedActionCategories, SnapshotSpan range, CancellationToken cancellationToken) { return Task.Factory.StartNew(() => { return false; TextExtent extent; if (!this.TryGetWordUnderCaret(out extent)) { return false; } var extentToken = new PSharpLexer().Tokenize(extent.Span.GetText()).FirstOrDefault(); if (extentToken == null) { return false; } var snapshot = extent.Span.Snapshot; var trackSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive); var preSpan = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start, trackSpan.GetStartPoint(snapshot).Position)); var tokens = new PSharpLexer().Tokenize(preSpan.GetText()); var parser = new PSharpParser(ParsingOptions.CreateDefault()); parser.ParseTokens(tokens); var expected = parser.GetExpectedTokenTypes(); if (this.IsExpectedTokenType(extentToken, expected)) { return false; } return extent.IsSignificant; }); }
public void TestMachineDeferEventDeclarationWithExtraComma() { var test = @" namespace Foo { machine M { group G { start state S { defer e1,e2,; } } } }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected event identifier.", parser.GetParsingErrorLog()); }
public void TestVoidMethodDeclaration() { var test = @" namespace Foo { machine M { start state S { } void Bar() { } } }"; var tokens = new PSharpLexer().Tokenize(test); var program = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false).ParseTokens(tokens); program.Rewrite(); var expected = @" using System; using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] class S : MachineState { } private void Bar(){ } } }"; Assert.AreEqual(expected.Replace(Environment.NewLine, string.Empty), program.GetSyntaxTree().ToString().Replace("\n", string.Empty)); }
public void TestUnexpectedTokenWithoutNamespace() { var test = "private"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Unexpected token.", parser.GetParsingErrorLog()); }
public void TestNamespaceDeclarationWithoutIdentifier() { var test = "namespace { }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected namespace identifier.", parser.GetParsingErrorLog()); }
public void TestIncorrectUsingDeclaration() { var test = "using System.Text"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected \";\".", parser.GetParsingErrorLog()); }
public void TestEventDeclarationWithoutNamespace() { var test = "event e;"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Must be declared inside a namespace.", parser.GetParsingErrorLog()); }
public void TestEventDeclarationWithoutNamespace() { var test = "event e;"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Must be declared inside a namespace.", parser.GetParsingErrorLog()); }
public void TestUnexpectedTokenWithoutNamespace() { var test = "private"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Unexpected token.", parser.GetParsingErrorLog()); }
public void TestUsingDeclarationWithoutIdentifier() { var test = "using;"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected identifier.", parser.GetParsingErrorLog()); }
public void TestPrivateEventDeclaration() { var test = @" namespace Foo { private event e; }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual(parser.GetParsingErrorLog(), "Event and machine declarations must be internal or public."); }
public void TestUsingDeclaration() { var test = "using System.Text;"; var tokens = new PSharpLexer().Tokenize(test); var program = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false).ParseTokens(tokens); program.Rewrite(); var expected = @" using System; using Microsoft.PSharp; using System.Text;"; Assert.AreEqual(expected.Replace(Environment.NewLine, string.Empty), program.GetSyntaxTree().ToString().Replace("\n", string.Empty)); }
public void TestMachineDeclarationWithoutState() { var test = @" namespace Foo { machine M { } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("A machine must declare at least one state.", parser.GetParsingErrorLog()); }
public void TestEventDeclarationWithGenericError2() { var test = @" namespace Foo { event e<; }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Invalid generic expression.", parser.GetParsingErrorLog()); }
public void TestMachineFieldDeclarationWithoutSemicolon() { var test = @" namespace Foo { machine M { machine N start state S { } } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected \"(\" or \";\".", parser.GetParsingErrorLog()); }
public void TestInternalFieldDeclaration() { var test = @" namespace Foo { machine M { internal int k; start state S { } } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("A field or method cannot be internal.", parser.GetParsingErrorLog()); }
public void TestEventDeclaration() { var test = @" namespace Foo { event e1; internal event e2; public event e3; }"; var tokens = new PSharpLexer().Tokenize(test); var program = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false). ParseTokens(tokens); program.Rewrite(); var expected = @" using System; using Microsoft.PSharp; namespace Foo { class e1 : Event { internal e1() : base() { } } internal class e2 : Event { internal e2() : base() { } } public class e3 : Event { internal e3() : base() { } } }"; Assert.AreEqual(expected.Replace(Environment.NewLine, string.Empty), program.GetSyntaxTree().ToString().Replace("\n", string.Empty)); }
public void TestEntryDeclarationWithUnexpectedIdentifier() { var test = @" namespace Foo { machine M { start state S { entry Bar {} } } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected \"{\".", parser.GetParsingErrorLog()); }
public void TestMachineColdGroup() { var test = @" namespace Foo { machine M { cold group G { } } }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("A state group cannot be cold.", parser.GetParsingErrorLog()); }
public void TestDeferEventDeclarationWithoutComma() { var test = @" namespace Foo { machine M { start state S { defer e1 e2; } } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected \",\".", parser.GetParsingErrorLog()); }
public void TestMachineDeclarationWithTwoBodies() { var test = @" namespace Foo { machine M { start state S { } } { } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Unexpected token.", parser.GetParsingErrorLog()); }
public void TestMachineDeclarationWithMoreThanOneStartState() { var test = @" namespace Foo { machine M { start state S1 {} start state S2 { } start state S3 { } } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("A machine can declare only a single start state.", parser.GetParsingErrorLog()); }
public void TestIntFieldDeclarationWithoutSemicolon() { var test = @" namespace Foo { machine M { int k start state S { } } }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected \"(\" or \";\".", parser.GetParsingErrorLog()); }
public void TestStateDeclarationWithMoreThanOneExit() { var test = @" namespace Foo { machine M { start state S { exit{} exit {} } } }"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Duplicate exit declaration.", parser.GetParsingErrorLog()); }
public void TestMonitorMethodInsideGroup() { var test = @" namespace Foo { monitor M { group G { void Bar() { } } } }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Unexpected token 'void'.", parser.GetParsingErrorLog()); }
/// <summary> /// Parses a P# syntax tree to C#. /// th /// </summary> /// <param name="tree">SyntaxTree</param> /// <param name="options">ParsingOptions</param> private void ParsePSharpSyntaxTree(SyntaxTree tree, ParsingOptions options) { var root = (CompilationUnitSyntax)tree.GetRoot(); var tokens = new PSharpLexer().Tokenize(root.ToFullString()); var program = new PSharpParser(this, tree, options).ParseTokens(tokens); this.PSharpPrograms.Add(program as PSharpProgram); this.ProgramMap.Add(program, tree); }
public void TestMonitorOnEventGotoStateDeclarationWithoutState() { var test = @" namespace Foo { monitor M { group G { start state S1 { on e goto; } } } }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected state identifier.", parser.GetParsingErrorLog()); }
public IEnumerable<SuggestedActionSet> GetSuggestedActions(ISuggestedActionCategorySet requestedActionCategories, SnapshotSpan range, CancellationToken cancellationToken) { return Enumerable.Empty<SuggestedActionSet>(); TextExtent extent; if (!this.TryGetWordUnderCaret(out extent) || !extent.IsSignificant) { return Enumerable.Empty<SuggestedActionSet>(); } var extentToken = new PSharpLexer().Tokenize(extent.Span.GetText()).FirstOrDefault(); if (extentToken == null) { return Enumerable.Empty<SuggestedActionSet>(); } var snapshot = extent.Span.Snapshot; var trackSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive); var preSpan = new SnapshotSpan(snapshot, new Span(snapshot.GetLineFromLineNumber(0).Start, trackSpan.GetStartPoint(snapshot).Position)); var tokens = new PSharpLexer().Tokenize(preSpan.GetText()); var parser = new PSharpParser(ParsingOptions.CreateDefault()); parser.ParseTokens(tokens); var expected = parser.GetExpectedTokenTypes(); if (this.IsExpectedTokenType(extentToken, expected)) { return Enumerable.Empty<SuggestedActionSet>(); } var errorFixAction = new ErrorFixSuggestedAction(trackSpan); return new SuggestedActionSet[] { new SuggestedActionSet(new ISuggestedAction[] { errorFixAction }) }; }
public void TestMonitorStateDeclarationWithMoreThanOneEntry() { var test = @" namespace Foo { monitor M { group G { start state S { entry {} entry{} } } } }"; ParsingOptions options = ParsingOptions.CreateDefault() .DisableThrowParsingException(); var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), options); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Duplicate entry declaration.", parser.GetParsingErrorLog()); }