ocarina-fe_real-parser.adb 57.6 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--               O C A R I N A . F E _ R E A L . P A R S E R                --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 2009-2011, European Space Agency (ESA).           --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with GNAT.Table;
with GNAT.Command_Line;
with Ocarina.FE_REAL.Lexer;
with Ocarina.FE_REAL.Parser_Errors;
with Ocarina.ME_REAL.Tokens;
with Ocarina.ME_REAL.REAL_Tree.Nodes;
with Ocarina.ME_REAL.REAL_Tree.Utils;
with Ocarina.ME_REAL.REAL_Tree.Nutils;
with Ocarina.REAL_Values;
with Ocarina.Builder.REAL;
with Ocarina.Parser;
with Ocarina.Files;
with Ocarina.Analyzer.REAL;
with Namet;

package body Ocarina.FE_REAL.Parser is
   use Ocarina.ME_REAL.REAL_Tree.Nodes;
   use Ocarina.ME_REAL.REAL_Tree.Utils;
   use Ocarina.ME_REAL.REAL_Tree.Nutils;
   use Ocarina.ME_REAL.Tokens;
   use Ocarina.Builder.REAL;
   use Ocarina.FE_REAL.Lexer;
   use Ocarina.FE_REAL.Parser_Errors;

   function P_Theorem return Node_Id;

   function P_Set_Range_Declaration return Node_Id;

   procedure P_Declarations (R : Node_Id; Success : out Boolean);
   --  Parse sets and variables declarations

   function Create_Check_Expression return Node_Id;

   function P_Check_Expression return Node_Id;
   --  FIXME :
   --  Bug with 'true' and 'false' boolean literals which
   --  cannot be parsed yet, cause unknown.

   function P_Set_Expression return Node_Id;
   function P_Higher_Level_Function return Value_Id;
   function P_Single_Set_Declaration return Node_Id;
   function P_Create_Set_Identifier return Node_Id;
   function P_Check_Subprogram_Call return Node_Id;
   function P_Ternary_Expression return Node_Id;
   function P_Expression return Node_Id;

   function P_Requirements return List_Id;
   --  Get required theorems.
   --  Does *not* check for existence (cf. analyzer)

   function P_Identifier return Node_Id;
   function Make_Literal (T : Token_Type) return Node_Id;

   procedure Skip_To_Theorem_End (Success : out Boolean);
   --  Whenever an error occurs in parsing, this procedure will
   --  search for the nearest 'end;' keywords, in order to avoid
   --  parsing errors to spread to the following theorems.

   Current_Theorem_Node : Node_Id;
   Current_Theorem_Name : Name_Id;

   package Expressions is new GNAT.Table (Node_Id, Natural, 1, 100, 10);
   package REAL_Libs is new GNAT.Table (Name_Id, Nat, 1, 10, 10);

   Preferences : constant array (OV_Equal .. OV_Power) of Natural
     := (OV_Power            => 1,
         OV_Not              => 2,
         OV_Star             => 3,
         OV_Slash            => 4,
         OV_Modulo           => 5,
         OV_Minus            => 6,
         OV_Plus             => 7,
         OV_Greater          => 8,
         OV_Less             => 9,
         OV_Greater_Equal    => 10,
         OV_Less_equal       => 11,
         OV_Different        => 12,
         OV_Equal            => 13,
         OV_Or               => 14,
         OV_And              => 15);

   ----------------------
   -- P_Set_Expression --
   ----------------------

   function P_Set_Expression return Node_Id is
      use Expressions;

      function Is_Expression_Completed return Boolean;
      --  Return True when there are no more token to read to complete
      --  the current expression.

      function P_Expression_Part return Node_Id;
      --  Return a node describing an expression. It is either a
      --  binary operator (an operator with no right expression
      --  assigned) or an expression value (a scoped name, a literal
      --  or an expression with an unary operator - that is a binary
      --  operator with a right inner expression and no left inner
      --  expression - or an expression with both inner expressions
      --  assigned). Note that whether an operator is a binary or
      --  unary operator is resolved in this routine. For a unary
      --  operator, we check that the previous token was a binary
      --  operator.

      function Is_Binary_Operator (E : Node_Id) return Boolean;
      --  Return True when N is an operator with the right expression
      --  *still* not assigned. Otherwise, an operator with a right
      --  expression is a value expression.

      function Is_Expression_Value (E : Node_Id) return Boolean;
      --  Return True when N is not an operator (literal or scoped
      --  name) or else when its right expression is assigned (unary
      --  operator).

      function Precede (L, R : Node_Id) return Boolean;
      --  Does operator L precedes operator R

      function Translate_Operator (T : Token_Type) return Operator_Id;
      --  Return the Operator_Id corresponding to the token read

      ------------------------
      -- Translate_Operator --
      ------------------------

      function Translate_Operator (T : Token_Type) return Operator_Id is
      begin
         case T is
            when T_Star =>
               return OV_Star;

            when T_Plus =>
               return OV_Plus;

            when T_Minus =>
               return OV_Minus;

            when others =>
               return OV_Invalid;
         end case;
      end Translate_Operator;

      -----------------------------
      -- Is_Expression_Completed --
      -----------------------------

      function Is_Expression_Completed return Boolean
      is
         T : constant Token_Type := Next_Token;
      begin
         return T /= T_Identifier
           and then T not in Predefined_Sets
           and then T /= T_Left_Paren
           and then not Is_Set_Operator (T);
      end Is_Expression_Completed;

      -------------------------
      -- Is_Expression_Value --
      -------------------------

      function Is_Expression_Value (E : Node_Id) return Boolean is
      begin
         return Kind (E) = K_Set
           or else (Kind (E) = K_Set_Expression
                    and then Present (Right_Expr (E)));
      end Is_Expression_Value;

      ------------------------
      -- Is_Binary_Operator --
      ------------------------

      function Is_Binary_Operator (E : Node_Id) return Boolean is
      begin
         return Kind (E) = K_Set_Expression
           and then Operator (E) in Operator_Values
           and then No (Right_Expr (E));
      end Is_Binary_Operator;

      -----------------------
      -- P_Expression_Part --
      -----------------------

      function P_Expression_Part return Node_Id is
         Expression     : Node_Id;
         Previous_Token : Token_Type;
         Op             : Operator_Id;
      begin
         case Next_Token is

            when T_Identifier =>

               --  We build a set reference with identifier name
               Scan_Token;
               Expression := New_Node (K_Set_Reference, Token_Location);
               Set_Name (Expression, To_Lower (Token_Name));
               Set_Predefined_Type (Expression, SV_No_Type);
               Set_Referenced_Set (Expression, No_Node);

            when T_Processor_Set .. T_Local_Set =>

               --  We build a set reference with predefined name
               Scan_Token;
               Expression := New_Node (K_Set_Reference, Token_Location);
               Set_Name (Expression, To_Lower (Token_Name));
               Set_Predefined_Type
                 (Expression, Translate_Predefined_Sets (Token));
               Set_Referenced_Set (Expression, No_Node);

            when T_Left_Paren =>

               --  Look for a parenthesized expression value

               --  past '(', no error possible

               Scan_Token (T_Left_Paren);

               Expression := P_Set_Expression;

               Scan_Token (T_Right_Paren);
               if Token = T_Error then
                  DPE (PC_Set_Expression, T_Right_Paren);
                  return No_Node;
               end if;

            when T_Plus | T_Minus | T_Star =>

               --  Look for a binary/unary operator

               Previous_Token := Token;
               Scan_Token;  --  past binary/unary operator

               Expression := New_Node (K_Set_Expression, Token_Location);
               Set_Set_Type (Expression, SV_No_Type);

               Op := Translate_Operator (Token);
               if Op = OV_Invalid then
                  DPE (PC_Set_Expression, EMC_Expected_Valid_Operator);
                  Set_Last (First - 1);
                  return No_Node;
               end if;
               Set_Operator (Expression, Op);

               --  Cannot have two following operators except in the
               --  special case above.

               if Is_Operator (Previous_Token) then
                  DPE (PC_Set_Expression, EMC_Expected_Valid_Operator);
                  return No_Node;
               end if;

            when others =>
               DPE (PC_Set_Expression, EMC_Cannot_Parse_Set_Expression);
               return No_Node;
         end case;

         return Expression;
      end P_Expression_Part;

      -------------
      -- Precede --
      -------------

      function Precede (L, R : Node_Id) return Boolean is
         Left_Operator  : constant Operator_Id := Operator (L);
         Right_Operator : constant Operator_Id := Operator (R);
      begin
         return Preferences (Left_Operator) <
           Preferences (Right_Operator);
      end Precede;

      Expr     : Node_Id;
      First    : Natural;
   begin

      --  Read enough expressions to push as first expression a binary
      --  operator with no right expression

      Expr := P_Expression_Part;
      if No (Expr) then
         return No_Node;
      end if;

      --  We must have first an expression value

      if Is_Binary_Operator (Expr) then
         DPE (PC_Set_Expression, EMC_Cannot_Parse_Set_Expression);
         return No_Node;
      end if;

      --  We have only one expression value

      if Is_Expression_Completed then
         return Expr;
      end if;

      Increment_Last;
      Table (Last) := Expr;
      First := Last;

      Expr := P_Expression_Part;
      if No (Expr) then
         Set_Last (First - 1);
         return No_Node;
      end if;

      --  We must have a binary operator as the first expression is an
      --  expression value.

      if not Is_Binary_Operator (Expr) then
         DPE (PC_Set_Expression, EMC_Cannot_Parse_Set_Expression);
         Set_Last (First - 1);
         return No_Node;
      end if;

      Set_Left_Expr (Expr, Table (Last));
      Table (Last) := Expr;

      --  Push expressions in stack and check that the top of the
      --  stack consists in one or more binary operators with no
      --  right expr and zero or one expression value.

      while not Is_Expression_Completed loop

         Expr := P_Expression_Part;
         if No (Expr) then
            return No_Node;
         end if;

         Increment_Last;
         Table (Last) := Expr;

         --  Check that this new expression is not a binary operator
         --  when the previous one is a binary operator with no right
         --  expression.

         if First < Last
           and then Is_Binary_Operator (Expr)
           and then No (Left_Expr (Expr))
           and then Is_Binary_Operator (Table (Last - 1))
         then
            DPE (PC_Set_Expression, EMC_Cannot_Parse_Set_Expression);
            Set_Last (First - 1);
            return No_Node;
         end if;

         --  Check whether we have a sequence of a binary operator
         --  (left operator), an expression value and another binary
         --  operator (right operator). In this case, if the left
         --  operator has a better precedence than the right one, we
         --  can reduce the global expression by assigning the
         --  expression value to the right expression of the left
         --  operator. Then as the left operator has already a left
         --  expression, it becomes an expression value which can be
         --  assign to the left expression of the right operation.
         --  Recompute the size of the expression stack.

         while First + 1 < Last
           and then Is_Expression_Value (Table (Last - 1))
           and then Precede (Table (Last - 2), Expr)
         loop
            Set_Right_Expr (Table (Last - 2), Table (Last - 1));
            Set_Left_Expr  (Table (Last), Table (Last - 2));
            Table (Last - 2) := Table (Last);
            Set_Last (Last - 2);
         end loop;
      end loop;

      --  The last expression is not a value. We cannot reduce the
      --  global expression

      if Is_Binary_Operator (Table (Last)) then
         DPE (PC_Set_Expression, EMC_Cannot_Parse_Set_Expression);
         Set_Last (First - 1);
         return No_Node;
      end if;

      --  Reduce the global expression

      while First < Last loop
         if No (Left_Expr (Table (Last - 1))) then
            Set_Right_Expr (Table (Last - 1), Table (Last));
            Set_Left_Expr  (Table (Last - 1), Table (Last - 2));
            Table (Last - 2) := Table (Last - 1);
            Set_Last (Last - 2);

         else
            Set_Right_Expr (Table (Last - 1), Table (Last));
            Set_Last (Last - 1);
         end if;
      end loop;

      Expr := Table (First);
      Set_Last (First - 1);

      return Expr;

   end P_Set_Expression;

   ------------------------
   -- P_Check_Expression --
   ------------------------

   function P_Check_Expression return Node_Id is
      use Expressions;

      function Is_Expression_Completed return Boolean;
      --  Return True when there are no more token to read to complete
      --  the current expression.

      function P_Expression_Part return Node_Id;
      --  Return a node describing an expression. It is either a
      --  binary operator (an operator with no right expression
      --  assigned) or an expression value (a scoped name, a literal
      --  or an expression with an unary operator - that is a binary
      --  operator with a right inner expression and no left inner
      --  expression - or an expression with both inner expressions
      --  assigned). Note that whether an operator is a binary or
      --  unary operator is resolved in this routine. For a unary
      --  operator, we check that the previous token was a binary
      --  operator.

      function Is_Binary_Operator (E : Node_Id) return Boolean;
      --  Return True when N is an operator with the right expression
      --  *still* not assigned. Otherwise, an operator with a right
      --  expression is a value expression.

      function Is_Expression_Value (E : Node_Id) return Boolean;
      --  Return True when N is not an operator (literal or scoped
      --  name) or else when its right expression is assigned (unary
      --  operator).

      function Precede (L, R : Node_Id) return Boolean;
      --  Does operator L precedes operator R

      function Translate_Operator (T : Token_Type) return Operator_Id;
      --  Return the operator corresponding to the parameter-given token

      ------------------------
      -- Translate_Operator --
      ------------------------

      function Translate_Operator (T : Token_Type) return Operator_Id is
      begin
         case T is
            when T_Or =>
               return OV_Or;

            when T_And =>
               return OV_And;

            when T_Not =>
               return OV_Not;

            when T_Equal =>
               return OV_Equal;

            when T_Different =>
               return OV_Different;

            when T_Greater =>
               return OV_Greater;

            when T_Less =>
               return OV_Less;

            when T_Greater_Equal =>
               return OV_Greater_Equal;

            when T_Less_Equal =>
               return OV_Less_Equal;

            when T_Minus =>
               return OV_Minus;

            when T_Plus =>
               return OV_Plus;

            when T_Modulo =>
               return OV_Modulo;

            when T_Star =>
               return OV_Star;

            when T_Slash =>
               return OV_Slash;

            when T_Power =>
               return OV_Power;

            when others =>
               return OV_Invalid;
         end case;
      end Translate_Operator;

      -----------------------------
      -- Is_Expression_Completed --
      -----------------------------

      function Is_Expression_Completed return Boolean
      is
         T : constant Token_Type := Next_Token;
      begin
         return T not in Literal_Type
           and then T /= T_Identifier
           and then T not in Selection_Function_Type
           and then T not in Verification_Function_Type
           and then T /= T_Left_Paren
           and then T not in T_Equal .. T_Power;
      end Is_Expression_Completed;

      -------------------------
      -- Is_Expression_Value --
      -------------------------

      function Is_Expression_Value (E : Node_Id) return Boolean is
      begin
         return Kind (E) = K_Literal
           or else Kind (E) = K_Identifier
           or else Kind (E) = K_Var_Reference
           or else (Operator (E) not in OV_Equal .. OV_Power
                    and then Operator (E) /= OV_Equal)
           or else Present (Right_Expr (E));
      end Is_Expression_Value;

      ------------------------
      -- Is_Binary_Operator --
      ------------------------

      function Is_Binary_Operator (E : Node_Id) return Boolean is
      begin
         return Kind (E) = K_Check_Expression
           and then Operator (E) in OV_Equal .. OV_Power
           and then No (Right_Expr (E));
      end Is_Binary_Operator;

      -----------------------
      -- P_Expression_Part --
      -----------------------

      function P_Expression_Part return Node_Id is
         Expression       : Node_Id;
         Right_Expr       : Node_Id;
         Previous_Token   : Token_Type;
         Op               : Operator_Id;
      begin
         case Next_Token is

            when T_Is_Subcomponent_Of .. T_Is_Connecting_To =>

               --  We build a check (in this case, verification)
               --  subprogram call

               Expression := P_Check_Subprogram_Call;
               if No (Expression) then
                  return No_Node;
               end if;

            when T_Get_Property_Value .. T_Sum =>

               --  We build a check subprogram call

               Expression := P_Check_Subprogram_Call;
               if No (Expression) then
                  return No_Node;
               end if;

            when T_Left_Paren =>

               --  Look for a parenthesized expression value

               Scan_Token;  --  past '('
               Expression := P_Expression;
               Scan_Token (T_Right_Paren);
               if Token = T_Error then
                  DPE (PC_Check_Expression, T_Right_Paren);
                  return No_Node;
               end if;

            when T_Equal .. T_Power =>

               --  Look for a binary/unary operator

               Previous_Token := Token;
               Scan_Token;  --  past binary/unary operator
               Expression := New_Node (K_Check_Expression, Token_Location);

               Op := Translate_Operator (Token);
               if Op = OV_Invalid then
                  DPE (PC_Check_Expression, EMC_Expected_Valid_Operator);
                  Set_Last (First - 1);
                  return No_Node;
               end if;
               Set_Operator (Expression, Op);

               --  Token is a real unary operator

               if Token = T_Not or else
                 (Token = T_Minus
                  and then Previous_Token /= T_Affect
                  and then Previous_Token /= T_Right_Paren
                  and then (Is_Operator (Previous_Token)
                            or else Previous_Token = T_Left_Paren))
               then
                  case Next_Token is
                     when T_Get_Property_Value .. T_Sum
                       | T_Left_Paren
                       | T_Identifier =>

                        Right_Expr := P_Expression;
                        if No (Right_Expr) then
                           return No_Node;
                        end if;
                        Set_Right_Expr (Expression, Right_Expr);

                     when others =>
                        DPE (PC_Check_Expression,
                             EMC_Expected_Valid_Operator);
                        return No_Node;
                  end case;

                  --  Cannot have two following operators except in the
                  --  special case above.

               elsif Is_Operator (Previous_Token) then
                  DPE (PC_Check_Expression, EMC_Unexpected_Operator);
                  return No_Node;
               end if;

            when T_False .. T_Wide_String_Literal =>

               --  Look for a literal

               Scan_Token;
               Expression := Make_Literal (Token);

            when T_Identifier =>

               --  The only identifier allowed are variables names

               Expression := P_Identifier;
               Expression := Make_Var_Reference (Name (Expression));

            when others =>
               DPE (PC_Check_Expression, EMC_Cannot_Parse_Check_Expression);
               return No_Node;
         end case;

         return Expression;
      end P_Expression_Part;

      -------------
      -- Precede --
      -------------

      function Precede (L, R : Node_Id) return Boolean is
         Left_Operator  : constant Operator_Id := Operator (L);
