Esempio n. 1
0
        public void FullPatternParsesChoiceCorrectly()
        {
            var pattern = SkriptPattern.ParsePattern(ParseContext.FromCode("(a|b|c)"));

            Assert.Single(pattern.Children);
            Assert.IsType <ChoicePatternElement>(pattern.Children.First());
        }
        public void ParseContextCanFindSingleBracket(string str)
        {
            var ctx = ParseContext.FromCode($"%{{{str}}}%");

            ctx.ReadNext(1); //Enter inside bracket

            Assert.Equal($"{{{str}}}", ctx.ReadUntilPosition(ctx.FindNextBracket('%', new[] { ('{', '}') })));
Esempio n. 3
0
        public void FunctionSignatureKnowsHowToParseSignatures(string signature)
        {
            var ctx = ParseContext.FromCode(signature);

            var sign = FunctionSignature.TryParse(ctx);

            Assert.NotNull(sign);
        }
Esempio n. 4
0
        public void FullPatternParsesMultipleChoiceCorrectly()
        {
            var pattern = SkriptPattern.ParsePattern(ParseContext.FromCode("(a|b)(c|d)"));

            Assert.Equal(2, pattern.Children.Count);
            Assert.IsType <ChoicePatternElement>(pattern.Children.First());
            Assert.IsType <ChoicePatternElement>(pattern.Children.Skip(1).First());
        }
Esempio n. 5
0
        public void FullPatternParsesMultipleInnerChoiceCorrectly()
        {
            var pattern = SkriptPattern.ParsePattern(ParseContext.FromCode("((a|e)|(b|f))((c|g)|(d|h))"));

            Assert.Equal(2, pattern.Children.Count);
            Assert.IsType <ChoicePatternElement>(pattern.Children.First());
            Assert.IsType <ChoicePatternElement>(pattern.Children.Skip(1).First());
            Assert.Equal("((a|e)|(b|f))((c|g)|(d|h))", pattern.RenderPattern());
        }
Esempio n. 6
0
        public void StringTypeParsesCorrectly(string input)
        {
            WorkspaceManager.Instance.KnownTypesManager.LoadKnownTypes();
            var stringPattern = SkriptPattern.ParsePattern(ParseContext.FromCode("%string%"));

            var result = stringPattern.Parse(input);

            Assert.True(result.IsSuccess);
            Assert.Single(result.Context.Matches);
            Assert.Equal(new Range(new Position(0, 0), new Position(0, input.Length)),
                         result.Context.Matches.First().Range);
        }
Esempio n. 7
0
            public static ChoiceGroupElement FromElement(string code)
            {
                var match = code;
                var split = match.Split("¦");

                int.TryParse(split.Length > 1 ? split.FirstOrDefault() : "0", out var parseMark);
                if (split.Length > 1)
                {
                    match = split.Skip(1).FirstOrDefault();
                }

                return(new ChoiceGroupElement(SkriptPattern.ParsePattern(ParseContext.FromCode(match)), parseMark));
            }
Esempio n. 8
0
        public void MixedStringTypeParsesCorrectly(string input, bool success = true)
        {
            WorkspaceManager.Instance.KnownTypesManager.LoadKnownTypes();
            var stringPattern = SkriptPattern.ParsePattern(ParseContext.FromCode("print[ln] %string%"));

            var result = stringPattern.Parse(input);

            Assert.Equal(success, result.IsSuccess);
            if (success)
            {
                Assert.Single(result.Context.Matches.OfType <ExpressionParseMatch>());
            }
        }
Esempio n. 9
0
        public void MixedDoubleStringTypeParsesCorrectly(string input, bool success = true)
        {
            WorkspaceManager.Instance.KnownTypesManager.LoadKnownTypes();
            var stringPattern = SkriptPattern.ParsePattern(ParseContext.FromCode("print[ln] %string% %string%"));

            var result = stringPattern.Parse(input);

            Assert.Equal(success, result.IsSuccess);
            if (success)
            {
                Assert.Equal(input.Contains("println") ? 3 : 2, result.Context.Matches.Count);
            }
        }
Esempio n. 10
0
        public void AllColorsCanBeParsedCorrectly()
        {
            var colors = new[]
            {
                "black", "dark grey", "dark gray", "grey", "light grey", "gray", "light gray", "silver", "white",
                "blue", "dark blue", "brown", "light blue", "indigo", "cyan", "aqua", "dark cyan", "dark aqua",
                "dark turquoise", "dark turquois", "light cyan", "light aqua", "turquoise", "turquois", "light blue",
                "green", "dark green", "light green", "lime", "lime green", "yellow", "light yellow", "orange", "gold",
                "dark yellow", "red", "dark red", "pink", "light red", "purple", "dark purple", "magenta",
                "light purple"
            };
            var pattern = SkriptPattern.ParsePattern("%color%");

            foreach (var color in colors)
            {
                var ctx    = ParseContext.FromCode(color);
                var result = pattern.Parse(ctx);

                Assert.True(result.IsSuccess, $"result.IsSuccess -> {color}");
                Assert.False(result.IsOptionallyMatched);
            }
        }
Esempio n. 11
0
        public void MixedVariableTypeParsesCorrectly(string input)
        {
            WorkspaceManager.Instance.KnownTypesManager.LoadKnownTypes();
            var stringPattern = SkriptPattern.ParsePattern(ParseContext.FromCode("print %string%"));

            var result = stringPattern.Parse("print " + input);

            Assert.True(result.IsSuccess, "result.IsSuccess");
            Assert.Single(result.Context.Matches);

            var match = result.Context.Matches.First();

            Assert.IsType <ExpressionParseMatch>(match);

            var expr = ((ExpressionParseMatch)match).Expression;

            Assert.IsType <Expression <SkriptVariable> >(expr);

            var variable = expr.Value as SkriptVariable;

            Assert.NotNull(variable);
            Assert.Equal(input, variable.ToString());
        }
Esempio n. 12
0
        public void ParenthesesTypeParsesCorrectly(string input)
        {
            WorkspaceManager.Instance.KnownTypesManager.LoadKnownTypes();
            var stringPattern = SkriptPattern.ParsePattern(ParseContext.FromCode("print %string%"));

            var result = stringPattern.Parse("print " + input);

            Assert.True(result.IsSuccess, "result.IsSuccess");
            Assert.Single(result.Context.Matches);

            var match = result.Context.Matches.First();

            Assert.IsType <ExpressionParseMatch>(match);

            var expr = ((ExpressionParseMatch)match).Expression;

            Assert.IsType <ParenthesesExpression>(expr);

            var parenthesesExpression = expr as ParenthesesExpression;

            Assert.NotNull(parenthesesExpression);
            Assert.Equal(input, parenthesesExpression.AsString());
        }
Esempio n. 13
0
        public void FunctionSignatureKnowsHowToParseRetardedSignatures()
        {
            //function retarded_func(b: strings = "thing","", c: integer) :: string
            var ctx = ParseContext.FromCode(
                "function retarded_func(b: strings = \"thing\",\"\", c: integer) :: string");
            var signature = FunctionSignature.TryParse(ctx);

            Assert.NotNull(signature);
            Assert.Equal(2, signature.Parameters.Count);

            var firstParam = signature.Parameters[0];

            Assert.Equal("b", firstParam.Name);
            Assert.Equal("strings", firstParam.Type.FinalTypeName);
            Assert.Equal("thing", firstParam.DefaultValue.GetExplicitValue <string>(0).GenericValue);
            Assert.Equal(string.Empty, firstParam.DefaultValue.GetExplicitValue <string>(1).GenericValue);


            var secondParam = signature.Parameters[1];

            Assert.Equal("c", secondParam.Name);
            Assert.Equal("integer", secondParam.Type.FinalTypeName);
        }
Esempio n. 14
0
        public void FunctionSignatureParserWorks(string name, string returnType = "", params string[] input)
        {
            var _      = NodeSignaturesManager.Instance;
            var result = $"function {name}({string.Join(", ", input)}){(returnType != "" ? $" :: {returnType}" : "")}";

            var ctx       = ParseContext.FromCode(result);
            var signature = SignatureParserHelper.TryParse <FunctionSignature>(ctx);

            Assert.NotNull(signature);

            Assert.Equal(result, signature.ToString());

            Assert.Equal(name, signature.Name);

            if (!returnType.IsEmpty())
            {
                Assert.Equal(returnType, signature.ReturnType.FinalTypeName);
            }

            Assert.Equal(input, signature.Parameters.Select(p => p.ToString()));

            Assert.True(ctx.HasFinishedLine, "ctx.HasFinishedLine");
        }
Esempio n. 15
0
        public void AllClickTypesCanBeParsedCorrectly()
        {
            var clicks = new[]
            {
                "left mouse button", "left mouse", "LMB", "left mouse button with shift", "left mouse with shift",
                "Shift+RMB", "right mouse button", "right mouse", "RMB", "right mouse button with shift",
                "right mouse with shift", "Shift+RMB", "window border using right mouse button",
                "window border using right mouse", "border using LMB", "window border using left mouse button",
                "window border using right mouse", "border using RMB", "middle mouse button", "middle mouse", "MMB",
                "number key", "0-9", "double click using mouse", "double click", "drop key", "drop item", "Q",
                "drop key with control", "drop stack", "Ctrl+Q", "creative action", "unknown", "unsupported", "custom"
            };

            var pattern = SkriptPattern.ParsePattern("%click type%");

            foreach (var click in clicks)
            {
                var ctx    = ParseContext.FromCode(click);
                var result = pattern.Parse(ctx);

                Assert.True(result.IsSuccess, $"result.IsSuccess -> {click}");
                Assert.False(result.IsOptionallyMatched);
            }
        }
Esempio n. 16
0
        public override void DoWork(SkriptFile file, int lineNumber, string rawContent, FileParseContext context)
        {
            AbstractFileNode resultNode = new BaseFileNode();

            NodeContentHelper.ApplyBasicNodeInfoToNode(rawContent, lineNumber, file, ref resultNode);

            if (resultNode.NodeContent.IsEmpty() && !resultNode.RawComment.IsEmpty())
            {
                AbstractFileNode commentNode = new CommentLineNode();
                NodeContentHelper.ApplyBasicNodeInfoToOtherNode(resultNode, ref commentNode);
                resultNode = commentNode;
            }
            else if (resultNode.NodeContent.IsEmpty() && resultNode.RawComment.IsEmpty())
            {
                AbstractFileNode emptyLineNode = new EmptyLineNode();
                emptyLineNode.MatchedSyntax =
                    new SyntaxMatch(SignatureElements.EmptyLine, ParseResult.Success(context));
                NodeContentHelper.ApplyBasicNodeInfoToOtherNode(resultNode, ref emptyLineNode);
                resultNode = emptyLineNode;
            }
            else
            {
                if (file.IsNodeVisible(resultNode))
                {
                    var ctx = ParseContext.FromCode(rawContent);

                    var signatureMatches = new List <(bool isSectionMismatch, AbstractFileNode node)>();
                    //Try to match to one of our known signatures
                    foreach (var(signatureNodeType, signatureDelegate) in NodeSignaturesManager.Instance.SignatureTypes
                             )
                    {
                        ctx.Matches.Clear();
                        ctx.CurrentPosition = context.IndentationChars;

                        var isSectionTypeMismatch = resultNode.IsSectionNode !=
                                                    (signatureNodeType.GetCustomAttribute <SectionNodeAttribute>() !=
                                                     null);


                        var tryParseResult = signatureDelegate.DynamicInvoke(ctx);

                        // We matched one signature
                        if (tryParseResult != null && ctx.HasFinishedLine)
                        {
                            var instance = signatureNodeType.NewInstance(tryParseResult);

                            if (instance is AbstractFileNode fileNode)
                            {
                                NodeContentHelper.ApplyBasicNodeInfoToOtherNode(resultNode, ref fileNode);
                                signatureMatches.Add((isSectionTypeMismatch, fileNode));
                            }

                            if (!isSectionTypeMismatch)
                            {
                                break;
                            }
                        }
                    }

                    var resultingNode = signatureMatches.FirstOrDefault(x => !x.isSectionMismatch).node;
                    if (resultingNode != null)
                    {
                        resultNode = resultingNode;
                    }
                }
            }

            file.Nodes[lineNumber] = resultNode;
        }
Esempio n. 17
0
        public void NormalSkriptPatternParsesCorrectly(string skPattern)
        {
            var pattern = SkriptPattern.ParsePattern(ParseContext.FromCode(skPattern));

            Assert.Equal(skPattern, pattern.RenderPattern());
        }
 public OptionalPatternElement(string contents) : base(contents)
 {
     Element = SkriptPattern.ParsePattern(ParseContext.FromCode(contents));
 }