jmespath.cpp
C++ implementation of JMESPath, a query language for JSON http://jmespath.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
parser.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Author: Róbert Márki <gsmiko@gmail.com>
4 ** Copyright (c) 2016 Róbert Márki
5 **
6 ** This file is part of the jmespath.cpp project which is distributed under
7 ** the MIT License (MIT).
8 **
9 ** Permission is hereby granted, free of charge, to any person obtaining a copy
10 ** of this software and associated documentation files (the "Software"), to
11 ** deal in the Software without restriction, including without limitation the
12 ** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 ** sell copies of the Software, and to permit persons to whom the Software is
14 ** furnished to do so, subject to the following conditions:
15 **
16 ** The above copyright notice and this permission notice shall be included in
17 ** all copies or substantial portions of the Software.
18 **
19 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 ** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ** DEALINGS IN THE SOFTWARE.
26 **
27 ****************************************************************************/
28 #ifndef PARSER_H
29 #define PARSER_H
30 #include "jmespath/types.h"
31 #include "jmespath/exceptions.h"
32 #include <boost/spirit/include/qi.hpp>
33 
34 namespace jmespath { namespace parser {
35 
36 namespace qi = boost::spirit::qi;
37 namespace encoding = qi::unicode;
38 
43 template <template<typename, typename> class T>
44 class Parser
45 {
46 public:
54  using GrammarType = T<IteratorType, encoding::space_type>;
58  using ResultType = typename GrammarType::start_type::attr_type;
59 
67  ResultType parse(const String& expression)
68  {
69  try
70  {
71  // create begin and end iterators to the expression
72  // retain the value of the begin iterator
73  IteratorType beginIt(expression.cbegin());
74  IteratorType it = beginIt;
75  IteratorType endIt(expression.cend());
76 
77  ResultType result;
78  // parse JMESPath expression and create the result
79  bool parsingSuccesful = qi::phrase_parse(it, endIt,
80  m_grammar, encoding::space,
81  result);
82  // if parsing wasn't sucessful or if the expression was
83  // only partially parsed
84  if (!parsingSuccesful || (it != endIt))
85  {
86  // throw exception and set the location of syntax error
87  auto syntaxErrorLocation = std::distance(beginIt, it);
88  auto exception = SyntaxError();
89  exception << InfoSyntaxErrorLocation(syntaxErrorLocation);
90  BOOST_THROW_EXCEPTION(exception);
91  }
92  return result;
93  }
94  catch(Exception& exception)
95  {
96  exception << InfoSearchExpression(expression);
97  throw;
98  }
99  }
100 
101 private:
106 };
107 }} // namespace jmespath::parser
108 #endif // PARSER_H
boost::error_info< struct tag_syntax_error_location, long > InfoSyntaxErrorLocation
InfoSyntaxErrorLocation contains the location of the syntax error in the JMESPath expression...
Definition: exceptions.h:50
The Parser class parses expressions with the specified grammar.
Definition: parser.h:44
ResultType parse(const String &expression)
Parses the given expression.
Definition: parser.h:67
UnicodeIteratorAdaptor IteratorType
Iterator type which will be used to instantiate the grammar.
Definition: parser.h:50
std::basic_string< Char > String
UTF-8 encoded string type.
Definition: types.h:44
boost::error_info< struct tag_search_expression, std::string > InfoSearchExpression
InfoSearchExpression contains the JMESPath expression being evaluated.
Definition: exceptions.h:43
GrammarType m_grammar
Grammar object used for parsing.
Definition: parser.h:105
typename GrammarType::start_type::attr_type ResultType
The type of the result of parsing.
Definition: parser.h:58
The SyntaxError struct represents a syntax error in the evaluated expression.
Definition: exceptions.h:80
T< IteratorType, encoding::space_type > GrammarType
The type of the grammar that will be instantiated.
Definition: parser.h:54
The Exception struct is the common base class for for all the exceptions thrown by the library...
Definition: exceptions.h:67
boost::u8_to_u32_iterator< String::const_iterator > UnicodeIteratorAdaptor
UTF-32 string iterator adaptor.
Definition: types.h:57