Skip to content

Commit

Permalink
feat: require constants to be typed (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Jan 20, 2023
1 parent 7098a99 commit 8dc8cd1
Show file tree
Hide file tree
Showing 27 changed files with 1,970 additions and 1,694 deletions.
3 changes: 3 additions & 0 deletions src/parser/internal/definition/constant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lexer::token::TokenKind;
use crate::parser::internal::definition::r#type;
use crate::parser::internal::expression;
use crate::parser::internal::identifier;
use crate::parser::internal::utils;
Expand All @@ -12,6 +13,7 @@ pub fn constant_definition(state: &mut State) -> ParseResult<ConstantDefinition>
Ok(ConstantDefinition {
comments: state.iterator.comments(),
r#const: utils::skip_keyword(state, TokenKind::Const)?,
type_definition: r#type::type_definition(state)?,
name: identifier::constant_identifier(state)?,
equals: utils::skip(state, TokenKind::Equals)?,
value: expression::create(state)?,
Expand All @@ -28,6 +30,7 @@ pub fn classish_constant_definition(
attributes: state.get_attributes(),
modifiers,
r#const: utils::skip_keyword(state, TokenKind::Const)?,
type_definition: r#type::type_definition(state)?,
name: identifier::constant_identifier(state)?,
equals: utils::skip(state, TokenKind::Equals)?,
value: expression::create(state)?,
Expand Down
11 changes: 10 additions & 1 deletion src/tree/definition/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::Serialize;
use crate::tree::comment::CommentGroup;
use crate::tree::definition::attribute::AttributeGroupDefinition;
use crate::tree::definition::modifier::ModifierGroupDefinition;
use crate::tree::definition::r#type::TypeDefinition;
use crate::tree::expression::Expression;
use crate::tree::identifier::Identifier;
use crate::tree::token::Keyword;
Expand All @@ -15,6 +16,7 @@ use crate::tree::Node;
pub struct ConstantDefinition {
pub comments: CommentGroup,
pub r#const: Keyword,
pub type_definition: TypeDefinition,
pub name: Identifier,
pub equals: usize,
pub value: Expression,
Expand All @@ -28,6 +30,7 @@ pub struct ClassishConstantDefinition {
pub attributes: Vec<AttributeGroupDefinition>,
pub modifiers: ModifierGroupDefinition,
pub r#const: Keyword,
pub type_definition: TypeDefinition,
pub name: Identifier,
pub equals: usize,
pub value: Expression,
Expand All @@ -48,7 +51,12 @@ impl Node for ConstantDefinition {
}

fn children(&self) -> Vec<&dyn Node> {
vec![&self.r#const, &self.name, &self.value]
vec![
&self.r#const,
&self.type_definition,
&self.name,
&self.value,
]
}

fn get_description(&self) -> String {
Expand Down Expand Up @@ -81,6 +89,7 @@ impl Node for ClassishConstantDefinition {

children.push(&self.modifiers);
children.push(&self.r#const);
children.push(&self.type_definition);
children.push(&self.name);
children.push(&self.value);

Expand Down
4 changes: 2 additions & 2 deletions tests/samples/0028/code.ara
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const FOO = 1;
const BAR = FOO;
const u8 FOO = 1;
const u8 BAR = FOO;
36 changes: 26 additions & 10 deletions tests/samples/0028/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ DefinitionTree {
value: "const",
position: 0,
},
type_definition: UnsignedInteger(
U8(
Keyword {
value: "u8",
position: 6,
},
),
),
name: Identifier {
position: 6,
position: 9,
value: "FOO",
},
equals: 10,
equals: 13,
value: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
value: "1",
position: 12,
position: 15,
},
),
),
semicolon: 13,
semicolon: 16,
},
),
Constant(
Expand All @@ -35,22 +43,30 @@ DefinitionTree {
},
const: Keyword {
value: "const",
position: 16,
position: 19,
},
type_definition: UnsignedInteger(
U8(
Keyword {
value: "u8",
position: 25,
},
),
),
name: Identifier {
position: 22,
position: 28,
value: "BAR",
},
equals: 26,
equals: 32,
value: Identifier(
Identifier {
position: 28,
position: 34,
value: "FOO",
},
),
semicolon: 31,
semicolon: 37,
},
),
],
eof: 33,
eof: 39,
}
2 changes: 1 addition & 1 deletion tests/samples/0029/code.ara
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const FOO = 1, BAR = 2;
const int FOO = 1, BAR = 2;
6 changes: 3 additions & 3 deletions tests/samples/0029/error.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[P0013]: unexpected token `,`, expected `;`
--> 0029/code.ara:1:14
--> 0029/code.ara:1:18
|
1 | const FOO = 1, BAR = 2;
| ^
1 | const int FOO = 1, BAR = 2;
| ^

error: failed to parse "0029/code.ara" due to the above issue(s)
= summary: 1 error(s)
Expand Down
6 changes: 3 additions & 3 deletions tests/samples/0043/code.ara
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
interface A extends B, C {
#[R]
const F = 344;
const u16 F = 344;

#[R]
public const O = 344;
public const u16 O = 344;

#[R]
#[P]
final public const R = 344, P = 214;
final public const u16 R = 344, P = 214;
}
6 changes: 3 additions & 3 deletions tests/samples/0043/error.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[P0013]: unexpected token `,`, expected `;`
--> 0043/code.ara:10:31
--> 0043/code.ara:10:35
|
10 | final public const R = 344, P = 214;
| ^
10 | final public const u16 R = 344, P = 214;
| ^

error: failed to parse "0043/code.ara" due to the above issue(s)
= summary: 1 error(s)
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/0055/code.ara
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Foo {
const foo = 1;
const u8 foo = 1;

public static function foo(): void {
$a = static::foo;
Expand Down
66 changes: 37 additions & 29 deletions tests/samples/0055/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,31 @@ DefinitionTree {
value: "const",
position: 16,
},
type_definition: UnsignedInteger(
U8(
Keyword {
value: "u8",
position: 22,
},
),
),
name: Identifier {
position: 22,
position: 25,
value: "foo",
},
equals: 26,
equals: 29,
value: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
value: "1",
position: 28,
position: 31,
},
),
),
semicolon: 29,
semicolon: 32,
},
),
ConcreteMethod(
Expand All @@ -64,48 +72,48 @@ DefinitionTree {
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 50,
position: 53,
modifiers: [
Public(
Keyword {
value: "public",
position: 36,
position: 39,
},
),
Static(
Keyword {
value: "static",
position: 43,
position: 46,
},
),
],
},
function: Keyword {
value: "function",
position: 50,
position: 53,
},
name: Identifier {
position: 59,
position: 62,
value: "foo",
},
templates: None,
parameters: FunctionLikeParameterListDefinition {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 62,
left_parenthesis: 65,
parameters: CommaSeparated {
inner: [],
commas: [],
},
right_parenthesis: 63,
right_parenthesis: 66,
},
return_type: FunctionLikeReturnTypeDefinition {
colon: 64,
colon: 67,
type_definition: Void(
Keyword {
value: "void",
position: 66,
position: 69,
},
),
},
Expand All @@ -114,7 +122,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
left_brace: 71,
left_brace: 74,
statements: [
Expression(
ExpressionStatement {
Expand All @@ -128,32 +136,32 @@ DefinitionTree {
},
left: Variable(
Variable {
position: 81,
position: 84,
name: "$a",
},
),
equals: 84,
equals: 87,
right: ClassOperation(
ConstantFetch {
comments: CommentGroup {
comments: [],
},
class: Identifier(
Identifier {
position: 86,
position: 89,
value: "static",
},
),
double_colon: 92,
double_colon: 95,
constant: Identifier {
position: 94,
position: 97,
value: "foo",
},
},
),
},
),
semicolon: 97,
semicolon: 100,
},
),
Expression(
Expand All @@ -168,42 +176,42 @@ DefinitionTree {
},
class: Identifier(
Identifier {
position: 108,
position: 111,
value: "static",
},
),
double_colon: 114,
double_colon: 117,
method: Identifier {
position: 116,
position: 119,
value: "foo",
},
generics: None,
arguments: ArgumentListExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 119,
left_parenthesis: 122,
arguments: CommaSeparated {
inner: [],
commas: [],
},
right_parenthesis: 120,
right_parenthesis: 123,
},
},
),
semicolon: 121,
semicolon: 124,
},
),
],
right_brace: 127,
right_brace: 130,
},
},
),
],
right_brace: 129,
right_brace: 132,
},
},
),
],
eof: 131,
eof: 134,
}
Loading

0 comments on commit 8dc8cd1

Please sign in to comment.