Minor changes to README, point to ada-mode grammar

This commit is contained in:
Emmanuel Briot 2022-10-24 13:16:31 +02:00
parent 2db1b28fb6
commit b100bdd8c1
5 changed files with 318 additions and 129 deletions

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# Tree-Sitter parser for Ada
The grammar is adapted from the work done by Stephen Leak for the
Emacs ada-mode. It was translated (partially for now) to tree-sitter
syntax, and slightly changed to reduce some conflicts. Tree-sitter
doesn't need a full syntax tree, so we can take some shortcuts in
the grammar.
## Installation
Execute the following commands:
```
npm install
npm run test
```

View File

@ -1,8 +0,0 @@
Tree-Sitter parser for Ada
Use:
npm install
npm run test

View File

@ -261,3 +261,103 @@ end P;
(identifier))))))))))))))) (identifier)))))))))))))))
(name (name
(identifier)))))) (identifier))))))
================================================================================
Raise exception
================================================================================
procedure P is
begin
raise Constraint_Error;
raise Constraint_Error with "msg";
end;
--------------------------------------------------------------------------------
(compilation
(compilation_unit
(proper_body
(subprogram_body
(subprogram_specification
(procedure_specification
(name
(identifier))))
(handled_sequence_of_statements
(sequence_of_statements
(statement
(simple_statement
(raise_statement
(name
(identifier)))))
(statement
(simple_statement
(raise_statement
(name
(identifier))
(expression
(relation
(simple_expression
(term
(factor
(primary
(string_literal))))))))))))))))
================================================================================
Function calls
================================================================================
procedure P is
A : Integer;
begin
A := Func (B => 1);
end;
--------------------------------------------------------------------------------
(compilation
(compilation_unit
(proper_body
(subprogram_body
(subprogram_specification
(procedure_specification
(name
(identifier))))
(non_empty_declarative_part
(declarative_item_pragma
(object_declaration
(defining_identifier_list
(identifier))
(subtype_indication
(name
(identifier))))))
(handled_sequence_of_statements
(sequence_of_statements
(statement
(simple_statement
(assignment_statement
(name
(identifier))
(assign_value
(expression
(relation
(simple_expression
(term
(factor
(primary
(name
(function_call
(name
(identifier))
(actual_parameter_part
(parameter_association
(component_choice_list
(selector_name
(identifier)))
(assoc_expression
(expression
(relation
(simple_expression
(term
(factor
(primary
(numeric_literal))))))))))))))))))))))))))))

View File

