Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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;
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, std::string_view filename, std::string_view 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 " + std::string{ 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, std::string{ "Couldn't open a file " }.append( filename ) );
102  return false;
103  }
104  return ParseStream<Grammar>( file, absolute_path, messages, root );
105  } else {
106  assert( included_from != nullptr );
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( std::string_view filename, std::string_view search_path, IncludedFiles* included, Messages* messages,
116  Node* root ) {
117  return Parse( Position(), filename, search_path, included, messages, root );
118 }
119 // ============================================================================
120 bool gp::Parse( const Position& from, std::string_view filename, std::string_view search_path, IncludedFiles* included,
121  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, std::string_view filename, std::string_view search_path,
128  IncludedFiles* included, Messages* messages, Node* root ) {
130  return ParseFile<Grammar>( from, filename, search_path, included, messages, root );
131 }
132 // ============================================================================
Write.stream
stream
Definition: Write.py:32
std::string
STL class.
graphanalysis.gr
gr
Definition: graphanalysis.py:132
Grammar.h
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
BaseIterator
std::string::const_iterator BaseIterator
Definition: Iterator.h:26
Gaudi::Parsers::Position::filename
const std::string & filename() const
Definition: Position.h:28
std::string::length
T length(T... args)
GaudiPython.GaudiAlgs.column
column
Definition: GaudiAlgs.py:1213
IteratorPosition
boost::spirit::classic::file_position_base< std::string > IteratorPosition
Definition: Iterator.h:29
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:43
Gaudi::Parsers::FileGrammar
Definition: Grammar.h:144
Gaudi::Parsers::Node
Definition: Node.h:35
ForwardIterator
boost::spirit::multi_pass< BaseIterator > ForwardIterator
Definition: Iterator.h:27
Gaudi::Parsers::IncludedFiles::AddFile
bool AddFile(std::string filename, Position from)
Definition: IncludedFiles.cpp:25
Gaudi::Parsers::IncludedFiles::GetPosition
bool GetPosition(std::string_view filename, const Position **pos) const
Definition: IncludedFiles.cpp:29
std::ifstream::clear
T clear(T... args)
Utils.h
IncludedFiles.h
Gaudi::Parsers::Utils
Definition: Utils.h:21
Gaudi::Parsers::Utils::replaceEnvironments
std::string replaceEnvironments(std::string_view input)
Definition: Utils.cpp:28
Gaudi::Parsers::Position
Definition: Position.h:23
Gaudi::Parsers::Messages
Definition: Messages.h:31
Gaudi::Parsers::Position::ToString
std::string ToString() const
Definition: Position.cpp:14
Gaudi::Parsers::Parse
bool Parse(std::string_view filename, std::string_view search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:115
Gaudi::Parsers::SkipperGrammar
Definition: Grammar.h:51
Iterator.h
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Parser.h
std::istreambuf_iterator
Gaudi::Parsers::IncludedFiles
Definition: IncludedFiles.h:26
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
std::string::append
T append(T... args)
Messages.h
Gaudi::Parsers::UnitsGrammar
Definition: Grammar.h:127
Gaudi::Parsers::Messages::AddError
void AddError(std::string_view error)
Definition: Messages.h:39
Gaudi::Parsers::Messages::AddWarning
void AddWarning(std::string_view warning)
Definition: Messages.h:37
Node.h
std::getline
T getline(T... args)
std::string::empty
T empty(T... args)
plotSpeedupsPyRoot.line
line
Definition: plotSpeedupsPyRoot.py:198
Gaudi::Parsers
Definition: DODBasicMapper.cpp:17
std::ifstream::seekg
T seekg(T... args)
Gaudi::Parsers::ParseUnits
bool ParseUnits(const Position &from, std::string_view filename, std::string_view search_path, IncludedFiles *included, Messages *messages, Node *root)
Definition: Parser.cpp:127
PathResolver.h
graphanalysis.filename
filename
Definition: graphanalysis.py:131
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:129
std::ifstream::eof
T eof(T... args)
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28
std::ifstream
STL class.