| The Gaudi Framework v30r0 (c919700c)

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