The Gaudi Framework  v33r2 (a6f0ec87)
Parser.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "Parser.h"
13 #include "Grammar.h"
14 #include "IncludedFiles.h"
15 #include "Iterator.h"
16 #include "Messages.h"
17 #include "Node.h"
18 #include "Utils.h"
19 #include <boost/filesystem.hpp>
20 #include <fmt/format.h>
21 #include <fstream>
22 
23 // ============================================================================
24 namespace classic = boost::spirit::classic;
25 namespace bf = boost::filesystem;
26 namespace gp = Gaudi::Parsers;
27 namespace gpu = Gaudi::Parsers::Utils;
28 namespace qi = boost::spirit::qi;
29 // ============================================================================
30 namespace {
31  // ============================================================================
32  void GetLastLineAndColumn( std::ifstream& ifs, int& line, int& column ) {
33  int n = 0;
34  std::string str;
35  while ( !ifs.eof() ) {
36  getline( ifs, str );
37  ++n;
38  }
39  line = n;
40  column = str.length() + 1;
41  ifs.clear();
42  ifs.seekg( 0, ifs.beg );
43  }
44 
45  template <typename Grammar>
46  bool ParseStream( std::ifstream& stream, const std::string& stream_name, gp::Messages* messages, gp::Node* root ) {
47 
48  int last_line, last_column;
49 
50  GetLastLineAndColumn( stream, last_line, last_column );
51 
53 
54  BaseIterator in_begin( input.begin() );
55  // convert input iterator to forward iterator, usable by spirit parser
56  ForwardIterator fwd_begin = boost::spirit::make_default_multi_pass( in_begin );
57  ForwardIterator fwd_end;
58 
59  // wrap forward iterator with position iterator, to record the position
60 
61  Iterator position_begin( fwd_begin, fwd_end, stream_name );
62  Iterator position_end;
63 
64  Grammar gr;
65  gp::SkipperGrammar<Iterator> skipper;
66 
67  root->value = stream_name;
68  bool result = qi::phrase_parse( position_begin, position_end, gr, skipper, *root );
69 
70  const IteratorPosition& pos = position_begin.get_position();
71  if ( result && ( pos.line == last_line ) && ( pos.column == last_column ) ) { return true; }
72 
73  messages->AddError( gp::Position( stream_name, pos.line, pos.column ), "parse error" );
74  return false;
75  }
76 
77  // ============================================================================
78  template <typename Grammar>
79  bool ParseFile( const gp::Position& from, const std::string& filename, const std::string& search_path,
80  gp::IncludedFiles* included, gp::Messages* messages, gp::Node* root ) {
81  std::string search_path_with_current_dir = gpu::replaceEnvironments( search_path );
82  if ( !from.filename().empty() ) { // Add current file directory to search_path
83  bf::path file_path( from.filename() );
84  search_path_with_current_dir =
85  file_path.parent_path().string() +
86  ( search_path_with_current_dir.empty() ? "" : ( "," + search_path_with_current_dir ) );
87  }
88  std::string absolute_path =
90 
91  if ( absolute_path.empty() ) {
92  messages->AddError( from, "Couldn't find a file " + filename + " in search path '" +
93  search_path_with_current_dir + "'" );
94  return false;
95  }
96  const gp::Position* included_from;
97  if ( !included->GetPosition( absolute_path, &included_from ) ) {
98  included->AddFile( absolute_path, from );
99  std::ifstream file{absolute_path};
100  if ( !file.is_open() ) {
101  messages->AddError( from, "Couldn't open a file " + filename );
102  return false;
103  }
104  return ParseStream<Grammar>( file, absolute_path, messages, root );
105  } else {
106  assert( included_from != NULL );
107  messages->AddWarning(
108  from, fmt::format( "File {} already included from {}", absolute_path, included_from->ToString() ) );
109  return true;
110  }
111  }
112  // ============================================================================
113 } // namespace
114 // ============================================================================
115 bool gp::Parse( const std::string& filename, const std::string& search_path, IncludedFiles* included,
116  Messages* messages, Node* root ) {
117  return Parse( Position(), filename, search_path, included, messages, root );
118 }
119 // ============================================================================
120 bool gp::Parse( const Position& from, const std::string& filename, const std::string& search_path,
121  IncludedFiles* included, Messages* messages, Node* root ) {
123  return ParseFile<Grammar>( from, filename, search_path, included, messages, root );
124 }
125 
126 // ============================================================================
127 bool gp::ParseUnits( const Position& from, const std::string& filename, const std::string& search_path,
128  IncludedFiles* included, Messages* messages, Node* root ) {
130  return ParseFile<Grammar>( from, filename, search_path, included, messages, root );
131 }
132 // ============================================================================
T empty(T... args)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
bool ParseUnits(const Position &from, const std::string &filename, const std::string &search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:127
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:26
std::string replaceEnvironments(const std::string &input)
Definition: Utils.cpp:28
T seekg(T... args)
boost::spirit::multi_pass< BaseIterator > ForwardIterator
Definition: Iterator.h:27
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:115
T clear(T... args)
T length(T... args)
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28
boost::spirit::classic::file_position_base< std::string > IteratorPosition
Definition: Iterator.h:29
STL class.