Beispiel #1
0
        public void Should_Provide_Error_With_Empty_Input(string emptyInputVariations)
        {
            SylvreProgram result = Parser.ParseSylvreInput(emptyInputVariations);

            Assert.IsTrue(result.HasParseErrors);
            Assert.AreEqual(1, result.ParseErrors.Count);
        }
Beispiel #2
0
        public void Should_Output_Valid_For_Loop_Block(string sylvreInput, string regexToMatch)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            StringAssert.IsMatch(regexToMatch, output.TranspiledCode);
        }
Beispiel #3
0
        public void Should_Not_Provide_Transpile_Errors(string sylvreInput)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsFalse(output.HasTranspileErrors);
        }
Beispiel #4
0
        public void Should_Output_Valid_JavaScript_Function_Return(string sylvreInput, string jsRegex)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            StringAssert.IsMatch(jsRegex, output.TranspiledCode);
        }
Beispiel #5
0
        public void Should_Provide_Transpile_Error_When_Assigning_To_Sylvre()
        {
            string        sylvreInput = "\nSylvre = 25.21#";
            SylvreProgram program     = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsTrue(output.HasTranspileErrors);
        }
Beispiel #6
0
        /// <summary>
        /// Transpiles a given Sylvre program to the target language.
        /// </summary>
        /// <param name="sylvreProgram">The Sylvre program generated by parsing an input.</param>
        /// <param name="targetLanguage">The target language to transpile the Sylvre program to.</param>
        /// <returns>The transpiled output in the target language.</returns>
        public static TranspileOutputBase TranspileSylvreToTarget(SylvreProgram sylvreProgram,
                                                                  TargetLanguage targetLanguage)
        {
            switch (targetLanguage)
            {
            case TargetLanguage.Javascript:
                return(new JavaScriptTranspiler().Transpile(sylvreProgram));

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #7
0
        public void Should_Provide_Specific_Amount_Of_Errors_Within_ParseErrors_Property()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Sylvre.Tests.Core.TestData.quickSort_bad_three_errors.syl";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    SylvreProgram result = Parser.ParseSylvreInput(reader.ReadToEnd());

                    Assert.AreEqual(3, result.ParseErrors.Count);
                }
        }