689
--         Right_Operator : constant Operator_Id := Operator (R);
690
      begin
691 692 693 694 695
         if Kind (R) = K_Check_Subprogram_Call then
            return True;
         end if;

         return Preferences (Left_Operator) < Preferences (Operator (R));
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
      end Precede;

      Expr     : Node_Id;
      First    : Natural;
   begin

      --  Read enough expressions to push as first expression a binary
      --  operator with no right expressio

      Expr := P_Expression_Part;
      if No (Expr) then
         return No_Node;
      end if;

      --  We must have first an expression value

      if Is_Binary_Operator (Expr) then
         DPE (PC_Check_Expression, EMC_Cannot_Parse_Check_Expression);
         return No_Node;
      end if;

      --  We have only one expression value

      if Is_Expression_Completed then
         return Expr;
      end if;

      Increment_Last;
      Table (Last) := Expr;
      First := Last;

      Expr := P_Expression_Part;
      if No (Expr) then
         Set_Last (First - 1);
         return No_Node;
      end if;

      --  We must have a binary operator as the first expression is an
      --  expression value.

      if not Is_Binary_Operator (Expr) then
         DPE (PC_Check_Expression, EMC_Cannot_Parse_Check_Expression);
         Set_Last (First - 1);
         return No_Node;
      end if;

      Set_Left_Expr (Expr, Table (Last));
      Table (Last) := Expr;

      --  Push expressions in stack and check that the top of the
      --  stack consists in one or more binary operators with no
      --  right expr and zero or one expression value.

      while not Is_Expression_Completed loop

         Expr := P_Expression_Part;
         if No (Expr) then
            return No_Node;
         end if;

         Increment_Last;
         Table (Last) := Expr;

         --  Check that this new expression is not a binary operator
         --  when the previous one is a binary operator with no right
         --  expression.

         if First < Last
           and then Is_Binary_Operator (Expr)
           and then No (Left_Expr (Expr))
           and then Is_Binary_Operator (Table (Last - 1))
         then
            DPE (PC_Check_Expression, EMC_Cannot_Parse_Check_Expression);
            Set_Last (First - 1);
            return No_Node;
         end if;

         --  Check whether we have a sequence of a binary operator
         --  (left operator), an expression value and another binary
         --  operator (right operator). In this case, if the left
         --  operator has a better precedence than the right one, we
         --  can reduce the global expression by assigning the
         --  expression value to the right expression of the left
         --  operator. Then as the left operator has already a left
         --  expression, it becomes an expression value which can be