@ -108,7 +108,6 @@ end;
(aspect_mark (aspect_mark
(identifier))))))))) (identifier)))))))))
================================================================================ ================================================================================
functions functions
================================================================================ ================================================================================
@ -261,6 +260,64 @@ function F2 (A : Integer) return Boolean
(name (name
(identifier))))))))))))))))) (identifier)))))))))))))))))
================================================================================
Expression function declare
================================================================================
function F2 (A : Integer) return Boolean
is (declare B : constant Integer := A + 1; begin B);
--------------------------------------------------------------------------------
(compilation
(compilation_unit
(expression_function_declaration
(function_specification
(name
(identifier))
(parameter_and_result_profile
(formal_part
(parameter_specification_list
(parameter_specification
(defining_identifier_list
(identifier))
(name
(identifier)))))
(result_profile
(name
(identifier)))))
(aggregate
(declare_expression
(declare_item
(object_declaration
(defining_identifier_list
(identifier))
(subtype_indication
(name
(identifier)))
(assign_value
(expression
(relation
(simple_expression
(term
(factor
(primary
(name
(identifier)))))
(binary_adding_operator)
(term
(factor
(primary
(numeric_literal))))))))))
(expression
(relation
(simple_expression
(term
(factor
(primary
(name
(identifier)))))))))))))
================================================================================ ================================================================================
Expression function raise Expression function raise
================================================================================ ================================================================================
@ -464,142 +521,142 @@ function F5 (A : Integer) return Boolean
(name (name
(identifier)))))))))))))))))))))))) (identifier))))))))))))))))))))))))
====== ================================================================================
Expression function case Expression function case
====== ================================================================================
function F (A : Integer) return Boolean function F (A : Integer) return Boolean
is (case A + 1 is is (case A + 1 is
when 0 .. 1 | 3 .. 4 => True, when 0 .. 1 | 3 .. 4 => True,
when others => False); when others => False);
------ --------------------------------------------------------------------------------
(compilation (compilation
(compilation_unit (compilation_unit
(expression_function_declaration (expression_function_declaration
(function_specification (function_specification
(name (name
(identifier)) (identifier))
(parameter_and_result_profile (parameter_and_result_profile
(formal_part (formal_part
(parameter_specification_list (parameter_specification_list
(parameter_specification (parameter_specification
(defining_identifier_list (defining_identifier_list
(identifier)) (identifier))
(name
(identifier)))))
(result_profile
(name (name
(identifier))))) (identifier)))))
(aggregate (result_profile
(conditional_expression (name
(case_expression (identifier)))))
(expression (aggregate
(relation (conditional_expression
(case_expression
(expression
(relation
(simple_expression
(term
(factor
(primary
(name
(identifier)))))
(binary_adding_operator)
(term
(factor
(primary
(numeric_literal)))))))
(case_expression_alternative
(discrete_choice_list
(discrete_choice
(range_g
(simple_expression (simple_expression
(term (term
(factor (factor
(primary (primary
(name (numeric_literal)))))
(identifier))))) (simple_expression
(binary_adding_operator)
(term (term
(factor (factor
(primary (primary
(numeric_literal))))))) (numeric_literal)))))))
(case_expression_alternative (discrete_choice
(discrete_choice_list (range_g
(discrete_choice (simple_expression
(range_g (term
(simple_expression (factor
(term (primary
(factor (numeric_literal)))))
(primary (simple_expression
(numeric_literal))))) (term
(simple_expression (factor
(term (primary
(factor (numeric_literal))))))))
(primary (expression
(numeric_literal))))))) (relation
(discrete_choice (simple_expression
(range_g (term
(simple_expression (factor
(term (primary
(factor (name
(primary (identifier)))))))))
(numeric_literal))))) (case_expression_alternative
(simple_expression (discrete_choice_list
(term (discrete_choice))
(factor (expression
(primary (relation
(numeric_literal)))))))) (simple_expression
(expression (term
(relation (factor
(simple_expression (primary
(term (name
(factor (identifier)))))))))))))))
(primary
(name
(identifier)))))))))
(case_expression_alternative
(discrete_choice_list
(discrete_choice))
(expression
(relation
(simple_expression
(term
(factor
(primary
(name
(identifier)))))))))))))))
===== ================================================================================
Expression function quantified Expression function quantified
===== ================================================================================
function F (A : My_Array) return Boolean function F (A : My_Array) return Boolean
is (for some E of A => E = 1); is (for some E of A => E = 1);
------ --------------------------------------------------------------------------------
(compilation (compilation
(compilation_unit (compilation_unit
(expression_function_declaration (expression_function_declaration
(function_specification (function_specification
(name (name
(identifier)) (identifier))
(parameter_and_result_profile (parameter_and_result_profile
(formal_part (formal_part
(parameter_specification_list (parameter_specification_list
(parameter_specification (parameter_specification
(defining_identifier_list (defining_identifier_list
(identifier)) (identifier))
(name
(identifier)))))
(result_profile
(name (name
(identifier))))) (identifier)))))
(aggregate (result_profile
(quantified_expression (name
(quantifier) (identifier)))))
(iterator_specification (aggregate
(identifier) (quantified_expression
(name (quantifier)
(identifier))) (iterator_specification
(assoc_expression (identifier)
(expression (name
(relation (identifier)))
(simple_expression (assoc_expression
(term (expression
(factor (relation
(primary (simple_expression
(name (term
(identifier)))))) (factor
(relational_operator) (primary
(simple_expression (name
(term (identifier))))))
(factor (relational_operator)
(primary (simple_expression
(numeric_literal))))))))))))) (term
(factor
(primary
(numeric_literal)))))))))))))

View File

