1 /* (C) Copyright 2005, 2006, 2007, 2008, 2009 Stijn van Dongen 2 * 3 * This file is part of tingea. You can redistribute and/or modify tingea 4 * under the terms of the GNU General Public License; either version 3 of the 5 * License or (at your option) any later version. You should have received a 6 * copy of the GPL along with tingea, in the file COPYING. 7 */ 8 9 #ifndef tingea_tok 10 #define tingea_tok 11 12 #include <string.h> 13 #include "inttypes.h" 14 #include "types.h" 15 #include "list.h" 16 #include "ting.h" 17 18 19 20 /* This is a first sketchy attempt at some parse/tokenize routines. 21 * The scope is not very well defined yet. Should it do basic constructs 22 * only, or aim for more power, possibly aided by callbacks, and 23 * god forbid, a state description? 24 * 25 * TODO 26 * quoted strings not yet implemented! 27 * SGML not yet implemented! 28 * 29 * unify with mcxIOExpect, possibly stuff from ding.h 30 * wide chars? 31 */ 32 33 34 #define MCX_TOK_MODE_UNIX 1 /* Unix escapes, including esc newlines */ 35 #define MCX_TOK_MODE_QUOTED 2 /* Quotes delimit tokens, hide brackets */ 36 #define MCX_TOK_MODE_PLAIN 4 37 #define MCX_TOK_MODE_SGML 8 /* &code; other? */ 38 39 #define MCX_TOK_DEL_WS 16 /* only delimiting whitespace */ 40 41 42 /* Returns first character not matching fbool, NULL if none. 43 */ 44 45 char* mcxTokSkip 46 ( const char* offset 47 , int (*fbool)(int c) 48 , ofs len 49 ) ; 50 51 52 /* 53 * Accounts for nesting. 54 * Will do '}', ')', ']', '>', assuming one of several conventions. 55 */ 56 57 mcxstatus mcxTokMatch 58 ( const char* offset 59 , char** end 60 , mcxbits mode 61 , ofs len /* considered if >= 0 */ 62 ) ; 63 64 65 /* 66 * Find some token, skipping over expressions. 67 * Either *pos == NULL and retval == STATUS_FAIL 68 * or *pos != NULL and retval == STATUS_OK 69 * or *pos != NULL and retval == STATUS_DONE 70 */ 71 72 mcxstatus mcxTokFind 73 ( const char* offset 74 , char* tok /* Only tok[0] considered for now! */ 75 , char** pos 76 , mcxbits mode 77 , ofs len /* considered if >= 0 */ 78 ) ; 79 80 81 /* fixme. 82 - document; 83 - add free routine. 84 - then perhaps optify. 85 */ 86 mcxLink* mcxTokArgs 87 ( const char* str 88 , long str_len 89 , int* n_args 90 , mcxbits opts 91 ) ; 92 93 94 typedef struct 95 { mcxTing* key 96 ; mcxLink* args 97 ; mcxbits opts 98 ; 99 } mcxTokFunc ; 100 101 102 void mcxTokFuncFree 103 ( mcxTokFunc* tf 104 ) ; 105 106 mcxstatus mcxTokExpectFunc 107 ( mcxTokFunc* tf 108 , const char* str 109 , dim str_len 110 , char** z 111 , int n_min 112 , int n_max 113 , int *n_args 114 ) ; 115 116 117 #endif 118 119 120