Ejemplo n.º 1
3
        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));
        }
Ejemplo n.º 2
1
        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;
            });
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
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());
        }
Ejemplo n.º 8
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());
        }
Ejemplo n.º 9
0
        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());
        }
Ejemplo n.º 10
0
        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());
        }
Ejemplo n.º 11
0
        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());
        }
Ejemplo n.º 12
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.");
        }
Ejemplo n.º 13
0
        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));
        }
Ejemplo n.º 14
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());
        }
Ejemplo n.º 15
0
        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());
        }
Ejemplo n.º 16
0
        public void TestIntFieldDeclarationWithoutSemicolon()
        {
            var test = @"
            namespace Foo {
            machine M {
            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("Expected \"(\" or \";\".",
                parser.GetParsingErrorLog());
        }
Ejemplo n.º 17
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());
        }
Ejemplo n.º 18
0
        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));
        }
Ejemplo n.º 19
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());
        }
Ejemplo n.º 20
0
        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());
        }
Ejemplo n.º 21
0
        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());
        }
Ejemplo n.º 22
0
        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());
        }
Ejemplo n.º 23
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());
        }
Ejemplo n.º 24
0
        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());
        }
Ejemplo n.º 25
0
        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 }) };
        }
Ejemplo n.º 26
0
        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());
        }
Ejemplo n.º 27
0
        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());
        }
Ejemplo n.º 28
0
        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());
        }
Ejemplo n.º 29
0
        /// <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);
        }
Ejemplo n.º 30
0
        internal int GetLineIndentation(ITextSnapshotLine line)
        {
            var options = this.TextView.Options;
            var tabSize = options.GetIndentSize();

            var indent = 0;
            if (line.LineNumber == 0)
            {
                return indent;
            }

            var currentLine = line;

            do
            {
                currentLine = line.Snapshot.GetLineFromLineNumber(currentLine.LineNumber - 1);
            }
            while (currentLine.LineNumber > 0 && currentLine.Length == 0);
            if (line.LineNumber == 0)
            {
                return indent;
            }

            var tokens = new PSharpLexer().Tokenize(currentLine.GetText());

            bool codeFound = false;
            foreach (var token in tokens)
            {
                if (token.Type == TokenType.WhiteSpace && !codeFound)
                {
                    foreach (var c in token.Text)
                    {
                        if (c == '\t')
                        {
                            indent += tabSize;
                        }
                        else
                        {
                            indent++;
                        }
                    }
                }
                else if (token.Type == TokenType.LeftCurlyBracket ||
                    token.Type == TokenType.MachineLeftCurlyBracket ||
                    token.Type == TokenType.StateLeftCurlyBracket)
                {
                    indent += tabSize;
                    break;
                }
                else if (!codeFound)
                {
                    codeFound = true;
                }
            }

            if (indent < 0)
            {
                indent = 0;
            }

            return indent;
        }