##  vim: ft=makoada

----------------
-- Is_Keyword --
----------------

function Is_Keyword
  (Token   : Token_Reference;
   Version : Language_Version) return Boolean
is
   TDH   : constant Token_Data_Handler_Access := Get_Token_TDH (Token);
   Index : constant Token_Or_Trivia_Index := Get_Token_Index (Token);
begin
   return Libadalang.Lexer.Is_Keyword (TDH.all, Index, Version);
end Is_Keyword;

---------------------------------
-- Create_Context_From_Project --
---------------------------------

function Create_Context_From_Project
  (Tree             : GNATCOLL.Projects.Project_Tree_Access;
   Project          : GNATCOLL.Projects.Project_Type :=
                        GNATCOLL.Projects.No_Project;
   Env              : GNATCOLL.Projects.Project_Environment_Access;
   Is_Project_Owner : Boolean := True;
   Event_Handler    : Event_Handler_Reference :=
                        No_Event_Handler_Ref;
   With_Trivia      : Boolean := True;
   Tab_Stop         : Positive := 8)
  return Analysis_Context
is
   Result : Internal_Context := Allocate_Context;

   EH_Int : Internal_Event_Handler_Access :=
     Wrap_Public_Event_Handler (Event_Handler);
   --  This creates a ref-counted object: we must decrease its ref-count
   --  whether we return normally or through an exception.
begin
   begin
      GPR_Impl.Initialize_Context_From_Project
        (Result,
         Tree,
         Project,
         Env,
         Is_Project_Owner,
         EH_Int,
         With_Trivia,
         Tab_Stop);
   exception
      when Libadalang.Project_Provider.Unsupported_View_Error =>
         Dec_Ref (EH_Int);
         Dec_Ref (Result);
         raise;
   end;
   Dec_Ref (EH_Int);

   return Context : constant Analysis_Context := Wrap_Context (Result) do

      --  Now that ``Context`` has one ownership share, release the one that
      --  ``Result`` has.

      Dec_Ref (Result);
   end return;
end Create_Context_From_Project;

---------------------------------
-- Create_Context_From_Project --
---------------------------------

function Create_Context_From_Project
  (Tree          : GPR2.Project.Tree.Object;
   Project       : GPR2.Project.View.Object := GPR2.Project.View.Undefined;
   Event_Handler : Event_Handler_Reference := No_Event_Handler_Ref;
   With_Trivia   : Boolean := True;
   Tab_Stop      : Positive := 8)
  return Analysis_Context
is
   Result : Internal_Context := Allocate_Context;

   EH_Int : Internal_Event_Handler_Access :=
     Wrap_Public_Event_Handler (Event_Handler);
   --  This creates a ref-counted object: we must decrease its ref-count
   --  whether we return normally or through an exception.
begin
   begin
      GPR_Impl.Initialize_Context_From_Project
        (Result, Tree, Project, EH_Int, With_Trivia, Tab_Stop);
   exception
      when Libadalang.Project_Provider.Unsupported_View_Error =>
         Dec_Ref (EH_Int);
         Dec_Ref (Result);
         raise;
   end;
   Dec_Ref (EH_Int);

   return Context : constant Analysis_Context := Wrap_Context (Result) do

      --  Now that ``Context`` has one ownership share, release the one that
      --  ``Result`` has.

      Dec_Ref (Result);
   end return;
end Create_Context_From_Project;
