blib/lib/ChordPro/lib/JSON/Relaxed/ErrorCodes.pm untested

filter: Statementstmt 0.0% 0 / 21 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 35 CC 3 SCAR 19.5
CC
3
Cov
0%
message
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 7.0
1 #! perl
2  
3 use v5.26;
4 use utf8;
5  
6 package JSON::Relaxed::ErrorCodes;
7  
8 use JSON::Relaxed::Parser; our $VERSION = $JSON::Relaxed::Parser::VERSION;
9  
10 =head1 JSON::Relaxed::ErrorCodes -- Error messages
11  
12 If the document cannot be parsed, JSON::Relaxed will normally throw an
13 exception.
14  
15 In legacy mode, JSON::Relaxed returns an undefined
16 value instead and sets the following error indicators:
17  
18 =over 4
19  
20 =item * $JSON::Relaxed::err_id
21  
22 A unique code for a specific error.
23  
24 =item * $JSON::Relaxed::err_msg
25  
26 An English description of the error, including an indication where the
27 error occurs.
28  
29 =back
30  
31 When using object-oriented mode, these can be easily retrieved using
32 the parser methods err_id() and err_msg().
33  
34 Following is a list of all error codes in JSON::Relaxed:
35  
36 =over 4
37  
38 =item * C<missing-input>
39  
40 No input was found. This can be caused by:
41  
42 $parser->decode()
43 $parser->decode(undef)
44  
45 =item * C<empty-input>
46  
47 The string to be parsed has no content beside whitespace and comments.
48  
49 $parser->decode('')
50 $parser->decode(' ')
51 $parser->decode('/* whatever */')
52  
53 =item * C<unclosed-inline-comment>
54  
55 A comment was started with /* but was never closed. For example:
56  
57 $parser->decode('/*')
58  
59 =item * C<invalid-structure-opening-character>
60  
61 The document opens with an invalid structural character like a comma or colon.
62 The following examples would trigger this error.
63  
64 $parser->decode(':')
65 $parser->decode(',')
66 $parser->decode('}')
67 $parser->decode(']')
68  
69 =item * C<multiple-structures>
70  
71 The document has multiple structures. JSON and RJSON only allow a document to
72 consist of a single hash, a single array, or a single string. The following
73 examples would trigger this error.
74  
75 $parse->decode('{}[]')
76 $parse->decode('{} "whatever"')
77 $parse->decode('"abc" "def"')
78  
79 =item * C<unknown-token-after-key>
80  
81 A hash key may only be followed by the closing hash brace or a colon. Anything
82 else triggers C<unknown-token-after-key>. So, the following examples would
83 trigger this error.
84  
85 $parse->decode("{a [ }") }
86 $parse->decode("{a b") }
87  
88 =item * C<unknown-token-for-hash-key>
89  
90 The parser encountered something besides a string where a hash key should be.
91 The following are examples of code that would trigger this error.
92  
93 $parse->decode('{{}}')
94 $parse->decode('{[]}')
95 $parse->decode('{]}')
96 $parse->decode('{:}')
97  
98 =item * C<unclosed-hash-brace>
99  
100 A hash has an opening brace but no closing brace. For example:
101  
102 $parse->decode('{x:1')
103  
104 =item * C<unclosed-array-brace>
105  
106 An array has an opening brace but not a closing brace. For example:
107  
108 $parse->decode('["x", "y"')
109  
110 =item * C<unexpected-token-after-colon>
111  
112 In a hash, a colon must be followed by a value. Anything else triggers this
113 error. For example:
114  
115 $parse->decode('{"a":,}')
116 $parse->decode('{"a":}')
117  
118 =item * C<missing-comma-between-array-elements>
119  
120 In an array, a comma must be followed by a value, another comma, or the closing
121 array brace. Anything else triggers this error. For example:
122  
123 $parse->decode('[ "x" "y" ]')
124 $parse->decode('[ "x" : ]')
125  
126 =item * C<unknown-array-token>
127  
128 This error exists just in case there's an invalid token in an array that
129 somehow wasn't caught by C<missing-comma-between-array-elements>. This error
130 shouldn't ever be triggered. If it is please L<let me know|JSON::Relaxed/"AUTHOR">.
131  
132 =item * C<unclosed-quote>
133  
134 This error is triggered when a quote isn't closed. For example:
135  
136 $parse->decode("'whatever")
137 $parse->decode('"whatever') }
138  
139 =back
140  
141 =cut
142  
143 my %msg =
144 ( 'missing-input' => 'the string to be parsed is empty or undefined',
145 'unknown-array-token' => 'unexpected array token',
146 'empty-input' =>
147 'the string to be parsed has no content',
148 'unclosed-inline-comment' =>
149 'a comment was started with /* but was never closed',
150 'invalid-structure-opening-character' =>
151 'expected opening brace or opening bracket',
152 'multiple-structures' =>
153 'the string being parsed contains more than one structure',
154 'unknown-token-after-key' =>
155 'expected colon, comma or closing brace after a hash key',
156 'unknown-token-for-hash-key' =>
157 'expected string, comma, or closing brace in a hash key',
158 'unclosed-hash-brace' =>
159 'missing closing brace for hash',
160 'unclosed-array-brace' =>
161 'missing closing brace for array',
162 'unexpected-token-after-colon' =>
163 'expected a value after a colon in a hash',
164 'missing-comma-between-array-elements' =>
165 'expected comma or closing array brace',
166 'unknown-array-token' =>
167 'unexpected token in array',
168 'unclosed-quote' =>
169 'missing closing quote for string'
170 );
171  
172 sub message {
173 my ( $self, $id, $aux ) = @_;
174 my $msg = $msg{$id} // ($id =~ s/-/ /gr);
175 if ( $aux ) {
176 # Calculate line/col from offset.
177 my @a = split( /\r\n?|\n/, $aux->parent->data );
178 if ( @a > 1 ) {
179 @a = split( /\r\n?|\n/, substr( $aux->parent->data, 0, $aux->offset ) );
180 $msg .= sprintf( " (line %d, col %d, before %s)",
181 0+@a, length($a[-1]), $aux->as_string );
182 }
183 else {
184 # Single line, assume string.
185 $msg .= sprintf( " (offset %d, before %s)",
186 $aux->offset, $aux->as_string );
187 }
188 }
189 else {
190 $msg .= " (at end of string)";
191 }
192 $msg;
193 }
194  
195 1;