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 
58 void *newToken       (struct tokenInfoClass *klass);
59 void *newTokenFull   (struct tokenInfoClass *klass, void *data);
60 void *newTokenByCopying (tokenInfo *src);
61 void *newTokenByCopyingFull (tokenInfo *src, void *data);
62 
63 void  flashTokenBacklog (struct tokenInfoClass *klass);
64 void  tokenDelete    (tokenInfo *token);
65 
66 void tokenReadFull   (tokenInfo *token, void *data);
67 void tokenRead       (tokenInfo *token);
68 void tokenUnreadFull (tokenInfo *token, void *data); /* DATA passed to copy method internally. */
69 void tokenUnread     (tokenInfo *token);
70 
71 
72 void tokenCopyFull   (tokenInfo *dest, tokenInfo *src, void *data);
73 void tokenCopy       (tokenInfo *dest, tokenInfo *src);
74 
75 /* Helper macro & functions */
76 
77 #define tokenIsType(TKN,T)     ((TKN)->type == TOKEN_##T)
78 #define tokenIsTypeVal(TKN,TV)   ((TKN)->type == (TV))
79 #define tokenIsKeyword(TKN,K)  ((TKN)->type == TKN->klass->typeForKeyword \
80 									&& (TKN)->keyword == KEYWORD_##K)
81 #define tokenIsEOF(TKN)      ((TKN)->type == (TKN)->klass->typeForEOF)
82 
83 #define tokenString(TKN)       (vStringValue ((TKN)->string))
84 #define tokenPutc(TKN,C)       (vStringPut ((TKN)->string, C))
85 #define tokenCat(TKN,VS)       (vStringCat ((TKN)->string, VS))
86 #define tokenCatS(TKN,S)       (vStringCatS ((TKN)->string, S))
87 #define tokenLast(TKN)         (vStringIsEmpty((TKN)->string)? '\0': vStringLast((TKN)->string))
88 
89 /* return true if t is found. In that case token holds an
90    language object type t.
91    return false if it reaches EOF. */
92 bool tokenSkipToType (tokenInfo *token, tokenType t);
93 bool tokenSkipToTypeFull (tokenInfo *token, tokenType t, void *data);
94 bool tokenSkipOverPair (tokenInfo *token);
95 bool tokenSkipOverPairFull (tokenInfo *token, void *data);
96 
97 #endif
98