Beispiel #8
0
        public void Should_Output_Valid_Else_Statement()
        {
            string        sylvreInput = "if (TRUE) <> else < >";
            SylvreProgram program     = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            StringAssert.IsMatch(@"""use strict"";.+else[\n ]*{[\n ]*}",
                                 output.TranspiledCode);
        }
Beispiel #9
0
        public void Should_Have_Errors_Bad_Sylvre()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Sylvre.Tests.Core.TestData.quickSort_bad_three_errors.syl";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    SylvreProgram result = Parser.ParseSylvreInput(reader.ReadToEnd());

                    Assert.IsTrue(result.HasParseErrors);
                }
        }
Beispiel #10
0
        /// <summary>
        /// Transpile a given Sylvre program into JavaScript.
        /// </summary>
        /// <param name="sylvreProgram">The Sylvre program to transpile.</param>
        /// <returns>The transpiled JavaScript output.</returns>
        public JavaScriptOutput Transpile(SylvreProgram sylvreProgram)
        {
            // using strict mode for JS -
            _output.Append("\"use strict\";");
            // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

            foreach (BlockContext block in sylvreProgram.SylvreCstRoot.block())
            {
                VisitBlock(block);
            }

            return(new JavaScriptOutput(_output.ToString(), _transpileErrors));
        }
Beispiel #11
0
        public void Should_Output_Valid_JavaScript_Unary_Decrement_Suffix()
        {
            string        sylvreInput = "var decrement#";
            string        jsRegex     = @"""use strict"";__var.*--[n ]*;";
            SylvreProgram program     = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            StringAssert.IsMatch(jsRegex, output.TranspiledCode);
        }
Beispiel #12
0
        public void Should_Output_Valid_While_Loop_Block()
        {
            string        sylvreInput = "loopwhile(FALSE) < >";
            SylvreProgram program     = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            StringAssert.IsMatch(@"""use strict"";while[\n ]*\(.*\)[\n ]*{[\n ]*}",
                                 output.TranspiledCode);
        }
Beispiel #13
0
        public void Should_Have_Empty_ParseErrors_Property_With_Good_Sylvre()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Sylvre.Tests.Core.TestData.quickSort.syl";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    SylvreProgram result = Parser.ParseSylvreInput(reader.ReadToEnd());

                    Assert.AreEqual(0, result.ParseErrors.Count);
                }
        }
Beispiel #14
0
        public void Should_Append_Two_Underscores_If_FuncName_Is_Reserved_Keyword(string sylvreInput, string regexToMatch)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsFalse(output.HasTranspileErrors);

            StringAssert.IsMatch(regexToMatch, output.TranspiledCode);
        }
        public void Should_Output_Valid_ComplexVarReference_IndexRef(string sylvreInput,
                                                                     string regexToMatch)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsFalse(output.HasTranspileErrors);

            StringAssert.IsMatch(regexToMatch, output.TranspiledCode);
        }
Beispiel #16
0
        public void Should_Give_Error_When_No_Member_Ref_After_Module_Ref(string sylvreInput)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsTrue(output.HasTranspileErrors);

            Assert.AreEqual("Missing a module member reference after module name.",
                            output.TranspileErrors.First().Message);
        }
Beispiel #17
0
        public void Should_Give_Error_When_Index_Reference_After_Module_Ref(string sylvreInput)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsTrue(output.HasTranspileErrors);

            Assert.AreEqual("An index reference is not allowed after a module reference.",
                            output.TranspileErrors.First().Message);
        }
Beispiel #18
0
        public void Should_Give_Error_When_Invalid_Module_Member_Ref_Is_Used(string sylvreInput)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsFalse(program.HasParseErrors);

            TranspileOutputBase output = Transpiler.TranspileSylvreToTarget(
                program, TargetLanguage.Javascript);

            Assert.IsTrue(output.HasTranspileErrors);

            Assert.AreEqual("This Sylvre module member does not exist.",
                            output.TranspileErrors.First().Message);
        }
Beispiel #19
0
        public void Should_Be_Specific_Second_Error_Within_Parse_Exception()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Sylvre.Tests.Core.TestData.quickSort_bad_three_errors.syl";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    SylvreProgram result = Parser.ParseSylvreInput(reader.ReadToEnd());

                    SylvreParseError secondError = (SylvreParseError)result.ParseErrors.ElementAt(1);
                    Assert.IsTrue(secondError.IsMismatchedInput);
                    Assert.AreEqual(";", secondError.Symbol);
                    Assert.AreEqual(46, secondError.Line);
                    Assert.AreEqual(17, secondError.CharPositionInLine);
                }
        }
Beispiel #20
0
        public void Should_Be_Specific_First_Error_Within_ParseErrors_Property()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Sylvre.Tests.Core.TestData.quickSort_bad_three_errors.syl";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    SylvreProgram result = Parser.ParseSylvreInput(reader.ReadToEnd());

                    SylvreParseError firstError = (SylvreParseError)result.ParseErrors.ElementAt(0);
                    Assert.IsFalse(firstError.IsMismatchedInput);
                    StringAssert.Contains("create temp num_array", firstError.Message);
                    Assert.AreEqual(32, firstError.Line);
                    Assert.AreEqual(13, firstError.CharPositionInLine);
                }
        }
Beispiel #21
0
        public void Should_Be_Specific_Third_Error_Within_Parse_Exception()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Sylvre.Tests.Core.TestData.quickSort_bad_three_errors.syl";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    SylvreProgram result = Parser.ParseSylvreInput(reader.ReadToEnd());

                    SylvreParseError thirdError = (SylvreParseError)result.ParseErrors.ElementAt(2);
                    Assert.IsFalse(thirdError.IsMismatchedInput);
                    StringAssert.Contains("quickSort", thirdError.Message);
                    Assert.AreEqual(53, thirdError.Line);
                    Assert.AreEqual(9, thirdError.CharPositionInLine);
                }
        }
Beispiel #22
0
        public void Should_Provide_Parse_Error_When_Variable_Name_Has_Underscore_Prefixes(string sylvreInput)
        {
            SylvreProgram program = Parser.ParseSylvreInput(sylvreInput);

            Assert.IsTrue(program.HasParseErrors);
        }