24#include <boost/phoenix/core.hpp>
25#include <boost/phoenix/fusion.hpp>
26#include <boost/phoenix/operator.hpp>
27#include <boost/phoenix/stl.hpp>
28#if ( BOOST_VERSION >= 187000 ) && ( BOOST_VERSION < 188000 )
29# define BOOST_ALLOW_DEPRECATED_HEADERS
31#include <boost/spirit/include/qi.hpp>
32#undef BOOST_ALLOW_DEPRECATED_HEADERS
33#include <boost/spirit/repository/include/qi_confix.hpp>
34#include <boost/spirit/repository/include/qi_iter_pos.hpp>
37#include <boost/filesystem.hpp>
45 namespace sp = boost::spirit;
46 namespace ph = boost::phoenix;
47 namespace qi = sp::qi;
48 namespace enc = sp::ascii;
49 namespace rep = sp::repository;
51 template <
typename Iterator>
54 comments = enc::space | rep::confix(
"/*",
"*/" )[*( qi::char_ -
"*/" )] |
55 rep::confix(
"//", ( sp::eol | sp::eoi ) )[*( qi::char_ - ( sp::eol | sp::eoi ) )];
60 template <
typename Iterator,
typename Skipper>
61 struct StringGrammar : qi::grammar<Iterator, std::string(), qi::locals<char>, Skipper> {
65 quote = enc::char_( qi::_r1 );
68 *( ( enc::char_(
'\\' ) >>
quote( qi::_a ) )[qi::_val += qi::_a] |
69 ( enc::char_[qi::_val += qi::_1] -
quote( qi::_a ) ) ) >>
76 template <
typename Iterator,
typename Skipper>
82 str = -qi::lit(
"::" )[qi::_val +=
"::"] >>
inner[qi::_val += qi::_1] >>
83 *( qi::lit(
"::" ) >>
inner[qi::_val += (
"::" + qi::_1 )] );
84 inner = qi::alpha >> *( qi::alnum | qi::char_(
'_' ) );
89 ph::function<NodeOperations>
op;
91 template <
typename Iterator,
typename Skipper>
95 boolean = enc::no_case[qi::lit(
"true" )[qi::_val =
true] | qi::lit(
"false" )[qi::_val =
false]];
99 template <
typename Iterator,
typename Skipper>
103 real = qi::raw[qi::double_][
op( qi::_val, qi::_1 )][
op( qi::_val,
Node::kReal )] >> -qi::char_(
'L' ) >>
104 -( -qi::char_(
'*' ) >>
gunit[
op( qi::_val, qi::_1 )] );
108 ph::function<NodeOperations>
op;
110 template <
typename Iterator,
typename Skipper>
114 unit = rep::qi::iter_pos[
op( qi::_val, qi::_1 )] >>
val[
op( qi::_val, qi::_1 )] >> -qi::lit(
'*' ) >>
115 gunit[
op( qi::_val, qi::_1 )] >> qi::lit(
'=' ) >>
122 ph::function<NodeOperations>
op;
124 template <
typename Iterator,
typename Skipper>
130 qi::eol )[*( qi::char_[qi::_a += qi::_1] - qi::eol )][
op( qi::_val,
Node::kShell )][
op( qi::_val, qi::_a )];
132 condition )[qi::_val = qi::_1][
op( qi::_val, qi::_a )];
137 *
statement[
op( qi::_b, qi::_1 )] )[
op( qi::_val, qi::_b )] >>
148 value[
op( qi::_val, qi::_1 )] >>
';';
149 property = rep::qi::iter_pos[
op( qi::_val, qi::_1 )] >>
152 oper = rep::qi::iter_pos[
op( qi::_val, qi::_1 )] >>
158 enc::char_(
'(' )[qi::_val =
')'] | enc::char_(
'[' )[qi::_val =
']'] | enc::char_(
'{' )[qi::_val =
'}'];
162 map_value = ( enc::char_(
'{' ) >> -(
pair[
op( qi::_val, qi::_1 )] %
',' ) >>
169 qi::rule<
Iterator,
Node(),
Skipper>
file,
include,
assign,
property,
property_ref,
oper,
map_value,
pair_value,
181 ph::function<NodeOperations>
op;
186namespace bf = boost::filesystem;
189namespace qi = boost::spirit::qi;
195 for (
size_t p = s.find( delim ); p != s.npos; p = s.find( delim ) ) {
196 s.remove_prefix( p + 1 );
199 return {
line,
s.size() + 1 };
202 template <
typename Grammar>
207 auto [last_line, last_column] = GetLastLineAndColumn( input );
211 ForwardIterator fwd_begin = boost::spirit::make_default_multi_pass( in_begin );
215 Iterator position_begin( fwd_begin, fwd_end, stream_name );
219 gp::SkipperGrammar<Iterator> skipper;
221 root->value = stream_name;
222 bool result = qi::phrase_parse( position_begin, position_end, gr, skipper, *root );
225 if ( result && ( pos.line == last_line ) && ( pos.column == last_column ) ) {
return true; }
227 messages->
AddError( gp::Position( stream_name, pos.line, pos.column ),
"parse error" );
231 template <
typename Grammar>
232 bool ParseFile(
const gp::Position& from, std::string_view filename, std::string_view search_path,
233 gp::IncludedFiles* included, gp::Messages* messages, gp::Node* root ) {
236 bf::path file_path( from.
filename() );
237 search_path_with_current_dir =
238 file_path.parent_path().string() +
239 ( search_path_with_current_dir.
empty() ?
"" : (
"," + search_path_with_current_dir ) );
244 if ( absolute_path.
empty() ) {
246 search_path_with_current_dir +
"'" );
249 const gp::Position* included_from;
250 if ( !included->
GetPosition( absolute_path, &included_from ) ) {
251 included->
AddFile( absolute_path, from );
257 return ParseStream<Grammar>( file, absolute_path, messages, root );
259 assert( included_from !=
nullptr );
261 from, std::format(
"File {} already included from {}", absolute_path, included_from->
ToString() ) );
269 return Parse(
Position(), filename, search_path, included, messages, root );
275 return ParseFile<Grammar>( from, filename, search_path, included, messages, root );
281 return ParseFile<Grammar>( from, filename, search_path, included, messages, root );
boost::spirit::multi_pass< BaseIterator > ForwardIterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
boost::spirit::classic::file_position_base< std::string > IteratorPosition
std::string::const_iterator BaseIterator
basic_ifstream< char > ifstream
basic_ostringstream< char > ostringstream
basic_string< char > string
basic_string & append(_InputIterator __first, _InputIterator __last)
bool empty() const noexcept
bool GetPosition(std::string_view filename, const Position **pos) const
bool AddFile(std::string filename, Position from)
void AddError(std::string_view error)
void AddWarning(std::string_view warning)
std::string ToString() const
const std::string & filename() const
static std::string find_file_from_list(const std::string &logical_file_name, const std::string &search_list, SearchType search_type=LocalSearch)
std::string replaceEnvironments(std::string_view input)
bool ParseUnits(const Position &from, std::string_view filename, std::string_view search_path, IncludedFiles *included, Messages *messages, Node *root)
bool Parse(std::string_view filename, std::string_view search_path, IncludedFiles *included, Messages *messages, Node *root)
SkipperGrammar< IteratorT > Skipper
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
qi::rule< Iterator, bool(), Skipper > boolean
qi::rule< Iterator, Node(), Skipper > pragma_tree
qi::rule< Iterator, Node(), qi::locals< Node, Node >, Skipper > condition
qi::rule< Iterator, Node(), qi::locals< char >, Skipper > vector_value
qi::rule< Iterator, Node(), Skipper > oper
qi::rule< Iterator, Node(), Skipper > include
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > value
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > statement
qi::rule< Iterator, Node(), Skipper > print_options
qi::rule< Iterator, Node(), Skipper > file
qi::rule< Iterator, Node(), Skipper > pair_value
qi::rule< Iterator, void(char)> end_vector
RealGrammar< Iterator, Skipper > greal
ph::function< NodeOperations > op
qi::rule< Iterator, Node(), Skipper > property_ref
qi::rule< Iterator, char()> begin_vector
qi::rule< Iterator, Node(), qi::locals< std::string > > shell
BoolGrammar< Iterator, Skipper > gbool
qi::rule< Iterator, Node(), Skipper > assign
qi::rule< Iterator, Node(), Skipper > pragma
qi::rule< Iterator, Node(), Skipper > simple_value
qi::rule< Iterator, Node(), Skipper > property
qi::rule< Iterator, Node(), Skipper > map_value
StringGrammar< Iterator, Skipper > gstring
qi::rule< Iterator, Node(), Skipper > pragma_dump_file
qi::rule< Iterator, Node(), Skipper > units
qi::rule< Iterator, Node(), Skipper > pragma_print
qi::rule< Iterator, Node(), Skipper > pair
IdentifierGrammar< Iterator, Skipper > gidentifier
qi::rule< Iterator, std::string()> inner
qi::rule< Iterator, Node(), Skipper > ident
qi::rule< Iterator, std::string(), Skipper > str
ph::function< NodeOperations > op
ph::function< NodeOperations > op
qi::rule< Iterator, Node(), T > real
IdentifierGrammar< Iterator, T > gunit
qi::rule< IteratorT > comments
qi::rule< Iterator, void(char)> quote
qi::rule< Iterator, char()> begin_quote
qi::rule< Iterator, std::string(), qi::locals< char >, Skipper > str
ph::function< NodeOperations > op
qi::rule< Iterator, Node(), Skipper > units
qi::rule< Iterator, Node(), Skipper > val
qi::rule< Iterator, Node(), Skipper > unit
IdentifierGrammar< Iterator, Skipper > gunit