|
| 1 | +PROGRAM_NAME='NAVFoundation.IniParser' |
| 2 | + |
| 3 | +/* |
| 4 | + _ _ _ ___ __ |
| 5 | +| \ | | ___ _ __ __ _ __ _| |_ ___ / \ \ / / |
| 6 | +| \| |/ _ \| '__/ _` |/ _` | __/ _ \ / _ \ \ / / |
| 7 | +| |\ | (_) | | | (_| | (_| | || __// ___ \ V / |
| 8 | +|_| \_|\___/|_| \__, |\__,_|\__\___/_/ \_\_/ |
| 9 | + |___/ |
| 10 | +
|
| 11 | +MIT License |
| 12 | +
|
| 13 | +Copyright (c) 2023 Norgate AV Services Limited |
| 14 | +
|
| 15 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | +of this software and associated documentation files (the "Software"), to deal |
| 17 | +in the Software without restriction, including without limitation the rights |
| 18 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 19 | +copies of the Software, and to permit persons to whom the Software is |
| 20 | +furnished to do so, subject to the following conditions |
| 21 | +
|
| 22 | +The above copyright notice and this permission notice shall be included in all |
| 23 | +copies or substantial portions of the Software. |
| 24 | +
|
| 25 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | +SOFTWARE. |
| 32 | +*/ |
| 33 | + |
| 34 | + |
| 35 | +#IF_NOT_DEFINED __NAV_FOUNDATION_INI_PARSER__ |
| 36 | +#DEFINE __NAV_FOUNDATION_INI_PARSER__ 'NAVFoundation.IniParser' |
| 37 | + |
| 38 | + |
| 39 | +#include 'NAVFoundation.IniParser.h.axi' |
| 40 | + |
| 41 | + |
| 42 | +define_function char NAVIniParserInit(_NAVIniParser parser, char source[]) { |
| 43 | + parser.source = source |
| 44 | + |
| 45 | + NAVLexerInit(parser.lexer, source, NAV_INI_TOKEN_SPEC) |
| 46 | + NAVLexerGetNextToken(parser.lexer, parser.lookahead) |
| 47 | + |
| 48 | + return true |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * Whether the token is a literal |
| 54 | + */ |
| 55 | +define_function char NAVIniParserTokenIsLiteral(_NAVLexerToken token) { |
| 56 | + return token.type == NAV_INI_TOKEN_TYPE_NUMBER || token.type == NAV_INI_TOKEN_TYPE_STRING |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +/** |
| 61 | + * Literal |
| 62 | + * : NumericLiteral |
| 63 | + * | StringLiteral |
| 64 | + * ; |
| 65 | + */ |
| 66 | +define_function char NAVIniParserParseLiteral(_NAVIniParser parser) { |
| 67 | + switch (parser.lookahead.type) { |
| 68 | + case NAV_INI_TOKEN_TYPE_NUMBER: { |
| 69 | + return NAVIniParserParseNumericLiteral(parser, parser.lookahead) |
| 70 | + } |
| 71 | + case NAV_INI_TOKEN_TYPE_STRING: { |
| 72 | + return NAVIniParserParseStringLiteral(parser, parser.lookahead) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR, |
| 77 | + __NAV_FOUNDATION_INI_PARSER__, |
| 78 | + 'NAVIniParserParseLiteral', |
| 79 | + "'Unexpected literal production'") |
| 80 | + return false |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +/** |
| 85 | + * StringLiteral |
| 86 | + * : STRING |
| 87 | + * ; |
| 88 | + */ |
| 89 | +define_function char NAVIniParserParseStringLiteral(_NAVIniParser parser, _NAVLexerToken token) { |
| 90 | + return NAVIniParserConsumeToken(parser, NAV_INI_TOKEN_TYPE_STRING, token) |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +/** |
| 95 | + * NumericLiteral |
| 96 | + * : NUMBER |
| 97 | + * ; |
| 98 | + */ |
| 99 | +define_function char NAVIniParserParseNumericLiteral(_NAVIniParser parser, _NAVLexerToken token) { |
| 100 | + return NAVIniParserConsumeToken(parser, NAV_INI_TOKEN_TYPE_NUMBER, token) |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/** |
| 105 | + * Expects a token of a given type and consumes it |
| 106 | + */ |
| 107 | +define_function char NAVIniParserConsumeToken(_NAVIniParser parser, char type[], _NAVLexerToken token) { |
| 108 | + #WARN 'Something could be wrong here' |
| 109 | + token = parser.lookahead |
| 110 | + |
| 111 | + if (token.type == 'NULL') { |
| 112 | + NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR, |
| 113 | + __NAV_FOUNDATION_INI_PARSER__, |
| 114 | + 'NAVIniParserConsumeToken', |
| 115 | + "'Unexpected end of input, expected: "', type, '"'") |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + if (token.type != type) { |
| 120 | + NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR, |
| 121 | + __NAV_FOUNDATION_INI_PARSER__, |
| 122 | + 'NAVIniParserConsumeToken', |
| 123 | + "'Unexpected token: "', token.value, '", expected: "', type, '"'") |
| 124 | + return false |
| 125 | + } |
| 126 | + |
| 127 | + // Advance to the next token |
| 128 | + NAVLexerGetNextToken(parser.lexer, parser.lookahead) |
| 129 | + |
| 130 | + return true |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +#END_IF // __NAV_FOUNDATION_INI_PARSER__ |
0 commit comments