1 /*---------------------------------------------------------------------------* 2 * SR_ExpressionParser.h * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 #ifndef __SR_EXPRESSION_PARSER_H 21 #define __SR_EXPRESSION_PARSER_H 22 23 24 25 #include "SR_SemprocPrefix.h" 26 #include "SR_SemprocDefinitions.h" 27 28 #include "SR_LexicalAnalyzer.h" 29 #include "SR_SymbolTable.h" 30 #include "SR_ExpressionEvaluator.h" 31 32 #include "ptypes.h" 33 #include "pstdio.h" 34 #include "pmemory.h" 35 36 #include "ESR_ReturnCode.h" 37 #include "ESR_Session.h" 38 #include "SR_Grammar.h" 39 40 #define SR_SemprocFunctionPtr SR_GrammarDispatchFunction 41 42 /** 43 * States in the finite state machine used for parsing. 44 */ 45 enum 46 { 47 /** 48 * Initial state for a new expression 49 */ 50 LHS_REQUIRED, 51 52 /** 53 * Equal sign required next 54 */ 55 OP_ASSIGN_REQUIRED, 56 57 /** 58 * Identifier (const or rule property reference) required next 59 */ 60 IDENTIFIER_REQUIRED, 61 62 /** 63 * Any operand other than equal sign required next 64 */ 65 OP_ANY_REQUIRED, 66 }; 67 68 /** 69 * Structure for holding function callbacks which may be registered by the programmer from the 70 * application, or internally as built-in functions (see ExpressionEvaluator.h) 71 */ 72 typedef struct FunctionCallback_t 73 { 74 /** 75 * The pointer to the function 76 */ 77 SR_SemprocFunctionPtr pfunction; 78 79 /** 80 * User data 81 */ 82 void* userData; 83 84 } 85 FunctionCallback; 86 87 88 /** 89 * The Parser. 90 */ 91 typedef struct ExpressionParser_t 92 { 93 /** 94 * The current state 95 */ 96 int state; 97 98 /** 99 * buffer for holding the token on the lhs of equal sign 100 */ 101 LCHAR lhs[MAX_STRING_LEN]; 102 103 /** 104 * buffer for holding the operator (which may be more than 1 char in the future!!!) 105 */ 106 LCHAR op[MAX_STRING_LEN]; 107 108 /** 109 * buffers for holding the idetifiers encountered on the rhs of this expression 110 */ 111 LCHAR identifiers[MAX_RHS_IDENTIFIERS][MAX_STRING_LEN]; 112 113 /** 114 * the number of identifiers encountered 115 */ 116 size_t idCount; 117 118 /** 119 * pointer to the appropriate buffer (above) for storing the next token encountered 120 * which may be an operator, identifier, etc... 121 */ 122 LCHAR *ptokenBuf; 123 124 125 /***************************/ 126 /* function callback stuff */ 127 /***************************/ 128 129 /** 130 * hashtable used for keeping track of registered function callbacks 131 */ 132 HashMap *pfunctions; 133 134 /** 135 * Array storing all the function callbacks 136 */ 137 FunctionCallback functions[MAX_FUNCTION_CALLBACKS]; 138 139 /** 140 * Pointer to the next available function callback slot in the array 141 */ 142 FunctionCallback *next; 143 144 /** 145 * Pointer to the current function to carry out in this expression (only one per expression !!!) 146 * Nesting of functions is NOT SUPPORTED 147 */ 148 SR_SemprocFunctionPtr pfunction; 149 150 /** 151 * Reference to current user data 152 */ 153 void* userData; 154 155 /** 156 * The current function name 157 */ 158 LCHAR functionName[MAX_STRING_LEN]; 159 160 /** 161 * Indicates when a function needs to be executed at the end of a statement. 162 */ 163 ESR_BOOL needToExecuteFunction; 164 } 165 ExpressionParser; 166 167 168 /** 169 * Create and Initialize. 170 * 171 * @param self pointer to the newly created parser 172 */ 173 SREC_SEMPROC_API ESR_ReturnCode EP_Init(ExpressionParser **self); 174 175 /** 176 * Free. 177 * 178 * @param self pointer to the parser 179 */ 180 SREC_SEMPROC_API ESR_ReturnCode EP_Free(ExpressionParser *self); 181 182 /** 183 * Do the parsing of the script. 184 * @param self pointer to the parser 185 * @param lexAnalyzer pointer to the lexical analyzer where the parser gets tokens from 186 * @param symtable pointer to the symbol table where the parser gets/sets key-value pairs 187 * @param evaluator pointer to the expression evaluator where calls functiosn to evaulate expressions 188 * @param hashmap pointer to a hashmap used to store the results of processing 189 */ 190 SREC_SEMPROC_API ESR_ReturnCode EP_parse(ExpressionParser* self, LexicalAnalyzer* lexAnalyzer, 191 SymbolTable* symtable, ExpressionEvaluator* evaluator, 192 HashMap** hashmap); 193 194 /** 195 * Register a function. 196 * @param self pointer to the parser 197 * @param name name of the function, as it will appear in the script 198 * @param data User data 199 * @param pfunction pointer to the function 200 * @return ESR_SUCCESS 201 */ 202 SREC_SEMPROC_API ESR_ReturnCode EP_RegisterFunction(ExpressionParser* self, const LCHAR* name, void* data, SR_SemprocFunctionPtr pfunction); 203 204 /** 205 * Lookup pointer to a registered function. 206 * @param self pointer to the parser 207 * @param name name of the function, as it will appear in the script 208 * @param pfunction pointer to the function 209 */ 210 SREC_SEMPROC_API ESR_ReturnCode EP_LookUpFunction(ExpressionParser* self, LCHAR* name, void** data, SR_SemprocFunctionPtr* pfunction); 211 212 213 #endif 214