Skip to content

Commit e93925c

Browse files
committed
chore(ini-parser): add files
1 parent 3134dd2 commit e93925c

File tree

5 files changed

+247
-2
lines changed

5 files changed

+247
-2
lines changed

.genlinxrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ build:
1313
- ./FileUtils
1414
- ./HashTable
1515
- ./Http
16+
- ./IniParser
1617
- ./Jsmn
1718
- ./Json
19+
- ./Lexer
1820
- ./LogicEngine
1921
- ./Math
2022
- ./McpBase

IniParser/NAVFoundation.IniParser.axi

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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__
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
PROGRAM_NAME='NAVFoundation.IniParser.h'
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_H__
36+
#DEFINE __NAV_FOUNDATION_INI_PARSER_H__ 'NAVFoundation.IniParser.h'
37+
38+
#include 'NAVFoundation.Core.axi'
39+
#include 'NAVFoundation.Lexer.axi'
40+
41+
42+
DEFINE_CONSTANT
43+
44+
constant char NAV_INI_TOKEN_TYPE_NULL[] = 'NULL'
45+
constant char NAV_INI_TOKEN_TYPE_BRACKET_OPEN[] = 'BRACKET_OPEN'
46+
constant char NAV_INI_TOKEN_TYPE_BRACKET_CLOSE[] = 'BRACKET_CLOSE'
47+
constant char NAV_INI_TOKEN_TYPE_TRUE[] = 'TRUE'
48+
constant char NAV_INI_TOKEN_TYPE_FALSE[] = 'FALSE'
49+
constant char NAV_INI_TOKEN_TYPE_NUMBER[] = 'NUMBER'
50+
constant char NAV_INI_TOKEN_TYPE_IDENTIFIER[] = 'IDENTIFIER'
51+
constant char NAV_INI_TOKEN_TYPE_ASSIGNMENT_OPERATOR[] = 'ASSIGNMENT_OPERATOR'
52+
constant char NAV_INI_TOKEN_TYPE_STRING[] = 'STRING'
53+
54+
constant char NAV_INI_TOKEN_SPEC[][][100] = {
55+
// End of file
56+
{ '\Z', 'EOF' },
57+
58+
// Whitespace
59+
{ '^;.*', 'NULL' },
60+
{ '^#.*', 'NULL' },
61+
62+
// Symbols
63+
{ '^\[', 'BRACKET_OPEN' },
64+
{ '^\]', 'BRACKET_CLOSE' },
65+
66+
// Boolean
67+
{ '^\btrue\b', 'TRUE' },
68+
{ '^\bfalse\b', 'FALSE' },
69+
70+
// Numbers
71+
{ '^\d+', 'NUMBER' },
72+
73+
// Identifiers
74+
{ '^[_a-zA-Z]\w*', 'IDENTIFIER' },
75+
76+
// Assignment operator
77+
{ '^=', 'ASSIGNMENT_OPERATOR' },
78+
79+
// Strings
80+
{ '^"[^"]*"', 'STRING' }
81+
}
82+
83+
84+
DEFINE_TYPE
85+
86+
struct _NAVIniSection {
87+
char id[255]
88+
_NAVStringKeyValuePair kvp[255]
89+
}
90+
91+
92+
struct _NAVIniFile {
93+
integer count
94+
_NAVIniSection sections[255]
95+
}
96+
97+
98+
struct _NAVIniParser {
99+
_NAVLexer lexer
100+
101+
char source[NAV_MAX_BUFFER * 2]
102+
_NAVLexerToken lookahead
103+
}
104+
105+
106+
#END_IF // __NAV_FOUNDATION_INI_PARSER_H__

IniParser/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NAVFoundation.IniParser
2+
3+
This is a simple INI file parser for NetLinx.

Lexer/NAVFoundation.Lexer.axi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ define_function char[NAV_MAX_BUFFER] NAVLexerMatchPattern(_NAVLexer lexer, char
136136
return ''
137137
}
138138

139-
lexer.cursor = lexer.cursor + match.length
140-
return match.text
139+
lexer.cursor = lexer.cursor + match.matches[1].length
140+
return match.matches[1].text
141141
}
142142

143143

0 commit comments

Comments
 (0)