Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
syntax.c
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 * *************************************************************************************/
34 #include "./api.h"
35 #include "./apip.h"
36 #include "../library/parserp.h"
37 #include "./syntax.h"
38 #include "./semantics.h"
39 #include "sabnf-grammar.h"
40 
41 // Define this macro to turn on tracing during the syntax parsing phase.
42 //#define SYNTAX_TRACE
43 #ifdef SYNTAX_TRACE
44 #endif /* SYNTAX_TRACE */
45 
51 void vApiSyntax(void* vpCtx, abool bStrict) {
52  if(!bApiValidate(vpCtx)){
53  vExContext();
54  }
55  api* spApi = (api*) vpCtx;
56  parser_config sInput;
57  parser_state sState;
58  syntax_data sData;
59  memset((void*) &sInput, 0, sizeof(sInput));
60  memset((void*) &sState, 0, sizeof(sState));
61  memset((void*) &sData, 0, sizeof(sData));
62  // make sure the grammar has been validated
63  if (!spApi->bInputValid) {
64  XTHROW(spApi->spException, "attempted syntax phase but input grammar not validated");
65  }
66  if(spApi->bSyntaxValid){
67  XTHROW(spApi->spException, "attempted syntax phase but syntax has already been validated)");
68  }
69 
70  // construct the parser
72 
73  // construct the AST
74  spApi->vpAst = vpAstCtor(spApi->vpParser);
75 
76 #ifdef SYNTAX_TRACE
77  vpTraceCtor(spApi->vpParser, "a", "a");
78 #endif /* SYNTAX_TRACE */
79 
80  if (!spApi->cpInput || !spApi->uiInputLength) {
81  XTHROW(spApi->spException, "expected input not found");
82  }
83  sInput.acpInput = (const achar*)spApi->cpInput;
84  sInput.uiInputLength = spApi->uiInputLength;
85  if(sizeof(achar) > sizeof(char)){
86  vVecClear(spApi->vpVecGrammar);
87  achar* acpInput = (achar*)vpVecPushn(spApi->vpVecGrammar, NULL, sInput.uiInputLength);
88  aint ui = 0;
89  for(; ui < sInput.uiInputLength; ui++){
90  acpInput[ui] = (achar)((uint8_t)spApi->cpInput[ui]);
91  }
92  sInput.acpInput = (const achar*)acpInput;
93  }
94  sInput.uiStartRule = SABNF_GRAMMAR_FILE;
95 
96  // set up the parser configuration
97  sData.spApi = spApi;
98  sData.bStrict = bStrict;
99  sData.vpAltStack = spApi->vpAltStack;
100  sInput.vpUserData = (void*) &sData;
101 
102  // set the parser and AST callback functions
105 
106  // parser the input grammar
107  vParserParse(spApi->vpParser, &sInput, &sState);
108 
109  // check the parser's state
110  if (!sState.uiSuccess) {
111  XTHROW(spApi->spException, "syntax phase - parser failed");
112  }
113 
114  // syntax success
115  spApi->bSyntaxValid = APG_TRUE;
116 }
117 
118 
syntax_data
The syntax data that gets passed to the syntax parser's callback functions.
Definition: syntax.h:44
bApiValidate
abool bApiValidate(void *vpCtx)
Validates an API context pointer.
Definition: api.c:104
vpParserCtor
void * vpParserCtor(exception *spException, void *vpParserInit)
The parser's constructor for file initialization data.
Definition: parser.c:67
api::cpInput
char * cpInput
Definition: apip.h:139
semantics.h
Header file for the semantic translation functions.
apip.h
Private header file for the APG API suite of functions.
vExContext
void vExContext()
Handles bad context pointers.
Definition: exception.c:126
api::bInputValid
abool bInputValid
APG_TRUE if theer is input and it has been validated, APG_FALSE otherwise.
Definition: apip.h:182
achar
uint_fast8_t achar
achar is the type for the parser's alphabet characters.
Definition: apg.h:91
vApiSyntax
void vApiSyntax(void *vpCtx, abool bStrict)
Parse the SABNF grammar to validate that the grammar structure is valid.
Definition: syntax.c:51
XTHROW
#define XTHROW(ctx, msg)
Exception throw macro.
Definition: exception.h:67
aint
uint_fast32_t aint
The APG parser's unsigned integer type.
Definition: apg.h:79
vpTraceCtor
void * vpTraceCtor(void *vpCtx)
The trace object constructor.
Definition: trace.c:92
parser_config
Defines the input string and other configuration parameters for the parser,.
Definition: parser.h:198
sabnf-grammar.h
parser_state
The parser's final state.
Definition: parser.h:183
api
The API context.
Definition: apip.h:123
api::vpVecGrammar
void * vpVecGrammar
The (achar) input grammar, if sizeof(achar) > sizeof(char).
Definition: apip.h:138
api::bSyntaxValid
abool bSyntaxValid
APG_TRUE if the input syntax is valid, APG_FALSE otherwise.
Definition: apip.h:183
api::vpAst
void * vpAst
context handle to the AST object
Definition: apip.h:129
vpVecPushn
void * vpVecPushn(void *vpCtx, void *vpElement, aint uiCount)
Adds one or more elements to the end of the array.
Definition: vector.c:221
vSabnfGrammarAstCallbacks
void vSabnfGrammarAstCallbacks(void *vpAstCtx)
Set the callback functions for the AST translation of the semantic phase parse to opcodes.
Definition: semantic-callbacks.c:740
api::uiInputLength
aint uiInputLength
The number of input characters.
Definition: apip.h:140
vSabnfGrammarRuleCallbacks
void vSabnfGrammarRuleCallbacks(void *vpParserCtx)
Set the parser's rule callback functions for the syntax phase.
Definition: syntax-callbacks.c:329
APG_TRUE
#define APG_TRUE
Definition: apg.h:291
syntax.h
Header file for the syntax phase functions.
vpAstCtor
void * vpAstCtor(void *vpParserCtx)
The AST object constructor.
Definition: ast.c:51
vpSabnfGrammarInit
void * vpSabnfGrammarInit
Definition: sabnf-grammar.c:1437
SABNF_GRAMMAR_FILE
#define SABNF_GRAMMAR_FILE
Definition: sabnf-grammar.h:77
abool
uint8_t abool
abool is the APG bool type.
Definition: apg.h:140
api::vpAltStack
void * vpAltStack
A temporary vector for the AST translator.
Definition: apip.h:128
api::vpParser
void * vpParser
context handle to the SABNF grammar parser object
Definition: apip.h:127
api::spException
exception * spException
Definition: apip.h:125
vVecClear
void vVecClear(void *vpCtx)
Clears all used elements in a vector component.
Definition: vector.c:420
api.h
Public header file for the APG API suite of functions.
vParserParse
void vParserParse(void *vpCtx, parser_config *spConfig, parser_state *spState)
Parse an input string of alphabet characters.
Definition: parser.c:268
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.