| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- R E P I N F O - I N P U T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2018-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
| 17 |
-- for more details. You should have received a copy of the GNU General -- |
| 18 |
-- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
with Alloc; |
| 27 |
with Csets; use Csets; |
| 28 |
with Hostparm; use Hostparm; |
| 29 |
with Namet; use Namet; |
| 30 |
with Output; use Output; |
| 31 |
with Snames; use Snames; |
| 32 |
with Table; |
| 33 |
with Ttypes; |
| 34 |
|
| 35 |
package body Repinfo.Input is |
| 36 |
|
| 37 |
SSU : Pos renames Ttypes.System_Storage_Unit; |
| 38 |
-- Value for Storage_Unit |
| 39 |
|
| 40 |
type JSON_Entity_Kind is (JE_Record_Type, JE_Array_Type, JE_Other); |
| 41 |
-- Kind of an entity |
| 42 |
|
| 43 |
type JSON_Entity_Node (Kind : JSON_Entity_Kind := JE_Other) is record |
| 44 |
Esize : Node_Ref_Or_Val; |
| 45 |
RM_Size : Node_Ref_Or_Val; |
| 46 |
case Kind is |
| 47 |
when JE_Record_Type => Variant : Nat; |
| 48 |
when JE_Array_Type => Component_Size : Node_Ref_Or_Val; |
| 49 |
when JE_Other => Dummy : Boolean; |
| 50 |
end case; |
| 51 |
end record; |
| 52 |
pragma Unchecked_Union (JSON_Entity_Node); |
| 53 |
-- Record to represent an entity |
| 54 |
|
| 55 |
package JSON_Entity_Table is new Table.Table ( |
| 56 |
Table_Component_Type => JSON_Entity_Node, |
| 57 |
Table_Index_Type => Nat, |
| 58 |
Table_Low_Bound => 1, |
| 59 |
Table_Initial => Alloc.Rep_JSON_Table_Initial, |
| 60 |
Table_Increment => Alloc.Rep_JSON_Table_Increment, |
| 61 |
Table_Name => "JSON_Entity_Table"); |
| 62 |
-- Table of entities |
| 63 |
|
| 64 |
type JSON_Component_Node is record |
| 65 |
Bit_Offset : Node_Ref_Or_Val; |
| 66 |
Esize : Node_Ref_Or_Val; |
| 67 |
end record; |
| 68 |
-- Record to represent a component |
| 69 |
|
| 70 |
package JSON_Component_Table is new Table.Table ( |
| 71 |
Table_Component_Type => JSON_Component_Node, |
| 72 |
Table_Index_Type => Nat, |
| 73 |
Table_Low_Bound => 1, |
| 74 |
Table_Initial => Alloc.Rep_JSON_Table_Initial, |
| 75 |
Table_Increment => Alloc.Rep_JSON_Table_Increment, |
| 76 |
Table_Name => "JSON_Component_Table"); |
| 77 |
-- Table of components |
| 78 |
|
| 79 |
type JSON_Variant_Node is record |
| 80 |
Present : Node_Ref_Or_Val; |
| 81 |
Variant : Nat; |
| 82 |
Next : Nat; |
| 83 |
end record; |
| 84 |
-- Record to represent a variant |
| 85 |
|
| 86 |
package JSON_Variant_Table is new Table.Table ( |
| 87 |
Table_Component_Type => JSON_Variant_Node, |
| 88 |
Table_Index_Type => Nat, |
| 89 |
Table_Low_Bound => 1, |
| 90 |
Table_Initial => Alloc.Rep_JSON_Table_Initial, |
| 91 |
Table_Increment => Alloc.Rep_JSON_Table_Increment, |
| 92 |
Table_Name => "JSON_Variant_Table"); |
| 93 |
-- Table of variants |
| 94 |
|
| 95 |
------------------------------------- |
| 96 |
-- Get_JSON_Component_Bit_Offset -- |
| 97 |
------------------------------------- |
| 98 |
|
| 99 |
function Get_JSON_Component_Bit_Offset |
| 100 |
(Name : String; |
| 101 |
Record_Name : String) return Node_Ref_Or_Val |
| 102 |
is |
| 103 |
Namid : constant Valid_Name_Id := Name_Find (Record_Name & '.' & Name); |
| 104 |
Index : constant Int := Get_Name_Table_Int (Namid); |
| 105 |
|
| 106 |
begin |
| 107 |
-- Return No_Uint if no information is available for the component |
| 108 |
|
| 109 |
if Index = 0 then |
| 110 |
return No_Uint; |
| 111 |
end if; |
| 112 |
|
| 113 |
return JSON_Component_Table.Table (Index).Bit_Offset; |
| 114 |
end Get_JSON_Component_Bit_Offset; |
| 115 |
|
| 116 |
------------------------------- |
| 117 |
-- Get_JSON_Component_Size -- |
| 118 |
------------------------------- |
| 119 |
|
| 120 |
function Get_JSON_Component_Size (Name : String) return Node_Ref_Or_Val is |
| 121 |
Namid : constant Valid_Name_Id := Name_Find (Name); |
| 122 |
Index : constant Int := Get_Name_Table_Int (Namid); |
| 123 |
|
| 124 |
begin |
| 125 |
-- Return No_Uint if no information is available for the component |
| 126 |
|
| 127 |
if Index = 0 then |
| 128 |
return No_Uint; |
| 129 |
end if; |
| 130 |
|
| 131 |
return JSON_Entity_Table.Table (Index).Component_Size; |
| 132 |
end Get_JSON_Component_Size; |
| 133 |
|
| 134 |
---------------------- |
| 135 |
-- Get_JSON_Esize -- |
| 136 |
---------------------- |
| 137 |
|
| 138 |
function Get_JSON_Esize (Name : String) return Node_Ref_Or_Val is |
| 139 |
Namid : constant Valid_Name_Id := Name_Find (Name); |
| 140 |
Index : constant Int := Get_Name_Table_Int (Namid); |
| 141 |
|
| 142 |
begin |
| 143 |
-- Return No_Uint if no information is available for the entity |
| 144 |
|
| 145 |
if Index = 0 then |
| 146 |
return No_Uint; |
| 147 |
end if; |
| 148 |
|
| 149 |
return JSON_Entity_Table.Table (Index).Esize; |
| 150 |
end Get_JSON_Esize; |
| 151 |
|
| 152 |
---------------------- |
| 153 |
-- Get_JSON_Esize -- |
| 154 |
---------------------- |
| 155 |
|
| 156 |
function Get_JSON_Esize |
| 157 |
(Name : String; |
| 158 |
Record_Name : String) return Node_Ref_Or_Val |
| 159 |
is |
| 160 |
Namid : constant Valid_Name_Id := Name_Find (Record_Name & '.' & Name); |
| 161 |
Index : constant Int := Get_Name_Table_Int (Namid); |
| 162 |
|
| 163 |
begin |
| 164 |
-- Return No_Uint if no information is available for the entity |
| 165 |
|
| 166 |
if Index = 0 then |
| 167 |
return No_Uint; |
| 168 |
end if; |
| 169 |
|
| 170 |
return JSON_Component_Table.Table (Index).Esize; |
| 171 |
end Get_JSON_Esize; |
| 172 |
|
| 173 |
------------------------ |
| 174 |
-- Get_JSON_RM_Size -- |
| 175 |
------------------------ |
| 176 |
|
| 177 |
function Get_JSON_RM_Size (Name : String) return Node_Ref_Or_Val is |
| 178 |
Namid : constant Valid_Name_Id := Name_Find (Name); |
| 179 |
Index : constant Int := Get_Name_Table_Int (Namid); |
| 180 |
|
| 181 |
begin |
| 182 |
-- Return No_Uint if no information is available for the entity |
| 183 |
|
| 184 |
if Index = 0 then |
| 185 |
return No_Uint; |
| 186 |
end if; |
| 187 |
|
| 188 |
return JSON_Entity_Table.Table (Index).RM_Size; |
| 189 |
end Get_JSON_RM_Size; |
| 190 |
|
| 191 |
----------------------- |
| 192 |
-- Read_JSON_Stream -- |
| 193 |
----------------------- |
| 194 |
|
| 195 |
procedure Read_JSON_Stream (Text : Text_Buffer; File_Name : String) is |
| 196 |
|
| 197 |
type Text_Position is record |
| 198 |
Index : Text_Ptr := 0; |
| 199 |
Line : Natural := 0; |
| 200 |
Column : Natural := 0; |
| 201 |
end record; |
| 202 |
-- Record to represent position in the text |
| 203 |
|
| 204 |
type Token_Kind is |
| 205 |
(J_NULL, |
| 206 |
J_TRUE, |
| 207 |
J_FALSE, |
| 208 |
J_NUMBER, |
| 209 |
J_INTEGER, |
| 210 |
J_STRING, |
| 211 |
J_ARRAY, |
| 212 |
J_OBJECT, |
| 213 |
J_ARRAY_END, |
| 214 |
J_OBJECT_END, |
| 215 |
J_COMMA, |
| 216 |
J_COLON, |
| 217 |
J_EOF); |
| 218 |
-- JSON token kind. Note that in ECMA 404 there is no notion of integer. |
| 219 |
-- Only numbers are supported. In our implementation we return J_INTEGER |
| 220 |
-- if there is no decimal part in the number. The semantic is that this |
| 221 |
-- is a J_NUMBER token that might be represented as an integer. Special |
| 222 |
-- token J_EOF means that end of stream has been reached. |
| 223 |
|
| 224 |
function Decode_Integer (Lo, Hi : Text_Ptr) return Uint; |
| 225 |
-- Decode and return the integer in Text (Lo .. Hi) |
| 226 |
|
| 227 |
function Decode_Name (Lo, Hi : Text_Ptr) return Valid_Name_Id; |
| 228 |
-- Decode and return the name in Text (Lo .. Hi) |
| 229 |
|
| 230 |
function Decode_Symbol (Lo, Hi : Text_Ptr) return TCode; |
| 231 |
-- Decode and return the expression symbol in Text (Lo .. Hi) |
| 232 |
|
| 233 |
procedure Error (Msg : String); |
| 234 |
pragma No_Return (Error); |
| 235 |
-- Print an error message and raise an exception |
| 236 |
|
| 237 |
procedure Read_Entity; |
| 238 |
-- Read an entity |
| 239 |
|
| 240 |
function Read_Name return Valid_Name_Id; |
| 241 |
-- Read a name |
| 242 |
|
| 243 |
function Read_Name_With_Prefix return Valid_Name_Id; |
| 244 |
-- Read a name and prepend a prefix |
| 245 |
|
| 246 |
function Read_Number return Uint; |
| 247 |
-- Read a number |
| 248 |
|
| 249 |
function Read_Numerical_Expr return Node_Ref_Or_Val; |
| 250 |
-- Read a numerical expression |
| 251 |
|
| 252 |
procedure Read_Record; |
| 253 |
-- Read a record |
| 254 |
|
| 255 |
function Read_String return Valid_Name_Id; |
| 256 |
-- Read a string |
| 257 |
|
| 258 |
procedure Read_Token |
| 259 |
(Kind : out Token_Kind; |
| 260 |
Token_Start : out Text_Position; |
| 261 |
Token_End : out Text_Position); |
| 262 |
-- Read a token and return it (this is a standard JSON lexer) |
| 263 |
|
| 264 |
procedure Read_Token_And_Error |
| 265 |
(TK : Token_Kind; |
| 266 |
Token_Start : out Text_Position; |
| 267 |
Token_End : out Text_Position); |
| 268 |
pragma Inline (Read_Token_And_Error); |
| 269 |
-- Read a specified token and error out on failure |
| 270 |
|
| 271 |
function Read_Variant_Part return Nat; |
| 272 |
-- Read a variant part |
| 273 |
|
| 274 |
procedure Skip_Value; |
| 275 |
-- Skip a value |
| 276 |
|
| 277 |
Pos : Text_Position := (Text'First, 1, 1); |
| 278 |
-- The current position in the text buffer |
| 279 |
|
| 280 |
Name_Buffer : Bounded_String (4 * Max_Name_Length); |
| 281 |
-- The buffer used to build full qualifed names |
| 282 |
|
| 283 |
Prefix_Len : Natural := 0; |
| 284 |
-- The length of the prefix present in Name_Buffer |
| 285 |
|
| 286 |
---------------------- |
| 287 |
-- Decode_Integer -- |
| 288 |
---------------------- |
| 289 |
|
| 290 |
function Decode_Integer (Lo, Hi : Text_Ptr) return Uint is |
| 291 |
Len : constant Nat := Int (Hi) - Int (Lo) + 1; |
| 292 |
|
| 293 |
begin |
| 294 |
-- Decode up to 9 characters manually, otherwise call into Uint |
| 295 |
|
| 296 |
if Len < 10 then |
| 297 |
declare |
| 298 |
Val : Int := 0; |
| 299 |
|
| 300 |
begin |
| 301 |
for J in Lo .. Hi loop |
| 302 |
Val := Val * 10 |
| 303 |
+ Character'Pos (Text (J)) - Character'Pos ('0'); |
| 304 |
end loop; |
| 305 |
return UI_From_Int (Val); |
| 306 |
end; |
| 307 |
|
| 308 |
else |
| 309 |
declare |
| 310 |
Val : Uint := Uint_0; |
| 311 |
|
| 312 |
begin |
| 313 |
for J in Lo .. Hi loop |
| 314 |
Val := Val * 10 |
| 315 |
+ Character'Pos (Text (J)) - Character'Pos ('0'); |
| 316 |
end loop; |
| 317 |
return Val; |
| 318 |
end; |
| 319 |
end if; |
| 320 |
end Decode_Integer; |
| 321 |
|
| 322 |
------------------- |
| 323 |
-- Decode_Name -- |
| 324 |
------------------- |
| 325 |
|
| 326 |
function Decode_Name (Lo, Hi : Text_Ptr) return Valid_Name_Id is |
| 327 |
begin |
| 328 |
-- Names are stored in lower case so fold them if need be |
| 329 |
|
| 330 |
if Is_Upper_Case_Letter (Text (Lo)) then |
| 331 |
declare |
| 332 |
S : String (Integer (Lo) .. Integer (Hi)); |
| 333 |
|
| 334 |
begin |
| 335 |
for J in Lo .. Hi loop |
| 336 |
S (Integer (J)) := Fold_Lower (Text (J)); |
| 337 |
end loop; |
| 338 |
|
| 339 |
return Name_Find (S); |
| 340 |
end; |
| 341 |
|
| 342 |
else |
| 343 |
declare |
| 344 |
S : constant String (Integer (Lo) .. Integer (Hi)); |
| 345 |
pragma Import (Ada, S); |
| 346 |
for S'Address use Text (Lo)'Address; |
| 347 |
|
| 348 |
begin |
| 349 |
return Name_Find (S); |
| 350 |
end; |
| 351 |
end if; |
| 352 |
end Decode_Name; |
| 353 |
|
| 354 |
--------------------- |
| 355 |
-- Decode_Symbol -- |
| 356 |
--------------------- |
| 357 |
|
| 358 |
function Decode_Symbol (Lo, Hi : Text_Ptr) return TCode is |
| 359 |
|
| 360 |
function Cmp12 (A, B : Character) return Boolean; |
| 361 |
pragma Inline (Cmp12); |
| 362 |
-- Compare Text (Lo + 1 .. Lo + 2) with A & B. |
| 363 |
|
| 364 |
------------- |
| 365 |
-- Cmp12 -- |
| 366 |
------------- |
| 367 |
|
| 368 |
function Cmp12 (A, B : Character) return Boolean is |
| 369 |
begin |
| 370 |
return Text (Lo + 1) = A and then Text (Lo + 2) = B; |
| 371 |
end Cmp12; |
| 372 |
|
| 373 |
Len : constant Nat := Int (Hi) - Int (Lo) + 1; |
| 374 |
|
| 375 |
-- Start of processing for Decode_Symbol |
| 376 |
|
| 377 |
begin |
| 378 |
case Len is |
| 379 |
when 1 => |
| 380 |
case Text (Lo) is |
| 381 |
when '+' => |
| 382 |
return Plus_Expr; |
| 383 |
when '-' => |
| 384 |
return Minus_Expr; -- or Negate_Expr |
| 385 |
when '*' => |
| 386 |
return Mult_Expr; |
| 387 |
when '<' => |
| 388 |
return Lt_Expr; |
| 389 |
when '>' => |
| 390 |
return Gt_Expr; |
| 391 |
when '&' => |
| 392 |
return Bit_And_Expr; |
| 393 |
when '#' => |
| 394 |
return Discrim_Val; |
| 395 |
when others => |
| 396 |
null; |
| 397 |
end case; |
| 398 |
when 2 => |
| 399 |
if Text (Lo) = '/' then |
| 400 |
case Text (Lo + 1) is |
| 401 |
when 't' => |
| 402 |
return Trunc_Div_Expr; |
| 403 |
when 'c' => |
| 404 |
return Ceil_Div_Expr; |
| 405 |
when 'f' => |
| 406 |
return Floor_Div_Expr; |
| 407 |
when 'e' => |
| 408 |
return Exact_Div_Expr; |
| 409 |
when others => |
| 410 |
null; |
| 411 |
end case; |
| 412 |
elsif Text (Lo + 1) = '=' then |
| 413 |
case Text (Lo) is |
| 414 |
when '<' => |
| 415 |
return Le_Expr; |
| 416 |
when '>' => |
| 417 |
return Ge_Expr; |
| 418 |
when '=' => |
| 419 |
return Eq_Expr; |
| 420 |
when '!' => |
| 421 |
return Ne_Expr; |
| 422 |
when others => |
| 423 |
null; |
| 424 |
end case; |
| 425 |
elsif Text (Lo) = 'o' and then Text (Lo + 1) = 'r' then |
| 426 |
return Truth_Or_Expr; |
| 427 |
end if; |
| 428 |
when 3 => |
| 429 |
case Text (Lo) is |
| 430 |
when '?' => |
| 431 |
if Cmp12 ('<', '>') then |
| 432 |
return Cond_Expr; |
| 433 |
end if; |
| 434 |
when 'a' => |
| 435 |
if Cmp12 ('b', 's') then |
| 436 |
return Abs_Expr; |
| 437 |
elsif Cmp12 ('n', 'd') then |
| 438 |
return Truth_And_Expr; |
| 439 |
end if; |
| 440 |
when 'm' => |
| 441 |
if Cmp12 ('a', 'x') then |
| 442 |
return Max_Expr; |
| 443 |
elsif Cmp12 ('i', 'n') then |
| 444 |
return Min_Expr; |
| 445 |
end if; |
| 446 |
when 'n' => |
| 447 |
if Cmp12 ('o', 't') then |
| 448 |
return Truth_Not_Expr; |
| 449 |
end if; |
| 450 |
when 'x' => |
| 451 |
if Cmp12 ('o', 'r') then |
| 452 |
return Truth_Xor_Expr; |
| 453 |
end if; |
| 454 |
when 'v' => |
| 455 |
if Cmp12 ('a', 'r') then |
| 456 |
return Dynamic_Val; |
| 457 |
end if; |
| 458 |
when others => |
| 459 |
null; |
| 460 |
end case; |
| 461 |
when 4 => |
| 462 |
if Text (Lo) = 'm' |
| 463 |
and then Text (Lo + 1) = 'o' |
| 464 |
and then Text (Lo + 2) = 'd' |
| 465 |
then |
| 466 |
case Text (Lo + 3) is |
| 467 |
when 't' => |
| 468 |
return Trunc_Mod_Expr; |
| 469 |
when 'c' => |
| 470 |
return Ceil_Mod_Expr; |
| 471 |
when 'f' => |
| 472 |
return Floor_Mod_Expr; |
| 473 |
when others => |
| 474 |
null; |
| 475 |
end case; |
| 476 |
end if; |
| 477 |
|
| 478 |
pragma Annotate |
| 479 |
(CodePeer, Intentional, |
| 480 |
"condition predetermined", "Error called as defensive code"); |
| 481 |
|
| 482 |
when others => |
| 483 |
null; |
| 484 |
end case; |
| 485 |
|
| 486 |
Error ("unknown symbol"); |
| 487 |
end Decode_Symbol; |
| 488 |
|
| 489 |
----------- |
| 490 |
-- Error -- |
| 491 |
----------- |
| 492 |
|
| 493 |
procedure Error (Msg : String) is |
| 494 |
L : constant String := Pos.Line'Img; |
| 495 |
C : constant String := Pos.Column'Img; |
| 496 |
|
| 497 |
begin |
| 498 |
Set_Standard_Error; |
| 499 |
Write_Eol; |
| 500 |
Write_Str (File_Name); |
| 501 |
Write_Char (':'); |
| 502 |
Write_Str (L (L'First + 1 .. L'Last)); |
| 503 |
Write_Char (':'); |
| 504 |
Write_Str (C (C'First + 1 .. C'Last)); |
| 505 |
Write_Char (':'); |
| 506 |
Write_Line (Msg); |
| 507 |
raise Invalid_JSON_Stream; |
| 508 |
end Error; |
| 509 |
|
| 510 |
------------------ |
| 511 |
-- Read_Entity -- |
| 512 |
------------------ |
| 513 |
|
| 514 |
procedure Read_Entity is |
| 515 |
Ent : JSON_Entity_Node; |
| 516 |
Nam : Name_Id := No_Name; |
| 517 |
Siz : Node_Ref_Or_Val; |
| 518 |
Token_Start : Text_Position; |
| 519 |
Token_End : Text_Position; |
| 520 |
TK : Token_Kind; |
| 521 |
|
| 522 |
begin |
| 523 |
Ent.Esize := No_Uint; |
| 524 |
Ent.RM_Size := No_Uint; |
| 525 |
Ent.Component_Size := No_Uint; |
| 526 |
|
| 527 |
-- Read the members as string : value pairs |
| 528 |
|
| 529 |
loop |
| 530 |
case Read_String is |
| 531 |
when Name_Name => |
| 532 |
Nam := Read_Name; |
| 533 |
when Name_Record => |
| 534 |
if Nam = No_Name then |
| 535 |
Error ("name expected"); |
| 536 |
end if; |
| 537 |
Ent.Variant := 0; |
| 538 |
Prefix_Len := Natural (Length_Of_Name (Nam)); |
| 539 |
Name_Buffer.Chars (1 .. Prefix_Len) := Get_Name_String (Nam); |
| 540 |
Read_Record; |
| 541 |
when Name_Variant => |
| 542 |
Ent.Variant := Read_Variant_Part; |
| 543 |
when Name_Size => |
| 544 |
Siz := Read_Numerical_Expr; |
| 545 |
Ent.Esize := Siz; |
| 546 |
Ent.RM_Size := Siz; |
| 547 |
when Name_Object_Size => |
| 548 |
Ent.Esize := Read_Numerical_Expr; |
| 549 |
when Name_Value_Size => |
| 550 |
Ent.RM_Size := Read_Numerical_Expr; |
| 551 |
when Name_Component_Size => |
| 552 |
Ent.Component_Size := Read_Numerical_Expr; |
| 553 |
when others => |
| 554 |
Skip_Value; |
| 555 |
end case; |
| 556 |
|
| 557 |
Read_Token (TK, Token_Start, Token_End); |
| 558 |
if TK = J_OBJECT_END then |
| 559 |
exit; |
| 560 |
elsif TK /= J_COMMA then |
| 561 |
Error ("comma expected"); |
| 562 |
end if; |
| 563 |
end loop; |
| 564 |
|
| 565 |
-- Store the entity into the table |
| 566 |
|
| 567 |
JSON_Entity_Table.Append (Ent); |
| 568 |
|
| 569 |
-- Associate the name with the entity |
| 570 |
|
| 571 |
if Nam = No_Name then |
| 572 |
Error ("name expected"); |
| 573 |
end if; |
| 574 |
|
| 575 |
Set_Name_Table_Int (Nam, JSON_Entity_Table.Last); |
| 576 |
end Read_Entity; |
| 577 |
|
| 578 |
----------------- |
| 579 |
-- Read_Name -- |
| 580 |
----------------- |
| 581 |
|
| 582 |
function Read_Name return Valid_Name_Id is |
| 583 |
Token_Start : Text_Position; |
| 584 |
Token_End : Text_Position; |
| 585 |
|
| 586 |
begin |
| 587 |
-- Read a single string |
| 588 |
|
| 589 |
Read_Token_And_Error (J_STRING, Token_Start, Token_End); |
| 590 |
|
| 591 |
return Decode_Name (Token_Start.Index + 1, Token_End.Index - 1); |
| 592 |
end Read_Name; |
| 593 |
|
| 594 |
----------------------------- |
| 595 |
-- Read_Name_With_Prefix -- |
| 596 |
----------------------------- |
| 597 |
|
| 598 |
function Read_Name_With_Prefix return Valid_Name_Id is |
| 599 |
Len : Natural; |
| 600 |
Lo, Hi : Text_Ptr; |
| 601 |
Token_Start : Text_Position; |
| 602 |
Token_End : Text_Position; |
| 603 |
|
| 604 |
begin |
| 605 |
-- Read a single string |
| 606 |
|
| 607 |
Read_Token_And_Error (J_STRING, Token_Start, Token_End); |
| 608 |
Lo := Token_Start.Index + 1; |
| 609 |
Hi := Token_End.Index - 1; |
| 610 |
|
| 611 |
-- Prepare for the concatenation with the prefix |
| 612 |
|
| 613 |
Len := Integer (Hi) - Integer (Lo) + 1; |
| 614 |
if Prefix_Len + 1 + Len > Name_Buffer.Max_Length then |
| 615 |
Error ("Name buffer too small"); |
| 616 |
end if; |
| 617 |
|
| 618 |
Name_Buffer.Length := Prefix_Len + 1 + Len; |
| 619 |
Name_Buffer.Chars (Prefix_Len + 1) := '.'; |
| 620 |
|
| 621 |
-- Names are stored in lower case so fold them if need be |
| 622 |
|
| 623 |
if Is_Upper_Case_Letter (Text (Lo)) then |
| 624 |
for J in Lo .. Hi loop |
| 625 |
Name_Buffer.Chars (Prefix_Len + 2 + Integer (J - Lo)) := |
| 626 |
Fold_Lower (Text (J)); |
| 627 |
end loop; |
| 628 |
|
| 629 |
else |
| 630 |
declare |
| 631 |
S : constant String (Integer (Lo) .. Integer (Hi)); |
| 632 |
pragma Import (Ada, S); |
| 633 |
for S'Address use Text (Lo)'Address; |
| 634 |
|
| 635 |
begin |
| 636 |
Name_Buffer.Chars (Prefix_Len + 2 .. Prefix_Len + 1 + Len) := S; |
| 637 |
end; |
| 638 |
end if; |
| 639 |
|
| 640 |
return Name_Find (Name_Buffer); |
| 641 |
end Read_Name_With_Prefix; |
| 642 |
|
| 643 |
------------------ |
| 644 |
-- Read_Number -- |
| 645 |
------------------ |
| 646 |
|
| 647 |
function Read_Number return Uint is |
| 648 |
Token_Start : Text_Position; |
| 649 |
Token_End : Text_Position; |
| 650 |
|
| 651 |
begin |
| 652 |
-- Only integers are to be expected here |
| 653 |
|
| 654 |
Read_Token_And_Error (J_INTEGER, Token_Start, Token_End); |
| 655 |
|
| 656 |
return Decode_Integer (Token_Start.Index, Token_End.Index); |
| 657 |
end Read_Number; |
| 658 |
|
| 659 |
-------------------------- |
| 660 |
-- Read_Numerical_Expr -- |
| 661 |
-------------------------- |
| 662 |
|
| 663 |
function Read_Numerical_Expr return Node_Ref_Or_Val is |
| 664 |
Code : TCode; |
| 665 |
Nop : Integer; |
| 666 |
Ops : array (1 .. 3) of Node_Ref_Or_Val; |
| 667 |
TK : Token_Kind; |
| 668 |
Token_Start : Text_Position; |
| 669 |
Token_End : Text_Position; |
| 670 |
|
| 671 |
begin |
| 672 |
-- Read either an integer or an expression |
| 673 |
|
| 674 |
Read_Token (TK, Token_Start, Token_End); |
| 675 |
if TK = J_INTEGER then |
| 676 |
return Decode_Integer (Token_Start.Index, Token_End.Index); |
| 677 |
|
| 678 |
elsif TK = J_OBJECT then |
| 679 |
-- Read the code of the expression and decode it |
| 680 |
|
| 681 |
if Read_String /= Name_Code then |
| 682 |
Error ("name expected"); |
| 683 |
end if; |
| 684 |
|
| 685 |
Read_Token_And_Error (J_STRING, Token_Start, Token_End); |
| 686 |
Code := Decode_Symbol (Token_Start.Index + 1, Token_End.Index - 1); |
| 687 |
Read_Token_And_Error (J_COMMA, Token_Start, Token_End); |
| 688 |
|
| 689 |
-- Read the array of operands |
| 690 |
|
| 691 |
if Read_String /= Name_Operands then |
| 692 |
Error ("operands expected"); |
| 693 |
end if; |
| 694 |
|
| 695 |
Read_Token_And_Error (J_ARRAY, Token_Start, Token_End); |
| 696 |
|
| 697 |
Nop := 0; |
| 698 |
Ops := (others => No_Uint); |
| 699 |
loop |
| 700 |
Nop := Nop + 1; |
| 701 |
Ops (Nop) := Read_Numerical_Expr; |
| 702 |
Read_Token (TK, Token_Start, Token_End); |
| 703 |
if TK = J_ARRAY_END then |
| 704 |
exit; |
| 705 |
elsif TK /= J_COMMA then |
| 706 |
Error ("comma expected"); |
| 707 |
end if; |
| 708 |
end loop; |
| 709 |
|
| 710 |
Read_Token_And_Error (J_OBJECT_END, Token_Start, Token_End); |
| 711 |
|
| 712 |
-- Resolve the ambiguity for '-' now |
| 713 |
|
| 714 |
if Code = Minus_Expr and then Nop = 1 then |
| 715 |
Code := Negate_Expr; |
| 716 |
end if; |
| 717 |
|
| 718 |
return Create_Node (Code, Ops (1), Ops (2), Ops (3)); |
| 719 |
|
| 720 |
else |
| 721 |
Error ("numerical expression expected"); |
| 722 |
end if; |
| 723 |
end Read_Numerical_Expr; |
| 724 |
|
| 725 |
------------------- |
| 726 |
-- Read_Record -- |
| 727 |
------------------- |
| 728 |
|
| 729 |
procedure Read_Record is |
| 730 |
Comp : JSON_Component_Node; |
| 731 |
First_Bit : Node_Ref_Or_Val := No_Uint; |
| 732 |
Is_First : Boolean := True; |
| 733 |
Nam : Name_Id := No_Name; |
| 734 |
Position : Node_Ref_Or_Val := No_Uint; |
| 735 |
TK : Token_Kind; |
| 736 |
Token_Start : Text_Position; |
| 737 |
Token_End : Text_Position; |
| 738 |
|
| 739 |
begin |
| 740 |
-- Read a possibly empty array of components |
| 741 |
|
| 742 |
Read_Token_And_Error (J_ARRAY, Token_Start, Token_End); |
| 743 |
|
| 744 |
loop |
| 745 |
Read_Token (TK, Token_Start, Token_End); |
| 746 |
if Is_First and then TK = J_ARRAY_END then |
| 747 |
exit; |
| 748 |
elsif TK /= J_OBJECT then |
| 749 |
Error ("object expected"); |
| 750 |
end if; |
| 751 |
|
| 752 |
-- Read the members as string : value pairs |
| 753 |
|
| 754 |
loop |
| 755 |
case Read_String is |
| 756 |
when Name_Name => |
| 757 |
Nam := Read_Name_With_Prefix; |
| 758 |
when Name_Discriminant => |
| 759 |
Skip_Value; |
| 760 |
when Name_Position => |
| 761 |
Position := Read_Numerical_Expr; |
| 762 |
when Name_First_Bit => |
| 763 |
First_Bit := Read_Number; |
| 764 |
when Name_Size => |
| 765 |
Comp.Esize := Read_Numerical_Expr; |
| 766 |
when others => |
| 767 |
Error ("invalid component"); |
| 768 |
end case; |
| 769 |
|
| 770 |
Read_Token (TK, Token_Start, Token_End); |
| 771 |
if TK = J_OBJECT_END then |
| 772 |
exit; |
| 773 |
elsif TK /= J_COMMA then |
| 774 |
Error ("comma expected"); |
| 775 |
end if; |
| 776 |
end loop; |
| 777 |
|
| 778 |
-- Compute Component_Bit_Offset from Position and First_Bit, |
| 779 |
-- either symbolically or literally depending on Position. |
| 780 |
|
| 781 |
if No (Position) or else No (First_Bit) then |
| 782 |
Error ("bit offset expected"); |
| 783 |
end if; |
| 784 |
|
| 785 |
if Position < Uint_0 then |
| 786 |
declare |
| 787 |
Bit_Position : constant Node_Ref_Or_Val := |
| 788 |
Create_Node (Mult_Expr, Position, UI_From_Int (SSU)); |
| 789 |
begin |
| 790 |
if First_Bit = Uint_0 then |
| 791 |
Comp.Bit_Offset := Bit_Position; |
| 792 |
else |
| 793 |
Comp.Bit_Offset := |
| 794 |
Create_Node (Plus_Expr, Bit_Position, First_Bit); |
| 795 |
end if; |
| 796 |
end; |
| 797 |
else |
| 798 |
Comp.Bit_Offset := Position * SSU + First_Bit; |
| 799 |
end if; |
| 800 |
|
| 801 |
-- Store the component into the table |
| 802 |
|
| 803 |
JSON_Component_Table.Append (Comp); |
| 804 |
|
| 805 |
-- Associate the name with the component |
| 806 |
|
| 807 |
if Nam = No_Name then |
| 808 |
Error ("name expected"); |
| 809 |
end if; |
| 810 |
|
| 811 |
Set_Name_Table_Int (Nam, JSON_Component_Table.Last); |
| 812 |
|
| 813 |
Read_Token (TK, Token_Start, Token_End); |
| 814 |
if TK = J_ARRAY_END then |
| 815 |
exit; |
| 816 |
elsif TK /= J_COMMA then |
| 817 |
Error ("comma expected"); |
| 818 |
end if; |
| 819 |
|
| 820 |
Is_First := False; |
| 821 |
end loop; |
| 822 |
end Read_Record; |
| 823 |
|
| 824 |
------------------ |
| 825 |
-- Read_String -- |
| 826 |
------------------ |
| 827 |
|
| 828 |
function Read_String return Valid_Name_Id is |
| 829 |
Token_Start : Text_Position; |
| 830 |
Token_End : Text_Position; |
| 831 |
Nam : Valid_Name_Id; |
| 832 |
|
| 833 |
begin |
| 834 |
-- Read the string and the following colon |
| 835 |
|
| 836 |
Read_Token_And_Error (J_STRING, Token_Start, Token_End); |
| 837 |
Nam := Decode_Name (Token_Start.Index + 1, Token_End.Index - 1); |
| 838 |
Read_Token_And_Error (J_COLON, Token_Start, Token_End); |
| 839 |
|
| 840 |
return Nam; |
| 841 |
end Read_String; |
| 842 |
|
| 843 |
------------------ |
| 844 |
-- Read_Token -- |
| 845 |
------------------ |
| 846 |
|
| 847 |
procedure Read_Token |
| 848 |
(Kind : out Token_Kind; |
| 849 |
Token_Start : out Text_Position; |
| 850 |
Token_End : out Text_Position) |
| 851 |
is |
| 852 |
procedure Next_Char; |
| 853 |
-- Update Pos to point to next char |
| 854 |
|
| 855 |
function Is_Whitespace return Boolean; |
| 856 |
pragma Inline (Is_Whitespace); |
| 857 |
-- Return True of current character is a whitespace |
| 858 |
|
| 859 |
function Is_Structural_Token return Boolean; |
| 860 |
pragma Inline (Is_Structural_Token); |
| 861 |
-- Return True if current character is one of the structural tokens |
| 862 |
|
| 863 |
function Is_Token_Sep return Boolean; |
| 864 |
pragma Inline (Is_Token_Sep); |
| 865 |
-- Return True if current character is a token separator |
| 866 |
|
| 867 |
procedure Delimit_Keyword (Kw : String); |
| 868 |
-- Helper function to parse tokens such as null, false and true |
| 869 |
|
| 870 |
--------------- |
| 871 |
-- Next_Char -- |
| 872 |
--------------- |
| 873 |
|
| 874 |
procedure Next_Char is |
| 875 |
begin |
| 876 |
if Pos.Index > Text'Last then |
| 877 |
Pos.Column := Pos.Column + 1; |
| 878 |
elsif Text (Pos.Index) = ASCII.LF then |
| 879 |
Pos.Column := 1; |
| 880 |
Pos.Line := Pos.Line + 1; |
| 881 |
else |
| 882 |
Pos.Column := Pos.Column + 1; |
| 883 |
end if; |
| 884 |
Pos.Index := Pos.Index + 1; |
| 885 |
end Next_Char; |
| 886 |
|
| 887 |
------------------- |
| 888 |
-- Is_Whitespace -- |
| 889 |
------------------- |
| 890 |
|
| 891 |
function Is_Whitespace return Boolean is |
| 892 |
begin |
| 893 |
return |
| 894 |
Pos.Index <= Text'Last |
| 895 |
and then |
| 896 |
(Text (Pos.Index) = ASCII.LF |
| 897 |
or else |
| 898 |
Text (Pos.Index) = ASCII.CR |
| 899 |
or else |
| 900 |
Text (Pos.Index) = ASCII.HT |
| 901 |
or else |
| 902 |
Text (Pos.Index) = ' '); |
| 903 |
end Is_Whitespace; |
| 904 |
|
| 905 |
------------------------- |
| 906 |
-- Is_Structural_Token -- |
| 907 |
------------------------- |
| 908 |
|
| 909 |
function Is_Structural_Token return Boolean is |
| 910 |
begin |
| 911 |
return |
| 912 |
Pos.Index <= Text'Last |
| 913 |
and then |
| 914 |
(Text (Pos.Index) = '[' |
| 915 |
or else |
| 916 |
Text (Pos.Index) = ']' |
| 917 |
or else |
| 918 |
Text (Pos.Index) = '{' |
| 919 |
or else |
| 920 |
Text (Pos.Index) = '}' |
| 921 |
or else |
| 922 |
Text (Pos.Index) = ',' |
| 923 |
or else |
| 924 |
Text (Pos.Index) = ':'); |
| 925 |
end Is_Structural_Token; |
| 926 |
|
| 927 |
------------------ |
| 928 |
-- Is_Token_Sep -- |
| 929 |
------------------ |
| 930 |
|
| 931 |
function Is_Token_Sep return Boolean is |
| 932 |
begin |
| 933 |
return |
| 934 |
Pos.Index > Text'Last |
| 935 |
or else |
| 936 |
Is_Whitespace |
| 937 |
or else |
| 938 |
Is_Structural_Token; |
| 939 |
end Is_Token_Sep; |
| 940 |
|
| 941 |
--------------------- |
| 942 |
-- Delimit_Keyword -- |
| 943 |
--------------------- |
| 944 |
|
| 945 |
procedure Delimit_Keyword (Kw : String) is |
| 946 |
pragma Unreferenced (Kw); |
| 947 |
begin |
| 948 |
while not Is_Token_Sep loop |
| 949 |
Token_End := Pos; |
| 950 |
Next_Char; |
| 951 |
end loop; |
| 952 |
end Delimit_Keyword; |
| 953 |
|
| 954 |
CC : Character; |
| 955 |
Can_Be_Integer : Boolean := True; |
| 956 |
|
| 957 |
-- Start of processing for Read_Token |
| 958 |
|
| 959 |
begin |
| 960 |
-- Skip leading whitespaces |
| 961 |
|
| 962 |
while Is_Whitespace loop |
| 963 |
Next_Char; |
| 964 |
end loop; |
| 965 |
|
| 966 |
-- Initialize token delimiters |
| 967 |
|
| 968 |
Token_Start := Pos; |
| 969 |
Token_End := Pos; |
| 970 |
|
| 971 |
-- End of stream reached |
| 972 |
|
| 973 |
if Pos.Index > Text'Last then |
| 974 |
Kind := J_EOF; |
| 975 |
return; |
| 976 |
end if; |
| 977 |
|
| 978 |
CC := Text (Pos.Index); |
| 979 |
|
| 980 |
if CC = '[' then |
| 981 |
Next_Char; |
| 982 |
Kind := J_ARRAY; |
| 983 |
return; |
| 984 |
elsif CC = ']' then |
| 985 |
Next_Char; |
| 986 |
Kind := J_ARRAY_END; |
| 987 |
return; |
| 988 |
elsif CC = '{' then |
| 989 |
Next_Char; |
| 990 |
Kind := J_OBJECT; |
| 991 |
return; |
| 992 |
elsif CC = '}' then |
| 993 |
Next_Char; |
| 994 |
Kind := J_OBJECT_END; |
| 995 |
return; |
| 996 |
elsif CC = ',' then |
| 997 |
Next_Char; |
| 998 |
Kind := J_COMMA; |
| 999 |
return; |
| 1000 |
elsif CC = ':' then |
| 1001 |
Next_Char; |
| 1002 |
Kind := J_COLON; |
| 1003 |
return; |
| 1004 |
elsif CC = 'n' then |
| 1005 |
Delimit_Keyword ("null"); |
| 1006 |
Kind := J_NULL; |
| 1007 |
return; |
| 1008 |
elsif CC = 'f' then |
| 1009 |
Delimit_Keyword ("false"); |
| 1010 |
Kind := J_FALSE; |
| 1011 |
return; |
| 1012 |
elsif CC = 't' then |
| 1013 |
Delimit_Keyword ("true"); |
| 1014 |
Kind := J_TRUE; |
| 1015 |
return; |
| 1016 |
elsif CC = '"' then |
| 1017 |
-- We expect a string |
| 1018 |
-- Just scan till the end the of the string but do not attempt |
| 1019 |
-- to decode it. This means that even if we get a string token |
| 1020 |
-- it might not be a valid string from the ECMA 404 point of |
| 1021 |
-- view. |
| 1022 |
|
| 1023 |
Next_Char; |
| 1024 |
while Pos.Index <= Text'Last and then Text (Pos.Index) /= '"' loop |
| 1025 |
if Text (Pos.Index) in ASCII.NUL .. ASCII.US then |
| 1026 |
Error ("control character not allowed in string"); |
| 1027 |
end if; |
| 1028 |
|
| 1029 |
if Text (Pos.Index) = '\' then |
| 1030 |
Next_Char; |
| 1031 |
if Pos.Index > Text'Last then |
| 1032 |
Error ("non terminated string token"); |
| 1033 |
end if; |
| 1034 |
|
| 1035 |
case Text (Pos.Index) is |
| 1036 |
when 'u' => |
| 1037 |
for Idx in 1 .. 4 loop |
| 1038 |
Next_Char; |
| 1039 |
if Pos.Index > Text'Last |
| 1040 |
or else (Text (Pos.Index) not in 'a' .. 'f' |
| 1041 |
and then |
| 1042 |
Text (Pos.Index) not in 'A' .. 'F' |
| 1043 |
and then |
| 1044 |
Text (Pos.Index) not in '0' .. '9') |
| 1045 |
then |
| 1046 |
Error ("invalid unicode escape sequence"); |
| 1047 |
end if; |
| 1048 |
end loop; |
| 1049 |
when '\' | '/' | '"' | 'b' | 'f' | 'n' | 'r' | 't' => |
| 1050 |
null; |
| 1051 |
when others => |
| 1052 |
Error ("invalid escape sequence"); |
| 1053 |
end case; |
| 1054 |
end if; |
| 1055 |
Next_Char; |
| 1056 |
end loop; |
| 1057 |
|
| 1058 |
-- No quote found report and error |
| 1059 |
|
| 1060 |
if Pos.Index > Text'Last then |
| 1061 |
Error ("non terminated string token"); |
| 1062 |
end if; |
| 1063 |
|
| 1064 |
Token_End := Pos; |
| 1065 |
|
| 1066 |
-- Go to next char and ensure that this is separator. Indeed |
| 1067 |
-- construction such as "string1""string2" are not allowed |
| 1068 |
|
| 1069 |
Next_Char; |
| 1070 |
if not Is_Token_Sep then |
| 1071 |
Error ("invalid syntax"); |
| 1072 |
end if; |
| 1073 |
Kind := J_STRING; |
| 1074 |
return; |
| 1075 |
elsif CC = '-' or else CC in '0' .. '9' then |
| 1076 |
-- We expect a number |
| 1077 |
if CC = '-' then |
| 1078 |
Next_Char; |
| 1079 |
end if; |
| 1080 |
|
| 1081 |
if Pos.Index > Text'Last then |
| 1082 |
Error ("invalid number"); |
| 1083 |
end if; |
| 1084 |
|
| 1085 |
-- Parse integer part of a number. Superfluous leading zeros are |
| 1086 |
-- not allowed. |
| 1087 |
|
| 1088 |
if Text (Pos.Index) = '0' then |
| 1089 |
Token_End := Pos; |
| 1090 |
Next_Char; |
| 1091 |
elsif Text (Pos.Index) in '1' .. '9' then |
| 1092 |
Token_End := Pos; |
| 1093 |
Next_Char; |
| 1094 |
while Pos.Index <= Text'Last |
| 1095 |
and then Text (Pos.Index) in '0' .. '9' |
| 1096 |
loop |
| 1097 |
Token_End := Pos; |
| 1098 |
Next_Char; |
| 1099 |
end loop; |
| 1100 |
else |
| 1101 |
Error ("invalid number"); |
| 1102 |
end if; |
| 1103 |
|
| 1104 |
if Is_Token_Sep then |
| 1105 |
-- Valid integer number |
| 1106 |
|
| 1107 |
Kind := J_INTEGER; |
| 1108 |
return; |
| 1109 |
elsif Text (Pos.Index) /= '.' |
| 1110 |
and then Text (Pos.Index) /= 'e' |
| 1111 |
and then Text (Pos.Index) /= 'E' |
| 1112 |
then |
| 1113 |
Error ("invalid number"); |
| 1114 |
end if; |
| 1115 |
|
| 1116 |
-- Check for a fractional part |
| 1117 |
|
| 1118 |
if Text (Pos.Index) = '.' then |
| 1119 |
Can_Be_Integer := False; |
| 1120 |
Token_End := Pos; |
| 1121 |
Next_Char; |
| 1122 |
if Pos.Index > Text'Last |
| 1123 |
or else Text (Pos.Index) not in '0' .. '9' |
| 1124 |
then |
| 1125 |
Error ("invalid number"); |
| 1126 |
end if; |
| 1127 |
|
| 1128 |
while Pos.Index <= Text'Last |
| 1129 |
and then Text (Pos.Index) in '0' .. '9' |
| 1130 |
loop |
| 1131 |
Token_End := Pos; |
| 1132 |
Next_Char; |
| 1133 |
end loop; |
| 1134 |
|
| 1135 |
end if; |
| 1136 |
|
| 1137 |
-- Check for exponent part |
| 1138 |
|
| 1139 |
if Pos.Index <= Text'Last |
| 1140 |
and then (Text (Pos.Index) = 'e' or else Text (Pos.Index) = 'E') |
| 1141 |
then |
| 1142 |
Token_End := Pos; |
| 1143 |
Next_Char; |
| 1144 |
if Pos.Index > Text'Last then |
| 1145 |
Error ("invalid number"); |
| 1146 |
end if; |
| 1147 |
|
| 1148 |
if Text (Pos.Index) = '-' then |
| 1149 |
-- Also a few corner cases can lead to an integer, assume |
| 1150 |
-- that the number is not an integer. |
| 1151 |
|
| 1152 |
Can_Be_Integer := False; |
| 1153 |
end if; |
| 1154 |
|
| 1155 |
if Text (Pos.Index) = '-' or else Text (Pos.Index) = '+' then |
| 1156 |
Next_Char; |
| 1157 |
end if; |
| 1158 |
|
| 1159 |
if Pos.Index > Text'Last |
| 1160 |
or else Text (Pos.Index) not in '0' .. '9' |
| 1161 |
then |
| 1162 |
Error ("invalid number"); |
| 1163 |
end if; |
| 1164 |
|
| 1165 |
while Pos.Index <= Text'Last |
| 1166 |
and then Text (Pos.Index) in '0' .. '9' |
| 1167 |
loop |
| 1168 |
Token_End := Pos; |
| 1169 |
Next_Char; |
| 1170 |
end loop; |
| 1171 |
end if; |
| 1172 |
|
| 1173 |
if Is_Token_Sep then |
| 1174 |
-- Valid decimal number |
| 1175 |
|
| 1176 |
if Can_Be_Integer then |
| 1177 |
Kind := J_INTEGER; |
| 1178 |
else |
| 1179 |
Kind := J_NUMBER; |
| 1180 |
end if; |
| 1181 |
return; |
| 1182 |
else |
| 1183 |
Error ("invalid number"); |
| 1184 |
end if; |
| 1185 |
elsif CC = EOF then |
| 1186 |
Kind := J_EOF; |
| 1187 |
else |
| 1188 |
Error ("Unexpected character"); |
| 1189 |
end if; |
| 1190 |
end Read_Token; |
| 1191 |
|
| 1192 |
---------------------------- |
| 1193 |
-- Read_Token_And_Error -- |
| 1194 |
---------------------------- |
| 1195 |
|
| 1196 |
procedure Read_Token_And_Error |
| 1197 |
(TK : Token_Kind; |
| 1198 |
Token_Start : out Text_Position; |
| 1199 |
Token_End : out Text_Position) |
| 1200 |
is |
| 1201 |
Kind : Token_Kind; |
| 1202 |
|
| 1203 |
begin |
| 1204 |
-- Read a token and errout out if not of the expected kind |
| 1205 |
|
| 1206 |
Read_Token (Kind, Token_Start, Token_End); |
| 1207 |
if Kind /= TK then |
| 1208 |
Error ("specific token expected"); |
| 1209 |
end if; |
| 1210 |
end Read_Token_And_Error; |
| 1211 |
|
| 1212 |
------------------------- |
| 1213 |
-- Read_Variant_Part -- |
| 1214 |
------------------------- |
| 1215 |
|
| 1216 |
function Read_Variant_Part return Nat is |
| 1217 |
Next : Nat := 0; |
| 1218 |
TK : Token_Kind; |
| 1219 |
Token_Start : Text_Position; |
| 1220 |
Token_End : Text_Position; |
| 1221 |
Var : JSON_Variant_Node; |
| 1222 |
|
| 1223 |
begin |
| 1224 |
-- Read a nonempty array of components |
| 1225 |
|
| 1226 |
Read_Token_And_Error (J_ARRAY, Token_Start, Token_End); |
| 1227 |
|
| 1228 |
loop |
| 1229 |
Read_Token_And_Error (J_OBJECT, Token_Start, Token_End); |
| 1230 |
|
| 1231 |
Var.Variant := 0; |
| 1232 |
|
| 1233 |
-- Read the members as string : value pairs |
| 1234 |
|
| 1235 |
loop |
| 1236 |
case Read_String is |
| 1237 |
when Name_Present => |
| 1238 |
Var.Present := Read_Numerical_Expr; |
| 1239 |
when Name_Record => |
| 1240 |
Read_Record; |
| 1241 |
when Name_Variant => |
| 1242 |
Var.Variant := Read_Variant_Part; |
| 1243 |
when others => |
| 1244 |
Error ("invalid variant"); |
| 1245 |
end case; |
| 1246 |
|
| 1247 |
Read_Token (TK, Token_Start, Token_End); |
| 1248 |
if TK = J_OBJECT_END then |
| 1249 |
exit; |
| 1250 |
elsif TK /= J_COMMA then |
| 1251 |
Error ("comma expected"); |
| 1252 |
end if; |
| 1253 |
end loop; |
| 1254 |
|
| 1255 |
-- Chain the variant and store it into the table |
| 1256 |
|
| 1257 |
Var.Next := Next; |
| 1258 |
JSON_Variant_Table.Append (Var); |
| 1259 |
Next := JSON_Variant_Table.Last; |
| 1260 |
|
| 1261 |
Read_Token (TK, Token_Start, Token_End); |
| 1262 |
if TK = J_ARRAY_END then |
| 1263 |
exit; |
| 1264 |
elsif TK /= J_COMMA then |
| 1265 |
Error ("comma expected"); |
| 1266 |
end if; |
| 1267 |
end loop; |
| 1268 |
|
| 1269 |
return Next; |
| 1270 |
end Read_Variant_Part; |
| 1271 |
|
| 1272 |
------------------ |
| 1273 |
-- Skip_Value -- |
| 1274 |
------------------ |
| 1275 |
|
| 1276 |
procedure Skip_Value is |
| 1277 |
Array_Depth : Natural := 0; |
| 1278 |
Object_Depth : Natural := 0; |
| 1279 |
TK : Token_Kind; |
| 1280 |
Token_Start : Text_Position; |
| 1281 |
Token_End : Text_Position; |
| 1282 |
|
| 1283 |
begin |
| 1284 |
-- Read a value without recursing |
| 1285 |
|
| 1286 |
loop |
| 1287 |
Read_Token (TK, Token_Start, Token_End); |
| 1288 |
|
| 1289 |
case TK is |
| 1290 |
when J_STRING | J_INTEGER | J_NUMBER => |
| 1291 |
null; |
| 1292 |
when J_ARRAY => |
| 1293 |
Array_Depth := Array_Depth + 1; |
| 1294 |
when J_ARRAY_END => |
| 1295 |
Array_Depth := Array_Depth - 1; |
| 1296 |
when J_OBJECT => |
| 1297 |
Object_Depth := Object_Depth + 1; |
| 1298 |
when J_OBJECT_END => |
| 1299 |
Object_Depth := Object_Depth - 1; |
| 1300 |
when J_COLON | J_COMMA => |
| 1301 |
if Array_Depth = 0 and then Object_Depth = 0 then |
| 1302 |
Error ("value expected"); |
| 1303 |
end if; |
| 1304 |
when others => |
| 1305 |
Error ("value expected"); |
| 1306 |
end case; |
| 1307 |
|
| 1308 |
exit when Array_Depth = 0 and then Object_Depth = 0; |
| 1309 |
end loop; |
| 1310 |
end Skip_Value; |
| 1311 |
|
| 1312 |
Token_Start : Text_Position; |
| 1313 |
Token_End : Text_Position; |
| 1314 |
TK : Token_Kind; |
| 1315 |
Is_First : Boolean := True; |
| 1316 |
|
| 1317 |
-- Start of processing for Read_JSON_Stream |
| 1318 |
|
| 1319 |
begin |
| 1320 |
-- Read a possibly empty array of entities |
| 1321 |
|
| 1322 |
Read_Token_And_Error (J_ARRAY, Token_Start, Token_End); |
| 1323 |
|
| 1324 |
loop |
| 1325 |
Read_Token (TK, Token_Start, Token_End); |
| 1326 |
if Is_First and then TK = J_ARRAY_END then |
| 1327 |
exit; |
| 1328 |
elsif TK /= J_OBJECT then |
| 1329 |
Error ("object expected"); |
| 1330 |
end if; |
| 1331 |
|
| 1332 |
Read_Entity; |
| 1333 |
|
| 1334 |
Read_Token (TK, Token_Start, Token_End); |
| 1335 |
if TK = J_ARRAY_END then |
| 1336 |
exit; |
| 1337 |
elsif TK /= J_COMMA then |
| 1338 |
Error ("comma expected"); |
| 1339 |
end if; |
| 1340 |
|
| 1341 |
Is_First := False; |
| 1342 |
end loop; |
| 1343 |
end Read_JSON_Stream; |
| 1344 |
|
| 1345 |
end Repinfo.Input; |