コード例 #1
0
        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()));
        }
コード例 #2
0
ファイル: StateGroupFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #3
0
        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));
        }
コード例 #4
0
ファイル: StateGroupFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #5
0
ファイル: StateGroupFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #6
0
ファイル: StateGroupFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #7
0
ファイル: StateGroupFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #8
0
ファイル: StateTests.cs プロジェクト: ashwini-mnnit/PSharp
        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));
        }
コード例 #9
0
ファイル: StateTests.cs プロジェクト: ashwini-mnnit/PSharp
        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));
        }
コード例 #10
0
        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;
                }
            }
        }
コード例 #11
0
ファイル: PSharpProject.cs プロジェクト: mattmccutchen/PSharp
        /// <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);
        }
コード例 #12
0
        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());
        }
コード例 #13
0
        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());
        }
コード例 #14
0
        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());
        }
コード例 #15
0
        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());
        }
コード例 #16
0
        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());
        }
コード例 #17
0
        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.");
        }
コード例 #18
0
ファイル: NamespaceFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #19
0
ファイル: NamespaceFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #20
0
ファイル: EventFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #21
0
        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());
        }
コード例 #22
0
        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());
        }
コード例 #23
0
        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());
        }
コード例 #24
0
ファイル: EventFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #25
0
        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());
        }
コード例 #26
0
        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());
        }
コード例 #27
0
ファイル: EventFailTests.cs プロジェクト: rekoder/PSharp
        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());
        }
コード例 #28
0
        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());
        }
コード例 #29
0
ファイル: EventTests.cs プロジェクト: ashwini-mnnit/PSharp
        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));
        }
コード例 #30
0
        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());
        }
コード例 #31
0
ファイル: PSharpProject.cs プロジェクト: jerickmsft/PSharp
        /// <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);
        }