< Summary

Class:NanoCLang.Processor
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Processor.cs
Covered lines:9
Uncovered lines:37
Coverable lines:46
Total lines:99
Line coverage:19.5% (9 of 46)
Covered branches:0
Total branches:2
Branch coverage:0% (0 of 2)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Compile(...)100%10%
Compile(...)100%10%
GetParser(...)100%1100%
GetParser(...)100%1100%
GetParser(...)100%10%
GetParser(...)100%10%
ParseProgram(...)100%10%
Compile(...)0%20%

File(s)

C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Processor.cs

#LineLine coverage
 1using Antlr4.Runtime;
 2using NanoCLang.Entities;
 3using System;
 4using System.IO;
 5
 6namespace 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>
 016        public static Program? Compile(string sourcefile) {
 017            var stream = new AntlrFileStream(sourcefile);
 018            return Compile(stream);
 019        }
 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>
 026        private static Program? Compile(ICharStream stream) {
 027            var lexer = new NanoCLexer(stream, Console.Out, Console.Error);
 028            var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel);
 029            return Compile(tokens);
 030        }
 31
 32        /// <inheritdoc cref="GetParser(string, TextWriter, TextWriter)"/>
 12333        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>
 12341        public static NanoCParser GetParser(string input, TextWriter output, TextWriter errorOutput) {
 12342            var stream = new AntlrInputStream(input);
 12343            var lexer = new NanoCLexer(stream, output, errorOutput);
 12344            var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel);
 12345            return new NanoCParser(tokens, output, errorOutput) {
 12346                BuildParseTree = true
 12347            };
 12348        }
 49        /// <inheritdoc cref="GetParser(Stream, TextWriter, TextWriter)"/>
 050        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>
 058        public static NanoCParser GetParser(Stream input, TextWriter output, TextWriter errorOutput) {
 059            var stream = new AntlrInputStream(input);
 060            var lexer = new NanoCLexer(stream, output, errorOutput);
 061            var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel);
 062            return new NanoCParser(tokens, output, errorOutput) {
 063                BuildParseTree = true
 064            };
 065        }
 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>
 074        public static Program ParseProgram(TextReader input, TextWriter output, TextWriter errorOutput) {
 075            var stream = new AntlrInputStream(input);
 076            var lexer = new NanoCLexer(stream, output, errorOutput);
 077            var tokens = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel);
 078            var parser = new NanoCParser(tokens, output, errorOutput) {
 079                BuildParseTree = true
 080            };
 081            return parser.ParseProgram();
 082        }
 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>
 088        private static Program? Compile(ITokenStream tokens) {
 089            var parser = new NanoCParser(tokens) {
 090                BuildParseTree = true
 091            };
 092            parser.AddErrorListener(new DiagnosticErrorListener(false));
 093            var program = parser.ParseProgram();
 094            if (parser.NumberOfSyntaxErrors > 0)
 095                return null;
 096            return program;
 097        }
 98    }
 99}