Skip to content

Commit

Permalink
Moved labels as leading comments to Expression references
Browse files Browse the repository at this point in the history
This allows labelling Separated, which was the remaining grammar Item that
needed it.
  • Loading branch information
ggiraldez committed May 28, 2024
1 parent 75c8f52 commit 0f86d80
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 104 deletions.
76 changes: 29 additions & 47 deletions crates/codegen/ebnf/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ impl Builder {

let variants = variants.iter().map(|EnumVariant { reference, enabled }| {
Value::new(
Self::build_label_comment(BuiltInLabel::Variant.as_ref()),
Self::build_ref(reference),
Self::build_ref(Some(BuiltInLabel::Variant.as_ref()), reference),
Self::build_enabled_comment(enabled),
)
});
Expand All @@ -167,20 +166,17 @@ impl Builder {

self.add_entry(name, Terminal::No, Inlined::No);

let label = BuiltInLabel::Item.as_ref();
let expression = if allow_empty.unwrap_or_default() {
Expression::new_zero_or_more(Self::build_ref(reference).into())
Expression::new_zero_or_more(Self::build_ref(Some(label), reference).into())
} else {
Expression::new_one_or_more(Self::build_ref(reference).into())
Expression::new_one_or_more(Self::build_ref(Some(label), reference).into())
};

self.add_definition(
name,
Self::build_enabled_comment(enabled),
Some(Value::new(
Self::build_label_comment(BuiltInLabel::Item.as_ref()),
expression,
None,
)),
Some(Value::new(expression, None)),
DefinitionKind::Sequence,
);
}
Expand All @@ -197,11 +193,11 @@ impl Builder {
self.add_entry(name, Terminal::No, Inlined::No);

let mut expression = Expression::new_sequence(vec![
Self::build_ref(reference),
Self::build_ref(Some(BuiltInLabel::Item.as_ref()), reference),
Expression::new_zero_or_more(
Expression::new_sequence(vec![
Self::build_ref(separator),
Self::build_ref(reference),
Self::build_ref(Some(BuiltInLabel::Separator.as_ref()), separator),
Self::build_ref(Some(BuiltInLabel::Item.as_ref()), reference),
])
.into(),
),
Expand All @@ -214,7 +210,7 @@ impl Builder {
self.add_definition(
name,
Self::build_enabled_comment(enabled),
Some(Value::new(None, expression, None)),
Some(Value::new(expression, None)),
DefinitionKind::Sequence,
);
}
Expand All @@ -235,8 +231,7 @@ impl Builder {
let PrecedenceExpression { name, operators } = precedence_expression.as_ref();

values.push(Value::new(
Self::build_label_comment(BuiltInLabel::Variant.as_ref()),
Self::build_ref(name),
Self::build_ref(Some(BuiltInLabel::Variant.as_ref()), name),
None,
));

Expand All @@ -251,8 +246,7 @@ impl Builder {
let PrimaryExpression { reference, enabled } = primary_expression;

values.push(Value::new(
Self::build_label_comment(BuiltInLabel::Variant.as_ref()),
Self::build_ref(reference),
Self::build_ref(Some(BuiltInLabel::Variant.as_ref()), reference),
Self::build_enabled_comment(enabled),
));
}
Expand Down Expand Up @@ -286,8 +280,7 @@ impl Builder {
leading_comments.push("Prefix unary operator".to_string());

values.push(Value::new(
Self::build_label_comment(BuiltInLabel::Operand.as_ref()),
Self::build_ref(base_name),
Self::build_ref(Some(BuiltInLabel::Operand.as_ref()), base_name),
None,
));
}
Expand All @@ -297,8 +290,7 @@ impl Builder {
values.insert(
0,
Value::new(
Self::build_label_comment(BuiltInLabel::Operand.as_ref()),
Self::build_ref(base_name),
Self::build_ref(Some(BuiltInLabel::Operand.as_ref()), base_name),
None,
),
);
Expand All @@ -309,14 +301,12 @@ impl Builder {
values.insert(
0,
Value::new(
Self::build_label_comment(BuiltInLabel::LeftOperand.as_ref()),
Self::build_ref(base_name),
Self::build_ref(Some(BuiltInLabel::LeftOperand.as_ref()), base_name),
None,
),
);
values.push(Value::new(
Self::build_label_comment(BuiltInLabel::RightOperand.as_ref()),
Self::build_ref(base_name),
Self::build_ref(Some(BuiltInLabel::RightOperand.as_ref()), base_name),
None,
));
}
Expand All @@ -326,14 +316,12 @@ impl Builder {
values.insert(
0,
Value::new(
Self::build_label_comment(BuiltInLabel::LeftOperand.as_ref()),
Self::build_ref(base_name),
Self::build_ref(Some(BuiltInLabel::LeftOperand.as_ref()), base_name),
None,
),
);
values.push(Value::new(
Self::build_label_comment(BuiltInLabel::RightOperand.as_ref()),
Self::build_ref(base_name),
Self::build_ref(Some(BuiltInLabel::RightOperand.as_ref()), base_name),
None,
));
}
Expand All @@ -348,14 +336,11 @@ impl Builder {
fields
.iter()
.map(|(identifier, field)| match field {
Field::Required { reference } => Value::new(
Self::build_label_comment(identifier),
Self::build_ref(reference),
None,
),
Field::Required { reference } => {
Value::new(Self::build_ref(Some(identifier), reference), None)
}
Field::Optional { reference, enabled } => Value::new(
Self::build_label_comment(identifier),
Expression::new_optional(Self::build_ref(reference).into()),
Expression::new_optional(Self::build_ref(Some(identifier), reference).into()),
Self::build_enabled_comment(enabled),
),
})
Expand All @@ -370,7 +355,7 @@ impl Builder {
self.add_definition(
name,
None,
Some(Value::new(None, Self::build_scanner(scanner), None)),
Some(Value::new(Self::build_scanner(scanner), None)),
DefinitionKind::Sequence,
);
}
Expand Down Expand Up @@ -398,7 +383,7 @@ impl Builder {
self.add_definition(
name,
leading_comments,
Some(Value::new(None, Self::build_keyword_value(value), None)),
Some(Value::new(Self::build_keyword_value(value), None)),
DefinitionKind::Sequence,
);
}
Expand All @@ -413,7 +398,7 @@ impl Builder {
self.add_definition(
name,
Self::build_enabled_comment(enabled),
Some(Value::new(None, Self::build_scanner(scanner), None)),
Some(Value::new(Self::build_scanner(scanner), None)),
DefinitionKind::Sequence,
);
}
Expand All @@ -431,7 +416,7 @@ impl Builder {
self.add_definition(
name,
Self::build_enabled_comment(enabled),
Some(Value::new(None, Self::build_scanner(scanner), None)),
Some(Value::new(Self::build_scanner(scanner), None)),
DefinitionKind::Sequence,
);
}
Expand Down Expand Up @@ -475,10 +460,6 @@ impl Builder {
}
}

fn build_label_comment(label: &str) -> Option<String> {
Some(format!("{label}:"))
}

fn build_scanner(scanner: &Scanner) -> Expression {
match scanner {
Scanner::Sequence { scanners } => {
Expand Down Expand Up @@ -521,12 +502,13 @@ impl Builder {
scanner,
not_followed_by: _,
} => Self::build_scanner(scanner),
Scanner::Fragment { reference } => Self::build_ref(reference),
Scanner::Fragment { reference } => Self::build_ref(None, reference),
}
}

fn build_ref(reference: &Identifier) -> Expression {
Expression::new_reference(reference.to_owned())
fn build_ref(label: Option<&str>, reference: &Identifier) -> Expression {
let leading_comment = label.map(|label| format!("{label}:"));
Expression::new_reference(leading_comment, reference.to_owned())
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/ebnf/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub enum DefinitionKind {
/// ```
#[derive(derive_new::new)]
pub struct Value {
pub leading_comment: Option<String>,
pub expression: Expression,
pub trailing_comment: Option<String>,
}
Expand Down Expand Up @@ -91,6 +90,7 @@ pub enum Expression {
atom: String,
},
Reference {
leading_comment: Option<String>,
reference: Identifier,
},
}
Expand Down
15 changes: 8 additions & 7 deletions crates/codegen/ebnf/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ impl<'s, W: EbnfWriter> Serializer<'s, W> {

for (index, value) in values.iter().enumerate() {
let Value {
leading_comment,
expression,
trailing_comment,
} = value;
Expand All @@ -88,11 +87,6 @@ impl<'s, W: EbnfWriter> Serializer<'s, W> {
))?;
}

if let Some(comment) = leading_comment {
self.serialize_comment(comment)?;
self.serialize_punctuation(" ")?;
}

self.serialize_expr(expression)?;

if index + 1 == values.len() {
Expand Down Expand Up @@ -153,7 +147,14 @@ impl<'s, W: EbnfWriter> Serializer<'s, W> {
Expression::Atom { atom } => {
self.serialize_string_literal(atom)?;
}
Expression::Reference { reference } => {
Expression::Reference {
leading_comment,
reference,
} => {
if let Some(comment) = leading_comment {
self.serialize_comment(comment)?;
self.serialize_punctuation(" ")?;
}
self.serialize_identifier(reference)?;
}
};
Expand Down
9 changes: 6 additions & 3 deletions crates/codegen/spec/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;

use codegen_ebnf::EbnfModel;
use codegen_language_definition::model::{Identifier, Language, Section, Topic, Item};
use codegen_language_definition::model::{Identifier, Item, Language, Section, Topic};
use inflector::Inflector;
use serde::Serialize;

Expand Down Expand Up @@ -94,9 +94,12 @@ impl SpecTopic {

// We need to also add any precedence expressions, as they define
// items but are not direct children of the topic
if let Item::Precedence{ item: precedence_item } = item {
if let Item::Precedence {
item: precedence_item,
} = item
{
for precedence_expr in &precedence_item.as_ref().precedence_expressions {
items.push(precedence_expr.as_ref().name.to_owned());
items.push(precedence_expr.as_ref().name.clone());
}
}
}
Expand Down
Loading

0 comments on commit 0f86d80

Please sign in to comment.