781
         --  assigned to the left expression of the right operation.
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
         --  Recompute the size of the expression stack.

         while First + 1 < Last
           and then Kind (Table (Last - 1)) /= K_Check_Subprogram_Call
           and then Is_Expression_Value (Table (Last - 1))
           and then Precede (Table (Last - 2), Expr)
         loop
            Set_Right_Expr (Table (Last - 2), Table (Last - 1));
            Set_Left_Expr  (Table (Last), Table (Last - 2));
            Table (Last - 2) := Table (Last);
            Set_Last (Last - 2);
         end loop;
      end loop;

      --  The last expression is not a value. We cannot reduce the
      --  global expression

      if Is_Binary_Operator (Table (Last)) then
         DPE (PC_Check_Expression, EMC_Cannot_Parse_Check_Expression);
         Set_Last (First - 1);
         return No_Node;
      end if;

      --  Reduce the global expression

      while First < Last loop
         if No (Left_Expr (Table (Last - 1))) then
            Set_Right_Expr (Table (Last - 1), Table (Last));
            Set_Left_Expr  (Table (Last - 1), Table (Last - 2));
            Table (Last - 2) := Table (Last - 1);
            Set_Last (Last - 2);

         else
            Set_Right_Expr (Table (Last - 1), Table (Last));
            Set_Last (Last - 1);
         end if;
      end loop;
      Expr := Table (First);
      Set_Last (First - 1);

      return Expr;
   end P_Check_Expression;

   --------------------------
   -- P_Ternary_Expression --
   --------------------------

   function P_Ternary_Expression return Node_Id is

      function Translate_Operator (T : Token_Type) return Operator_Id;

      ------------------------
      -- Translate_Operator --
      ------------------------

      function Translate_Operator (T : Token_Type) return Operator_Id is
      begin
         case T is
            when T_If =>
               return OV_If_Then_Else;
            when others =>
               return OV_Invalid;
         end case;
      end Translate_Operator;

      N : Node_Id;
      E1 : Node_Id;
      E2 : Node_Id;
      E3 : Node_Id;
   begin
      N := New_Node (K_Ternary_Expression, Token_Location);

      Scan_Token;
      Set_Operator (N, Translate_Operator (Token));

      case Token is
         when T_If =>
            E1 := P_Expression;
            if No (E1) then
               return No_Node;
            end if;

            Scan_Token (T_Then);
            if Token = T_Error then
               DPE (PC_Ternary_Expression, T_Then);
            end if;

            E2 := P_Expression;
            if No (E2) then
               return No_Node;
            end if;

            Scan_Token (T_Else);
            if Token = T_Error then
               DPE (PC_Ternary_Expression, T_Else);
            end if;

            E3 := P_Expression;
            if No (E3) then
               return No_Node;
            end if;

         when others =>
            return No_Node;
      end case;

      Set_Left_Expr (N, E1);
      Set_Right_Expr (N, E2);
      Set_Third_Expr (N, E3);

      return N;
   end P_Ternary_Expression;

   ------------------
   -- P_Expression --
   ------------------

   function P_Expression return Node_Id is
      L : Location;
   begin
      Save_Lexer (L);
      Scan_Token;
      if Token = T_If then
         Restore_Lexer (L);
         return P_Ternary_Expression;
      else
         Restore_Lexer (L);
         return P_Check_Expression;
      end if;
   end P_Expression;

   -----------------------------
   -- P_Check_Subprogram_Call --
   -----------------------------

   function P_Check_Subprogram_Call return Node_Id is
      N     : Node_Id;
      L     : constant List_Id := New_List (K_List_Id, Token_Location);
      Param : Node_Id;
   begin
      N := New_Node (K_Check_Subprogram_Call, Token_Location);

      if Next_Token not in Verification_Function_Type and then
        Next_Token not in Selection_Function_Type then
         Scan_Token;
         DPE (PC_Check_Subprogram_Call, EMC_Expected_Predefined_Function_Name);
         return No_Node;
      end if;
      Set_Variable_Position (N, Value_Id (0));
      Set_Identifier (N, P_Identifier);
      Set_Code (N, Translate_Function_Code (Token));

      --  it's possible that a subprogram was called without args
      --  (eg. when passed as parameter)

      if Next_Token /= T_Left_Paren then
         Set_Parameters (N, No_List);
         return N;
      else
         --  No error possible

         Scan_Token (T_Left_Paren);
      end if;

      --  We parse all parameters

      while Next_Token /= T_Right_Paren loop

         if Next_Token = T_EOF then
            DPE (PC_Check_Subprogram_Call, T_Right_Paren);
            return No_Node;
         end if;

         --  We could have either a literal, a set name (identifier
         --  or predefined set) or a check expression.

         case Next_Token is

            when T_Get_Property_Value .. T_Sum =>

               --  Verification function name

               Param := P_Check_Subprogram_Call;
               Append_Node_To_List (Param, L);

            when T_Is_Subcomponent_Of .. T_Is_Connecting_To =>

               --  selection function name

               Param := P_Check_Subprogram_Call;
               Append_Node_To_List (Param, L);

            when T_Processor_Set .. T_Local_Set =>

               --  We build a set reference with predefined name

               Scan_Token;
               Param := New_Node (K_Set_Reference, Token_Location);
               Set_Name (Param, To_Lower (Token_Name));
               Set_Predefined_Type
                 (Param, Translate_Predefined_Sets (Token));
               Set_Referenced_Set (Param, No_Node);
               Append_Node_To_List (Param, L);

            when T_Identifier =>

               --  Must be a set name or the global set identifier or
               --  a variable name

               Scan_Token;
               Param := New_Node (K_Identifier, Token_Location);
               Set_Name (Param, Token_Name);
               Append_Node_To_List (Param, L);

            when T_Integer_Literal .. T_Wide_String_Literal =>

               --  A literal

               Scan_Token;
               Param := Make_Literal (Token);
               Append_Node_To_List (Param, L);

            when T_Left_Paren =>

               --  A selection expression

               Param := Create_Check_Expression;
               Append_Node_To_List (Param, L);

            when others =>
               DPE (PC_Check_Subprogram_Call,
                    EMC_Expected_Valid_Function_Parameter);
               return No_Node;
         end case;

         if Next_Token /= T_Comma then

            if Next_Token /= T_Right_Paren then
               Scan_Token;
               DPE (PC_Check_Subprogram_Call, T_Right_Paren);
               return No_Node;
            end if;
         else
            --  We pass the comma, no error possible

            Scan_Token (T_Comma);
         end if;

      end loop;

      Scan_Token (T_Right_Paren);
      if Token = T_Error then
         DPE (PC_Check_Subprogram_Call, T_Right_Paren);
         return No_Node;
      end if;

      Set_Parameters (N, L);
      return N;

   end P_Check_Subprogram_Call;

   ------------------
   -- Make_Literal --
   ------------------

   function Make_Literal (T : Token_Type) return Node_Id
   is
      use Ocarina.REAL_Values;

      Const : constant Node_Id := New_Node (K_Literal, Token_Location);
   begin
      case T is
         when T_String_Literal | T_Wide_String_Literal =>
            Set_Value (Const, New_String_Value
                       (To_Lower (String_Literal_Value)));

         when T_True =>
            Set_Value (Const, New_Boolean_Value (True));

         when T_False =>
            Set_Value (Const, New_Boolean_Value (False));

         when T_Integer_Literal =>
            Set_Value (Const, New_Integer_Value
                       (Value => Unsigned_Long_Long (Integer_Literal_Value),
                        Base => Unsigned_Short_Short (Integer_Literal_Base)));

         when T_Floating_Point_Literal =>
            Set_Value (Const, New_Real_Value
                       (Float_Literal_Value));

         when others =>
            DPE (PC_Make_Literal, EMC_Expected_Literal_Value);
            return No_Node;
      end case;

      return Const;
   end Make_Literal;

   -----------------------------
   -- P_Create_Set_Identifier --
   -----------------------------

   function P_Create_Set_Identifier return Node_Id is
      N     : Node_Id;
      Ident : Node_Id;
      Param : Node_Id := No_Node;
      L     : List_Id := No_List;
   begin
      --  Set identifier can be either parametrized
      --  or not

      --  First we read the set identifier

      Ident := P_Identifier;
      if No (Ident) then
         return No_Node;
      end if;

      --  Then if we have a left parenthesis
      --  the set is parametrized by an identifier

      if Next_Token = T_Left_Paren then

         --  We skip the '('
         Scan_Token;

         Param := P_Identifier;

         if No (Param) then
            return No_Node;
         end if;

         Scan_Token (T_Right_Paren);
         if Token = T_Error then
            DPE (PC_Create_Set_Identifier, T_Right_Paren);
            return No_Node;
         end if;

         L := New_List (K_List_Id, Token_Location);
         Append_Node_To_List (Param, L);
      end if;

      N := New_Node (K_Parametrized_Identifier, Token_Location);
      Set_Identifier (N, Ident);
      Set_Parameters (N, L);

      return N;
   end P_Create_Set_Identifier;

   ------------------------------
   -- P_Single_Var_Declaration --
   ------------------------------

   function P_Single_Var_Declaration return Node_Id is
      Var_Decl  : Node_Id;
      Var_Id    : Node_Id;
      Var, P    : Node_Id;
      Trm_Id    : Node_Id;
      Params    : List_Id := No_List;
      Global    : Boolean;
   begin
      --  Parse the variable range keyword (var or global)

      Scan_Token;
      case Token is
         when T_Var =>
            Global := False;
         when T_Global =>
            Global := True;
         when others =>
            DPE (PC_Single_Variable_Declaration, EMC_Variable_Declaration);
            return No_Node;
      end case;

      --  Create variable identifier

      Var_Id := P_Identifier;
      if No (Var_Id) then
         return No_Node;
      end if;
      Var := Make_Var_Reference (Name (Var_Id));

      --  Parse the affectation (":=")

      Scan_Token (T_Affect);
      if Token = T_Error then
         DPE (PC_Single_Variable_Declaration, T_Affect);
         return No_Node;
      end if;

      --  A variable declaration can be done either with
      --  a expression or with a call to another theorem

      if Next_Token = T_Compute then
         Scan_Token (T_Compute);

         Var_Decl := New_Node (K_Variable_Decl_Compute, Token_Location);

         Trm_Id := P_Identifier;
         if No (Trm_Id) then
            return No_Node;
         end if;

         if Next_Token = T_Left_Paren then
            Scan_Token (T_Left_Paren);
            Params := New_List (K_List_Id, Token_Location);

            --  The first parameter must be the sub-theorem domain
            --  Thus a set or a variable

            case Next_Token is

               when T_Identifier =>
                  Scan_Token;
                  P := New_Node (K_Identifier, Token_Location);
                  Set_Name (P, To_Lower (Token_Name));
                  Append_Node_To_List (P, Params);

               when T_Processor_Set .. T_Local_Set =>
                  Scan_Token;
                  P := New_Node (K_Set_Reference, Token_Location);
                  Set_Name (P, To_Lower (Token_Name));
                  Set_Predefined_Type
                    (P, Translate_Predefined_Sets (Token));
                  Set_Referenced_Set (P, No_Node);
                  Append_Node_To_List (P, Params);

               when others =>
                  DPE (PC_Single_Variable_Declaration,
                       EMC_Subtheorem_Parameter);
                  return No_Node;
            end case;

            if Next_Token = T_Comma then
               Scan_Token (T_Comma);

               --  We parse all others parameters

               while Next_Token /= T_Right_Paren loop

                  --  We could have either a literal, or a variable

                  case Next_Token is

                     when T_EOF =>
                        DPE (PC_Single_Variable_Declaration,
                             EMC_Unexpected_Error);
                        return No_Node;

                     when T_Integer_Literal .. T_Wide_String_Literal =>
                        Scan_Token;
                        P := Make_Literal (Token);
                        Append_Node_To_List (P, Params);

                     when T_Identifier =>
                        Scan_Token;
                        P := New_Node (K_Identifier, Token_Location);
                        Set_Name (P, To_Lower (Token_Name));
                        Append_Node_To_List (P, Params);

                     when others =>
                        DPE (PC_Single_Variable_Declaration,
                             EMC_Unexpected_Token);
                        return No_Node;
                  end case;

                  if Next_Token /= T_Comma then

                     if Next_Token /= T_Right_Paren then
                        Scan_Token;
                        DPE (PC_Single_Variable_Declaration, T_Comma);
                        return No_Node;
                     end if;
                  else
                     --  We pass the comma, no error possible
                     Scan_Token (T_Comma);
                  end if;
               end loop;
            end if;

            Scan_Token (T_Right_Paren);
            if Token = T_Error then
               DPE (PC_Single_Variable_Declaration, T_Right_Paren);
               return No_Node;
            end if;
         end if;

         Set_Parameters (Var_Decl, Params);
         Set_True_Params (Var_Decl, No_List);
         Set_Domain (Var_Decl, No_Node);
         Set_Var_Ref (Var_Decl, Var);
         Set_Theorem_Name (Var_Decl, Name (Trm_Id));
      else
         declare
            use Ocarina.REAL_Values;
            D, C : Node_Id;
            V    : Value_Id := No_Value;
            L    : Location;
         begin
            Var_Decl := New_Node (K_Variable_Decl_Expression, Token_Location);

            D := New_Node (K_Return_Expression, Token_Location);

            Save_Lexer (L);
            Scan_Token;
            if Token in Higher_Level_Function_Type then
               V := Translate_Function_Code (Token);
               Set_Range_Function (D, V);
            else
               Restore_Lexer (L);
            end if;

            C := P_Expression;

            Set_Check_Expression (D, C);
            Set_Return_Expr (Var_Decl, D);
            Set_Var_Ref (Var_Decl, Var);
         end;
      end if;

      Scan_Token (T_Semi_Colon);
      if Token = T_Error then
         DPE (PC_Single_Variable_Declaration, T_Semi_Colon);
         return No_Node;
      end if;

      --  Set scope status

      if Global then
         Set_Is_Global (Var_Decl, Value_Id (1));
      else
         Set_Is_Global (Var_Decl, Value_Id (0));
      end if;

      return Var_Decl;
   end P_Single_Var_Declaration;

   ------------------------------
   -- P_Single_Set_Declaration --
   ------------------------------

   function P_Single_Set_Declaration return Node_Id is
      Set_Decl  : Node_Id;
      Set_Id    : Node_Id;
      Set_Expr  : Node_Id;
      Set_Def   : Node_Id;
      State     : Location;
      State_2   : Location;
      Is_Dependant : Boolean := False;
   begin

      --  We parse the set name
      --  wich can be parametrized

      Save_Lexer (State);
      Set_Id := P_Create_Set_Identifier;
      if No (Set_Id) then
         return No_Node;
      end if;

      --  We check weither the declared set is parametrized or not

      Save_Lexer (State_2);
      Restore_Lexer (State);
      Scan_Token (T_Identifier);
      Scan_Token;
      if Token = T_Left_Paren then
         Is_Dependant := True;
      end if;
      Restore_Lexer (State_2);

      --  Then we should have ':=' token

      Scan_Token (T_Affect);
      if Token = T_Error then
         DPE (PC_Single_Set_Declaration, T_Affect);
         return No_Node;
      end if;

      --  We enter braces

      Scan_Token (T_Left_Brace);
      if Token = T_Error then
         DPE (PC_Single_Set_Declaration, T_Left_Brace);
         return No_Node;
      end if;

      Set_Decl := New_Node (K_Set_Declaration, Token_Location);
      Set_Parametrized_Expr (Set_Decl, Set_Id);
      if Is_Dependant then
         Set_Dependant (Set_Decl, Value_Id (1));
      else
         Set_Dependant (Set_Decl, Value_Id (0));
      end if;

      --  Parse the local variable

      Set_Local_Variable (Set_Decl,
                          Make_Var_Reference (Name (P_Identifier)));
      if Token = T_Error then
         DPE (PC_Single_Set_Declaration, EMC_Unexpected_Error);
         return No_Node;
      end if;

      --  Then we should have 'in' token

      Scan_Token (T_In);
      if Token = T_Error then
         DPE (PC_Single_Set_Declaration, T_In);
         return No_Node;
      end if;

      --  We parse the set expression

      Set_Expr := P_Set_Expression;
      Set_Local_Set_Expression (Set_Decl, Set_Expr);

      --  We pass the sothat

      Scan_Token (T_Sothat);
