Skip to content

Commit

Permalink
Merge branch 'topic/1045' into 'master'
Browse files Browse the repository at this point in the history
Un-revert "Implement the is_keyword property."

See merge request eng/libadalang/libadalang!1484
  • Loading branch information
Roldak committed Dec 1, 2023
2 parents 2802a1d + a23f18a commit 4f66758
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ada/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,16 @@ def standard_unit():
"""
pass

@langkit_property(return_type=T.Bool, public=True,
external=True, uses_entity_info=False, uses_envs=False)
def is_keyword(token=T.Token, language_version=T.Symbol):
"""
Static method. Return whether the given token is considered a keyword
in the given version of Ada. Supported values for the language version
argument are: "Ada_83", "Ada_95", "Ada_2005", "Ada_2012", "Ada_2022".
"""
pass

std = Property(
Self.standard_unit.root.cast(T.CompilationUnit)
.body.cast(T.LibraryItem).item.as_bare_entity,
Expand Down
28 changes: 28 additions & 0 deletions extensions/src/libadalang-implementation-extensions.adb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ with GNATCOLL.GMP.Integers;

with Langkit_Support.Adalog.Debug;
with Langkit_Support.Bump_Ptr;
with Langkit_Support.Symbols;
with Langkit_Support.Text; use Langkit_Support.Text;

with Libadalang.Analysis; use Libadalang.Analysis;
with Libadalang.Common;
with Libadalang.Config_Pragmas_Impl;
with Libadalang.Doc_Utils;
with Libadalang.Env_Hooks;
Expand Down Expand Up @@ -156,6 +158,32 @@ package body Libadalang.Implementation.Extensions is
Rule => Default_Grammar_Rule);
end Ada_Node_P_Standard_Unit;

---------------------------
-- Ada_Node_P_Is_Keyword --
---------------------------

function Ada_Node_P_Is_Keyword
(Node : Bare_Ada_Node;
Token : Token_Reference;
Language_Version : Symbol_Type) return Boolean
is
pragma Unreferenced (Node);

Version_Text : constant String :=
Langkit_Support.Symbols.Image (Language_Version);

Version : Common.Language_Version;
begin
begin
Version := Common.Language_Version'Value (Version_Text);
exception
when Constraint_Error =>
raise Precondition_Failure
with "Unknown Ada version " & Version_Text;
end;
return Is_Keyword (Token, Version);
end Ada_Node_P_Is_Keyword;

--------------------------------------
-- Ada_Node_P_Filter_Is_Imported_By --
--------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions extensions/src/libadalang-implementation-extensions.ads
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ package Libadalang.Implementation.Extensions is
function Ada_Node_P_Standard_Unit
(Node : Bare_Ada_Node) return Internal_Unit;

function Ada_Node_P_Is_Keyword
(Node : Bare_Ada_Node;
Token : Token_Reference;
Language_Version : Symbol_Type) return Boolean;

function Ada_Node_P_Filter_Is_Imported_By
(Node : Bare_Ada_Node;
Units : Internal_Unit_Array_Access;
Expand Down
14 changes: 14 additions & 0 deletions testsuite/tests/properties/is_keyword/test.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
procedure Test is
protected type A is
I : Float := 0.1;
end A;

type A is synchronized interface;

overriding function Bar is null;

I : access Float := A.I'Access;
B : Boolean := (for some I in 1 .. 10 => I mod 2 = 0);
begin
null;
end Test;
6 changes: 6 additions & 0 deletions testsuite/tests/properties/is_keyword/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'protected' is a keyword in Ada 2012 but not in Ada 83
'synchronized' is a keyword in Ada 2012 but not in Ada 83
'interface' is a keyword in Ada 2012 but not in Ada 83
'overriding' is a keyword in Ada 2012 but not in Ada 83
'some' is a keyword in Ada 2012 but not in Ada 83
Unknown Ada version ada_2013
17 changes: 17 additions & 0 deletions testsuite/tests/properties/is_keyword/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import libadalang as lal


ctx = lal.AnalysisContext()
u = ctx.get_from_file("test.adb")

for token in u.iter_tokens():
is_83_token = u.root.p_is_keyword(token, "ada_83")
is_12_token = u.root.p_is_keyword(token, "ada_2012")
if is_12_token and not is_83_token:
print(f"'{token.text}' is a keyword in Ada 2012 but not in Ada 83")

# Try checking a token with an invalid Ada version
try:
u.root.p_is_keyword(u.first_token, "ada_2013")
except Exception as e:
print(e)
2 changes: 2 additions & 0 deletions testsuite/tests/properties/is_keyword/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: python
input_sources: [test.adb]

0 comments on commit 4f66758

Please sign in to comment.