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
noderank.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 NODERANK_H
29 #define NODERANK_H
30 #include "src/ast/allnodes.h"
31 
32 namespace jmespath { namespace parser {
33 
39 template <typename T>
40 inline int nodeRank(const T&)
41 {
42  return 0;
43 }
44 
50 template <typename... Args>
51 inline int nodeRank(const boost::variant<Args...>& variant)
52 {
53  return boost::apply_visitor([](const auto& node){
54  return nodeRank(node);
55  }, variant);
56 }
57 
64 template <>
65 inline int nodeRank(const ast::ExpressionNode& node)
66 {
67  if(node.isNull())
68  {
69  return -1;
70  }
71  else
72  {
73  return nodeRank(node.value);
74  }
75 }
76 
77 template <>
78 inline int nodeRank(const ast::SubexpressionNode&)
79 {
80  return 1;
81 }
82 
83 template <>
84 inline int nodeRank(const ast::BracketSpecifierNode& node)
85 {
86  return nodeRank(node.value);
87 }
88 
89 template <>
90 inline int nodeRank(const ast::IndexExpressionNode& node)
91 {
92  return nodeRank(node.bracketSpecifier);
93 }
94 
95 template <>
96 inline int nodeRank(const ast::ArrayItemNode&)
97 {
98  return 1;
99 }
100 
101 template <>
103 {
104  return 2;
105 }
106 
107 template <>
109 {
110  return 2;
111 }
112 
113 template <>
114 inline int nodeRank(const ast::ListWildcardNode&)
115 {
116  return 2;
117 }
118 
119 template <>
120 inline int nodeRank(const ast::HashWildcardNode&)
121 {
122  return 2;
123 }
124 
125 template <>
127 {
128  return 2;
129 }
130 
131 template <>
133 {
134  return 3;
135 }
136 
137 template <>
139 {
140  return 4;
141 }
142 
143 template <>
145 {
146  return 5;
147 }
148 
149 template <>
150 inline int nodeRank(const ast::OrExpressionNode&)
151 {
152  return 6;
153 }
154 
155 template <>
157 {
158  return 7;
159 }
161 }} // namespace jmespath::parser
162 #endif // NODERANK_H
The ComparatorExpressionNode class represents a JMESPath comparator expression.
Definition: comparatorexpressionnode.h:39
The ListWildcardNode class represents a JMESPath list wildcard expression.
Definition: listwildcardnode.h:38
BracketSpecifierNode bracketSpecifier
The bracket specifier in an index expression.
Definition: indexexpressionnode.h:92
The HashWildcardNode class represents a JMESPath hash wildcard expression.
Definition: hashwildcardnode.h:38
The BracketSpecifierNode class represents a JMESPath bracket specifier.
Definition: bracketspecifiernode.h:44
The AndExpressionNode class represents a JMESPath and expression.
Definition: andexpressionnode.h:38
The SliceExpressionNode class represents a JMESPath slice expression.
Definition: sliceexpressionnode.h:41
ValueType value
The variable which stores the node that this object represents.
Definition: variantnode.h:126
int nodeRank(const T &)
Returns the rank of the given node object's type.
Definition: noderank.h:40
The PipeExpressionNode class represents a JMESPath pipe expression.
Definition: pipeexpressionnode.h:38
The SubexpressionNode class represents a JMESPath subexpression.
Definition: subexpressionnode.h:37
The FilterExpressionNode class represents a JMESPath filter expression.
Definition: filterexpressionnode.h:39
The OrExpressionNode class represents a JMESPath or expression.
Definition: orexpressionnode.h:38
The IndexExpressionNode class represents a JMESPath index expression.
Definition: indexexpressionnode.h:39
The NotExpressionNode class represents a JMESPath not expression.
Definition: notexpressionnode.h:39
bool isNull() const
Returns whether this object has been initialized.
Definition: variantnode.h:114
The ArrayItemNode class represents a JMESPath array index expression.
Definition: arrayitemnode.h:40
The FlattenOperatorNode class represents a JMESPath flatten operator.
Definition: flattenoperatornode.h:39
The ExpressionNode class represents a JMESPath expression.
Definition: expressionnode.h:56