Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EBNF grammar as doc comments for TerminalKind and NonterminalKind #1169

Merged
merged 33 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9de66b7
Add a new step to the WASM build/check step, which is to generate the…
mjoerussell Nov 22, 2024
9310ba8
Generated file changes
mjoerussell Nov 22, 2024
3434960
Regenerating .d.ts files using new .wit doc comments - documentation …
mjoerussell Nov 22, 2024
746c0c9
Merge `transpile_wasm` and `generate_types` into one function. The pu…
mjoerussell Nov 25, 2024
096ad6d
Update jco submodule to latest update
mjoerussell Nov 25, 2024
4f3ca90
Revert jco submodule to a951a94
mjoerussell Nov 26, 2024
e3d69db
Bump wasm-tools dependency version back to 1.216.0
mjoerussell Nov 26, 2024
b3967db
Formatting: blank lines
mjoerussell Nov 26, 2024
1dd5ece
Update comment on --no-typescript option
mjoerussell Nov 26, 2024
d5c365c
Merge with latest changes from main
mjoerussell Nov 26, 2024
15fb954
Updating `KindsModel` so that terminal and nonterminal kinds include …
mjoerussell Nov 27, 2024
e955ece
Fix lifetime hack on SpecModel. Previously I changed SpecModel's `lan…
mjoerussell Nov 27, 2024
f07d1ae
Update jco
mjoerussell Nov 27, 2024
744ebfb
Add a short into to the beginning of the EBNF documentation for each …
mjoerussell Nov 27, 2024
12615dc
Update generated files
mjoerussell Nov 27, 2024
09250da
Merge branch 'main' into feature/wit-ebnf-doc
mjoerussell Nov 27, 2024
1954175
Merge branch 'main' into feature/wit-ebnf-doc
mjoerussell Dec 2, 2024
824f9a9
Merge branch 'main' into feature/wit-ts-doc-comments
mjoerussell Dec 2, 2024
7faf212
Revert Cargo.lock
mjoerussell Dec 2, 2024
f2552a9
Merge branch 'feature/wit-ts-doc-comments' into feature/wit-ebnf-doc
mjoerussell Dec 2, 2024
c9741d7
In `nonterminal_kind.rs` and `terminal_kind.rs`, add allow directives…
mjoerussell Dec 2, 2024
80f4fee
Use `EbnfBuilder` instead of `SpecBuilder` in `kinds/mod.rs`. Remove …
mjoerussell Dec 3, 2024
e5abb21
Rename KindBuilder to EbnfBuilder
mjoerussell Dec 3, 2024
3073a05
P
mjoerussell Dec 3, 2024
b70b9e1
Merge branch 'main' into feature/wit-ebnf-doc
mjoerussell Dec 4, 2024
c6865c3
Regenerated wit outputs with code blocks in doc comments
mjoerussell Dec 4, 2024
ec33b54
Add code block formatting to terminal/nonterminal_kind.rs
mjoerussell Dec 6, 2024
db69514
Remove lingering newline
mjoerussell Dec 6, 2024
0b8f697
Merge branch 'main' into feature/wit-ebnf-doc
mjoerussell Dec 10, 2024
fc44c1c
Change jco revision to correct commit
mjoerussell Dec 11, 2024
fcadf08
Move clippy allows to be struct-level instead of file-level
mjoerussell Dec 11, 2024
261ca80
Merge branch 'main' into feature/wit-ebnf-doc
mjoerussell Dec 12, 2024
ce50167
Regenerate files
mjoerussell Dec 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{# This needs to stay in sync with the wit-bindgen output #}
#[allow(clippy::doc_markdown)]
#[allow(clippy::doc_link_with_quotes)]

{% if model.kinds.nonterminal_kinds|length <= 256 %} #[repr(u8)] {% else %} #[repr(u16)] {% endif %}
#[derive(
Debug,
Expand All @@ -21,8 +24,10 @@ pub enum NonterminalKind {
Stub3,
{%- else -%}
{%- for variant in model.kinds.nonterminal_kinds -%}
{# variant.documentation | indent(prefix = "/// ", first = true, blank = true) #}
{{ variant }},
/// ```ebnf
{{ variant.documentation | indent(prefix = "/// ", first = true, blank = true) }}
/// ```
{{ variant.id }},
{%- endfor -%}
{%- endif -%}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
)]
#[derive(Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
#[allow(clippy::doc_markdown)]
#[allow(clippy::doc_link_with_quotes)]
pub enum TerminalKind {
// Built-in:
UNRECOGNIZED,
Expand All @@ -27,8 +29,10 @@ pub enum TerminalKind {
Stub3,
{%- else -%}
{%- for variant in model.kinds.terminal_kinds -%}
{# variant.documentation | indent(prefix = "/// ", first = true, blank = true) #}
{{ variant }},
/// ```ebnf
{{ variant.documentation | indent(prefix = "/// ", first = true, blank = true) }}
/// ```
{{ variant.id }},
{%- endfor -%}
{%- endif -%}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ interface cst {
stub3,
{%- else %}
{%- for variant in model.kinds.nonterminal_kinds %}
{{ variant | wit_case }},
/// This kind represents a `{{ variant.id }}` node, with the following structure:
///
/// ```ebnf
{%- for line in variant.documentation | split(pat="\n") %}
/// {{ line }}
{%- endfor %}
OmarTawfik marked this conversation as resolved.
Show resolved Hide resolved
/// ```
{{ variant.id | wit_case }},
{%- endfor %}
{%- endif %}
}
Expand All @@ -35,7 +42,14 @@ interface cst {
stub3,
{%- else %}
{%- for variant in model.kinds.terminal_kinds %}
{{ variant | wit_case }},
/// This kind represents a `{{ variant.id }}` node, with the following structure:
///
/// ```ebnf
{%- for line in variant.documentation | split(pat="\n") %}
/// {{ line }}
{%- endfor %}
/// ```
{{ variant.id | wit_case }},
{%- endfor %}
{%- endif %}
}
Expand Down
1 change: 1 addition & 0 deletions crates/codegen/runtime/generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false

[dependencies]
anyhow = { workspace = true }
codegen_ebnf = { workspace = true }
codegen_language_definition = { workspace = true }
indexmap = { workspace = true }
Inflector = { workspace = true }
Expand Down
58 changes: 48 additions & 10 deletions crates/codegen/runtime/generator/src/kinds/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
use std::collections::BTreeSet;
use std::rc::Rc;

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

#[derive(Serialize, Ord, Eq, PartialEq, PartialOrd)]
pub struct Kind {
id: Identifier,
documentation: String,
}

struct EbnfBuilder {
model: EbnfModel,
writer: PlainWriter,
}

impl EbnfBuilder {
fn new(language: &Rc<model::Language>) -> EbnfBuilder {
EbnfBuilder {
model: EbnfModel::build(language),
writer: PlainWriter::default(),
}
}

fn build(&mut self, name: &Identifier) -> Kind {
if self.model.serialize(name, &mut self.writer).is_ok() {
Kind {
id: name.clone(),
documentation: self.writer.flush(),
}
} else {
Kind {
id: name.clone(),
documentation: String::new(),
}
}
}
}

#[derive(Serialize)]
pub struct KindsModel {
/// Defines the `NonterminalKind` enum variants.
nonterminal_kinds: BTreeSet<Identifier>,
nonterminal_kinds: BTreeSet<Kind>,
/// Defines the `TerminalKind` enum variants.
terminal_kinds: BTreeSet<Identifier>,
terminal_kinds: BTreeSet<Kind>,
/// Defines `TerminalKind::is_trivia` method.
trivia_scanner_names: BTreeSet<Identifier>,
/// Defines `EdgeLabel` enum variants.
Expand Down Expand Up @@ -37,32 +73,34 @@ impl Default for KindsModel {
}

impl KindsModel {
pub fn from_language(language: &model::Language) -> Self {
pub fn from_language(language: &Rc<model::Language>) -> Self {
let mut kind_builder = EbnfBuilder::new(language);

let terminal_kinds = language
.items()
.filter(|item| item.is_terminal() && !matches!(item, Item::Fragment { .. }))
.map(|item| item.name().clone())
.map(|item| kind_builder.build(item.name()))
.collect();

let mut nonterminal_kinds = BTreeSet::default();
for item in language.items() {
match item {
Item::Struct { item } => {
nonterminal_kinds.insert(item.name.clone());
nonterminal_kinds.insert(kind_builder.build(&item.name));
}
Item::Enum { item } => {
nonterminal_kinds.insert(item.name.clone());
nonterminal_kinds.insert(kind_builder.build(&item.name));
}
Item::Repeated { item } => {
nonterminal_kinds.insert(item.name.clone());
nonterminal_kinds.insert(kind_builder.build(&item.name));
}
Item::Separated { item } => {
nonterminal_kinds.insert(item.name.clone());
nonterminal_kinds.insert(kind_builder.build(&item.name));
}
Item::Precedence { item } => {
nonterminal_kinds.insert(item.name.clone());
nonterminal_kinds.insert(kind_builder.build(&item.name));
for op in &item.precedence_expressions {
nonterminal_kinds.insert(op.name.clone());
nonterminal_kinds.insert(kind_builder.build(&op.name));
}
}
// Terminals
Expand Down
Loading
Loading