1 /* 2 * Copyright (c) 2016, Masatake YAMATO <yamato@redhat.com> 3 * Copyright (c) 2016, Red Hat, Inc. 4 * 5 * This source code is released for free distribution under the terms of the 6 * GNU General Public License version 2 or (at your option) any later version. 7 * 8 */ 9 10 #include "general.h" /* must always come first */ 11 #include "mio.h" 12 #include "objpool.h" 13 #include "vstring.h" 14 15 #ifndef CTAGS_MAIN_TOKEN_H 16 #define CTAGS_MAIN_TOKEN_H 17 18 struct tokenClass; 19 struct tokenTypePair; 20 21 typedef short tokenType; 22 typedef short tokenKeyword; 23 24 typedef struct sTokenInfo { 25 tokenType type; 26 tokenKeyword keyword; 27 vString *string; 28 struct tokenInfoClass *klass; 29 unsigned long lineNumber; 30 MIOPos filePosition; 31 } tokenInfo; 32 33 struct tokenTypePair { 34 tokenType start; 35 tokenType end; 36 }; 37 38 #define TOKEN(X) ((tokenInfo *)X) 39 40 struct tokenInfoClass { 41 unsigned int nPreAlloc; 42 tokenType typeForUndefined; 43 tokenKeyword keywordNone; 44 tokenType typeForKeyword; 45 tokenType typeForEOF; 46 size_t extraSpace; 47 struct tokenTypePair *pairs; 48 unsigned int pairCount; 49 void (*init) (tokenInfo *token, void *data); 50 void (*read) (tokenInfo *token, void *data); 51 void (*clear) (tokenInfo *token); 52 void (*delete) (tokenInfo *token); 53 void (*copy) (tokenInfo *dest, tokenInfo *src, void *data); 54 objPool *pool; 55 ptrArray *backlog; 56 57 /* read_counter is incremented every time when reading a 58 * new token from the input stream unless the new token is EOF. 59 * 60 * When filling a tokenInfo from an entry in the backlog, we don't 61 * regard it as "reading a new token". 62 */ 63 int read_counter; 64 }; 65 66 void *newToken (struct tokenInfoClass *klass); 67 void *newTokenFull (struct tokenInfoClass *klass, void *data); 68 void *newTokenByCopying (tokenInfo *src); 69 void *newTokenByCopyingFull (tokenInfo *src, void *data); 70 71 void flashTokenBacklog (struct tokenInfoClass *klass); 72 void tokenDelete (tokenInfo *token); 73 74 void tokenReadFull (tokenInfo *token, void *data); 75 void tokenRead (tokenInfo *token); 76 void tokenUnreadFull (tokenInfo *token, void *data); /* DATA passed to copy method internally. */ 77 void tokenUnread (tokenInfo *token); 78 79 80 void tokenCopyFull (tokenInfo *dest, tokenInfo *src, void *data); 81 void tokenCopy (tokenInfo *dest, tokenInfo *src); 82 83 /* Helper macro & functions */ 84 85 #define tokenIsType(TKN,T) ((TKN)->type == TOKEN_##T) 86 #define tokenIsTypeVal(TKN,TV) ((TKN)->type == (TV)) 87 #define tokenIsKeyword(TKN,K) ((TKN)->type == TKN->klass->typeForKeyword \ 88 && (TKN)->keyword == KEYWORD_##K) 89 #define tokenIsEOF(TKN) ((TKN)->type == (TKN)->klass->typeForEOF) 90 91 #define tokenString(TKN) (vStringValue ((TKN)->string)) 92 #define tokenPutc(TKN,C) (vStringPut ((TKN)->string, C)) 93 #define tokenCat(TKN,VS) (vStringCat ((TKN)->string, VS)) 94 #define tokenCatS(TKN,S) (vStringCatS ((TKN)->string, S)) 95 #define tokenLast(TKN) (vStringIsEmpty((TKN)->string)? '\0': vStringLast((TKN)->string)) 96 97 /* return true if t is found. In that case token holds an 98 language object type t. 99 return false if it reaches EOF. */ 100 bool tokenSkipToType (tokenInfo *token, tokenType t); 101 bool tokenSkipToTypeFull (tokenInfo *token, tokenType t, void *data); 102 bool tokenSkipOverPair (tokenInfo *token); 103 bool tokenSkipOverPairFull (tokenInfo *token, void *data); 104 105 #endif 106