| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Parser.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // STD:
3 // ============================================================================
4 #include <fstream>
5 // ============================================================================
6 // BOOST:
7 // ============================================================================
8 #include <boost/format.hpp>
9 #include <boost/filesystem.hpp>
10 // ============================================================================
11 #include "Parser.h"
12 #include "Messages.h"
13 #include "Node.h"
14 #include "Grammar.h"
15 #include "Iterator.h"
16 #include "IncludedFiles.h"
17 #include "Utils.h"
18 // ============================================================================
19 // Gaudi:
20 // ============================================================================
21 #include "GaudiKernel/PathResolver.h"
22 // ============================================================================
23 namespace classic = boost::spirit::classic;
24 namespace bf = boost::filesystem;
25 namespace gp= Gaudi::Parsers;
26 namespace gpu= Gaudi::Parsers::Utils;
27 namespace qi = boost::spirit::qi;
28 // ============================================================================
29 namespace {
30 // ============================================================================
31 void GetLastLineAndColumn(std::ifstream& ifs, int& line, int& column) {
32 int n = 0;
33 std::string str;
34 while(!ifs.eof()) {
35 getline(ifs, str);
36 ++n;
37 }
38 line = n;
39 column = str.length()+1;
40 ifs.clear();
41 ifs.seekg(0, ifs.beg);
42 }
43
44 template<typename Grammar>
45 bool ParseStream(std::ifstream& stream,
46 const std::string& stream_name, gp::Messages* messages,
47 gp::Node* root) {
48
49 int last_line, last_column;
50
51 GetLastLineAndColumn(stream, last_line, last_column);
52
53 std::string input((std::istreambuf_iterator<char>(stream)),
54 std::istreambuf_iterator<char>());
55
56 BaseIterator in_begin(input.begin());
57 // convert input iterator to forward iterator, usable by spirit parser
58 ForwardIterator fwd_begin =
59 boost::spirit::make_default_multi_pass(in_begin);
60 ForwardIterator fwd_end;
61
62 // wrap forward iterator with position iterator, to record the position
63
64 Iterator position_begin(fwd_begin, fwd_end, stream_name);
65 Iterator position_end;
66
67 Grammar gr;
68 gp::SkipperGrammar<Iterator> skipper;
69
70 root->value = stream_name;
71 bool result = qi::phrase_parse(position_begin,
72 position_end, gr, skipper, *root);
73
74 const IteratorPosition& pos = position_begin.get_position();
75 if (result && (pos.line == last_line) && (pos.column == last_column)) {
76 return true;
77 }
78
79 messages->AddError(gp::Position(stream_name, pos.line,
80 pos.column),"parse error");
81 return false;
82 }
83
84 // ============================================================================
85 template<typename Grammar>
86 bool ParseFile(const gp::Position& from, const std::string& filename,
87 const std::string& search_path,
88 gp::IncludedFiles* included, gp::Messages* messages,gp::Node* root) {
89 std::string search_path_with_current_dir =
90 gpu::replaceEnvironments(search_path);
91 if (!from.filename().empty()) { // Add current file directory to search_path
92 bf::path file_path(from.filename());
93 search_path_with_current_dir = file_path.parent_path().string()
94 + (search_path_with_current_dir.empty()?""
95 :("," + search_path_with_current_dir));
96 }
97 std::string absolute_path = System::PathResolver::find_file_from_list(
98 gpu::replaceEnvironments(filename), search_path_with_current_dir);
99
100 if (absolute_path.empty()) {
101 messages->AddError(from, "Couldn't find a file " + filename+
102 " in search path '"+ search_path_with_current_dir+"'");
103 return false;
104 }
105 const gp::Position* included_from;
106 if (!included->GetPosition(absolute_path, &included_from)){
107 included->AddFile(absolute_path, from);
108 std::ifstream file{absolute_path};
109 if (!file.is_open()) {
110 messages->AddError(from, "Couldn't open a file "+filename);
111 return false;
112 }
113 return ParseStream<Grammar>(file, absolute_path, messages, root);
114 } else {
115 assert(included_from != NULL);
116 messages->AddWarning(from,
117 str(boost::format("File %1% already included from %2%")
118 % absolute_path % included_from->ToString()));
119 return true;
120 }
121 }
122 // ============================================================================
123 }
124 // ============================================================================
125 bool gp::Parse(const std::string& filename,
126 const std::string& search_path, IncludedFiles* included,
127 Messages* messages, Node* root){
128 return Parse(Position(), filename, search_path, included, messages,
129 root);
130 }
131 // ============================================================================
132 bool gp::Parse(const Position& from,
133 const std::string& filename,
134 const std::string& search_path, IncludedFiles* included,
135 Messages* messages, Node* root) {
136 using Grammar = FileGrammar<Iterator,SkipperGrammar<Iterator>>;
137 return ParseFile<Grammar>(from, filename, search_path, included,
138 messages, root);
139 }
140
141 // ============================================================================
142 bool gp::ParseUnits(const Position& from,
143 const std::string& filename,
144 const std::string& search_path, IncludedFiles* included,
145 Messages* messages, Node* root) {
146 using Grammar = UnitsGrammar<Iterator,SkipperGrammar<Iterator>>;
147 return ParseFile<Grammar>(from, filename, search_path, included,
148 messages, root);
149 }
150 // ============================================================================
IncludedFiles.h
std::string::empty
T empty(T...args)
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Parser.h
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:772
Gaudi::Parsers::ParseUnits
bool ParseUnits(const Position &from, const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:142
std::istreambuf_iterator
Messages.h
graphanalysis.gr
gr
Definition: graphanalysis.py:110
System::PathResolver::find_file_from_list
static std::string find_file_from_list(const std::string &logical_file_name, const std::string &search_list, SearchType search_type=LocalSearch)
Definition: PathResolver.cpp:139
std::getline
T getline(T...args)
BaseIterator
std::string::const_iterator BaseIterator
Definition: Iterator.h:16
graphanalysis.filename
string filename
Definition: graphanalysis.py:109
Gaudi::Parsers::Utils::replaceEnvironments
std::string replaceEnvironments(const std::string &input)
Definition: Utils.cpp:18
std::ifstream::seekg
T seekg(T...args)
ForwardIterator
boost::spirit::multi_pass< BaseIterator > ForwardIterator
Definition: Iterator.h:17
std::string
STL class.
std::ifstream::eof
T eof(T...args)
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:217
Utils.h
Gaudi::Parsers::Utils
Definition: Utils.h:9
Gaudi::Parsers::Node
Definition: Node.h:24
Gaudi::Parsers::Parse
bool Parse(const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:125
Gaudi::Parsers::FileGrammar
Definition: Grammar.h:148
Node.h
Gaudi::Parsers::Messages
Definition: Messages.h:20
Gaudi::Parsers::Position
Definition: Position.h:12
boost::spirit::qi
Definition: iter_pos.hpp:67
std::ifstream::clear
T clear(T...args)
Iterator.h
Gaudi::Parsers::UnitsGrammar
Definition: Grammar.h:128
std::string::length
T length(T...args)
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
Gaudi::Parsers
Definition: DODBasicMapper.cpp:7
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:14
PathResolver.h
Gaudi::Parsers::IncludedFiles
Definition: IncludedFiles.h:15
IteratorPosition
boost::spirit::classic::file_position_base< std::string > IteratorPosition
Definition: Iterator.h:20
std::ifstream
STL class.
plotSpeedupsPyRoot.line
line
Definition: plotSpeedupsPyRoot.py:164
GaudiPython.GaudiAlgs.column
column
Definition: GaudiAlgs.py:1071
Grammar.h