00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _TINYAP_H__
00020 #define _TINYAP_H__
00021
00024 #define TINYAP_VERSION PACKAGE_VERSION
00025
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #endif
00051 #ifndef _TINYAP_AST_H__
00052 #endif
00053 typedef struct _tinyap_t* tinyap_t;
00054 #ifndef _WAST_DEFINED
00055 typedef struct _walkable_ast_t* wast_t;
00056 typedef struct _wast_iter_t* wast_iterator_t;
00057 #define _WAST_DEFINED
00058 #endif
00059
00060 #include "bootstrap.h"
00061
00064
00066 void tinyap_init();
00067
00071 tinyap_t tinyap_new();
00072
00074 const char* tinyap_get_whitespace(tinyap_t);
00076 void tinyap_set_whitespace(tinyap_t,const char*);
00078 void tinyap_set_whitespace_regexp(tinyap_t,const char*);
00079
00081 const char* tinyap_get_grammar(tinyap_t);
00083 void tinyap_set_grammar(tinyap_t,const char*);
00084
00086 ast_node_t tinyap_get_grammar_ast(tinyap_t);
00088 void tinyap_set_grammar_ast(tinyap_t,ast_node_t);
00089
00091 const char* tinyap_get_source_file(tinyap_t);
00093 const char* tinyap_get_source_buffer(tinyap_t);
00095 unsigned int tinyap_get_source_buffer_length(tinyap_t);
00099 void tinyap_set_source_file(tinyap_t,const char*);
00101 void tinyap_set_source_buffer(tinyap_t,const char*,const unsigned int);
00102
00103
00104
00108 int tinyap_parse(tinyap_t);
00112 int tinyap_parse_as_grammar(tinyap_t);
00113
00118 int tinyap_parsed_ok(const tinyap_t);
00119
00122 ast_node_t tinyap_get_output(const tinyap_t);
00123
00126 int tinyap_get_error_col(const tinyap_t);
00129 int tinyap_get_error_row(const tinyap_t);
00130
00133 const char* tinyap_get_error(const tinyap_t);
00134
00137 void tinyap_delete(tinyap_t);
00138
00141 void tinyap_plug(tinyap_t parser, const char*plugin, const char*plug);
00142
00145 void tinyap_plug_node(tinyap_t parser, ast_node_t pin, const char*plugin, const char* plug);
00146
00149 void tinyap_append_grammar(tinyap_t parser, ast_node_t supp);
00156 int tinyap_node_is_nil(const ast_node_t);
00160 int tinyap_node_is_list(const ast_node_t);
00163 unsigned int tinyap_list_get_size(const ast_node_t);
00166 ast_node_t tinyap_list_get_element(const ast_node_t,int);
00170 int tinyap_node_is_string(const ast_node_t);
00173 const char* tinyap_node_get_string(const ast_node_t);
00176 ast_node_t tinyap_node_get_operand(const ast_node_t,int);
00179 int tinyap_node_get_operand_count(const ast_node_t);
00182 const char* tinyap_node_get_operator(const ast_node_t);
00183
00186 int tinyap_node_get_row(const ast_node_t);
00189 int tinyap_node_get_col(const ast_node_t);
00190
00193 void tinyap_serialize_to_file(const ast_node_t,const char*);
00196 const char* tinyap_serialize_to_string(const ast_node_t);
00197
00201 wast_t tinyap_make_wast(const ast_node_t);
00202
00206 ast_node_t tinyap_make_ast(const wast_t);
00207
00210 void tinyap_free_wast(const wast_t);
00211
00214 wast_iterator_t tinyap_wi_new(const wast_t);
00215 #define wi_new tinyap_wi_new
00216
00219 wast_iterator_t tinyap_wi_reset(wast_iterator_t);
00220 #define wi_reset tinyap_wi_reset
00221
00224 void tinyap_wi_delete(wast_iterator_t);
00225 #define wi_delete tinyap_wi_delete
00226
00229 wast_t tinyap_wi_node(wast_iterator_t);
00230 #define wi_node tinyap_wi_node
00231
00234 wast_iterator_t tinyap_wi_up(wast_iterator_t);
00235 #define wi_up tinyap_wi_up
00236
00239 wast_iterator_t tinyap_wi_down(wast_iterator_t);
00240 #define wi_down tinyap_wi_down
00241
00244 wast_iterator_t tinyap_wi_next(wast_iterator_t);
00245 #define wi_next tinyap_wi_next
00246
00249 wast_iterator_t tinyap_wi_backup(wast_iterator_t);
00250 #define wi_backup tinyap_wi_backup
00251
00254 wast_iterator_t tinyap_wi_restore(wast_iterator_t);
00255 #define wi_restore tinyap_wi_restore
00256
00259 wast_iterator_t tinyap_wi_validate(wast_iterator_t);
00260 #define wi_validate tinyap_wi_validate
00261
00264 wast_t tinyap_wi_root(wast_iterator_t);
00265 #define wi_root tinyap_wi_root
00266
00269 wast_t tinyap_wi_parent(wast_iterator_t);
00270 #define wi_root tinyap_wi_root
00271
00274 wast_iterator_t tinyap_wi_dup(const wast_iterator_t);
00275 #define wi_dup tinyap_wi_dup
00276
00279 int tinyap_wi_on_root(wast_iterator_t);
00280 #define wi_on_root tinyap_wi_on_root
00281
00284 int tinyap_wi_on_leaf(wast_iterator_t);
00285 #define wi_on_leaf tinyap_wi_on_leaf
00286
00289 int tinyap_wi_has_next(wast_iterator_t);
00290 #define wi_has_next tinyap_wi_has_next
00291
00294 const char* tinyap_unparse(wast_t grammar, wast_t ast);
00295
00299 void* tinyap_walk_output(const char* pilot_name, void* pilot_init_data);
00300
00304 void* tinyap_walk(const wast_t subtree, const char* pilot_name, void* pilot_init_data);
00307 #ifdef __cplusplus
00308 }
00309
00310 namespace TinyaP {
00311
00312 class AstNode {
00313 public:
00314 AstNode(ast_node_t ptr)
00315 { handle=ptr; initCache(ptr,0); }
00316 AstNode()
00317 { handle=0; cache=0; }
00318 ~AstNode()
00319 { if(cache) delete[] cache; }
00320 bool isNil() const
00321 { return tinyap_node_is_nil(handle); }
00322 bool isList() const
00323 { return tinyap_node_is_list(handle); }
00324 bool isOp() const
00325 { return tinyap_node_is_list(handle)&&tinyap_node_is_string(tinyap_list_get_element(handle,0)); }
00326 bool isString() const
00327 { return tinyap_node_is_string(handle); }
00328 const char* getString() const
00329 { return tinyap_node_get_string(handle); }
00330 AstNode* getOperand(const int n) const
00331 { return cache[n+1]; }
00332 int getOperandCount() const
00333 { return cache_sz-1; }
00334 const char* getOperator() const
00335 { return tinyap_node_get_string(tinyap_list_get_element(handle,0)); }
00336 AstNode* getElement(const int n) const
00337 { return cache[n]; }
00338 const char*toString() const
00339 { return tinyap_serialize_to_string(handle); }
00340 void toFile(const char*fnam) const
00341 { return tinyap_serialize_to_file(handle,fnam); }
00342
00343 int getRow() const
00344 { return tinyap_node_get_row(handle); }
00345
00346 int getCol() const
00347 { return tinyap_node_get_col(handle); }
00348
00349 ast_node_t getHandle() const
00350 { return handle; }
00351 private:
00352 ast_node_t handle;
00353 AstNode**cache;
00354 int cache_sz;
00355 void initCache(ast_node_t ptr,int length)
00356 {
00357 if(tinyap_node_is_string(ptr))
00358 {
00359 if(length==0)
00360 {
00361 cache=0;
00362 } else
00363 {
00364 cache_sz=length+1;
00365 cache=new AstNode* [cache_sz];
00366 cache[length]=new AstNode(ptr);
00367 }
00368 } else if(tinyap_node_is_list(ptr))
00369 {
00370 initCache(tinyap_list_get_element(ptr,1),length+1);
00371 } else if(ptr==0)
00372 {
00373 cache_sz=length;
00374 cache = new AstNode* [cache_sz];
00375 }
00376 }
00377 };
00378
00379 class Parser {
00380 public: Parser()
00384 { handle=tinyap_new(); grammar=output=0; }
00385 static Parser& New()
00390 { return *new Parser(); }
00391 ~Parser()
00394 {
00395 tinyap_delete(handle);
00396 if(grammar) delete grammar;
00397 if(output) delete output;
00398 }
00399
00400 const char*getGrammar() const
00401 { return tinyap_get_grammar(handle); }
00402
00403 Parser& setGrammar(const char*gs)
00404 {
00405 tinyap_set_grammar(handle,gs);
00406 if(grammar) delete grammar;
00407 grammar=new AstNode(tinyap_get_grammar_ast(handle));
00408 return *this;
00409 }
00410
00411 const char*getWhitespace() const
00412 { return tinyap_get_whitespace(handle); }
00413
00414 Parser& setWhitespace(const char* ws)
00415 { tinyap_set_whitespace(handle, ws); return *this; }
00416
00417 Parser& setWhitespaceRegexp(const char*wsre)
00418 { tinyap_set_whitespace_regexp(handle,wsre); return *this; }
00419
00420 AstNode* getGrammarAst() const
00421 { return grammar; }
00422
00423 Parser& setGrammarAST(const AstNode*gast)
00424 {
00425 if(grammar) delete grammar;
00426 tinyap_set_grammar_ast(handle,gast->getHandle());
00427 grammar=new AstNode(gast->getHandle());
00428 return *this;
00429 }
00430
00431 const char*getSourceFile() const
00432 { return tinyap_get_source_file(handle); }
00433
00434 const char*getSourceBuffer() const
00435 { return tinyap_get_source_buffer(handle); }
00436
00437 unsigned int getSourceBufferLength() const
00438 { return tinyap_get_source_buffer_length(handle); }
00439
00440 Parser& setSourceFile(const char*fnam)
00441 { tinyap_set_source_file(handle,fnam); return *this; }
00442
00443 Parser& setSourceBuffer(const char*buf,const unsigned int len)
00444 { tinyap_set_source_buffer(handle,buf,len); return *this; }
00445
00446 bool parse()
00447 {
00448 tinyap_parse(handle);
00449 if(output) delete output;
00450 output=new AstNode(tinyap_get_output(handle));
00451 return tinyap_parsed_ok(handle);
00452 }
00453
00454 bool parseAsGrammar()
00455 {
00456 if(tinyap_parse_as_grammar(handle))
00457 {
00458 if(grammar) delete grammar;
00459 grammar=new AstNode(tinyap_get_grammar_ast(handle));
00460 if(output) delete output;
00461 output=new AstNode(tinyap_get_output(handle));
00462 }
00463 return tinyap_parsed_ok(handle);
00464 }
00465
00466 bool parsedOK() const
00467 { return tinyap_parsed_ok(handle); }
00468
00469 AstNode* getOutput() const
00470 { return output; }
00471
00472 int getErrorCol() const
00473 { return tinyap_get_error_col(handle); }
00474
00475 int getErrorRow() const
00476 { return tinyap_get_error_row(handle); }
00477
00478 const char* getError() const
00479 { return tinyap_get_error(handle); }
00480
00481 tinyap_t getHandle() const
00482 { return handle; }
00483 private:
00484 tinyap_t handle;
00485 AstNode*grammar,*output;
00486 };
00487 }
00488
00489
00490 #endif
00491 #endif
00492
00493