| | | 1 | | using NanoCLang.Entities; |
| | | 2 | | using NanoCLang.Environemnts; |
| | | 3 | | using System; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides an extension class for the NanoC parser that extends it by shortcuts for <see cref="Base"/> entity pars |
| | | 8 | | /// </summary> |
| | | 9 | | public static class NanoCParserExtensions { |
| | 0 | 10 | | internal static Exception ParseException => new FormatException("No entity was parsed!"); |
| | | 11 | | /// <summary> |
| | | 12 | | /// Use the parser to parse an entity and check if the entity was parsed successfully without syntax errors. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="T">Type of entity to parse.</typeparam> |
| | | 15 | | /// <param name="parser">Parser that executes the parsing.</param> |
| | | 16 | | /// <param name="parse">Function that invokes the parser and returns the entity.</param> |
| | | 17 | | /// <returns>Parsed entity.</returns> |
| | | 18 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 126 | 19 | | public static T ParseEntity<T>(this NanoCParser parser, Func<NanoCParser, T?> parse) where T : class { |
| | 126 | 20 | | var entity = parse(parser); |
| | 126 | 21 | | return entity is null || parser.NumberOfSyntaxErrors > 0 ? throw ParseException : entity; |
| | 126 | 22 | | } |
| | | 23 | | /// <summary> |
| | | 24 | | /// Parses a compilation unit including a single program before the end of input. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 27 | | /// <returns>Parsed compilation unit</returns> |
| | | 28 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 38 | 29 | | public static Program ParseProgram(this NanoCParser parser) => parser.ParseEntity(parser => parser.singleProgram |
| | | 30 | | /// <summary> |
| | | 31 | | /// Parses a single pure expression before the end of input. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 34 | | /// <returns>Parsed pure expression.</returns> |
| | | 35 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 50 | 36 | | public static PureExpression ParsePureExpression(this NanoCParser parser) => parser.ParseEntity(parser => parser |
| | | 37 | | /// <summary> |
| | | 38 | | /// Parses a single expression before the end of input. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 41 | | /// <returns>Parsed expression.</returns> |
| | | 42 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 98 | 43 | | public static Expression ParseExpression(this NanoCParser parser) => parser.ParseEntity(parser => parser.singleE |
| | | 44 | | /// <summary> |
| | | 45 | | /// Parses a single location before the end of input. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 48 | | /// <returns>Parsed location.</returns> |
| | | 49 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 0 | 50 | | public static Location ParseLocation(this NanoCParser parser) => parser.ParseEntity(parser => parser.singleLocat |
| | | 51 | | /// <summary> |
| | | 52 | | /// Parses a single index before the end of input. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 55 | | /// <returns>Parsed index.</returns> |
| | | 56 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 0 | 57 | | public static Entities.Index ParseIndex(this NanoCParser parser) => parser.ParseEntity(parser => parser.singleIn |
| | | 58 | | /// <summary> |
| | | 59 | | /// Parses a single basic type before the end of input. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 62 | | /// <returns>Parsed basic type.</returns> |
| | | 63 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 10 | 64 | | public static BasicType ParseBasicType(this NanoCParser parser) => parser.ParseEntity(parser => parser.singleBas |
| | | 65 | | /// <summary> |
| | | 66 | | /// Parses a single refined type before the end of input. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 69 | | /// <returns>Parsed refined type.</returns> |
| | | 70 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 38 | 71 | | public static RefinedType ParseRefinedType(this NanoCParser parser) => parser.ParseEntity(parser => parser.singl |
| | | 72 | | /// <summary> |
| | | 73 | | /// Parses a single heap before the end of input. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 76 | | /// <returns>Parsed heap.</returns> |
| | | 77 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 6 | 78 | | public static Heap ParseHeap(this NanoCParser parser) => parser.ParseEntity(parser => parser.ParseHeapElements() |
| | | 79 | | /// <summary> |
| | | 80 | | /// Parses a single list of heap elements before the end of input. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 83 | | /// <returns>Parsed heap elements.</returns> |
| | | 84 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 6 | 85 | | public static IHeapElement[] ParseHeapElements(this NanoCParser parser) => parser.ParseEntity(parser => parser.s |
| | | 86 | | /// <summary> |
| | | 87 | | /// Parses a single block before the end of input. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 90 | | /// <returns>Parsed block.</returns> |
| | | 91 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 0 | 92 | | public static BlockType[] ParseBlock(this NanoCParser parser) => parser.ParseEntity(parser => parser.singleBlock |
| | | 93 | | /// <summary> |
| | | 94 | | /// Parses a single function definition before the end of input. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 97 | | /// <returns>Parsed function definition.</returns> |
| | | 98 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 0 | 99 | | public static FunctionDefinition ParseFunctionDefinition(this NanoCParser parser) => parser.ParseEntity(parser = |
| | | 100 | | /// <summary> |
| | | 101 | | /// Parses a single function schema before the end of input. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="parser">Parser that is used to parse the input.</param> |
| | | 104 | | /// <returns>Parsed function schema.</returns> |
| | | 105 | | /// <exception cref="FormatException">No entity was parsed successfully.</exception> |
| | 6 | 106 | | public static RawFunctionSchema ParseFunctionSchema(this NanoCParser parser) => parser.ParseEntity(parser => par |
| | | 107 | | } |
| | | 108 | | } |