Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
CppDemoCallbacks.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  APG Version 6.3
3  Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved
4 
5  author: Lowell D. Thomas
6  email: lowell@coasttocoastresearch.com
7  website: http://www.coasttocoastresearch.com
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see
21  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
22  or write to the Free Software Foundation, Inc.,
23  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
25 
26 #include "Apg.h"
27 #include "ApgUtilities.h"
28 #include "CppDemo.h"
29 #include <iostream>
38 void CppDemo::vDefineSyntaxCallbacks(APG_CALLBACK* sppRuleCallbacks, APG_CALLBACK* sppUdtCallbacks){
39  if(sppRuleCallbacks){
40  memset((void*)sppRuleCallbacks, 0, sizeof(APG_CALLBACK) * CppDemo::RULEID_COUNT);
41  sppRuleCallbacks[CppDemo::RULEID_CRLF] = CppDemo::uiSyn_CRLF;
42  sppRuleCallbacks[CppDemo::RULEID_HELLO] = CppDemo::uiSyn_Hello;
43  sppRuleCallbacks[CppDemo::RULEID_HELLOWORLD] = CppDemo::uiSyn_HelloWorld;
44  }
45  if(sppUdtCallbacks){
46  memset((void*)sppUdtCallbacks, 0, sizeof(APG_CALLBACK) * CppDemo::UDTID_COUNT);
47  sppUdtCallbacks[CppDemo::UDTID_U_WORLD] = CppDemo::uiSyn_u_World;
48  }
49 }
50 
51 void CppDemo::vDefineAstCallbacks(APG_CALLBACK* sppRuleCallbacks, APG_CALLBACK* sppUdtCallbacks){
52  if(sppRuleCallbacks){
53  memset((void*)sppRuleCallbacks, 0, sizeof(APG_CALLBACK) * CppDemo::RULEID_COUNT);
54  sppRuleCallbacks[CppDemo::RULEID_CRLF] = CppDemo::uiAst_CRLF;
55  sppRuleCallbacks[CppDemo::RULEID_HELLO] = CppDemo::uiAst_Hello;
56  sppRuleCallbacks[CppDemo::RULEID_HELLOWORLD] = CppDemo::uiAst_HelloWorld;
57  }
58  if(sppUdtCallbacks){
59  memset((void*)sppUdtCallbacks, 0, sizeof(APG_CALLBACK) * CppDemo::UDTID_COUNT);
60  sppUdtCallbacks[CppDemo::UDTID_U_WORLD] = CppDemo::uiAst_u_World;
61  }
62 }
63 
64 void CppDemo::vDefineAstNodes(apg_uint* uipRuleCallbacks, apg_uint* uipUdtCallbacks){
65  if(uipRuleCallbacks){
66  memset((void*)uipRuleCallbacks, 0, sizeof(apg_uint) * CppDemo::RULEID_COUNT);
67  uipRuleCallbacks[CppDemo::RULEID_CRLF] = APG_TRUE;
68  uipRuleCallbacks[CppDemo::RULEID_HELLO] = APG_TRUE;
69  uipRuleCallbacks[CppDemo::RULEID_HELLOWORLD] = APG_TRUE;
70  }
71  if(uipUdtCallbacks){
72  memset((void*)uipUdtCallbacks, 0, sizeof(apg_uint) * CppDemo::UDTID_COUNT);
73  uipUdtCallbacks[CppDemo::UDTID_U_WORLD] = APG_TRUE;
74  }
75 }
76 
77 // template for Rule syntax callback functions
78 static apg_uint uiRule(APG_CBDATA* spData, const char* cpName){
79  apg_uint uiRet = APG_FALSE;
80  const char* cpState = "<none>";
81  switch(spData->uiState){
82  case PRE_PARSE:
83  cpState = "PRE_PARSE";
84  break;
85  case NOMATCH:
86  cpState = "NOMATCH";
87  break;
88  case MATCH:
89  cpState = "MATCH";
90  break;
91  case EMPTY:
92  cpState = "EMPTY";
93  break;
94  }
95  return uiRet; // if APG_TRUE this function will override the normal parsing of this rule
96 }
97 apg_uint CppDemo::uiSyn_CRLF(APG_CBDATA* spData){return uiRule(spData, "uiRule_Crlf");}
98 apg_uint CppDemo::uiSyn_Hello(APG_CBDATA* spData){return uiRule(spData, "uiRule_Hello");}
99 apg_uint CppDemo::uiSyn_HelloWorld(APG_CBDATA* spData){return uiRule(spData, "uiRule_HelloWorld");}
100 
101 // template for UDT syntax callback functions
102 apg_uint CppDemo::uiSyn_u_World(APG_CBDATA* spData){
103  apg_uint uiRet = APG_FALSE;
104  const apg_achar* acpPhrase = spData->acpSrc + spData->uiPhraseOffset;
105  apg_uint uiMaxPhraseLength = spData->uiSrcLen - spData->uiPhraseOffset;
106  static apg_uint uiWorldLen = 5;
107  static apg_achar acaWorld[5] = {(apg_achar)'w', (apg_achar)'o', (apg_achar)'r', (apg_achar)'l', (apg_achar)'d'};
108  apg_uint uiLen;
109 
110  if(spData->uiState == PRE_PARSE){
111  uiLen = 0;
112  if(uiMaxPhraseLength >= uiWorldLen){
113  for(uiLen = 0; uiLen < uiWorldLen; uiLen++){
114  apg_achar acSrc = acpPhrase[uiLen];
115  acSrc = (acSrc >= (apg_achar)65 && acSrc <= (apg_achar)90) ? acSrc + 32 : acSrc;
116  if(acSrc != acaWorld[uiLen]){break;}
117  }
118  }
119  spData->uiPhraseLength = (uiLen == uiWorldLen) ? uiWorldLen : APG_UNDEFINED;
120  }
121  return uiRet; // ignored by parser for UDTs
122 }
123 
124 static apg_uint uiAstRule(APG_CBDATA* spData, const char* cpName){
125  apg_uint uiRet = APG_FALSE;
126  const char* cpState = "<none>";
127  switch(spData->uiState){
128  case PRE_AST:
129  cpState = "PRE_AST";
130  break;
131  case POST_AST:
132  cpState = "POST_AST";
133  break;
134  }
135  return uiRet; // if APG_TRUE this function will override the normal parsing of this rule
136 }
137 apg_uint CppDemo::uiAst_CRLF(APG_CBDATA* spData){return uiAstRule(spData, "uiAst_CRLF");}
138 apg_uint CppDemo::uiAst_Hello(APG_CBDATA* spData){return uiAstRule(spData, "uiAst_Hello");}
139 apg_uint CppDemo::uiAst_HelloWorld(APG_CBDATA* spData){return uiAstRule(spData, "uiAst_HelloWorld");}
140 
141 // template for UDT AST callback functions
142 apg_uint CppDemo::uiAst_u_World(APG_CBDATA* spData){
143  switch(spData->uiState){
144  case PRE_AST:
145  printf("uiAstTranslate: uiAst_u_World: PRE_PARSE\n");
146  break;
147  case POST_AST:
148  printf("uiAstTranslate: uiAst_u_World: POST_AST\n");
149  break;
150  }
151  return APG_TRUE; // return value ignored by parser
152 }
POST_AST
#define POST_AST
Definition: Apg.h:556
APG_CBDATA::acpSrc
const apg_achar * acpSrc
Definition: Apg.h:455
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
APG_CALLBACK
apg_uint(* APG_CALLBACK)(APG_CBDATA *spData)
Definition: Apg.h:515
APG_CBDATA::uiPhraseOffset
apg_uint uiPhraseOffset
Definition: Apg.h:457
PRE_PARSE
#define PRE_PARSE
Definition: Apg.h:526
Apg.h
Required header file for all APG-generated parsers. Contains important configuration macros and decla...
APG_CBDATA::uiPhraseLength
apg_uint uiPhraseLength
Definition: Apg.h:459
EMPTY
#define EMPTY
Definition: Apg.h:538
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
APG_CBDATA::uiState
apg_uint uiState
Definition: Apg.h:458
ApgUtilities.h
Declarations of all APG utility functions.
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
APG_CBDATA
The data structure passed to all syntax and AST callback functions, both rule and UDT.
Definition: Apg.h:453
APG_CBDATA::uiSrcLen
apg_uint uiSrcLen
Definition: Apg.h:456
APG_UNDEFINED
#define APG_UNDEFINED
Definition: Apg.h:194
MATCH
#define MATCH
Definition: Apg.h:544
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
PRE_AST
#define PRE_AST
Definition: Apg.h:550
NOMATCH
#define NOMATCH
Definition: Apg.h:532
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/licenses.html or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.