Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
parser.h
Go to the documentation of this file.
1 /* *************************************************************************************
2  Copyright (c) 2021, Lowell D. Thomas
3  All rights reserved.
4 
5  This file is part of APG Version 7.0.
6  APG Version 7.0 may be used under the terms of the BSD 2-Clause License.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 * *************************************************************************************/
30 #ifndef LIB_PARSER_H_
31 #define LIB_PARSER_H_
32 
42 #define ID_ALT 1
44 #define ID_CAT 2
45 #define ID_REP 3
46 #define ID_RNM 4
47 #define ID_TRG 5
48 #define ID_TBS 6
49 #define ID_TLS 7
50 
54 #define ID_UDT 8
56 #define ID_AND 9
57 #define ID_NOT 10
58 #define ID_BKR 11
59 #define ID_BKA 12
60 #define ID_BKN 13
61 #define ID_ABG 14
62 #define ID_AEN 15
63 #define ID_GEN 19
66 
71 #define ID_ACTIVE 20
73 #define ID_MATCH 21
74 #define ID_NOMATCH 22
75 #define ID_EMPTY 23
76 
80 #define ID_PPPT_NOMATCH 0
82 #define ID_PPPT_MATCH 1
83 #define ID_PPPT_EMPTY 2
84 #define ID_PPPT_ACTIVE 3
86 
90 #define ID_AST_PRE 30
92 #define ID_AST_POST 31
93 #define ID_AST_OK 32
94 #define ID_AST_SKIP 33
95 
99 #define ID_ATTR_N 40
101 #define ID_ATTR_R 41
102 #define ID_ATTR_MR 42
103 
108 #define ID_LOOKAROUND_NONE 50
110 #define ID_LOOKAROUND_AHEAD 51
111 #define ID_LOOKAROUND_BEHIND 52
112 
117 #define ID_BKR_MODE_U 60
119 #define ID_BKR_MODE_P 61
120 #define ID_BKR_CASE_S 62
121 #define ID_BKR_CASE_I 63
122 
124 
134 typedef struct {
135  // only the 3 members below are to be changed by user
136  void* vpUserData;
155  // Parser will produce unpredictable results if user changes any of the values below.
156  const achar* acpString;
168  // For system use only.
169  void* vpCtx;
170  void* vpMem;
171 } callback_data;
172 
178 typedef void (*parser_callback)(callback_data* spData);
179 
183 typedef struct{
192 } parser_state;
193 
198 typedef struct {
199  const achar* acpInput;
210  void* vpUserData;
213 } parser_config;
214 
215 void* vpParserCtor(exception* spException, void* vpParserInit);
216 void vParserDtor(void* vpCtx);
217 abool bParserValidate(void* vpCtx);
218 void vParserParse(void* vpCtx, parser_config* spConfig, parser_state* spState);
219 aint uiParserRuleLookup(void* vpCtx, const char* cpRuleName);
220 aint uiParserUdtLookup(void* vpCtx, const char* cpUdtName);
221 const char* cpParserRuleName(void* vpCtx, aint uiRuleIndex);
222 const char* cpParserUdtName(void* vpCtx, aint uiUdtIndex);
223 void vParserSetRuleCallback(void* vpCtx, aint uiRuleId, parser_callback pfnCallback);
224 void vParserSetUdtCallback(void* vpCtx, aint uiUdtId, parser_callback pfnCallback);
225 
226 #endif /* LIB_PARSER_H_ */
parser_config::bParseSubString
abool bParseSubString
If true (non-zero), only parse the defined sub-string of the input string.
Definition: parser.h:202
parser_config::uiSubStringBeg
aint uiSubStringBeg
The first character of the sub-string to parse. Must be < uiInputLength or exception is thrown.
Definition: parser.h:203
parser_state::uiSuccess
aint uiSuccess
True (>0) if the input string was matched in its entirety, false (0) otherwise.
Definition: parser.h:184
callback_data::uiUDTIndex
aint uiUDTIndex
[read only] The UDT index of this UDT's callback. APG_UNDEFINED if this is a rule callback.
Definition: parser.h:164
vParserParse
void vParserParse(void *vpCtx, parser_config *spConfig, parser_state *spState)
Parse an input string of alphabet characters.
Definition: parser.c:268
callback_data::uiParserOffset
aint uiParserOffset
[read only] Offset from acpString to the first character to match
Definition: parser.h:160
callback_data::acpString
const achar * acpString
[read only] Pointer to the input sub-string,
Definition: parser.h:156
parser_config::acpInput
const achar * acpInput
Pointer to the input string.
Definition: parser.h:199
callback_data::uiParserState
aint uiParserState
[read only] ID_ACTIVE if the parser is going down the tree. ID_MATCH or ID_NOMATCH if coming up the t...
Definition: parser.h:158
parser_config::vpUserData
void * vpUserData
Pointer to user data, if any. Not examined or used by the parser in any way. Presented to the user's ...
Definition: parser.h:210
achar
uint_fast8_t achar
achar is the type for the parser's alphabet characters.
Definition: apg.h:91
parser_state::uiStringLength
aint uiStringLength
Length of the input string.
Definition: parser.h:189
uiParserRuleLookup
aint uiParserRuleLookup(void *vpCtx, const char *cpRuleName)
Find the rule index corresponding to a rule name.
Definition: parser.c:440
callback_data::uiRuleIndex
aint uiRuleIndex
[read only] The rule index of this rule's callback. APG_UNDEFINED if this is a UDT callback.
Definition: parser.h:163
vpParserInit
const void * vpParserInit
Definition: sabnf-grammar.c:1422
parser_callback
void(* parser_callback)(callback_data *spData)
User-written callback function prototype.
Definition: parser.h:178
aint
uint_fast32_t aint
The APG parser's unsigned integer type.
Definition: apg.h:79
parser_config::uiInputLength
aint uiInputLength
Number of input string alphabet characters.
Definition: parser.h:200
vParserSetRuleCallback
void vParserSetRuleCallback(void *vpCtx, aint uiRuleId, parser_callback pfnCallback)
Set a call back function for a specific rule.
Definition: parser.c:386
callback_data::uiParserPhraseLength
aint uiParserPhraseLength
[read only] The parser's matched phrase length if uiParserState is ID_MATCH or ID_NOMATCH....
Definition: parser.h:161
parser_config
Defines the input string and other configuration parameters for the parser,.
Definition: parser.h:198
callback_data::uiCallbackState
aint uiCallbackState
[input/output] Rule name (RNM) callback functions: If ID_ACTIVE, the parser takes no action....
Definition: parser.h:139
vParserSetUdtCallback
void vParserSetUdtCallback(void *vpCtx, aint uiUdtId, parser_callback pfnCallback)
Set a call back function for a specific UDT.
Definition: parser.c:407
parser_state
The parser's final state.
Definition: parser.h:183
exception
A structure to describe the type and location of a caught exception.
Definition: exception.h:47
callback_data::uiCallbackPhraseLength
aint uiCallbackPhraseLength
[input/output] The phrase length of the matched phrase if the callback function returns ID_MATCH.
Definition: parser.h:145
callback_data
The data struct passed to each callback function.
Definition: parser.h:134
parser_config::uiLookBehindLength
aint uiLookBehindLength
The maximum length to look behind for a match. Use 0 or APG_INFINITE for infinite look behind.
Definition: parser.h:208
parser_state::uiState
aint uiState
One of ID_EMPTY, ID_MATCH or ID_NOMATCH. Note that it is possible for the parser to match a phrase wi...
Definition: parser.h:185
callback_data::vpMem
void * vpMem
[read only] Do not use. For system use only.
Definition: parser.h:170
cpParserUdtName
const char * cpParserUdtName(void *vpCtx, aint uiUdtIndex)
Find the UDT name corresponding to a UDT index.
Definition: parser.c:491
callback_data::vpCtx
void * vpCtx
[read only] Do not use. For system use only.
Definition: parser.h:169
callback_data::vpUserData
void * vpUserData
[input/output] User-defined data passed to to the parser in parser_config.
Definition: parser.h:136
parser_config::uiSubStringLength
aint uiSubStringLength
The number of characters in the sub-string. If 0, then the remainder of the string from uiSubStringBe...
Definition: parser.h:205
cpParserRuleName
const char * cpParserRuleName(void *vpCtx, aint uiRuleIndex)
Find the rule name from the corresponding index.
Definition: parser.c:469
parser_state::uiMaxTreeDepth
aint uiMaxTreeDepth
The maximum tree depth reached during the parse.
Definition: parser.h:190
parser_state::uiHitCount
aint uiHitCount
The number of nodes visited during the traversal of the parse tree.
Definition: parser.h:191
abool
uint8_t abool
abool is the APG bool type.
Definition: apg.h:140
parser_state::uiPhraseLength
aint uiPhraseLength
Length of the matched phrase.
Definition: parser.h:188
parser_config::uiStartRule
aint uiStartRule
Index of the start rule. Any rule in the SABNF grammar may be used as the start rule.
Definition: parser.h:201
vpParserCtor
void * vpParserCtor(exception *spException, void *vpParserInit)
The parser's constructor for file initialization data.
Definition: parser.c:67
uiParserUdtLookup
aint uiParserUdtLookup(void *vpCtx, const char *cpUdtName)
callback_data::uiStringLength
aint uiStringLength
[read only] The input string length.
Definition: parser.h:157
bParserValidate
abool bParserValidate(void *vpCtx)
Validate the context pointer of a parser.
Definition: parser.c:422
callback_data::spException
exception * spException
[read only] Use to throw exceptions back to the parser's catch block scope: e.g. XTHROW(spException,...
Definition: parser.h:165
vParserDtor
void vParserDtor(void *vpCtx)
Clears the parser component's context and frees all heap memory associated with this parser.
Definition: parser.c:245
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.