1403

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
      if Token = T_Error then
         DPE (PC_Single_Set_Declaration, T_Sothat);
         return No_Node;
      end if;

      --  We parse set definition

      Set_Def := P_Expression;
      if No (Set_Def) then
         return No_Node;
      end if;
1415

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
      Set_Selection_Expression (Set_Decl, Set_Def);

      --  We read the right brace (exit of set declaration)

      Scan_Token (T_Right_Brace);
      if Token = T_Error then
         DPE (PC_Single_Set_Declaration, T_Right_Brace);
         return No_Node;
      end if;

      --  We pass the semi-colon

      Scan_Token (T_Semi_Colon);
      if Token =  T_Error then
         DPE (PC_Single_Set_Declaration, T_Semi_Colon);
         return No_Node;
      end if;

      return Set_Decl;
   end P_Single_Set_Declaration;

   --------------------
   -- P_Requirements --
   --------------------

   function P_Requirements return List_Id is
      L : constant List_Id := New_List (K_List_Id, Token_Location);
      P : Node_Id;
   begin

      if Next_Token /= T_Requires then
         return L;
      else
         Scan_Token;
      end if;

      Scan_Token (T_Left_Paren);
      if Token = T_Error then
         DPE (PC_Requirements_List, T_Left_Paren);
         return No_List;
      end if;

      loop
         Scan_Token (T_Identifier);
         if Token = T_Error then
            DPE (PC_Requirements_List, T_Identifier);
            return No_List;
         end if;

         P := New_Node (K_Required_Theorem, Token_Location);
         Set_Theorem_Name (P, To_Lower (Token_Name));
         Append_Node_To_List (P, L);

         Scan_Token;

         case Token is
            when T_And =>
               null;

            when T_Right_Paren =>
               exit;

            when others =>
               DPE (PC_Requirements_List, EMC_Wrong_List_Connector);
               return No_List;
         end case;

      end loop;

      Scan_Token (T_Semi_Colon);
      if Token = T_Error then
         DPE (PC_Requirements_List, T_Semi_Colon);
         return No_List;
      end if;

      return L;
   end P_Requirements;

   --------------------
   -- P_Declarations --
   --------------------

   procedure P_Declarations
     (R : Node_Id; Success : out boolean)
   is
      pragma Assert (Kind (R) = K_Theorem);

      Declarations_List : constant List_Id := New_List
        (K_List_id, Token_Location);
      Declaration       : Node_Id := No_Node;
      Stop              : Boolean := False;
   begin
      Success := True;
      while not Stop and then Success loop

         case Next_token is

            when T_Identifier =>  --  Set declaration
               Declaration := P_Single_Set_Declaration;
               if No (Declaration) then
                  Success := False;
               else
                  Append_Node_To_List (Declaration, Declarations_List);
               end if;

            when T_Var | T_Global =>  --  Variable declaration
               Declaration := P_Single_Var_Declaration;
               if No (Declaration) then
                  Success := False;
               else
                  Append_Node_To_List (Declaration, Declarations_List);
               end if;

            when T_Return | T_Requires | T_Check =>
               Stop := True;

            when others =>
               DPE (PC_Declarations_List, EMC_Declaration_Parameter);
               Success := False;
         end case;

      end loop;

      if Success then
         Set_Declarations (R, Declarations_List);
      end if;
   end P_Declarations;

   -----------------------------
   -- Create_Check_Expression --
   -----------------------------

   function Create_Check_Expression return Node_Id is
      N : Node_Id;
   begin
      --  We pass the left parenthesis

      Scan_Token (T_Left_Paren);
      if Token = T_Error then
         DPE (PC_Create_Check_Expression, T_Left_Paren);
         return No_Node;
      end if;

      N := P_Expression;
      if No (N) then
         return No_Node;
      end if;

      --  We pass the right parenthesis

      Scan_Token (T_Right_Paren);
      if Token = T_Error then
         DPE (PC_Create_Check_Expression, T_Right_Paren);
         return No_Node;
      end if;

      return N;
   end Create_Check_Expression;

   ------------------
   -- P_Identifier --
   ------------------

   function P_Identifier return Node_Id is
      use Ocarina.REAL_Values;

      Identifier : Node_Id;
   begin
      Scan_Token;

      if Token /= T_Identifier and then
        Token not in Selection_Function_Type and then
        Token not in Verification_Function_Type then
         DPE (PC_Identifier_Declaration, EMC_Used_Keyword);
         return No_Node;
      end if;
      Identifier := New_Node (K_Identifier, Token_Location);
      Set_Name (Identifier, To_Lower (Token_Name));
      return Identifier;
   end P_Identifier;

   -----------------------------
   -- P_Set_Range_Declaration --
   -----------------------------

   function P_Set_Range_Declaration return Node_Id is
      Range_Decl : Node_Id;
      Identifier : Node_Id;
      Elem       : Node_Id;
      Set_Expr   : Node_Id;
   begin
      Range_Decl := New_Node (K_Range_Declaration, Token_Location);

      --  Scan 'foreach'

      Scan_Token  (T_Foreach);
      if Token = T_Error then
         DPE (PC_Set_Declarations, T_Foreach);
         return No_Node;
      end if;

      --  Scan the element name

      Identifier := P_Identifier;
      if No (Identifier) then
         DPE (PC_Set_Declarations, T_Identifier);
         return No_Node;
      end if;

      Elem := New_Node (K_Element, Token_Location);
      Set_Identifier (Elem, Identifier);
      Set_Element_Type (Elem, SV_No_Type);

      --  Scan 'in'

      Scan_Token (T_In);
      if Token = T_Error then
         DPE (PC_Set_Declarations, T_In);
         return No_Node;
      end if;

      Set_Expr := P_Set_Expression;
      if No (Set_Expr) then
         return No_Node;
      end if;

      --  Scan 'do'

      Scan_Token (T_Do);
      if Token = T_Error then
         DPE (PC_Set_Declarations, T_Do);
         return No_Node;
      end if;

      Set_Range_Variable (Range_Decl, Elem);
      Set_Range_Set (Range_Decl, Set_Expr);
      Set_Variable_Ref (Range_Decl,
                        New_Node (K_Var_Reference, Token_Location));

      return Range_Decl;
   end P_Set_Range_Declaration;

   -----------------------------
   -- P_Higher_Level_Function --
   -----------------------------

   function P_Higher_Level_Function return Value_Id
   is
      use Ocarina.REAL_Values;

      State : Location;
      V     : Value_Id := No_Value;
   begin
      Save_Lexer (State);

      Scan_Token (T_Left_Paren);
      if Token = T_Error then
         DPE (PC_Function, T_Left_Paren);
         Restore_Lexer (State);
         return No_Value;
      end if;

      Scan_Token;
      if Token in Higher_Level_Function_Type then
         V := Translate_Function_Code (Token);
      else
         Restore_Lexer (State);
      end if;

      return V;
   end P_Higher_Level_Function;

   ---------------
   -- P_Theorem --
   ---------------

   function P_Theorem return Node_Id is
      use Ocarina.REAL_Values;

      Node         : Node_Id;
      Identifier   : Node_Id;
      Range_Decl   : Node_Id;
      Test         : Node_Id;
      Returns      : Node_Id;
      Requirements : List_Id;
      V            : Value_Id;
      Success      : Boolean;
   begin
      Node := New_Node (K_Theorem, Token_Location);
      Set_Used_Set (Node, New_List (K_List_Id, Token_Location));
      Set_Used_Var (Node, New_List (K_List_Id, Token_Location));
      Set_Local_Var (Node, New_List (K_List_Id, Token_Location));

      Scan_Token;
      Identifier := New_Node (K_Identifier, Token_Location);
      Set_Name (Identifier, To_Lower (Token_Name));
      Set_Identifier (Node, Identifier);
      Set_Related_Entity (Node, Owner_Node);
      Current_Theorem_Name := Name (Identifier);

      --  This line allow to find back the theorem's name

      Current_Theorem_Node := Node;

      Range_Decl := P_Set_Range_Declaration;

      if No (Range_Decl) then
         return No_Node;
      else
         Set_Range_Declaration (Node, Range_Decl);
      end if;

      P_Declarations (Node, Success);

      if not Success then
         return No_Node;
      end if;

      Requirements := P_Requirements;
      if Requirements /= No_List then
         Set_Required_Theorems (Node, Requirements);
      else
         return No_Node;
      end if;

      --  Here we have either a check or a return token
      Scan_Token;
      if Token /= T_Check and then Token /= T_Return then
         DPE (PC_Theorem, EMC_Testing_Token_Expected);
         return No_Node;
      end if;

      if Token = T_Check then
         Test := Create_Check_Expression;
         if No (Test) then
            return No_Node;
         else
            Set_Check_Expression (Node, Test);
         end if;

         Set_Return_Expression (Node, No_Node);
      else
         --  Return expression
         --  "return expressions" are actually the same
         --  as "check expressions"

         Returns := New_Node (K_Return_Expression, Token_Location);
         V := P_Higher_Level_Function;
         Set_Range_Function (Returns, V);

         Test := Create_Check_Expression;

         if V /= No_Value then
            Scan_Token (T_Right_Paren);
            if Token = T_Error then
               DPE (PC_Theorem, T_Right_Paren);
               return No_Node;
            end if;
         end if;

         if No (Test) then
            return No_Node;
         else
            Set_Check_Expression (Returns, Test);
            Set_Return_Expression (Node, Returns);
         end if;
         Set_Check_Expression (Node, No_Node);
      end if;

      --  We pass the semi-colon

      Scan_Token;
      if Token /= T_Semi_Colon then
         DPE (PC_Theorem, T_Semi_Colon);
         return No_Node;
      end if;

      return Node;
   end P_Theorem;

   -------------------------
   -- Skip_To_Theorem_End --
   -------------------------

   procedure Skip_To_Theorem_End (Success : out Boolean) is
      End_Found : Boolean := (Token = T_End);
   begin
      Success := True;

      --  FIXME :
      --  put back all references to scan_token (x) => scan_token in parser,
      --  in Order To Be Able To Find Back The "end" Keyword when It Was
      --  Unexpectedly Parsed.

      while not End_Found loop
         Scan_Token;

         exit when Token = T_EOF;

         if Token = T_End then
            End_Found := True;
         end if;
      end loop;

      if not End_Found then
         DPE (PC_Main, T_End);
         Success := False;
         return;
      end if;

      Scan_Token;
      case Token is

         when T_Identifier =>
            if To_Lower (Token_Name) /= Current_Theorem_Name then
               DPE (PC_Main, EMC_Non_Matching_Theorem_Name);
               Success := False;
               return;
            end if;
            Scan_Token (T_Semi_Colon);
            if Token = T_Error then
               DPE (PC_Main, T_Semi_Colon);
               Success := False;
               return;
            end if;

         when T_Semi_Colon =>
            return;

         when others =>
            DPE (PC_Main, T_Semi_Colon);
            Success := False;
            return;
      end case;

   end Skip_To_Theorem_End;

   -------------
   -- Process --
   -------------

   function Process
     (AADL_Root : Node_Id;
      From      : Location;
      To        : Location := No_Location)
     return Node_Id
   is
      pragma Unreferenced (AADL_Root);

      Buffer      : Location;
      Root        : constant Node_Id := New_Node (K_Root_Node, From);
      N           : Node_Id;
      Success     : Boolean := True;
      State       : Location;
   begin
      Buffer := From;
