Rename 'type defintion' to 'type specification'

This commit is contained in:
Jeff 2024-01-23 14:35:57 -05:00
parent 8224f7fe3c
commit ed6e4cfd1a
15 changed files with 96 additions and 94 deletions

View File

@ -2,13 +2,13 @@ use serde::{Deserialize, Serialize};
use crate::{
AbstractTree, AssignmentOperator, Error, Format, Identifier, Map, Result, Statement,
SyntaxNode, SyntaxPosition, Type, TypeDefinition, Value,
SyntaxNode, SyntaxPosition, Type, TypeSpecification, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Assignment {
identifier: Identifier,
type_definition: Option<TypeDefinition>,
type_specification: Option<TypeSpecification>,
operator: AssignmentOperator,
statement: Statement,
@ -25,8 +25,8 @@ impl AbstractTree for Assignment {
let identifier = Identifier::from_syntax(identifier_node, source, context)?;
let type_node = syntax_node.child(1).unwrap();
let type_definition = if type_node.kind() == "type_definition" {
Some(TypeDefinition::from_syntax(type_node, source, context)?)
let type_specification = if type_node.kind() == "type_specification" {
Some(TypeSpecification::from_syntax(type_node, source, context)?)
} else {
None
};
@ -38,7 +38,7 @@ impl AbstractTree for Assignment {
let statement = Statement::from_syntax(statement_node, source, context)?;
if let AssignmentOperator::Equal = operator {
let r#type = if let Some(definition) = &type_definition {
let r#type = if let Some(definition) = &type_specification {
definition.inner().clone()
} else {
statement.expected_type(context)?
@ -49,7 +49,7 @@ impl AbstractTree for Assignment {
Ok(Assignment {
identifier,
type_definition,
type_specification,
operator,
statement,
syntax_position: syntax_node.range().into(),
@ -59,21 +59,21 @@ impl AbstractTree for Assignment {
fn check_type(&self, source: &str, context: &Map) -> Result<()> {
let actual_type = self.statement.expected_type(context)?;
if let Some(type_definition) = &self.type_definition {
if let Some(type_specification) = &self.type_specification {
match self.operator {
AssignmentOperator::Equal => {
type_definition
type_specification
.inner()
.check(&actual_type)
.map_err(|error| error.at_source_position(source, self.syntax_position))?;
}
AssignmentOperator::PlusEqual => {
if let Type::List(item_type) = type_definition.inner() {
if let Type::List(item_type) = type_specification.inner() {
item_type.check(&actual_type).map_err(|error| {
error.at_source_position(source, self.syntax_position)
})?;
} else {
type_definition
type_specification
.inner()
.check(&self.identifier.expected_type(context)?)
.map_err(|error| {
@ -142,8 +142,8 @@ impl Format for Assignment {
fn format(&self, output: &mut String, indent_level: u8) {
self.identifier.format(output, indent_level);
if let Some(type_definition) = &self.type_definition {
type_definition.format(output, indent_level);
if let Some(type_specification) = &self.type_specification {
type_specification.format(output, indent_level);
}
output.push(' ');

View File

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{
AbstractTree, Block, Error, Format, Function, Identifier, Map, Result, SyntaxNode,
SyntaxPosition, Type, TypeDefinition, Value,
SyntaxPosition, Type, TypeSpecification, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
@ -98,15 +98,16 @@ impl AbstractTree for FunctionNode {
parameters.push(identifier);
}
if child.kind() == "type_definition" {
let type_definition = TypeDefinition::from_syntax(child, source, outer_context)?;
if child.kind() == "type_specification" {
let type_specification =
TypeSpecification::from_syntax(child, source, outer_context)?;
parameter_types.push(type_definition.take_inner());
parameter_types.push(type_specification.take_inner());
}
}
let return_type_node = node.child(child_count - 2).unwrap();
let return_type = TypeDefinition::from_syntax(return_type_node, source, outer_context)?;
let return_type = TypeSpecification::from_syntax(return_type_node, source, outer_context)?;
let function_context = Map::new();

View File

@ -27,7 +27,7 @@ pub mod math;
pub mod math_operator;
pub mod statement;
pub mod r#type;
pub mod type_definition;
pub mod type_specification;
pub mod value_node;
pub mod r#while;
pub mod r#yield;
@ -37,7 +37,7 @@ pub use {
function_call::*, function_expression::*, function_node::*, identifier::*, if_else::*,
index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*,
math::*, math_operator::*, r#for::*, r#match::*, r#type::*, r#while::*, r#yield::*,
statement::*, type_definition::*, value_node::*,
statement::*, type_specification::*, value_node::*,
};
use serde::{Deserialize, Serialize};

View File

@ -3,11 +3,11 @@ use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Error, Format, Map, Result, SyntaxNode, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct TypeDefinition {
pub struct TypeSpecification {
r#type: Type,
}
impl TypeDefinition {
impl TypeSpecification {
pub fn new(r#type: Type) -> Self {
Self { r#type }
}
@ -21,14 +21,14 @@ impl TypeDefinition {
}
}
impl AbstractTree for TypeDefinition {
impl AbstractTree for TypeSpecification {
fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "type_definition", node)?;
Error::expect_syntax_node(source, "type_specification", node)?;
let type_node = node.child(1).unwrap();
let r#type = Type::from_syntax(type_node, source, context)?;
Ok(TypeDefinition { r#type })
Ok(TypeSpecification { r#type })
}
fn run(&self, source: &str, context: &Map) -> Result<Value> {
@ -40,7 +40,7 @@ impl AbstractTree for TypeDefinition {
}
}
impl Format for TypeDefinition {
impl Format for TypeSpecification {
fn format(&self, output: &mut String, indent_level: u8) {
output.push('<');
self.r#type.format(output, indent_level);

View File

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{
AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier,
List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefinition, Value,
List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeSpecification, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -67,16 +67,17 @@ impl AbstractTree for ValueNode {
current_type = None;
}
if child.kind() == "type_definition" {
current_type =
Some(TypeDefinition::from_syntax(child, source, context)?.take_inner());
if child.kind() == "type_specification" {
current_type = Some(
TypeSpecification::from_syntax(child, source, context)?.take_inner(),
);
}
if child.kind() == "statement" {
let statement = Statement::from_syntax(child, source, context)?;
if let Some(type_definition) = &current_type {
type_definition.check(&statement.expected_type(context)?)?;
if let Some(type_specification) = &current_type {
type_specification.check(&statement.expected_type(context)?)?;
}
child_nodes.insert(current_key.clone(), (statement, current_type.clone()));
@ -130,9 +131,9 @@ impl AbstractTree for ValueNode {
Some(Identifier::from_syntax(child_syntax_node, source, context)?);
}
if child_syntax_node.kind() == "type_definition" {
if child_syntax_node.kind() == "type_specification" {
current_type = Some(
TypeDefinition::from_syntax(child_syntax_node, source, context)?
TypeSpecification::from_syntax(child_syntax_node, source, context)?
.take_inner(),
);
}

View File

@ -1,7 +1,7 @@
//! Types that represent runtime values.
use crate::{
error::{Error, Result},
Function, Identifier, List, Map, Structure, Type, TypeDefinition,
Function, Identifier, List, Map, Structure, Type, TypeSpecification,
};
use serde::{
@ -81,7 +81,7 @@ impl Value {
for (key, (value, _)) in map.variables().unwrap().iter() {
identifier_types.push((
Identifier::new(key.clone()),
TypeDefinition::new(value.r#type()),
TypeSpecification::new(value.r#type()),
));
}

View File

@ -27,7 +27,7 @@ x <int> = y
(statement
(assignment
(identifier)
(type_definition
(type_specification
(type))
(assignment_operator)
(statement

View File

@ -11,7 +11,7 @@ Anonymous Function
(expression
(value
(function
(type_definition
(type_specification
(type))
(block
(statement
@ -39,12 +39,12 @@ foobar = (x <int>, y <int>) <int> {
(value
(function
(identifier)
(type_definition
(type_specification
(type))
(identifier)
(type_definition
(type_specification
(type))
(type_definition
(type_specification
(type))
(block
(statement
@ -132,9 +132,9 @@ Anonymous Function Call
(value
(function
(identifier)
(type_definition
(type_specification
(type))
(type_definition
(type_specification
(type))
(block
(statement
@ -202,7 +202,7 @@ x(() <bool> { true })
(expression
(value
(function
(type_definition
(type_specification
(type))
(block
(statement

View File

@ -34,14 +34,14 @@ Map with Types
(value
(map
(identifier)
(type_definition
(type_specification
(type))
(statement
(expression
(value
(integer))))
(identifier)
(type_definition
(type_specification
(type
(type)))
(statement

View File

@ -15,10 +15,10 @@ struct {
(value
(structure
(identifier)
(type_definition
(type_specification
(type))
(identifier)
(type_definition
(type_specification
(type)))))))
================================================================================
@ -46,17 +46,17 @@ Foo = struct {
(value
(structure
(identifier)
(type_definition
(type_specification
(type))
(identifier)
(type_definition
(type_specification
(type))
(statement
(expression
(value
(float))))
(identifier)
(type_definition
(type_specification
(type
(identifier)))
(statement

View File

@ -96,7 +96,7 @@ module.exports = grammar({
choice(
seq(
$.identifier,
$.type_definition,
$.type_specification,
),
seq(
$.identifier,
@ -105,7 +105,7 @@ module.exports = grammar({
),
seq(
$.identifier,
$.type_definition,
$.type_specification,
'=',
$.statement,
),
@ -128,7 +128,7 @@ module.exports = grammar({
),
seq(
$.identifier,
$.type_definition,
$.type_specification,
'=',
$.statement,
),
@ -224,7 +224,7 @@ module.exports = grammar({
repeat1(
seq(
$.identifier,
optional($.type_definition),
optional($.type_specification),
'=',
$.statement,
optional(','),
@ -310,7 +310,7 @@ module.exports = grammar({
assignment: $ =>
seq(
$.identifier,
optional($.type_definition),
optional($.type_specification),
$.assignment_operator,
$.statement,
),
@ -387,7 +387,7 @@ module.exports = grammar({
seq('return', $.statement),
),
type_definition: $ =>
type_specification: $ =>
seq('<', $.type, '>'),
type: $ =>
@ -430,12 +430,12 @@ module.exports = grammar({
repeat(
seq(
$.identifier,
$.type_definition,
$.type_specification,
optional(','),
),
),
')',
$.type_definition,
$.type_specification,
$.block,
),

View File

@ -29,7 +29,7 @@
[
(type)
(type_definition)
(type_specification)
] @type
(assignment_operator) @operator.assignment

View File

@ -280,7 +280,7 @@
},
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
}
]
},
@ -310,7 +310,7 @@
},
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
},
{
"type": "STRING",
@ -377,7 +377,7 @@
},
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
},
{
"type": "STRING",
@ -673,7 +673,7 @@
"members": [
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
},
{
"type": "BLANK"
@ -939,7 +939,7 @@
"members": [
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
},
{
"type": "BLANK"
@ -1207,7 +1207,7 @@
]
}
},
"type_definition": {
"type_specification": {
"type": "SEQ",
"members": [
{
@ -1387,7 +1387,7 @@
},
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
},
{
"type": "CHOICE",
@ -1410,7 +1410,7 @@
},
{
"type": "SYMBOL",
"name": "type_definition"
"name": "type_specification"
},
{
"type": "SYMBOL",

View File

@ -20,7 +20,7 @@
"named": true
},
{
"type": "type_definition",
"type": "type_specification",
"named": true
}
]
@ -173,7 +173,7 @@
"named": true
},
{
"type": "type_definition",
"type": "type_specification",
"named": true
}
]
@ -396,7 +396,7 @@
"named": true
},
{
"type": "type_definition",
"type": "type_specification",
"named": true
}
]
@ -462,7 +462,7 @@
"named": true
},
{
"type": "type_definition",
"type": "type_specification",
"named": true
}
]
@ -577,7 +577,7 @@
"named": true
},
{
"type": "type_definition",
"type": "type_specification",
"named": true
}
]
@ -603,7 +603,7 @@
}
},
{
"type": "type_definition",
"type": "type_specification",
"named": true,
"fields": {},
"children": {

View File

@ -114,7 +114,7 @@ enum {
sym_while = 95,
sym_for = 96,
sym_return = 97,
sym_type_definition = 98,
sym_type_specification = 98,
sym_type = 99,
sym_function = 100,
sym_function_expression = 101,
@ -232,7 +232,7 @@ static const char * const ts_symbol_names[] = {
[sym_while] = "while",
[sym_for] = "for",
[sym_return] = "return",
[sym_type_definition] = "type_definition",
[sym_type_specification] = "type_specification",
[sym_type] = "type",
[sym_function] = "function",
[sym_function_expression] = "function_expression",
@ -350,7 +350,7 @@ static const TSSymbol ts_symbol_map[] = {
[sym_while] = sym_while,
[sym_for] = sym_for,
[sym_return] = sym_return,
[sym_type_definition] = sym_type_definition,
[sym_type_specification] = sym_type_specification,
[sym_type] = sym_type,
[sym_function] = sym_function,
[sym_function_expression] = sym_function_expression,
@ -762,7 +762,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = {
.visible = true,
.named = true,
},
[sym_type_definition] = {
[sym_type_specification] = {
.visible = true,
.named = true,
},
@ -5960,7 +5960,7 @@ static const uint16_t ts_small_parse_table[] = {
STATE(46), 1,
sym_assignment_operator,
STATE(393), 1,
sym_type_definition,
sym_type_specification,
ACTIONS(234), 2,
anon_sym_PLUS_EQ,
anon_sym_DASH_EQ,
@ -7156,7 +7156,7 @@ static const uint16_t ts_small_parse_table[] = {
STATE(46), 1,
sym_assignment_operator,
STATE(390), 1,
sym_type_definition,
sym_type_specification,
ACTIONS(234), 2,
anon_sym_PLUS_EQ,
anon_sym_DASH_EQ,
@ -10269,7 +10269,7 @@ static const uint16_t ts_small_parse_table[] = {
STATE(29), 1,
sym_assignment_operator,
STATE(391), 1,
sym_type_definition,
sym_type_specification,
ACTIONS(234), 2,
anon_sym_PLUS_EQ,
anon_sym_DASH_EQ,
@ -19415,7 +19415,7 @@ static const uint16_t ts_small_parse_table[] = {
STATE(40), 1,
sym_assignment_operator,
STATE(392), 1,
sym_type_definition,
sym_type_specification,
ACTIONS(234), 2,
anon_sym_PLUS_EQ,
anon_sym_DASH_EQ,
@ -20336,7 +20336,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(232), 1,
anon_sym_LT,
STATE(421), 1,
sym_type_definition,
sym_type_specification,
ACTIONS(224), 2,
anon_sym_DASH,
anon_sym_GT,
@ -21532,7 +21532,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(827), 1,
anon_sym_LT,
STATE(480), 1,
sym_type_definition,
sym_type_specification,
[18949] = 4,
ACTIONS(3), 1,
sym__comment,
@ -21594,7 +21594,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(848), 1,
anon_sym_EQ,
STATE(453), 1,
sym_type_definition,
sym_type_specification,
[19038] = 4,
ACTIONS(3), 1,
sym__comment,
@ -21791,7 +21791,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(890), 1,
anon_sym_EQ,
STATE(431), 1,
sym_type_definition,
sym_type_specification,
[19322] = 4,
ACTIONS(3), 1,
sym__comment,
@ -21885,14 +21885,14 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(827), 1,
anon_sym_LT,
STATE(421), 1,
sym_type_definition,
sym_type_specification,
[19455] = 3,
ACTIONS(3), 1,
sym__comment,
ACTIONS(827), 1,
anon_sym_LT,
STATE(405), 1,
sym_type_definition,
sym_type_specification,
[19465] = 3,
ACTIONS(3), 1,
sym__comment,
@ -21906,7 +21906,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(827), 1,
anon_sym_LT,
STATE(427), 1,
sym_type_definition,
sym_type_specification,
[19485] = 2,
ACTIONS(3), 1,
sym__comment,
@ -21933,7 +21933,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(827), 1,
anon_sym_LT,
STATE(407), 1,
sym_type_definition,
sym_type_specification,
[19523] = 2,
ACTIONS(3), 1,
sym__comment,
@ -21958,7 +21958,7 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(827), 1,
anon_sym_LT,
STATE(429), 1,
sym_type_definition,
sym_type_specification,
[19557] = 2,
ACTIONS(3), 1,
sym__comment,
@ -21971,14 +21971,14 @@ static const uint16_t ts_small_parse_table[] = {
ACTIONS(827), 1,
anon_sym_LT,
STATE(399), 1,
sym_type_definition,
sym_type_specification,
[19575] = 3,
ACTIONS(3), 1,
sym__comment,
ACTIONS(827), 1,
anon_sym_LT,
STATE(408), 1,
sym_type_definition,
sym_type_specification,
[19585] = 3,
ACTIONS(3), 1,
sym__comment,
@ -23019,8 +23019,8 @@ static const TSParseActionEntry ts_parse_actions[] = {
[809] = {.entry = {.count = 1, .reusable = true}}, SHIFT(287),
[811] = {.entry = {.count = 1, .reusable = false}}, REDUCE(aux_sym_type_repeat1, 2),
[813] = {.entry = {.count = 1, .reusable = false}}, SHIFT(425),
[815] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_type_definition, 3),
[817] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_type_definition, 3),
[815] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_type_specification, 3),
[817] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_type_specification, 3),
[819] = {.entry = {.count = 1, .reusable = true}}, SHIFT(27),
[821] = {.entry = {.count = 1, .reusable = true}}, SHIFT(395),
[823] = {.entry = {.count = 1, .reusable = true}}, SHIFT(86),