Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
parser-translate-init.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 * *************************************************************************************/
37 #include "./lib.h"
38 #include "./parserp.h"
39 
43 void vTranslateRules(parser* spCtx, rule* spRules, opcode* spOpcodes, luint* luipData) {
44  aint ui;
45  aint uj = 0;
46  rule* spRule = spRules;
47  for (ui = 0; ui < spCtx->uiRuleCount; ui++, spRule++) {
48  spRule->uiRuleIndex = (aint)luipData[uj++];
49  spRule->ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
50  spRule->cpRuleName = spCtx->cpStringTable + luipData[uj++];
51  spRule->spOp = spOpcodes + luipData[uj++];
52  spRule->uiOpcodeCount = (aint)luipData[uj++];
53  spRule->uiEmpty = (aint)luipData[uj++];
54  }
55 }
56 
60 void vTranslateUdts(parser* spCtx, udt* spUdts, luint* luipData) {
61  if (spCtx->uiUdtCount) {
62  aint ui;
63  aint uj = 0;
64  udt* spUdt = spUdts;
65  for (ui = 0; ui < spCtx->uiUdtCount; ui++, spUdt++) {
66  spUdt->uiUdtIndex = (aint)luipData[uj++];
67  spUdt->cpUdtName = spCtx->cpStringTable + luipData[uj++];
68  spUdt->uiEmpty = (aint)luipData[uj++];
69  }
70  }
71 }
72 
76 void vTranslateOpcodes(parser* spCtx, rule* spRules, udt* spUdts, opcode* spOpcodes, luint* luipData) {
77  aint ui;
78  aint uj = 0;
79  opcode* spOp = spOpcodes;
80  for (ui = 0; ui < spCtx->uiOpcodeCount; ui++, spOp++) {
81  spOp->sGen.uiId = (aint)luipData[uj++];
82  switch (spOp->sGen.uiId) {
83  case ID_ALT:
84  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
85  spOp->sAlt.uipChildList = spCtx->uipChildList + luipData[uj++];
86  spOp->sAlt.uiChildCount = (aint)luipData[uj++];
87  break;
88  case ID_CAT:
89  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
90  spOp->sCat.uipChildList = spCtx->uipChildList + luipData[uj++];
91  spOp->sCat.uiChildCount = (aint)luipData[uj++];
92  break;
93  case ID_REP:
94  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
95  spOp->sRep.uiMin= (aint)luipData[uj++];
96  if(luipData[uj] == (luint)-1){
97  spOp->sRep.uiMax= (aint)-1;
98  }else{
99  spOp->sRep.uiMax= (aint)luipData[uj];
100  }
101  uj++;
102  break;
103  case ID_RNM:
104  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
105  spOp->sRnm.spRule = spRules + luipData[uj++];
106  break;
107  case ID_TRG:
108  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
109  spOp->sTrg.acMin = (achar)luipData[uj++];
110  spOp->sTrg.acMax = (achar)luipData[uj++];
111  break;
112  case ID_TLS:
113  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
114  spOp->sTls.acpStrTbl = spCtx->acpAcharTable + luipData[uj++];
115  spOp->sTls.uiStrLen = (aint)luipData[uj++];
116  break;
117  case ID_TBS:
118  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
119  spOp->sTbs.acpStrTbl = spCtx->acpAcharTable + luipData[uj++];
120  spOp->sTbs.uiStrLen = (aint)luipData[uj++];
121  break;
122  case ID_UDT:
123  spOp->sGen.ucpPpptMap = NULL;
124  spOp->sUdt.spUdt = spUdts + luipData[uj++];
125  spOp->sUdt.uiEmpty = (aint)luipData[uj++];
126  break;
127  case ID_BKR:
128  spOp->sGen.ucpPpptMap = NULL;
129  spOp->sBkr.uiRuleIndex = (aint)luipData[uj++];
130  spOp->sBkr.uiCase = (aint)luipData[uj++];
131  spOp->sBkr.uiMode = (aint)luipData[uj++];
132  break;
133  case ID_AND:
134  case ID_NOT:
135  spOp->sGen.ucpPpptMap = spCtx->ucpMaps + luipData[uj++];
136  break;
137  // these opcodes have no PPPT map
138  case ID_BKA:
139  case ID_BKN:
140  case ID_ABG:
141  case ID_AEN:
142  spOp->sGen.ucpPpptMap = NULL;
143  break;
144  default:
145  XTHROW(spCtx->spException, "unrecognized opcode found in initialization data");
146  break;
147  }
148 // vPrintOneOpcode(spCtx, spOp, APG_FALSE, stdout);
149  }
150 }
151 
lib.h
This header "#include"s all publid lib headers and other standard headers needed by most objects.
ID_RNM
#define ID_RNM
rule name
Definition: parser.h:46
parserp.h
Private header for the SABNF parser.
vTranslateRules
void vTranslateRules(parser *spCtx, rule *spRules, opcode *spOpcodes, luint *luipData)
Translate the initialization data for the rules into the internal rules format.
Definition: parser-translate-init.c:43
ID_ALT
#define ID_ALT
alternation
Definition: parser.h:43
ID_BKR
#define ID_BKR
back reference to a previously matched rule or UDT name
Definition: parser.h:58
ID_UDT
#define ID_UDT
user-defined terminal
Definition: parser.h:55
vTranslateOpcodes
void vTranslateOpcodes(parser *spCtx, rule *spRules, udt *spUdts, opcode *spOpcodes, luint *luipData)
Translate the initialization data for the opcodes into the internal opcode format.
Definition: parser-translate-init.c:76
achar
uint_fast8_t achar
achar is the type for the parser's alphabet characters.
Definition: apg.h:91
rule::spOp
const union opcode_tag * spOp
Pointer to the first opcode of the rule.
Definition: parserp.h:122
vTranslateUdts
void vTranslateUdts(parser *spCtx, udt *spUdts, luint *luipData)
Translate the initialization data for the UDTs into the internal UDT format.
Definition: parser-translate-init.c:60
ID_NOT
#define ID_NOT
negative look ahead
Definition: parser.h:57
ID_AND
#define ID_AND
positive look ahead
Definition: parser.h:56
XTHROW
#define XTHROW(ctx, msg)
Exception throw macro.
Definition: exception.h:67
rule::uiRuleIndex
aint uiRuleIndex
The rule index - zero-based order in which the rule appears in the SABNF grammar.
Definition: parserp.h:127
aint
uint_fast32_t aint
The APG parser's unsigned integer type.
Definition: apg.h:79
ID_CAT
#define ID_CAT
concatenation
Definition: parser.h:44
udt::cpUdtName
const char * cpUdtName
Pointer to the (null-terminated) ASCII rule name.
Definition: parserp.h:134
ID_TRG
#define ID_TRG
terminal range
Definition: parser.h:47
ID_REP
#define ID_REP
repetition
Definition: parser.h:45
luint
uintmax_t luint
luint is used to cast integers suitable for the %"PRIuMAX" printf format.
Definition: apg.h:133
udt::uiEmpty
aint uiEmpty
APG_TRUE if this UDT can be empty, APG_FALSE otherwise. Parser will throw an exception if this if fal...
Definition: parserp.h:137
ID_BKA
#define ID_BKA
positive look behind
Definition: parser.h:59
ID_TLS
#define ID_TLS
terminal literal string
Definition: parser.h:49
rule
Data structure for a single rule.
Definition: parserp.h:119
rule::uiOpcodeCount
aint uiOpcodeCount
Number of opcodes in this rule.
Definition: parserp.h:123
ID_BKN
#define ID_BKN
negative look behind
Definition: parser.h:60
rule::uiEmpty
aint uiEmpty
APG_TRUE if this rule can be empty, APG_FALSE otherwise.
Definition: parserp.h:126
ID_ABG
#define ID_ABG
anchor - beginning of string
Definition: parser.h:61
ID_TBS
#define ID_TBS
terminal binary string
Definition: parser.h:48
rule::ucpPpptMap
const uint8_t * ucpPpptMap
Pointer to the PPPT map for the rule.
Definition: parserp.h:121
udt
Data structure for a single UDT.
Definition: parserp.h:133
udt::uiUdtIndex
aint uiUdtIndex
The UDT index - the zero-based order in which the UDT appears in the SABNF grammar.
Definition: parserp.h:139
rule::cpRuleName
const char * cpRuleName
Pointer to the (null-terminated) ASCII rule name.
Definition: parserp.h:120
ID_AEN
#define ID_AEN
anchor - end of string
Definition: parser.h:62
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.