1872

1873 1874 1875
      if To /= No_Location then
         Buffer.EOF := To.Last_Pos;
      end if;
1876

1877
      Restore_Lexer (Buffer);
1878

1879 1880 1881
      Set_Theorems (Root, New_List (K_List_Id, From));

      Scan_Token;
1882

1883 1884
      while Token = T_Theorem loop
         N := P_Theorem;
1885

1886 1887 1888 1889 1890 1891 1892 1893
         Skip_To_Theorem_End (Success);

         if No (N) or else not Success then
            N := Current_Theorem_Node;
            DPE (PC_Main, EMC_Syntax_Error);
            Success := False;
            exit;
         end if;
1894

1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
         Append_Node_To_List (N, Theorems (Root));

         --  Pass to the next theorem

         Save_Lexer (State);
         Scan_Token;
      end loop;

      if Success then
         if Token /= T_EOF then
            DPE (PC_Main, T_EOF);
            return No_Node;
         end if;

         --  the AADL main parser need the EOF ("**}") token to be the
         --  first lexem available after a successful annex parsing

         Restore_Lexer (State);

         return Root;
      else
         DPE (PC_Main, EMC_Syntax_Error);
         return No_Node;
      end if;
   end Process;

   ----------
   -- Init --
   ----------

   procedure Init
   is
      use GNAT.Command_Line;
      use Ocarina.Parser;
      use Namet;

      C : Character;
   begin
      Current_Theorem_Node := No_Node;

      Initialize_Option_Scan;
      loop
         C := Getopt ("* real_lib:");
         case C is
            when ASCII.NUL =>
               exit;

            when 'r' =>
               if Full_Switch = "real_lib" then
                  REAL_Libs.Append (Get_String_Name (Parameter));
               end if;

            when others =>
               null;
         end case;
      end loop;

      REAL_Language := Get_String_Name (Language);
      Register_Parser (Ocarina.ME_REAL.Tokens.Language, Process'Access);

      --  If a REAL library file had been defined, we
      --  parse and register it.

      for J in REAL_Libs.First .. REAL_Libs.Last loop
         declare
            use Ocarina.Analyzer.REAL;
            use Ocarina.Files;

            Buffer            : Location;
            REAL_External_Lib : Node_Id;
         begin
            Buffer := Load_File (REAL_Libs.Table (J));
            if Buffer = No_Location then
               Display_And_Exit
                 ("could not load file "
                    & Get_Name_String (REAL_Libs.Table (J)));
            end if;

            REAL_External_Lib := Process (No_Node, Buffer);
            if No (REAL_External_Lib) then
               Display_And_Exit ("could not parse REAL specification");
            end if;

            Register_Library_Theorems (REAL_External_Lib);
         end;
      end loop;
   end Init;

end Ocarina.FE_REAL.Parser;