jsonata-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jsonata/jexception.py ADDED
@@ -0,0 +1,232 @@
1
+ #
2
+ # Copyright Robert Yokota
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License")
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Derived from the following code:
17
+ #
18
+ # Project name: jsonata-java
19
+ # Copyright Dashjoin GmbH. https://dashjoin.com
20
+ # Licensed under the Apache License, Version 2.0 (the "License")
21
+ #
22
+
23
+ import re
24
+
25
+ from typing import Any, Optional
26
+
27
+
28
+ class JException(RuntimeError):
29
+ error: str
30
+ location: int
31
+ current: Optional[Any]
32
+ expected: Optional[Any]
33
+
34
+ type: Optional[str]
35
+
36
+ # remaining: Sequence[tokenizer.Tokenizer.Token] | None
37
+
38
+ def __init__(self, error, location=0, current_token=None, expected=None):
39
+ super().__init__(JException.msg(error, location, current_token, expected))
40
+ self.error = error
41
+ self.location = location
42
+ self.current = current_token
43
+ self.expected = expected
44
+
45
+ self.type = None
46
+ self.remaining = None
47
+
48
+ #
49
+ # Returns the error code, i.e. S0201
50
+ # @return
51
+ #
52
+ def get_error(self) -> str:
53
+ return self.error
54
+
55
+ #
56
+ # Returns the error location (in characters)
57
+ # @return
58
+ #
59
+ def get_location(self) -> int:
60
+ return self.location
61
+
62
+ #
63
+ # Returns the current token
64
+ # @return
65
+ #
66
+ def get_current(self) -> Optional[Any]:
67
+ return self.current
68
+
69
+ #
70
+ # Returns the expected token
71
+ # @return
72
+ #
73
+ def get_expected(self) -> Optional[Any]:
74
+ return self.expected
75
+
76
+ #
77
+ # Returns the error message with error details in the text.
78
+ # Example: Syntax error: ")" {code=S0201 position=3}
79
+ # @return
80
+ #
81
+ def get_detailed_error_message(self) -> str:
82
+ return JException.msg(self.error, self.location, self.current, self.expected, True)
83
+
84
+ #
85
+ # Generate error message from given error code
86
+ # Codes are defined in Jsonata.errorCodes
87
+ #
88
+ # Fallback: if error code does not exist, return a generic message
89
+ #
90
+ # @param error
91
+ # @param location
92
+ # @param arg1
93
+ # @param arg2
94
+ # @param details True = add error details as text, false = don't add details (use getters to retrieve details)
95
+ # @return
96
+ #
97
+ @staticmethod
98
+ def msg(error: str, location: int, arg1: Optional[Any], arg2: Optional[Any], details: bool = False) -> str:
99
+ message = JException.error_codes.get(error)
100
+
101
+ if message is None:
102
+ # unknown error code
103
+ return "JSonataException " + str(error) + (
104
+ " {code=unknown position=" + str(location) + " arg1=" + arg1 + " arg2=" + arg2 + "}" if details else "")
105
+
106
+ formatted = message
107
+
108
+ # Replace any {{var}} with format "{}"
109
+ formatted = re.sub("\\{\\{\\w+\\}\\}", "{}", formatted)
110
+
111
+ formatted = formatted.format(arg1, arg2)
112
+
113
+ if details:
114
+ formatted = formatted + " {code=" + error
115
+ if location >= 0:
116
+ formatted += " position=" + str(location)
117
+ formatted += "}"
118
+ return formatted
119
+
120
+ #
121
+ # Error codes
122
+ #
123
+ # Sxxxx - Static errors (compile time)
124
+ # Txxxx - Type errors
125
+ # Dxxxx - Dynamic errors (evaluate time)
126
+ # 01xx - tokenizer
127
+ # 02xx - parser
128
+ # 03xx - regex parser
129
+ # 04xx - Object signature parser/evaluator
130
+ # 10xx - evaluator
131
+ # 20xx - operators
132
+ # 3xxx - functions (blocks of 10 for each function)
133
+ #
134
+ error_codes = {
135
+ "S0101": "String literal must be terminated by a matching quote",
136
+ "S0102": "Number out of range: {{token}}",
137
+ "S0103": "Unsupported escape sequence: \\{{token}}",
138
+ "S0104": "The escape sequence \\u must be followed by 4 hex digits",
139
+ "S0105": "Quoted property name must be terminated with a backquote (`)",
140
+ "S0106": "Comment has no closing tag",
141
+ "S0201": "Syntax error: {{token}}",
142
+ "S0202": "Expected {{value}}, got {{token}}",
143
+ "S0203": "Expected {{value}} before end of expression",
144
+ "S0204": "Unknown operator: {{token}}",
145
+ "S0205": "Unexpected token: {{token}}",
146
+ "S0206": "Unknown expression type: {{token}}",
147
+ "S0207": "Unexpected end of expression",
148
+ "S0208": "Parameter {{value}} of Object definition must be a variable name (start with $)",
149
+ "S0209": "A predicate cannot follow a grouping expression in a step",
150
+ "S0210": "Each step can only have one grouping expression",
151
+ "S0211": "The symbol {{token}} cannot be used as a unary operator",
152
+ "S0212": "The left side of := must be a variable name (start with $)",
153
+ "S0213": "The literal value {{value}} cannot be used as a step within a path expression",
154
+ "S0214": "The right side of {{token}} must be a variable name (start with $)",
155
+ "S0215": "A context variable binding must precede any predicates on a step",
156
+ "S0216": "A context variable binding must precede the \"order-by\" clause on a step",
157
+ "S0217": "The object representing the \"parent\" cannot be derived from this expression",
158
+ "S0301": "Empty regular expressions are not allowed",
159
+ "S0302": "No terminating / in regular expression",
160
+ "S0402": "Choice groups containing parameterized types are not supported",
161
+ "S0401": "Type parameters can only be applied to functions and arrays",
162
+ "S0500": "Attempted to evaluate an expression containing syntax error(s)",
163
+ "T0410": "Argument {{index}} of Object {{token}} does not match Object signature",
164
+ "T0411": "Context value is not a compatible type with argument {{index}} of Object {{token}}",
165
+ "T0412": "Argument {{index}} of Object {{token}} must be an array of {{type}}",
166
+ "D1001": "Number out of range: {{value}}",
167
+ "D1002": "Cannot negate a non-numeric value: {{value}}",
168
+ "T1003": "Key in object structure must evaluate to a string; got: {{value}}",
169
+ "D1004": "Regular expression matches zero length string",
170
+ "T1005": "Attempted to invoke a non-function. Did you mean ${{{token}}}?",
171
+ "T1006": "Attempted to invoke a non-function",
172
+ "T1007": "Attempted to partially apply a non-function. Did you mean ${{{token}}}?",
173
+ "T1008": "Attempted to partially apply a non-function",
174
+ "D1009": "Multiple key definitions evaluate to same key: {{value}}",
175
+ "T1010": "The matcher Object argument passed to Object {{token}} does not return the correct object structure",
176
+ "T2001": "The left side of the {{token}} operator must evaluate to a number",
177
+ "T2002": "The right side of the {{token}} operator must evaluate to a number",
178
+ "T2003": "The left side of the range operator (..) must evaluate to an integer",
179
+ "T2004": "The right side of the range operator (..) must evaluate to an integer",
180
+ "D2005": "The left side of := must be a variable name (start with $)",
181
+ # defunct - replaced by S0212 parser error
182
+ "T2006": "The right side of the Object application operator ~> must be a function",
183
+ "T2007": "Type mismatch when comparing values {{value}} and {{value2}} in order-by clause",
184
+ "T2008": "The expressions within an order-by clause must evaluate to numeric or string values",
185
+ "T2009": "The values {{value}} and {{value2}} either side of operator {{token}} must be of the same data type",
186
+ "T2010": "The expressions either side of operator {{token}} must evaluate to numeric or string values",
187
+ "T2011": "The insert/update clause of the transform expression must evaluate to an object: {{value}}",
188
+ "T2012": "The delete clause of the transform expression must evaluate to a string or array of strings: {{value}}",
189
+ "T2013": "The transform expression clones the input object using the $clone() function. This has been overridden in the current scope by a non-function.",
190
+ "D2014": "The size of the sequence allocated by the range operator (..) must not exceed 1e6. Attempted to allocate {{value}}.",
191
+ "D3001": "Attempting to invoke string Object on Infinity or NaN",
192
+ "D3010": "Second argument of replace Object cannot be an empty string",
193
+ "D3011": "Fourth argument of replace Object must evaluate to a positive number",
194
+ "D3012": "Attempted to replace a matched string with a non-string value",
195
+ "D3020": "Third argument of split Object must evaluate to a positive number",
196
+ "D3030": "Unable to cast value to a number: {{value}}",
197
+ "D3040": "Third argument of match Object must evaluate to a positive number",
198
+ "D3050": "The second argument of reduce Object must be a Object with at least two arguments",
199
+ "D3060": "The sqrt Object cannot be applied to a negative number: {{value}}",
200
+ "D3061": "The power Object has resulted in a value that cannot be represented as a JSON number: base={{value}}, exponent={{exp}}",
201
+ "D3070": "The single argument form of the sort Object can only be applied to an array of strings or an array of numbers. Use the second argument to specify a comparison function",
202
+ "D3080": "The picture string must only contain a maximum of two sub-pictures",
203
+ "D3081": "The sub-picture must not contain more than one instance of the \"decimal-separator\" character",
204
+ "D3082": "The sub-picture must not contain more than one instance of the \"percent\" character",
205
+ "D3083": "The sub-picture must not contain more than one instance of the \"per-mille\" character",
206
+ "D3084": "The sub-picture must not contain both a \"percent\" and a \"per-mille\" character",
207
+ "D3085": "The mantissa part of a sub-picture must contain at least one character that is either an \"optional digit character\" or a member of the \"decimal digit family\"",
208
+ "D3086": "The sub-picture must not contain a passive character that is preceded by an active character and that is followed by another active character",
209
+ "D3087": "The sub-picture must not contain a \"grouping-separator\" character that appears adjacent to a \"decimal-separator\" character",
210
+ "D3088": "The sub-picture must not contain a \"grouping-separator\" at the end of the integer part",
211
+ "D3089": "The sub-picture must not contain two adjacent instances of the \"grouping-separator\" character",
212
+ "D3090": "The integer part of the sub-picture must not contain a member of the \"decimal digit family\" that is followed by an instance of the \"optional digit character\"",
213
+ "D3091": "The fractional part of the sub-picture must not contain an instance of the \"optional digit character\" that is followed by a member of the \"decimal digit family\"",
214
+ "D3092": "A sub-picture that contains a \"percent\" or \"per-mille\" character must not contain a character treated as an \"exponent-separator\"",
215
+ "D3093": "The exponent part of the sub-picture must comprise only of one or more characters that are members of the \"decimal digit family\"",
216
+ "D3100": "The radix of the formatBase Object must be between 2 and 36. It was given {{value}}",
217
+ "D3110": "The argument of the toMillis Object must be an ISO 8601 formatted timestamp. Given {{value}}",
218
+ "D3120": "Syntax error in expression passed to Object eval: {{value}}",
219
+ "D3121": "Dynamic error evaluating the expression passed to Object eval: {{value}}",
220
+ "D3130": "Formatting or parsing an integer as a sequence starting with {{value}} is not supported by this implementation",
221
+ "D3131": "In a decimal digit pattern, all digits must be from the same decimal group",
222
+ "D3132": "Unknown component specifier {{value}} in date/time picture string",
223
+ "D3133": "The \"name\" modifier can only be applied to months and days in the date/time picture string, not {{value}}",
224
+ "D3134": "The timezone integer format specifier cannot have more than four digits",
225
+ "D3135": "No matching closing bracket \"]\" in date/time picture string",
226
+ "D3136": "The date/time picture string is missing specifiers required to parse the timestamp",
227
+ "D3137": "{{{message}}}",
228
+ "D3138": "The $single() Object expected exactly 1 matching result. Instead it matched more.",
229
+ "D3139": "The $single() Object expected exactly 1 matching result. Instead it matched 0.",
230
+ "D3140": "Malformed URL passed to ${{{function_name}}}(): {{value}}",
231
+ "D3141": "{{{message}}}"
232
+ }