Skip to content

Commit

Permalink
infra improvements (#1150)
Browse files Browse the repository at this point in the history
minor improvements to the codegen backend based on earlier changes in
#1101 and #1120

- build npm packages in parallel
- rename `BuiltInLabel` to `PredefinedLabel` to disambiguate from the
newly introduced built-ins in bindings.
- rename `wasm32-wasi` to `wasm32-wasip1`, as it is already deprecated
in nightly.
  • Loading branch information
OmarTawfik authored Nov 22, 2024
1 parent d9e7b78 commit 249a5b1
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions crates/codegen/ebnf/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;

use codegen_language_definition::model::{
BuiltInLabel, EnumItem, EnumVariant, Field, FragmentItem, Identifier, Item, KeywordDefinition,
KeywordItem, KeywordValue, Language, OperatorModel, PrecedenceExpression, PrecedenceItem,
PrecedenceOperator, PrimaryExpression, RepeatedItem, Scanner, SeparatedItem, StructItem,
TokenDefinition, TokenItem, TriviaItem, VersionSpecifier,
EnumItem, EnumVariant, Field, FragmentItem, Identifier, Item, KeywordDefinition, KeywordItem,
KeywordValue, Language, OperatorModel, PrecedenceExpression, PrecedenceItem,
PrecedenceOperator, PredefinedLabel, PrimaryExpression, RepeatedItem, Scanner, SeparatedItem,
StructItem, TokenDefinition, TokenItem, TriviaItem, VersionSpecifier,
};
use indexmap::IndexMap;
use inflector::Inflector;
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Builder {

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

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

let label = BuiltInLabel::Item.as_ref();
let label = PredefinedLabel::Item.as_ref();
let expression = if allow_empty.unwrap_or_default() {
Expression::new_zero_or_more(Self::build_ref(Some(label), reference).into())
} else {
Expand All @@ -178,11 +178,11 @@ impl Builder {
self.add_entry(name, Terminal::No, Inlined::No);

let mut expression = Expression::new_sequence(vec![
Self::build_ref(Some(BuiltInLabel::Item.as_ref()), reference),
Self::build_ref(Some(PredefinedLabel::Item.as_ref()), reference),
Expression::new_zero_or_more(
Expression::new_sequence(vec![
Self::build_ref(Some(BuiltInLabel::Separator.as_ref()), separator),
Self::build_ref(Some(BuiltInLabel::Item.as_ref()), reference),
Self::build_ref(Some(PredefinedLabel::Separator.as_ref()), separator),
Self::build_ref(Some(PredefinedLabel::Item.as_ref()), reference),
])
.into(),
),
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Builder {
let PrecedenceExpression { name, operators } = precedence_expression.as_ref();

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

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

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

values.push(Value::new(
Self::build_ref(Some(BuiltInLabel::Operand.as_ref()), base_name),
Self::build_ref(Some(PredefinedLabel::Operand.as_ref()), base_name),
None,
));
}
Expand All @@ -275,7 +275,7 @@ impl Builder {
values.insert(
0,
Value::new(
Self::build_ref(Some(BuiltInLabel::Operand.as_ref()), base_name),
Self::build_ref(Some(PredefinedLabel::Operand.as_ref()), base_name),
None,
),
);
Expand All @@ -286,12 +286,12 @@ impl Builder {
values.insert(
0,
Value::new(
Self::build_ref(Some(BuiltInLabel::LeftOperand.as_ref()), base_name),
Self::build_ref(Some(PredefinedLabel::LeftOperand.as_ref()), base_name),
None,
),
);
values.push(Value::new(
Self::build_ref(Some(BuiltInLabel::RightOperand.as_ref()), base_name),
Self::build_ref(Some(PredefinedLabel::RightOperand.as_ref()), base_name),
None,
));
}
Expand All @@ -301,12 +301,12 @@ impl Builder {
values.insert(
0,
Value::new(
Self::build_ref(Some(BuiltInLabel::LeftOperand.as_ref()), base_name),
Self::build_ref(Some(PredefinedLabel::LeftOperand.as_ref()), base_name),
None,
),
);
values.push(Value::new(
Self::build_ref(Some(BuiltInLabel::RightOperand.as_ref()), base_name),
Self::build_ref(Some(PredefinedLabel::RightOperand.as_ref()), base_name),
None,
));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub struct Topic {
Clone, Copy, Debug, strum_macros::AsRefStr, strum_macros::EnumIter, strum_macros::VariantNames,
)]
#[strum(serialize_all = "snake_case")]
pub enum BuiltInLabel {
pub enum PredefinedLabel {
Item,
Variant,
Separator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#[derive(Clone, Copy)]
pub enum EdgeLabel {
// Built-in:
{% for label in model.kinds.built_in_labels -%}
{% for label in model.kinds.predefined_labels -%}
{{ label | pascal_case }},
{%- endfor %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface cst {

/// Represents the different types of relationships between nodes in the syntax tree.
enum edge-label {
{%- for label in model.kinds.built_in_labels %}
{%- for label in model.kinds.predefined_labels %}
{{ label | wit_case }},
{%- endfor %}

Expand Down
12 changes: 6 additions & 6 deletions crates/codegen/runtime/generator/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use codegen_language_definition::model::{self, BuiltInLabel};
use codegen_language_definition::model::{self, PredefinedLabel};
use indexmap::{IndexMap, IndexSet};
use serde::Serialize;

Expand Down Expand Up @@ -208,7 +208,7 @@ impl AstModel {
base_name: &model::Identifier,
expression: &model::PrecedenceExpression,
) {
let operand = |label: BuiltInLabel| Field {
let operand = |label: PredefinedLabel| Field {
label: label.as_ref().into(),
r#type: Some(base_name.clone()),
is_optional: false,
Expand All @@ -224,17 +224,17 @@ impl AstModel {
match operator.model {
model::OperatorModel::Prefix => {
fields.extend(self.convert_fields(&operator.fields));
fields.push(operand(BuiltInLabel::Operand));
fields.push(operand(PredefinedLabel::Operand));
}
model::OperatorModel::Postfix => {
fields.push(operand(BuiltInLabel::Operand));
fields.push(operand(PredefinedLabel::Operand));
fields.extend(self.convert_fields(&operator.fields));
}
model::OperatorModel::BinaryLeftAssociative
| model::OperatorModel::BinaryRightAssociative => {
fields.push(operand(BuiltInLabel::LeftOperand));
fields.push(operand(PredefinedLabel::LeftOperand));
fields.extend(self.convert_fields(&operator.fields));
fields.push(operand(BuiltInLabel::RightOperand));
fields.push(operand(PredefinedLabel::RightOperand));
}
};

Expand Down
8 changes: 4 additions & 4 deletions crates/codegen/runtime/generator/src/kinds/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::BTreeSet;

use codegen_language_definition::model::{self, BuiltInLabel, Identifier, Item};
use codegen_language_definition::model::{self, Identifier, Item, PredefinedLabel};
use serde::Serialize;
use strum::VariantNames;

Expand All @@ -14,8 +14,8 @@ pub struct KindsModel {
trivia_scanner_names: BTreeSet<Identifier>,
/// Defines `EdgeLabel` enum variants.
labels: BTreeSet<Identifier>,
/// Built-in labels for edges.
built_in_labels: &'static [&'static str],
/// Predefined labels for edges.
predefined_labels: &'static [&'static str],
// Defines the `LexicalContext(Type)` enum and type-level variants.
lexical_contexts: BTreeSet<Identifier>,
/// Defines the root `NonterminalKind` for a source file of the language.
Expand All @@ -29,7 +29,7 @@ impl Default for KindsModel {
terminal_kinds: BTreeSet::default(),
trivia_scanner_names: BTreeSet::default(),
labels: BTreeSet::default(),
built_in_labels: BuiltInLabel::VARIANTS,
predefined_labels: PredefinedLabel::VARIANTS,
lexical_contexts: BTreeSet::default(),
root_kind: Identifier::from("Stub1"),
}
Expand Down
31 changes: 17 additions & 14 deletions crates/codegen/runtime/generator/src/parser/grammar/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::Deref;
use std::rc::Rc;

use codegen_language_definition::model::{
self, BuiltInLabel, FieldsErrorRecovery, Identifier, Item, Language,
self, FieldsErrorRecovery, Identifier, Item, Language, PredefinedLabel,
};
use indexmap::IndexMap;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -336,7 +336,7 @@ fn resolve_trivia(
match resolve_grammar_element(&reference, ctx) {
GrammarElement::ScannerDefinition(parser) => {
// Hack: This is a sequence of a single scanner in order to emit the names
ParserDefinitionNode::Sequence(vec![Labeled::with_builtin_label(
ParserDefinitionNode::Sequence(vec![Labeled::with_predefined_label(
kind.label(),
ParserDefinitionNode::ScannerDefinition(parser),
)])
Expand Down Expand Up @@ -456,14 +456,17 @@ fn resolve_choice(item: model::EnumItem, ctx: &mut ResolveCtx) -> ParserDefiniti
})
.collect();

ParserDefinitionNode::Choice(Labeled::with_builtin_label(BuiltInLabel::Variant, variants))
.versioned(item.enabled)
ParserDefinitionNode::Choice(Labeled::with_predefined_label(
PredefinedLabel::Variant,
variants,
))
.versioned(item.enabled)
}

fn resolve_repeated(item: model::RepeatedItem, ctx: &mut ResolveCtx) -> ParserDefinitionNode {
let reference = Box::new(resolve_grammar_element(&item.reference, ctx).into_parser_def_node());

let repeated = Labeled::with_builtin_label(BuiltInLabel::Item, reference);
let repeated = Labeled::with_predefined_label(PredefinedLabel::Item, reference);

if item.allow_empty.unwrap_or_default() {
ParserDefinitionNode::ZeroOrMore(repeated).versioned(item.enabled)
Expand All @@ -477,8 +480,8 @@ fn resolve_separated(item: model::SeparatedItem, ctx: &mut ResolveCtx) -> Parser
let separator = resolve_grammar_element(&item.separator, ctx).into_parser_def_node();

let separated = ParserDefinitionNode::SeparatedBy(
Labeled::with_builtin_label(BuiltInLabel::Item, Box::new(reference)),
Labeled::with_builtin_label(BuiltInLabel::Separator, Box::new(separator)),
Labeled::with_predefined_label(PredefinedLabel::Item, Box::new(reference)),
Labeled::with_predefined_label(PredefinedLabel::Separator, Box::new(separator)),
);

if item.allow_empty.unwrap_or_default() {
Expand Down Expand Up @@ -507,8 +510,8 @@ fn resolve_precedence(
"Precedence operator {item} has no primary expressions",
item = item.name
),
_ => ParserDefinitionNode::Choice(Labeled::with_builtin_label(
BuiltInLabel::Variant,
_ => ParserDefinitionNode::Choice(Labeled::with_predefined_label(
PredefinedLabel::Variant,
primaries,
)),
});
Expand Down Expand Up @@ -593,10 +596,10 @@ enum TriviaKind {
}

impl TriviaKind {
fn label(self) -> BuiltInLabel {
fn label(self) -> PredefinedLabel {
match self {
TriviaKind::Leading => BuiltInLabel::LeadingTrivia,
TriviaKind::Trailing => BuiltInLabel::TrailingTrivia,
TriviaKind::Leading => PredefinedLabel::LeadingTrivia,
TriviaKind::Trailing => PredefinedLabel::TrailingTrivia,
}
}
}
Expand Down Expand Up @@ -645,7 +648,7 @@ impl VersionWrapped for ParserDefinitionNode {
trait LabeledExt<T> {
fn anonymous(node: T) -> Self;
fn with_ident_name(name: Identifier, node: T) -> Self;
fn with_builtin_label(name: BuiltInLabel, node: T) -> Self;
fn with_predefined_label(name: PredefinedLabel, node: T) -> Self;
}

impl<T> LabeledExt<T> for Labeled<T> {
Expand All @@ -663,7 +666,7 @@ impl<T> LabeledExt<T> for Labeled<T> {
}
}

fn with_builtin_label(label: BuiltInLabel, value: T) -> Self {
fn with_predefined_label(label: PredefinedLabel, value: T) -> Self {
Self {
label: label.as_ref().to_owned(),
value,
Expand Down
1 change: 1 addition & 0 deletions crates/infra/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ clap_complete = { workspace = true }
infra_utils = { workspace = true }
itertools = { workspace = true }
markdown = { workspace = true }
rayon = { workspace = true }
semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
13 changes: 6 additions & 7 deletions crates/infra/cli/src/commands/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{Parser, ValueEnum};
use infra_utils::cargo::CargoWorkspaceCommands;
use infra_utils::commands::Command;
use infra_utils::terminal::Terminal;
use rayon::iter::{ParallelBridge, ParallelIterator};
use strum::IntoEnumIterator;

use crate::toolchains::mkdocs::Mkdocs;
Expand Down Expand Up @@ -40,7 +41,7 @@ impl OrderedCommand for CheckCommand {
match self {
CheckCommand::Cargo => check_cargo(),
CheckCommand::Rustdoc => check_rustdoc(),
CheckCommand::Npm => check_npm()?,
CheckCommand::Npm => check_npm(),
CheckCommand::Mkdocs => check_mkdocs(),
};

Expand Down Expand Up @@ -71,12 +72,10 @@ fn check_rustdoc() {
.run();
}

fn check_npm() -> Result<()> {
for package in WasmPackage::iter() {
package.build()?;
}

Ok(())
fn check_npm() {
WasmPackage::iter()
.par_bridge()
.for_each(|package| package.build().unwrap());
}

fn check_mkdocs() {
Expand Down
2 changes: 1 addition & 1 deletion crates/infra/cli/src/toolchains/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use infra_utils::commands::Command;
use infra_utils::paths::{FileWalker, PathExtensions};
use strum_macros::EnumIter;

pub const WASM_TARGET: &str = "wasm32-wasi";
pub const WASM_TARGET: &str = "wasm32-wasip1";

#[derive(Clone, Copy, EnumIter)]
pub enum WasmPackage {
Expand Down

0 comments on commit 249a5b1

Please sign in to comment.