Debian Codegraph @

wxcrafter/src/functions_parser.cpp

raw
1 #include "functions_parser.h"
2 #include <y.tab.h>
3
4 FunctionsParser::FunctionsParser(wxcWidget::Map_t& connectedEvents, const wxString& clsname, const wxString& header)
5 : m_allEvents(connectedEvents)
6 , m_classname(clsname)
7 {
8 m_scanner.SetText(header.mb_str(wxConvUTF8).data());
9 }
10
11 FunctionsParser::~FunctionsParser() {}
12
13 //////////////////////////////////////////////////////////
14 // Macros
15 //////////////////////////////////////////////////////////
16
17 #define YYNEXT_RET_ON_EOF() \
18 { \
19 type = m_scanner.yylex(); \
20 if(type == 0) return; \
21 }
22 #define INC_DEPTH_CONTINUE() \
23 { \
24 depth++; \
25 continue; \
26 }
27
28 #define DEC_DEPTH_BREAK_IF_ZERO() \
29 { \
30 depth--; \
31 if(depth == 0) { \
32 break; \
33 } else { \
34 continue; \
35 } \
36 }
37
38 int FunctionsParser::ReadClassName(wxString& clsname)
39 {
40 clsname.clear();
41 int type = 0;
42
43 while(true) {
44 type = m_scanner.yylex();
45 if(type == 0) return 0;
46
47 if(type == IDENTIFIER) {
48 clsname = m_scanner.YYText();
49
50 } else if(type == '{' || type == ':') {
51 return type;
52
53 } else if(type == ';') {
54 // we probably encountered a forward declaration or 'friend' statement
55 clsname.Clear();
56 return (int)';';
57 }
58 }
59 return 0;
60 }
61
62 void FunctionsParser::Parse()
63 {
64 int type = 0;
65 int depth = 0;
66
67 // search for the class first
68 while(true) {
69 YYNEXT_RET_ON_EOF();
70
71 if(type == lexCLASS) {
72 // YYNEXT_RET_ON_EOF();
73
74 // read all tokens until we find a ':' or '{'
75 wxString identifierName;
76 // ReadClassName consumes all tokens from the current position
77 // until it hits a ':' or '{'
78 // the last IDENTIFIER consumed is the class name
79 type = ReadClassName(identifierName);
80 if(type == 0) return;
81
82 if(identifierName == m_classname) { break; }
83 }
84 }
85
86 if(type == '{') {
87 // we already consumed the '{' char by calling to ReadClassName()
88 depth = 1;
89
90 } else {
91 // search for the open curly brace
92 while(true) {
93 YYNEXT_RET_ON_EOF();
94 if(type == '{') {
95 depth = 1;
96 break;
97 }
98 }
99 }
100
101 while(true) {
102 YYNEXT_RET_ON_EOF();
103
104 if(type == '{') {
105 INC_DEPTH_CONTINUE();
106
107 } else if(type == '}') {
108 DEC_DEPTH_BREAK_IF_ZERO();
109
110 } else if(type == lexVOID && depth == 1) {
111 // Check the next type, we only check if detph == 1 (meaning we are still inside our class scope)
112 YYNEXT_RET_ON_EOF();
113
114 if(type == '{') {
115 INC_DEPTH_CONTINUE();
116
117 } else if(type == '}') {
118 DEC_DEPTH_BREAK_IF_ZERO();
119
120 } else if(type == IDENTIFIER) {
121 CheckIfFunctionsExists(wxString(m_scanner.YYText(), wxConvUTF8));
122 }
123 }
124 }
125 }
126
127 void FunctionsParser::CheckIfFunctionsExists(const wxString& name) { m_allEvents.erase(name); }