public void ParseNext_UnknownBacktick() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence("`"); target.Parse(source).Value.Should().Match(TokenType.Unknown, "`"); }
public TranspileResult TranspileFile(string inputFileName, string outputFileName = null) { CompilationUnitNode ast; using (var source = new StreamCharacterSequence(inputFileName, Encoding.UTF8)) { var lexicalGrammar = LexicalGrammar.GetParser(); var tokenizer = lexicalGrammar .ToSequence(source) .Where(t => t.Value.Type != TokenType.Comment) .Select(r => r.Value); var result = _grammar.CompilationUnits.Parse(tokenizer); ast = result.Value; ast.FileName = inputFileName; } var validateResults = ast.Validate(); if (validateResults.Any()) { return(TranspileResult.ForValidationFailure(validateResults)); } outputFileName = outputFileName ?? inputFileName + ".cs"; using (var outStream = new StreamWriter(outputFileName, false)) { var preamble = Formatting.GetGeneratedFilePreamble("Scoop", inputFileName); outStream.WriteLine(preamble); new CSharpTranspileVisitor(outStream).Visit(ast); outStream.Flush(); } return(TranspileResult.ForSuccess(outputFileName)); }
public void ParseNext_CharacterEscapedHexValue() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence("'\\x41'"); target.Parse(source).Value.Should().Match(TokenType.Character, "'\\x41'"); }
public void ParseNext_WhitespaceOperator() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(" + "); target.Parse(source).Value.Should().Match(TokenType.Operator, "+"); }
public void ParseNext_EOF() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(""); target.Parse(source).Value.Type.Should().Be(TokenType.EndOfInput); }
/// <summary> /// Convenience method to handle parsing a string into an AstNode /// </summary> /// <typeparam name="TOutput"></typeparam> /// <param name="parser"></param> /// <param name="s"></param> /// <returns></returns> public static TOutput Parse <TOutput>(this IParser <Token, TOutput> parser, string s) { var tokenizer = LexicalGrammar.GetParser() .ToSequence(new StringCharacterSequence(s)) .Select(r => r.Value); return(parser.Parse(tokenizer).Value); }
public void ParseNext_IntegerDivideInteger() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(@"1/2"); target.Parse(source).Value.Should().Match(TokenType.Integer, "1"); target.Parse(source).Value.Should().Match(TokenType.Operator, "/"); target.Parse(source).Value.Should().Match(TokenType.Integer, "2"); }
public void ParseNext_Word() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence("test class @class"); target.Parse(source).Value.Should().Match(TokenType.Word, "test"); target.Parse(source).Value.Should().Match(TokenType.Word, "class"); target.Parse(source).Value.Should().Match(TokenType.Word, "@class"); }
public void ParseNext_ClassDefinition() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(@"public class MyClass { }"); target.Parse(source).Value.Should().Match(TokenType.Word, "public"); target.Parse(source).Value.Should().Match(TokenType.Word, "class"); target.Parse(source).Value.Should().Match(TokenType.Word, "MyClass"); target.Parse(source).Value.Should().Match(TokenType.Operator, "{"); target.Parse(source).Value.Should().Match(TokenType.Operator, "}"); }
public void ParseNext_MultiLineComment1() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(@" /* This is a comment */ "); var result = target.Parse(source).Value; result.Type.Should().Be(TokenType.EndOfInput); result.Frontmatter[1].Should().Be("/* This is a comment */"); }
public void ParseNext_SingleLineComment() { //Assert.Inconclusive("Comments aren't returned from the lexer right now"); var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(@" // This is a comment "); var result = target.Parse(source).Value; result.Type.Should().Be(TokenType.EndOfInput); result.Frontmatter[1].Should().Be("// This is a comment"); }
public void ParseNext_MethodDefinition() { var target = LexicalGrammar.GetParser(); var source = new StringCharacterSequence(@"public void MyMethod() { }"); target.Parse(source).Value.Should().Match(TokenType.Word, "public"); target.Parse(source).Value.Should().Match(TokenType.Word, "void"); target.Parse(source).Value.Should().Match(TokenType.Word, "MyMethod"); target.Parse(source).Value.Should().Match(TokenType.Operator, "("); target.Parse(source).Value.Should().Match(TokenType.Operator, ")"); target.Parse(source).Value.Should().Match(TokenType.Operator, "{"); target.Parse(source).Value.Should().Match(TokenType.Operator, "}"); }
public void ParseNext_CSharpLiteral_Simple() { var source = new StringCharacterSequence(@"c# {obj.Method();}"); LexicalGrammar.GetParser().Parse(source).Value.Should().Match(TokenType.CSharpLiteral, "obj.Method();"); }
public void ParseNext_CSharpLiteral_Character() { var source = new StringCharacterSequence(@"c# {'x'}"); LexicalGrammar.GetParser().Parse(source).Value.Should().Match(TokenType.CSharpLiteral, "'x'"); }
public void ParseNext_CSharpLiteral_AtStringBackslashEscapedQuote() { var source = new StringCharacterSequence(@"c# {@""quo\""""ted""}"); LexicalGrammar.GetParser().Parse(source).Value.Should().Match(TokenType.CSharpLiteral, "@\"quo\\\"\"ted\""); }
public void ParseNext_CSharpLiteral_StringBrace() { var source = new StringCharacterSequence(@"c# {""quo}ted""}"); LexicalGrammar.GetParser().Parse(source).Value.Should().Match(TokenType.CSharpLiteral, "\"quo}ted\""); }
public void ParseNext_CSharpLiteral_Braces() { var source = new StringCharacterSequence(@"c# {if(x==2){y();}}"); LexicalGrammar.GetParser().Parse(source).Value.Should().Match(TokenType.CSharpLiteral, "if(x==2){y();}"); }