Langkit_Support: add a generic package for iterator interfaces

This will be used as a base for iterators that will yield AST nodes for
the Find primitive and possibly for lazy collections in the properties
DSL.

Change-Id: I54450824dc5066b0ec46004e0f8f2cd6486e3d22
TN: P225-028
This commit is contained in:
Pierre-Marie de Rodat
2016-02-29 19:05:41 +01:00
committed by Raphaël Amiard
parent db4a5bef1c
commit a62ea044e7
3 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package body Langkit_Support.Iterators is
-------------
-- Iterate --
-------------
procedure Iterate
(I : in out Iterator'Class;
Proc : access procedure (Element : Element_Type))
is
Has_Next : Boolean;
Element : Element_Type;
begin
loop
I.Next (Has_Next, Element);
exit when not Has_Next;
Proc (Element);
end loop;
end Iterate;
end Langkit_Support.Iterators;

View File

@@ -0,0 +1,37 @@
-- This generic package provides for each Element_Type an interface that
-- used to implement iterators.
--
-- Beyond the fact that it's super simple, the difference with standard
-- iterators is that iteration is destructive: once an element has been
-- yielded, it's not possible to re-yield it once more.
--
-- The analogy with standard Ada containers is that there's no way to keep a
-- cursor to preserve an iteration state.
with Ada.Unchecked_Deallocation;
generic
type Element_Type is private;
package Langkit_Support.Iterators is
type Iterator is interface;
-- Iterator interface: iterator consumers do not need to mind about
-- concrete interator implementations.
procedure Next
(I : in out Iterator;
Has_Next : out Boolean;
Element : out Element_Type) is abstract;
-- Get the next iteration element. If there was no element to yield
-- anymore, set Has_Next to False. Otherwise, set it to True and set
-- Element.
procedure Iterate
(I : in out Iterator'Class;
Proc : access procedure (Element : Element_Type));
-- Consume completely the I iterator, calling Proc on all yielded elements
procedure Destroy is new Ada.Unchecked_Deallocation
(Iterator'Class, Iterator_Access);
end Langkit_Support.Iterators;

View File

@@ -22,6 +22,7 @@ library project Langkit_Support is
"Langkit_Support.Bump_Ptr",
"Langkit_Support.Bump_Ptr.Vectors",
"Langkit_Support.Diagnostics",
"Langkit_Support.Iterators",
"Langkit_Support.Extensions",
"Langkit_Support.Lexical_Env",
"Langkit_Support.Packrat",