public static ISyntaxTree CreateSyntaxTree(Stream file, string filePath, out IReadOnlyList <ParseError> parseErrors)
        {
            var input = new AntlrInputStream(file);
            var lexer = new TypemakerLexer(input);

            var errors        = new List <ParseError>();
            var errorListener = new ReportingErrorListener(errors, lexer.Vocabulary);

            lexer.AddErrorListener(errorListener);

            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new TypemakerParser(tokenStream);

            parser.AddErrorListener(errorListener);

            var compilationUnitContext = parser.compilation_unit();

            parseErrors = errors;
            if (errors.Count > 0)
            {
                return(null);
            }

            var visitor = new CompilationUnitVisitor(filePath);
            var tree    = visitor.Visit(compilationUnitContext);
            var tokens  = tokenStream.GetTokens();

            tree.Build(tokens);

            return(tree);
        }
Ejemplo n.º 2
0
 public static void Main(string[] args)
 {
     using (FileStream fs = new FileStream("../../../../Typemaker.Compiler/test.tm", FileMode.Open, FileAccess.Read))
     {
         var input      = new AntlrInputStream(fs);
         var lexer      = new TypemakerLexer(input);
         var tokens     = lexer.GetAllTokens();
         var tokenNames = String.Join(" ", tokens.Select(x => lexer.Vocabulary.GetDisplayName(x.Type)));
         Console.WriteLine(tokenNames);
         Console.ReadKey();
     }
 }
 static void SourceMatchTokens(string source, List <int> expectedTokens)
 {
     using (var ss = new StringReader(source))
     {
         var input              = new AntlrInputStream(ss);
         var lexer              = new TypemakerLexer(input);
         var tokenInterfaces    = lexer.GetAllTokens();
         var actualTokens       = tokenInterfaces.Select(x => x.Type).ToList();
         var translatedExpected = expectedTokens.Select(x => lexer.Vocabulary.GetSymbolicName(x)).ToList();
         var translatedActual   = actualTokens.Select(x => lexer.Vocabulary.GetSymbolicName(x)).ToList();
         Assert.Equal(expectedTokens.Count, actualTokens.Count);
         for (var I = 0; I < expectedTokens.Count; ++I)
         {
             Assert.True(actualTokens[I] == expectedTokens[I], "Expected " + translatedExpected[I] + " got " + translatedActual[I]);
         }
     }
 }