The Gaudi Framework  v29r0 (ff2e7097)
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 // ============================================================================
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 
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 {
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 {
138  return ParseFile<Grammar>( from, filename, search_path, included, messages, root );
139 }
140 // ============================================================================
T empty(T...args)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
bool ParseUnits(const Position &from, const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:134
static std::string find_file_from_list(const std::string &logical_file_name, const std::string &search_list, SearchType search_type=LocalSearch)
T getline(T...args)
std::string::const_iterator BaseIterator
Definition: Iterator.h:16
std::string replaceEnvironments(const std::string &input)
Definition: Utils.cpp:18
T seekg(T...args)
boost::spirit::multi_pass< BaseIterator > ForwardIterator
Definition: Iterator.h:17
STL class.
T eof(T...args)
bool Parse(const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:120
T clear(T...args)
T length(T...args)
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
boost::spirit::classic::file_position_base< std::string > IteratorPosition
Definition: Iterator.h:19
STL class.