private bool IsExtentTokenExpected(SnapshotSpan range, TextExtent extent, out ITrackingSpan trackSpan, out PSharpParser parser) { string extentText = extent.Span.GetText(); var extentToken = string.IsNullOrWhiteSpace(extentText) ? null : new PSharpLexer().Tokenize(extentText).FirstOrDefault(); if (extentToken == null) { trackSpan = null; parser = null; return(false); } // TODO: Minimize the re-parse span for speed. var snapshot = extent.Span.Snapshot; 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()); parser = new PSharpParser(ParsingOptions.CreateForVsLanguageService()); try { parser.ParseTokens(tokens); } catch (ParsingException) { // Parsing exception is expected } return(this.IsExpectedTokenType(extentToken, parser.GetExpectedTokenTypes())); }
public void TestMonitorIgnoreEventDeclarationWithExtraComma() { var test = @" namespace Foo { monitor M { group G { start state S { ignore 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 TestNamespaceDeclaration2() { var test = @" namespace Foo { } namespace 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 { } namespace Bar { }"; Assert.AreEqual(expected.Replace(Environment.NewLine, string.Empty), program.GetSyntaxTree().ToString().Replace("\n", string.Empty)); }
public void TestMachineEntryDeclarationWithUnexpectedIdentifier() { var test = @" namespace Foo { machine M { group G { start state S { entry 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("Expected \"{\".", parser.GetParsingErrorLog()); }
public void TestMonitorStateDeclarationWithMoreThanOneExit() { var test = @" namespace Foo { monitor M { group G { start state S { exit{} exit {} } } } }"; 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 exit declaration.", parser.GetParsingErrorLog()); }
public void TestMonitorOnEventDeclarationWithoutHandler() { var test = @" namespace Foo { monitor M { group G { start state S1 { on 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("Expected \"do\", \"goto\" or \"push\".", parser.GetParsingErrorLog()); }
public void TestMachineOnEventGotoStateDeclarationWithoutSemicolon() { var test = @" namespace Foo { machine M { group G { start state S1 { on e goto S2 } } } }"; 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 \";\".", parser.GetParsingErrorLog()); }
public void TestIgnoreEventDeclaration2() { var test = @" namespace Foo { machine M { start state S { ignore e1, e2; } } }"; 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] [IgnoreEvents(typeof(e1), typeof(e2))] class S : MachineState { } } }"; Assert.AreEqual(expected.Replace(Environment.NewLine, string.Empty), program.GetSyntaxTree().ToString().Replace("\n", string.Empty)); }
public void TestEntryDeclaration() { var test = @" namespace Foo { machine M { start state S { entry{} } } }"; 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 { protected override void OnEntry(){}} } }"; Assert.AreEqual(expected.Replace(Environment.NewLine, string.Empty), program.GetSyntaxTree().ToString().Replace("\n", string.Empty)); }
public IEnumerable <ITagSpan <PSharpTokenTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (spans.Count == 0) { yield break; } foreach (var span in spans) { var snapshot = span.Snapshot; var currSpan = span; var containingLine = currSpan.Start.GetContainingLine(); var tokens = new PSharpLexer().Tokenize(containingLine.GetText()); this.DetectComment(tokens); this.DetectTypeIdentifiers(tokens); var currLoc = containingLine.Start.Position; foreach (var token in tokens) { var tokenSpan = new SnapshotSpan(currSpan.Snapshot, new Span(currLoc, token.Text.Length)); yield return(new TagSpan <PSharpTokenTag>(tokenSpan, new PSharpTokenTag(token.Type))); currLoc += token.Text.Length; } } }
/// <summary> /// Parses a P# syntax tree to C#. /// th /// </summary> /// <param name="tree">SyntaxTree</param> private void ParsePSharpSyntaxTree(SyntaxTree tree) { var root = (CompilationUnitSyntax)tree.GetRoot(); var tokens = new PSharpLexer().Tokenize(root.ToFullString()); var program = new PSharpParser(this, tree).ParseTokens(tokens); this.PSharpPrograms.Add(program as PSharpProgram); this.ProgramMap.Add(program, tree); }
internal static void AssertFailedTestLog(string expectedResult, string test) { 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.Equal(expectedResult, parser.GetParsingErrorLog()); }
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 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 TestUsingDeclarationWithoutIdentifier() { var test = "using;"; var parser = new PSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), false); var tokens = new PSharpLexer().Tokenize(test); var program = parser.ParseTokens(tokens); Assert.AreEqual("Expected identifier.", 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 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 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 TestNamespaceDeclarationWithoutIdentifier() { var test = "namespace { }"; 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 namespace identifier.", 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 TestIncorrectUsingDeclaration() { var test = "using System.Text"; 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 \";\".", parser.GetParsingErrorLog()); }
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 TestProtectedMonitorDeclaration() { var test = @" namespace Foo { protected monitor M { } }"; 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.Equal("A monitor cannot be declared as protected.", parser.GetParsingErrorLog()); }
public void TestProtectedEventDeclaration() { var test = @" namespace Foo { protected 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("Event and machine declarations must be internal or public.", 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 TestEventDeclarationWithGenericError1() { 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("Expected \"(\" or \";\".", 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 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()); }