Skip to content

Commit

Permalink
chore: stop allowing multiple constant entries (#39)
Browse files Browse the repository at this point in the history
* remove entries and make `ConstantDefinition` self contained

* remove entries and make `ClassishConstantDefinition` self contained
  • Loading branch information
KennedyTedesco committed Jan 20, 2023
1 parent b349d8b commit 7098a99
Show file tree
Hide file tree
Showing 19 changed files with 2,318 additions and 2,644 deletions.
29 changes: 6 additions & 23 deletions src/parser/internal/definition/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@ use crate::parser::result::ParseResult;
use crate::parser::state::State;
use crate::tree::definition::constant::ClassishConstantDefinition;
use crate::tree::definition::constant::ConstantDefinition;
use crate::tree::definition::constant::ConstantDefinitionEntry;
use crate::tree::definition::modifier::ModifierGroupDefinition;

pub fn constant_definition(state: &mut State) -> ParseResult<ConstantDefinition> {
Ok(ConstantDefinition {
comments: state.iterator.comments(),
r#const: utils::skip_keyword(state, TokenKind::Const)?,
entries: utils::at_least_one_comma_separated(
state,
&|state| {
Ok(ConstantDefinitionEntry {
name: identifier::constant_identifier(state)?,
equals: utils::skip(state, TokenKind::Equals)?,
value: expression::create(state)?,
})
},
TokenKind::SemiColon,
)?,
name: identifier::constant_identifier(state)?,
equals: utils::skip(state, TokenKind::Equals)?,
value: expression::create(state)?,
semicolon: utils::skip_semicolon(state)?,
})
}
Expand All @@ -37,17 +28,9 @@ pub fn classish_constant_definition(
attributes: state.get_attributes(),
modifiers,
r#const: utils::skip_keyword(state, TokenKind::Const)?,
entries: utils::at_least_one_comma_separated(
state,
&|state| {
Ok(ConstantDefinitionEntry {
name: identifier::constant_identifier(state)?,
equals: utils::skip(state, TokenKind::Equals)?,
value: expression::create(state)?,
})
},
TokenKind::SemiColon,
)?,
name: identifier::constant_identifier(state)?,
equals: utils::skip(state, TokenKind::Equals)?,
value: expression::create(state)?,
semicolon: utils::skip_semicolon(state)?,
})
}
49 changes: 9 additions & 40 deletions src/tree/definition/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ use crate::tree::definition::modifier::ModifierGroupDefinition;
use crate::tree::expression::Expression;
use crate::tree::identifier::Identifier;
use crate::tree::token::Keyword;
use crate::tree::utils::CommaSeparated;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ConstantDefinitionEntry {
pub name: Identifier,
pub equals: usize,
pub value: Expression,
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ConstantDefinition {
pub comments: CommentGroup,
pub r#const: Keyword,
pub entries: CommaSeparated<ConstantDefinitionEntry>,
pub name: Identifier,
pub equals: usize,
pub value: Expression,
pub semicolon: usize,
}

Expand All @@ -35,28 +28,12 @@ pub struct ClassishConstantDefinition {
pub attributes: Vec<AttributeGroupDefinition>,
pub modifiers: ModifierGroupDefinition,
pub r#const: Keyword,
pub entries: CommaSeparated<ConstantDefinitionEntry>,
pub name: Identifier,
pub equals: usize,
pub value: Expression,
pub semicolon: usize,
}

impl Node for ConstantDefinitionEntry {
fn initial_position(&self) -> usize {
self.name.initial_position()
}

fn final_position(&self) -> usize {
self.value.final_position()
}

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

fn get_description(&self) -> String {
"constant entry definition".to_string()
}
}

impl Node for ConstantDefinition {
fn comments(&self) -> Option<&CommentGroup> {
Some(&self.comments)
Expand All @@ -71,13 +48,7 @@ impl Node for ConstantDefinition {
}

fn children(&self) -> Vec<&dyn Node> {
let mut children: Vec<&dyn Node> = vec![&self.r#const];

for entry in &self.entries.inner {
children.push(entry);
}

children
vec![&self.r#const, &self.name, &self.value]
}

fn get_description(&self) -> String {
Expand Down Expand Up @@ -110,10 +81,8 @@ impl Node for ClassishConstantDefinition {

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

for entry in &self.entries.inner {
children.push(entry);
}
children.push(&self.name);
children.push(&self.value);

children
}
Expand Down
3 changes: 2 additions & 1 deletion tests/samples/0028/code.ara
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const FOO = 1;
const FOO = 1;
const BAR = FOO;
60 changes: 38 additions & 22 deletions tests/samples/0028/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,48 @@ DefinitionTree {
value: "const",
position: 0,
},
entries: CommaSeparated {
inner: [
ConstantDefinitionEntry {
name: Identifier {
position: 6,
value: "FOO",
name: Identifier {
position: 6,
value: "FOO",
},
equals: 10,
value: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
equals: 10,
value: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
value: "1",
position: 12,
},
),
),
value: "1",
position: 12,
},
],
commas: [],
},
),
),
semicolon: 13,
},
),
Constant(
ConstantDefinition {
comments: CommentGroup {
comments: [],
},
const: Keyword {
value: "const",
position: 16,
},
name: Identifier {
position: 22,
value: "BAR",
},
equals: 26,
value: Identifier(
Identifier {
position: 28,
value: "FOO",
},
),
semicolon: 31,
},
),
],
eof: 14,
eof: 33,
}
9 changes: 9 additions & 0 deletions tests/samples/0029/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[P0013]: unexpected token `,`, expected `;`
--> 0029/code.ara:1:14
|
1 | const FOO = 1, BAR = 2;
| ^

error: failed to parse "0029/code.ara" due to the above issue(s)
= summary: 1 error(s)

60 changes: 0 additions & 60 deletions tests/samples/0029/tree.txt

This file was deleted.

13 changes: 11 additions & 2 deletions tests/samples/0043/code.ara
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
const A = 3,
B = 3;
interface A extends B, C {
#[R]
const F = 344;

#[R]
public const O = 344;

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

error: failed to parse "0043/code.ara" due to the above issue(s)
= summary: 1 error(s)

60 changes: 0 additions & 60 deletions tests/samples/0043/tree.txt

This file was deleted.

Loading

0 comments on commit 7098a99

Please sign in to comment.