@ -77,6 +77,7 @@ module.exports = grammar({
[$.defining_identifier_list, $.object_renaming_declaration, [$.defining_identifier_list, $.object_renaming_declaration,
$.exception_renaming_declaration, $._direct_name], $.exception_renaming_declaration, $._direct_name],
[$.defining_identifier_list, $._direct_name], [$.defining_identifier_list, $._direct_name],
[$.defining_identifier_list, $.object_renaming_declaration],
// 'generic' . 'package' ... // 'generic' . 'package' ...
[$.generic_formal_part, $.generic_renaming_declaration], [$.generic_formal_part, $.generic_renaming_declaration],
@ -104,6 +105,9 @@ module.exports = grammar({
// 'type' identifier 'is' 'new' subtype_indication . 'with' . // 'type' identifier 'is' 'new' subtype_indication . 'with' .
[$.private_extension_declaration, $.derived_type_definition], [$.private_extension_declaration, $.derived_type_definition],
[$.function_call, $.procedure_call_statement],
[$.function_call, $.name],
], ],
// inline: $ => [ // inline: $ => [
@ -136,10 +140,8 @@ module.exports = grammar({
// Simpler definition for games than the standard grammer // Simpler definition for games than the standard grammer
// name: $ => choice( // name: $ => choice(
// $._direct_name,
// $.explicit_dereference, // $.explicit_dereference,
// $.selected_component, // $.selected_component,
// $.attribute_reference,
// $.function_call, // $.function_call,
// $.character_literal, // $.character_literal,
// $.qualified_expression, // $.qualified_expression,
@ -158,12 +160,11 @@ module.exports = grammar({
'.', '.',
$.name, $.name,
)), )),
// repeat(seq(
// '.',
// $.identifier,
// )),
), ),
$.attribute_reference, $.attribute_reference,
$.function_call,
//$.string_literal, // from ada-mode, but seems wrong.
// // Added to primary instead
), ),
name_list: $ => comma_separated_list_of($.name), name_list: $ => comma_separated_list_of($.name),
@ -511,6 +512,7 @@ module.exports = grammar({
reservedWord('null'), reservedWord('null'),
$.aggregate, $.aggregate,
$.name, $.name,
$.string_literal, // ada-mode puts this in name instead
// $.allocator, // $.allocator,
), ),
access_type_definition: $ => seq( access_type_definition: $ => seq(
@ -569,7 +571,7 @@ module.exports = grammar({
comma_separated_list_of($.parameter_association), comma_separated_list_of($.parameter_association),
$.conditional_expression, $.conditional_expression,
$.quantified_expression, $.quantified_expression,
// $.declare_expression, $.declare_expression,
), ),
')', ')',
), ),
@ -599,6 +601,16 @@ module.exports = grammar({
), ),
$.assoc_expression, $.assoc_expression,
), ),
declare_expression: $ => seq(
reservedWord('declare'),
repeat($.declare_item),
reservedWord('begin'),
$.expression,
),
declare_item: $ => choice(
$.object_declaration,
$.object_renaming_declaration,
),
quantifier: $ => choice( quantifier: $ => choice(
reservedWord('all'), reservedWord('all'),
reservedWord('some'), reservedWord('some'),
@ -626,7 +638,7 @@ module.exports = grammar({
choice( choice(
$.conditional_expression, $.conditional_expression,
$.quantified_expression, $.quantified_expression,
// $.declare_expression, $.declare_expression,
), ),
')', ')',
), ),
@ -1499,7 +1511,7 @@ module.exports = grammar({
// $.requeue_statement, // $.requeue_statement,
$.delay_statement, $.delay_statement,
// $.abort_statement, // $.abort_statement,
// $.raise_statement, $.raise_statement,
$.pragma_g, $.pragma_g,
), ),
statement: $ => seq( statement: $ => seq(
@ -1558,6 +1570,17 @@ module.exports = grammar({
optional($.actual_parameter_part), optional($.actual_parameter_part),
';', ';',
), ),
raise_statement: $ => seq(
reservedWord('raise'),
optional(seq(
$.name,
optional(seq(
reservedWord('with'),
$.expression, // ada-mode allows "raise CE with raise with ..."
)),
)),
';',
),
loop_statement: $ => seq( loop_statement: $ => seq(
optional($.loop_label), optional($.loop_label),
optional($.iteration_scheme), optional($.iteration_scheme),