xref: /Universal-ctags/parsers/cxx/cxx_token.h (revision d325d3e6cf56987737f10b7c02f7aa9821681a22)
1 #ifndef ctags_cxx_token_h_
2 #define ctags_cxx_token_h_
3 /*
4 *   Copyright (c) 2016, Szymon Tomasz Stefanek
5 *
6 *   This source code is released for free distribution under the terms of the
7 *   GNU General Public License version 2 or (at your option) any later version.
8 *
9 *   This module contains functions for parsing and scanning C++ source files
10 */
11 
12 #include "general.h"
13 #include "mio.h"
14 #include "vstring.h"
15 
16 #include "cxx_keyword.h"
17 
18 // We assume that the compiler is capable of generating 32 bit wide enums
19 // This is used as enumeration but also as mask in several functions.
20 //
21 // DON'T FORGET TO RUN misc/gencxxtypedumper.sh after updating the elements.
22 //
23 enum CXXTokenType
24 {
25 	CXXTokenTypeEOF = 1,
26 	CXXTokenTypeIdentifier = (1 << 1),
27 	CXXTokenTypeKeyword = (1 << 2),
28 	CXXTokenTypeNumber = (1 << 3),
29 	CXXTokenTypeSingleColon = (1 << 4),
30 	CXXTokenTypeMultipleColons = (1 << 5),
31 	CXXTokenTypeSemicolon = (1 << 6),
32 	CXXTokenTypeComma = (1 << 7), // ,
33 	CXXTokenTypeAssignment = (1 << 8), // =
34 	CXXTokenTypeOperator = (1 << 9), // != == += ++ -= -- / whatever
35 	CXXTokenTypeUnknown = (1 << 10),
36 	CXXTokenTypeDotOperator = (1 << 11), // .
37 	CXXTokenTypePointerOperator = (1 << 12), // ->
38 	CXXTokenTypeStringConstant = (1 << 13),
39 	CXXTokenTypeStar = (1 << 14), // *
40 	CXXTokenTypeAnd = (1 << 15), // &
41 	CXXTokenTypeMultipleAnds = (1 << 16), // &&
42 	CXXTokenTypeCharacterConstant = (1 << 17),
43 	CXXTokenTypeMultipleDots = (1 << 18), // ...
44 
45 	// These must come in pairs. Note that the opening
46 	// tokens can be shifted by 4 to get the matching closing
47 	// tokens can be shifted by 8 to get the matching subchain marker below
48 	CXXTokenTypeOpeningBracket = (1 << 19), // {
49 	CXXTokenTypeOpeningParenthesis = (1 << 20), // (
50 	CXXTokenTypeOpeningSquareParenthesis = (1 << 21), // [
51 	CXXTokenTypeSmallerThanSign = (1 << 22), // <
52 
53 	CXXTokenTypeClosingBracket = (1 << 23), // }
54 	CXXTokenTypeClosingParenthesis = (1 << 24), // )
55 	CXXTokenTypeClosingSquareParenthesis = (1 << 25), // ]
56 	CXXTokenTypeGreaterThanSign = (1 << 26), // >
57 
58 	// Subchains (caution: read the comment above about CXXTokenTypeOpeningBracket)
59 	CXXTokenTypeBracketChain = (1 << 27), // {...}
60 	CXXTokenTypeParenthesisChain = (1 << 28), // (...)
61 	CXXTokenTypeSquareParenthesisChain = (1 << 29), // [...]
62 	CXXTokenTypeAngleBracketChain = (1 << 30), // <...>
63 };
64 
65 // Forward decl
66 typedef struct _CXXTokenChain CXXTokenChain;
67 
68 
69 
70 typedef struct _CXXToken
71 {
72 	enum CXXTokenType eType;
73 	vString * pszWord;
74 	CXXKeyword eKeyword;
75 	CXXTokenChain * pChain; // this is NOT the parent chain!
76 	bool bFollowedBySpace;
77 
78 	int iLineNumber;
79 	MIOPos oFilePosition;
80 
81 	struct _CXXToken * pNext;
82 	struct _CXXToken * pPrev;
83 
84 	// These members are used by the scope management functions to store
85 	// scope information. Only cxxScope* functions can make sense of it.
86 	// In other contexts these are simply left
87 	// uninitialized and must be treated as undefined.
88 	unsigned char uInternalScopeType;
89 	unsigned char uInternalScopeAccess;
90 } CXXToken;
91 
92 CXXToken * cxxTokenCreate(void);
93 void cxxTokenDestroy(CXXToken * t);
94 
95 // A shortcut for quickly creating a fake token.
96 CXXToken * cxxTokenCopy(CXXToken *pToken);
97 
98 // A shortcut for quickly creating keyword tokens.
99 CXXToken * cxxTokenCreateKeyword(int iLineNumber,MIOPos oFilePosition,CXXKeyword eKeyword);
100 
101 CXXToken * cxxTokenCreateAnonymousIdentifier(unsigned int uTagKind);
102 
103 #define cxxTokenTypeIsOneOf(_pToken,_uTypes) (_pToken->eType & (_uTypes))
104 #define cxxTokenTypeIs(_pToken,_eType) (_pToken->eType == _eType)
105 #define cxxTokenIsKeyword(_pToken,_eKeyword) \
106 		( \
107 			(_pToken->eType == CXXTokenTypeKeyword) && \
108 			(_pToken->eKeyword == _eKeyword) \
109 		)
110 #define cxxTokenIsNonConstantKeyword(_pToken) \
111 		( \
112 			cxxTokenTypeIs(_pToken,CXXTokenTypeKeyword) && \
113 			(!cxxKeywordIsConstant(_pToken->eKeyword)) \
114 		)
115 
116 // FIXME: Bad argument order
117 void cxxTokenAppendToString(vString * s,CXXToken * t);
118 
119 void cxxTokenAPIInit(void);
120 void cxxTokenAPINewFile(void);
121 void cxxTokenAPIDone(void);
122 
123 void cxxTokenReduceBackward (CXXToken *pStart);
124 
125 #endif //!ctags_cxx_token_h_
126