| | | 1 | | using Antlr4.Runtime; |
| | | 2 | | using NanoCLang.Entities; |
| | | 3 | | using System; |
| | | 4 | | using System.IO; |
| | | 5 | | |
| | | 6 | | namespace NanoCLang { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides the interface to other assemblies that compile source files. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class Processor { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Compile the source file and returns the program entity. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="sourcefile">File to be compiled.</param> |
| | | 15 | | /// <returns>Program entity described by the NanoC source file.</returns> |
| | 0 | 16 | | public static Program? Compile(string sourcefile) { |
| | 0 | 17 | | var stream = new AntlrFileStream(sourcefile); |
| | 0 | 18 | | return Compile(stream); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Compile the source stream and returns the program entity. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="stream">File stream to be compiled.</param> |
| | | 25 | | /// <returns>Program entity described by the NanoC char stream.</returns> |
| | 0 | 26 | | private static Program? Compile(ICharStream stream) { |
| | 0 | 27 | | var lexer = new NanoCLexer(stream, Console.Out, Console.Error); |
| | 0 | 28 | | var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel); |
| | 0 | 29 | | return Compile(tokens); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="GetParser(string, TextWriter, TextWriter)"/> |
| | 123 | 33 | | public static NanoCParser GetParser(string input) => GetParser(input, Console.Out, Console.Error); |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets a NanoC string parser for the given <paramref name="input"/> string. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="input">Input string to the parser.</param> |
| | | 38 | | /// <param name="output">Output stream to write to.</param> |
| | | 39 | | /// <param name="errorOutput">Error stream to write to.</param> |
| | | 40 | | /// <returns>Parser for the input string.</returns> |
| | 123 | 41 | | public static NanoCParser GetParser(string input, TextWriter output, TextWriter errorOutput) { |
| | 123 | 42 | | var stream = new AntlrInputStream(input); |
| | 123 | 43 | | var lexer = new NanoCLexer(stream, output, errorOutput); |
| | 123 | 44 | | var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel); |
| | 123 | 45 | | return new NanoCParser(tokens, output, errorOutput) { |
| | 123 | 46 | | BuildParseTree = true |
| | 123 | 47 | | }; |
| | 123 | 48 | | } |
| | | 49 | | /// <inheritdoc cref="GetParser(Stream, TextWriter, TextWriter)"/> |
| | 0 | 50 | | public static NanoCParser GetParser(Stream input) => GetParser(input, Console.Out, Console.Error); |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets a NanoC stream parser for the given <paramref name="input"/> string. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="input">Input stream to the parser.</param> |
| | | 55 | | /// <param name="output">Output stream to write to.</param> |
| | | 56 | | /// <param name="errorOutput">Error stream to write to.</param> |
| | | 57 | | /// <returns>Parser for the input stream.</returns> |
| | 0 | 58 | | public static NanoCParser GetParser(Stream input, TextWriter output, TextWriter errorOutput) { |
| | 0 | 59 | | var stream = new AntlrInputStream(input); |
| | 0 | 60 | | var lexer = new NanoCLexer(stream, output, errorOutput); |
| | 0 | 61 | | var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel); |
| | 0 | 62 | | return new NanoCParser(tokens, output, errorOutput) { |
| | 0 | 63 | | BuildParseTree = true |
| | 0 | 64 | | }; |
| | 0 | 65 | | } |
| | | 66 | | /// <summary> |
| | | 67 | | /// Parse a program from a <see cref="TextReader"/> <paramref name="input"/>. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="input">Input stream to the parser.</param> |
| | | 70 | | /// <param name="output">Output stream to write to.</param> |
| | | 71 | | /// <param name="errorOutput">Error stream to write to.</param> |
| | | 72 | | /// <returns>Parsed program.</returns> |
| | | 73 | | /// <exception cref="FormatException">Program was not parsed successfully.</exception> |
| | 0 | 74 | | public static Program ParseProgram(TextReader input, TextWriter output, TextWriter errorOutput) { |
| | 0 | 75 | | var stream = new AntlrInputStream(input); |
| | 0 | 76 | | var lexer = new NanoCLexer(stream, output, errorOutput); |
| | 0 | 77 | | var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel); |
| | 0 | 78 | | var parser = new NanoCParser(tokens, output, errorOutput) { |
| | 0 | 79 | | BuildParseTree = true |
| | 0 | 80 | | }; |
| | 0 | 81 | | return parser.ParseProgram(); |
| | 0 | 82 | | } |
| | | 83 | | /// <summary> |
| | | 84 | | /// Compile the token stream and returns the program entity. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="tokens">NanoC token stream to be compiled.</param> |
| | | 87 | | /// <returns>Program entity described by the lexed NanoC token stream.</returns> |
| | 0 | 88 | | private static Program? Compile(ITokenStream tokens) { |
| | 0 | 89 | | var parser = new NanoCParser(tokens) { |
| | 0 | 90 | | BuildParseTree = true |
| | 0 | 91 | | }; |
| | 0 | 92 | | parser.AddErrorListener(new DiagnosticErrorListener(false)); |
| | 0 | 93 | | var program = parser.ParseProgram(); |
| | 0 | 94 | | if (parser.NumberOfSyntaxErrors > 0) |
| | 0 | 95 | | return null; |
| | 0 | 96 | | return program; |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |