Gaudi Framework, version v23r8

Home   Generated: Fri May 31 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Parser.cpp
Go to the documentation of this file.
1 // $Id:$
2 // ============================================================================
3 // STD:
4 // ============================================================================
5 #include <fstream>
6 // ============================================================================
7 // BOOST:
8 // ============================================================================
9 #include <boost/format.hpp>
10 #include <boost/filesystem.hpp>
11 // ============================================================================
12 #include "Parser.h"
13 #include "Messages.h"
14 #include "Node.h"
15 #include "Grammar.h"
16 #include "Iterator.h"
17 #include "IncludedFiles.h"
18 #include "Utils.h"
19 // ============================================================================
20 // Gaudi:
21 // ============================================================================
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 template<typename Grammar>
33 bool ParseStream(std::ifstream& stream,
34  const std::string& stream_name, gp::Messages* messages,
35  gp::Node* root) {
36  // iterate over stream input
37  BaseIterator in_begin(stream);
38  // convert input iterator to forward iterator, usable by spirit parser
39  ForwardIterator fwd_begin =
40  boost::spirit::make_default_multi_pass(in_begin);
41  ForwardIterator fwd_end;
42 
43  // wrap forward iterator with position iterator, to record the position
44 
45  Iterator position_begin(fwd_begin, fwd_end, stream_name);
46  Iterator position_end;
47 
48  Grammar gr;
49  gp::SkipperGrammar<Iterator> skipper;
50 
51  root->value = stream_name;
52  bool result = qi::phrase_parse(position_begin,
53  position_end, gr, skipper, *root);
54  if (result && (position_begin==position_end)) {
55  return true;
56  }
57  const IteratorPosition& pos = position_begin.get_position();
58  messages->AddError(gp::Position(stream_name, pos.line,
59  pos.column),"parse error");
60  return false;
61 }
62 
63 // ============================================================================
64 template<typename Grammar>
65 bool ParseFile(const gp::Position& from, const std::string& filename,
66  const std::string& search_path,
67  gp::IncludedFiles* included, gp::Messages* messages,gp::Node* root) {
68  std::string search_path_with_current_dir =
69  gpu::replaceEnvironments(search_path);
70  if (!from.filename().empty()) { // Add current file directory to search_path
71  bf::path file_path(from.filename());
72  search_path_with_current_dir = file_path.parent_path().string()
73  + (search_path_with_current_dir.empty()?""
74  :("," + search_path_with_current_dir));
75  }
77  gpu::replaceEnvironments(filename), search_path_with_current_dir);
78 
79  if (absolute_path.empty()) {
80  messages->AddError(from, "Couldn't find a file " + filename+
81  " in search path '"+ search_path_with_current_dir+"'");
82  return false;
83  }
84  const gp::Position* included_from;
85  if (!included->GetPosition(absolute_path, &included_from)){
86  included->AddFile(absolute_path, from);
87  std::ifstream file(absolute_path.c_str());
88  if (!file.is_open()) {
89  messages->AddError(from, "Couldn't open a file "+filename);
90  return false;
91  }
92  return ParseStream<Grammar>(file, absolute_path, messages, root);
93  } else {
94  assert(included_from != NULL);
95  messages->AddWarning(from,
96  str(boost::format("File %1% already included from %2%")
97  % absolute_path % included_from->ToString()));
98  return true;
99  }
100 }
101 // ============================================================================
102 }
103 // ============================================================================
104 bool gp::Parse(const std::string& filename,
105  const std::string& search_path, IncludedFiles* included,
106  Messages* messages, Node* root){
107  return Parse(Position(), filename, search_path, included, messages,
108  root);
109 }
110 // ============================================================================
111 bool gp::Parse(const Position& from,
112  const std::string& filename,
113  const std::string& search_path, IncludedFiles* included,
114  Messages* messages, Node* root) {
116  return ParseFile<Grammar>(from, filename, search_path, included,
117  messages, root);
118 }
119 
120 // ============================================================================
121 bool gp::ParseUnits(const Position& from,
122  const std::string& filename,
123  const std::string& search_path, IncludedFiles* included,
124  Messages* messages, Node* root) {
126  return ParseFile<Grammar>(from, filename, search_path, included,
127  messages, root);
128 }
129 // ============================================================================

Generated at Fri May 31 2013 15:09:05 for Gaudi Framework, version v23r8 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004