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); }
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); }
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); }
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); }
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); }
/// <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(); } }
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); } }
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); }
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); } }
/// <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)); }
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); }
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); }
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); } }
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); }
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); }
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); }
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); }
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); } }
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); } }
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); } }
public void Should_Provide_Parse_Error_When_Variable_Name_Has_Underscore_Prefixes(string sylvreInput) { SylvreProgram program = Parser.ParseSylvreInput(sylvreInput); Assert.IsTrue(program.HasParseErrors); }