mirror of
https://github.com/AdaCore/langkit.git
synced 2026-02-12 12:28:12 -08:00
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:
committed by
Raphaël Amiard
parent
db4a5bef1c
commit
a62ea044e7
21
langkit/support/langkit_support-iterators.adb
Normal file
21
langkit/support/langkit_support-iterators.adb
Normal 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;
|
||||
37
langkit/support/langkit_support-iterators.ads
Normal file
37
langkit/support/langkit_support-iterators.ads
Normal 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;
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user