exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
protoGrammars.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
17 #define RULE_EXTENSION_FACTOR 20
18 #define PRODUTION_EXTENSION_FACTOR 20
19 
20 #include "protoGrammars.h"
21 #include "memManagement.h"
22 #include "ioUtil.h"
23 #include "dynamicArray.h"
24 
26 {
27  pg->contentIndex = 0;
28 
29  return createDynArray(&pg->dynArray, sizeof(ProtoRuleEntry), rulesDim);
30 }
31 
33 {
34  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
35  Index ruleId;
36 
37  TRY(addEmptyDynEntry(&pg->dynArray, (void **) ruleEntry, &ruleId));
38 
39  return createDynArray(&((*ruleEntry)->dynArray), sizeof(Production), prodDim);
40 }
41 
42 errorCode addProduction(ProtoRuleEntry* ruleEntry, EventType eventType, Index typeId, QNameID qnameID, SmallIndex nonTermID)
43 {
44  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
45  Production *newProd;
46  Index newProdId;
47 
48  TRY(addEmptyDynEntry(&ruleEntry->dynArray, (void**)&newProd, &newProdId));
49 
50  SET_PROD_EXI_EVENT(newProd->content, eventType);
51  newProd->typeId = typeId;
52  newProd->qnameId = qnameID;
53  SET_PROD_NON_TERM(newProd->content, nonTermID);
54 
55  return EXIP_OK;
56 }
57 
59 {
60  Index ruleIter;
61  Index prodIter;
62  uint16_t attrCount;
63  boolean hasEE;
64 
65  exiGrammar->props = 0;
66  SET_SCHEMA_GR(exiGrammar->props);
67  SET_CONTENT_INDEX(exiGrammar->props, pg->contentIndex);
68  exiGrammar->count = pg->count;
69 
70  exiGrammar->rule = (GrammarRule*) memManagedAllocate(memlist, sizeof(GrammarRule)*(pg->count));
71  if(exiGrammar->rule == NULL)
73 
74  for(ruleIter = 0; ruleIter < pg->count; ruleIter++)
75  {
76  attrCount = 0;
77  hasEE = FALSE;
78 
79  exiGrammar->rule[ruleIter].production = (Production*) memManagedAllocate(memlist, sizeof(Production)*pg->rule[ruleIter].count);
80  if(exiGrammar->rule[ruleIter].production == NULL)
82 
83  exiGrammar->rule[ruleIter].pCount = pg->rule[ruleIter].count;
84  exiGrammar->rule[ruleIter].meta = 0;
85 
86  for(prodIter = 0; prodIter < pg->rule[ruleIter].count; prodIter++)
87  {
88  if(GET_PROD_EXI_EVENT_CLASS(pg->rule[ruleIter].prod[prodIter].content) == EVENT_AT_CLASS)
89  attrCount++;
90  else if(GET_PROD_EXI_EVENT(pg->rule[ruleIter].prod[prodIter].content) == EVENT_EE)
91  hasEE = TRUE;
92  exiGrammar->rule[ruleIter].production[prodIter] = pg->rule[ruleIter].prod[prodIter];
93  }
94 
95  RULE_SET_AT_COUNT(exiGrammar->rule[ruleIter].meta, attrCount);
96  if(hasEE)
97  RULE_SET_CONTAIN_EE(exiGrammar->rule[ruleIter].meta);
98  }
99 
100  return EXIP_OK;
101 }
102 
104 {
105  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
106  ProtoRuleEntry* pRuleEntry;
107  Index i;
108  Index j;
109 
110  TRY(createProtoGrammar(src->count, dest));
111 
112  dest->contentIndex = src->contentIndex;
113  for (i = 0; i < src->count; i++)
114  {
115  TRY(addProtoRule(dest, src->rule[i].count, &pRuleEntry));
116 
117  for (j = 0; j < src->rule[i].count; j++)
118  {
119  TRY(addProduction(pRuleEntry, GET_PROD_EXI_EVENT(src->rule[i].prod[j].content), src->rule[i].prod[j].typeId, src->rule[i].prod[j].qnameId, GET_PROD_NON_TERM(src->rule[i].prod[j].content)));
120  }
121  }
122 
123  return EXIP_OK;
124 }
125 
126 
128 {
129  Index i;
130  for (i = 0; i < pg->count; i++)
131  {
132  destroyDynArray(&pg->rule[i].dynArray);
133  }
135 }
136 
137 #if EXIP_DEBUG == ON && DEBUG_GRAMMAR_GEN == ON
138 
140 {
141  Index j = 0;
142  Production* tmpProd;
143 
144  DEBUG_MSG(INFO, EXIP_DEBUG, ("\n>RULE\n"));
145  DEBUG_MSG(INFO, EXIP_DEBUG, ("NT-%u:", (unsigned int) nonTermID));
146 
147  DEBUG_MSG(INFO, EXIP_DEBUG, ("\n"));
148 
149  for(j = 0; j < rule->count; j++)
150  {
151  tmpProd = &rule->prod[j];
152  DEBUG_MSG(INFO, EXIP_DEBUG, ("\t"));
153 
154  switch(GET_PROD_EXI_EVENT(tmpProd->content))
155  {
156  case EVENT_SD:
157  DEBUG_MSG(INFO, EXIP_DEBUG, ("SD "));
158  break;
159  case EVENT_ED:
160  DEBUG_MSG(INFO, EXIP_DEBUG, ("ED "));
161  break;
162  case EVENT_SE_QNAME:
163  DEBUG_MSG(INFO, EXIP_DEBUG, ("SE (qname: %u:%u) ", (unsigned int) tmpProd->qnameId.uriId, (unsigned int) tmpProd->qnameId.lnId));
164  break;
165  case EVENT_SE_URI:
166  DEBUG_MSG(INFO, EXIP_DEBUG, ("SE (uri) "));
167  break;
168  case EVENT_SE_ALL:
169  DEBUG_MSG(INFO, EXIP_DEBUG, ("SE (*) "));
170  break;
171  case EVENT_EE:
172  DEBUG_MSG(INFO, EXIP_DEBUG, ("EE "));
173  break;
174  case EVENT_AT_QNAME:
175  DEBUG_MSG(INFO, EXIP_DEBUG, ("AT (qname %u:%u) [%d] ", (unsigned int) tmpProd->qnameId.uriId, (unsigned int) tmpProd->qnameId.lnId, (unsigned int) tmpProd->typeId));
176  break;
177  case EVENT_AT_URI:
178  DEBUG_MSG(INFO, EXIP_DEBUG, ("AT (uri) "));
179  break;
180  case EVENT_AT_ALL:
181  DEBUG_MSG(INFO, EXIP_DEBUG, ("AT (*) "));
182  break;
183  case EVENT_CH:
184  DEBUG_MSG(INFO, EXIP_DEBUG, ("CH [%d] ", (unsigned int) tmpProd->typeId));
185  break;
186  case EVENT_NS:
187  DEBUG_MSG(INFO, EXIP_DEBUG, ("NS "));
188  break;
189  case EVENT_CM:
190  DEBUG_MSG(INFO, EXIP_DEBUG, ("CM "));
191  break;
192  case EVENT_PI:
193  DEBUG_MSG(INFO, EXIP_DEBUG, ("PI "));
194  break;
195  case EVENT_DT:
196  DEBUG_MSG(INFO, EXIP_DEBUG, ("DT "));
197  break;
198  case EVENT_ER:
199  DEBUG_MSG(INFO, EXIP_DEBUG, ("ER "));
200  break;
201  case EVENT_SC:
202  DEBUG_MSG(INFO, EXIP_DEBUG, ("SC "));
203  break;
204  case EVENT_VOID:
205  DEBUG_MSG(INFO, EXIP_DEBUG, (" "));
206  break;
207  default:
208  return EXIP_UNEXPECTED_ERROR;
209  }
211  {
212  DEBUG_MSG(INFO, EXIP_DEBUG, ("NT-%u", (unsigned int) GET_PROD_NON_TERM(tmpProd->content)));
213  }
214 
215  DEBUG_MSG(INFO, EXIP_DEBUG, ("\n"));
216  }
217  return EXIP_OK;
218 }
219 
221 {
222  Index j;
223 
224  DEBUG_MSG(INFO, EXIP_DEBUG, ("\nProtoGrammar (%d):\n", pgr->contentIndex));
225 
226  for(j = 0; j < pgr->count; j++)
227  {
228  printProtoGrammarRule(j, &pgr->rule[j]);
229  }
230 
231  return EXIP_OK;
232 }
233 
234 #endif // EXIP_DEBUG