48 #include "../api/api.h"
49 #include "../api/apip.h"
50 #include "../api/attributes.h"
54 #include "../library/parserp.h"
55 #include "../utilities/utilities.h"
56 static aint s_uiTreeDepth = 0;
57 static void vRuleOpen(
api* spApi,
aint uiRuleIndex);
58 static void vRuleLeaf(
api* spApi,
aint uiRuleIndex, uint8_t* ucpMap);
59 static void vRuleClose(
api* spApi,
aint uiRuleIndex);
60 static void vOpcodeOpen(
api* spApi,
api_op* spOp);
61 static void vOpcodeClose(
api* spApi,
api_op* spOp);
62 static void vIndent(
aint uiIndent);
63 static void vPrintMap(
api* spApi, uint8_t* ucpMap);
64 #define TRACE_RULE_OPEN(n) vRuleOpen(spApi, (n))
65 #define TRACE_RULE_LEAF(n, m) vRuleLeaf(spApi, (n), (m))
66 #define TRACE_RULE_CLOSE(n) vRuleClose(spApi, (n))
67 #define TRACE_OPCODE_OPEN(o) vOpcodeOpen(spApi, (o))
68 #define TRACE_OPCODE_CLOSE(o) vOpcodeClose(spApi, (o))
70 #define TRACE_RULE_OPEN(n)
71 #define TRACE_RULE_LEAF(n, m)
72 #define TRACE_RULE_CLOSE(n)
73 #define TRACE_OPCODE_OPEN(o)
74 #define TRACE_OPCODE_CLOSE(o)
77 static void vSetMapValGen(uint8_t* ucpMap,
luint luiOffset,
luint luiChar, uint8_t ucVal);
78 static uint8_t ucGetMapValGen(
api* spApi, uint8_t* ucpMap,
luint luiOffset,
luint luiChar);
79 static void vGetMaps(
api* spApi);
80 static int iCompOps(
const void* vpL,
const void* vpR);
81 static int iCompName(
const void* vpL,
const void* vpR);
82 static int iNameInsensitiveCompare(
char* cpL,
char* cpR);
83 static int iMatchRule(
api_rule* spRule,
aint uiRuleCount,
char* cpName);
84 static void vCopyMap(uint8_t* ucpDst, uint8_t* ucpSrc,
aint uiLen);
85 static void vClearMap(uint8_t* ucpMap,
aint uiLen);
86 static void vRuleMap(
api* spApi,
aint uiRuleIndex, uint8_t* ucpMap);
87 static void vAltMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap);
88 static void vCatMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap);
89 static void vRepMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap);
90 static void vOpcodeMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap);
101 void vApiPppt(
void* vpCtx,
char** cppProtectedRules,
aint uiProtectedRules){
105 api* spApi = (
api*) vpCtx;
110 "attempted PPPT construction but opcodes (vApiOpcodes()) have not been constructed");
116 XTHROW(spApi->
spException,
"Partially-Predictive Parsing Tables cannot be used for this grammar. The maximum character is too large - 0xFFFFFFFFFFFFFFFF");
118 if(cppProtectedRules && uiProtectedRules){
121 memcpy((
void*)saRules, (
void*)spApi->
spRules,
sizeof(saRules));
123 for(ui = 0; ui < uiProtectedRules; ui++){
124 int iIndex = iMatchRule(saRules, spApi->
uiRuleCount, cppProtectedRules[ui]);
127 snprintf(caBuf, 256,
"PPPT protected rules: %s is not a valid rule name", cppProtectedRules[ui]);
149 for(; lu <= spOp->
luiMax; lu++){
168 printf(
"CHARACTER MAP\n");
169 printf(
"character | char val | undec val\n");
170 printf(
"----------|----------|----------\n");
172 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharMax; lu++){
174 printf(
"%9"PRIuMAX
" | %8"PRIuMAX
" | ", (
luint)lu, (
luint)uc);
176 printf(
"%9"PRIuMAX
"\n", (
luint)uc);
179 printf(
"%9s | %8"PRIuMAX
" | ",
"EOS", (
luint)uc);
181 printf(
"%9"PRIuMAX
"\n", (
luint)uc);
194 switch (spOp->
uiId) {
250 api* spApi = (
api*) vpCtx;
252 XTHROW(spApi->
spException,
"this function may not be called prior to vApiOpcodes()");
264 static void vSetMapValGen(uint8_t* ucpMap,
luint luiOffset,
luint luiChar, uint8_t ucVal){
265 ucpMap[luiChar - luiOffset] = ucVal;
274 static uint8_t ucGetMapValGen(
api* spApi, uint8_t* ucpMap,
luint luiOffset,
luint luiChar){
279 return ucpMap[luiChar - luiOffset];
286 static void vCopyMap(uint8_t* ucpDst, uint8_t* ucpSrc,
aint uiLen){
287 uint8_t* ucpEnd = ucpDst + uiLen;
288 while(ucpDst < ucpEnd){
289 *ucpDst++ = *ucpSrc++;
292 static void vClearMap(uint8_t* ucpMap,
aint uiLen){
293 memset((
void*)ucpMap, 0,
sizeof(uint8_t) * uiLen);
296 static void vGetMaps(
api* spApi){
300 memcpy((
void*)saRules, (
void*)spApi->
spRules,
sizeof(saRules));
308 printf(
"opcode count | name\n");
309 printf(
"------------ | ----\n");
310 for(ui = 0; ui < spApi->
uiRuleCount; ui++, spRule++){
317 vRuleMap(spApi, saRules[ui].uiIndex, ucaMap);
321 static void vRuleMap(
api* spApi,
aint uiRuleIndex, uint8_t* ucpMap){
333 vOpcodeMap(spApi, spOp, ucpMap);
337 memcpy((
void*)ucpRuleMap, (
void*)ucpMap, (
sizeof(uint8_t) * spApi->
luiPpptMapSize));
351 static void vAltMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap){
358 uint8_t* ucpChild = ucaChildren;
359 for (ui = 0; ui < uiCount; ui++, ucpChild += spApi->
luiPpptMapSize) {
361 vOpcodeMap(spApi, spChildOp, ucpChild);
363 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharEos; lu++){
364 ucpChild = ucaChildren;
365 for (ui = 0; ui < uiCount; ui++, ucpChild += spApi->
luiPpptMapSize) {
366 ucChildVal = ucGetMapValGen(spApi, ucpChild, spApi->
luiAcharMin, lu);
368 vSetMapValGen(ucpMap, spApi->
luiAcharMin, lu, ucChildVal);
380 static void vCatMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap){
387 uint8_t* ucpChild = ucaChildren;
389 for (ui = 0; ui < uiCount; ui++, ucpChild += spApi->
luiPpptMapSize) {
391 vOpcodeMap(spApi, spChildOp, ucpChild);
393 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharEos; lu++){
395 ucChildVal = ucGetMapValGen(spApi, ucaChildren, spApi->
luiAcharMin, lu);
401 static void vRepMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap){
405 vOpcodeMap(spApi, (spOp + 1), ucaChildMap);
406 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharEos; lu++){
407 ucChildVal = ucGetMapValGen(spApi, ucaChildMap, spApi->
luiAcharMin, lu);
421 static void vOpcodeMap(
api* spApi,
api_op* spOp, uint8_t* ucpMap){
425 switch (spOp->
uiId) {
427 vAltMap(spApi, spOp, ucpMap);
430 vCatMap(spApi, spOp, ucpMap);
433 vRepMap(spApi, spOp, ucpMap);
436 vRuleMap(spApi, spOp->
uiIndex, ucpMap);
439 vOpcodeMap(spApi, (spOp + 1), ucpMap);
440 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharEos; lu++){
441 uint8_t ucVal = ucGetMapValGen(spApi, ucpMap, spApi->
luiAcharMin, lu);
448 vOpcodeMap(spApi, (spOp + 1), ucpMap);
450 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharEos; lu++){
451 uint8_t ucVal = ucGetMapValGen(spApi, ucpMap, spApi->
luiAcharMin, lu);
487 for(lu = spOp->
luiMin; lu <= spOp->luiMax; lu++){
504 switch (spOp->
uiId) {
531 static int iCompOps(
const void* vpL,
const void* vpR){
542 static int iNameInsensitiveCompare(
char* cpL,
char* cpR){
543 aint uiLenL = strlen(cpL);
544 aint uiLenR = strlen(cpR);
546 aint uiLesser = uiLenL < uiLenR ? uiLenL : uiLenR;
549 if (l >= 65 && l <= 90) {
553 if (r >= 65 && r <= 90) {
565 if (uiLenL < uiLenR) {
568 if (uiLenL > uiLenR) {
573 static int iCompName(
const void* vpL,
const void* vpR) {
576 return iNameInsensitiveCompare(spL->
cpName, spR->
cpName);
579 static int iMatchRule(
api_rule* spRule,
aint uiRuleCount,
char* cpName){
584 iR = uiRuleCount - 1;
586 iM = ((iR - iL) / 2 + iL);
587 iComp = iNameInsensitiveCompare(spRule[iM].cpName, cpName);
600 static const char* cpMapVal(uint8_t ucVal){
601 static char* caVal[5] = {
"N",
"M",
"E",
"A",
"U"};
608 static void vPrintMap(
api* spApi, uint8_t* ucpMap){
611 for(lu = spApi->
luiAcharMin; lu <= spApi->luiAcharEos; lu++){
612 ucVal = ucGetMapValGen(spApi, ucpMap, spApi->
luiAcharMin, lu);
613 printf(
" %"PRIuMAX
"%s", lu, cpMapVal(ucVal));
620 static void vRuleOpen(
api* spApi,
aint uiRuleIndex) {
621 vIndent(s_uiTreeDepth);
625 static void vRuleLeaf(
api* spApi,
aint uiRuleIndex, uint8_t* ucpMap) {
627 vIndent(s_uiTreeDepth);
629 vPrintMap(spApi, ucpMap);
631 static void vRuleClose(
api* spApi,
aint uiRuleIndex) {
633 vIndent(s_uiTreeDepth);
636 vPrintMap(spApi, ucpMap);
638 static void vOpcodeOpen(
api* spApi,
api_op* spOp) {
639 vIndent(s_uiTreeDepth);
643 static void vOpcodeClose(
api* spApi,
api_op* spOp) {
645 vIndent(s_uiTreeDepth);
648 vPrintMap(spApi, ucpMap);
650 static void vIndent(
aint uiIndent) {