-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpp_output_handler.hpp
186 lines (162 loc) · 5.14 KB
/
cpp_output_handler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* ap_cpp_output_handler.hpp -
* @author: Jonathan Beard
* @version: Sun Jul 7 18:24:28 2013
*/
#ifndef _CPP_OUTPUT_HANDLER_HPP_
#define _CPP_OUTPUT_HANDLER_HPP_ 1
#include <vector>
#include <cstdint>
#include <cinttypes>
#include <string>
#include <ostream>
#include <fstream>
#include <iostream>
#include <sstream>
#include "FindLine.hpp"
#include "common.hpp"
namespace Raft{
class Data;
struct File{
File( std::string name ) : lineno( 0 ),
filename( name ){};
File( int64_t n,
std::string name ) : lineno( n ),
filename( name ){};
File( const File &f )
{
lineno = f.lineno;
filename = f.filename;
}
std::ostream& print( std::ostream &stream )
{
stream << "filename \"" << filename << "\" around line (" << (lineno) << ")";
return( stream );
}
bool SameFile( const std::string name )
{
const int8_t equal( 0 );
return( filename.compare( name ) == equal );
}
/**
* GetCurrentLine - returns the current line as a string,
* takes in a valid data struct to check for the proper
* color coding, at the moment this is ignored but will
* be added in the future.
* @param d - Data&
* @return std::string
*/
std::string GetCurrentLine( Data &d )
{
assert( filename.compare( "" ) != 0 );
#if(0)
std::ifstream input;
input.open( filename, std::ifstream::in );
int64_t curr_line( 0 );
std::string line;
while( ++curr_line <= lineno && input.good() )
{
std::getline( input, line );
}
input.close();
#endif
std::string current_parse_stream( d.get_rf_parsestream().str() );
std::string line( FindLine::findLine( filename,
lineno,
current_parse_stream ) );
d.reset_rf_parsestream();
return( line );
}
int64_t lineno;
std::string filename;
};
/**
* CPP_OutputHandler - simple class to handle
* the C Pre-processors output format for the parser,
* encapsulates everything through the AddUpdate
* methods. Callers should interact by using the
* AddUpdate to update the current file from the parser
* and use the IsHeadIncluded file to see if the current
* file was included from some other file. The
* PeekBelowHead method offers a way to get the place
* in the previous file from which this file was included,
* the Peek Head method gives a way to look at the current
* file and the current line number.
*/
class CPP_OutputHandler{
public:
/* public constructor */
CPP_OutputHandler( Data &d );
/* public destructor */
virtual ~CPP_OutputHandler();
/**
* AddUpdate - for use to update internal data structures
* when a new pragma is reached in the C Pre-processor output.
* This particular function is for use when the pragma has a
* flag associated with it.
*
* @param lineno - int64_t
* @param name - name
* @param int8_t - CPP flag
*/
void AddUpdate( int64_t lineno,
std::string name,
int8_t flag );
/**
* AddUpdate - for use to update internal data structures
* when a new pragma is reached in the C Pre-Processor output.
* This particular function is for use when the pragma has no
* flags associated with it.
*
* @param lineno - int64_t
* @param name - name
*/
void AddUpdate( int64_t lineno,
std::string name );
/**
* IncrementHead - increments the file that is at the head.
* Useful when the parser increments lines within the
* cpp output. These updates will be reflected in the
* various Peek() methods below.
*/
void IncrementHead();
/**
* IsHeadIncludedFile - returns true if the current head of
* the internal data structure is an included file from another
* file.
* @return bool - true if head is an included file
*/
bool IsHeadIncludedFile();
/**
* PeekBelowHead - returns the text representation of line number
* and file name of the file below the current head. This method
* is useful is the current head is an included file and there is
* an error in head, it might be useful for the user to know where
* the file was included from.
* @return std::string - representation of the file below head
*/
std::string PeekBelowHead();
/**
* PeekHead - returns a text representation of the line number
* and file name of the file at head.
* @return std::string - representation of the file at head
*/
std::string PeekHead();
std::string GetHeadCurrentLine();
bool IsComment();
void StartComment();
void EndComment();
protected:
virtual void add_file_object( File *f );
virtual bool has_head();
virtual File& get_head();
virtual bool has_below_head();
virtual File& get_below_head();
virtual void remove_head();
private:
std::vector< File* > cpp_access_queue;
bool comment;
Data &data;
};
} /* end namespace AP */
#endif /* END _CPP_OUTPUT_HANDLER_HPP_ */