Skip to content

Commit

Permalink
Co-locate parser generator logic (#972)
Browse files Browse the repository at this point in the history
Part of #638, removes the `codegen_grammar` crate

Instead of having the definitions and the parser generation separate, we
now co-locate this and provide a parser-specific model (for MVC
rendering introduced in #962) for the code generation. This mostly moves
code around and documents a bit more what we already have.

This is fairly non-controversial and I am for #638 to be incremental, so
this is meant to progress the issue rather than fix it in one giant PR
to keep up the velocity and land it as soon as possible, accounting for
my upcoming move to Hardhat soon. If this generally makes sense, I'd
love to have a rubber stamp for it; for now, I'll work on top of this to
further migrate off DSLv1 definitions.
  • Loading branch information
Xanewok authored May 22, 2024
1 parent be943b7 commit d1b348e
Show file tree
Hide file tree
Showing 22 changed files with 431 additions and 438 deletions.
12 changes: 1 addition & 11 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish = false
resolver = "2"
members = [
"crates/codegen/ebnf",
"crates/codegen/grammar",
"crates/codegen/language/definition",
"crates/codegen/language/internal_macros",
"crates/codegen/language/macros",
Expand Down Expand Up @@ -47,7 +46,6 @@ members = [
# Internal
#
codegen_ebnf = { path = "crates/codegen/ebnf" }
codegen_grammar = { path = "crates/codegen/grammar" }
codegen_language_definition = { path = "crates/codegen/language/definition" }
codegen_language_internal_macros = { path = "crates/codegen/language/internal_macros" }
codegen_language_macros = { path = "crates/codegen/language/macros" }
Expand Down
16 changes: 0 additions & 16 deletions crates/codegen/grammar/Cargo.toml

This file was deleted.

15 changes: 0 additions & 15 deletions crates/codegen/grammar/src/lib.rs

This file was deleted.

12 changes: 6 additions & 6 deletions crates/codegen/runtime/cargo/src/runtime/kinds.rs.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum NonTerminalKind {
Stub2,
Stub3,
{%- else -%}
{%- for variant in model.nonterminal_kinds -%}
{%- for variant in model.parser.nonterminal_kinds -%}
{# variant.documentation | indent(prefix = "/// ", first = true, blank = true) #}
{{ variant }},
{%- endfor -%}
Expand Down Expand Up @@ -63,7 +63,7 @@ pub enum EdgeLabel {
Stub2,
Stub3,
{%- else -%}
{% for variant in model.labels -%}
{% for variant in model.parser.labels -%}
{{ variant | pascal_case }},
{%- endfor -%}
{%- endif -%}
Expand Down Expand Up @@ -95,7 +95,7 @@ pub enum TerminalKind {
Stub2,
Stub3,
{%- else -%}
{%- for variant in model.terminal_kinds -%}
{%- for variant in model.parser.terminal_kinds -%}
{# variant.documentation | indent(prefix = "/// ", first = true, blank = true) #}
{{ variant }},
{%- endfor -%}
Expand All @@ -109,7 +109,7 @@ impl metaslang_cst::TerminalKind for TerminalKind {
{%- else -%}
matches!(
self,
{%- for variant in model.trivia_scanner_names -%}
{%- for variant in model.parser.trivia_scanner_names -%}
| Self::{{ variant }}
{%- endfor -%}
)
Expand All @@ -125,7 +125,7 @@ pub(crate) enum LexicalContext {
Stub2,
Stub3,
{%- else -%}
{%- for context_name, _ in model.scanner_contexts %}
{%- for context_name, _ in model.parser.scanner_contexts %}
{{ context_name }},
{%- endfor %}
{%- endif -%}
Expand All @@ -140,7 +140,7 @@ pub(crate) trait IsLexicalContext {
#[allow(non_snake_case)]
pub(crate) mod LexicalContextType {
{%- if not rendering_in_stubs -%}
{%- for context_name, _ in model.scanner_contexts %}
{%- for context_name, _ in model.parser.scanner_contexts %}
pub struct {{ context_name }};

impl super::IsLexicalContext for {{ context_name }} {
Expand Down
18 changes: 9 additions & 9 deletions crates/codegen/runtime/cargo/src/runtime/language.rs.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::parser_support::{
#[cfg_attr(feature = "slang_napi_interfaces", napi(namespace = "language"))]
pub struct Language {
{%- if not rendering_in_stubs -%}
{%- for version in model.referenced_versions -%}
{%- for version in model.parser.referenced_versions -%}
pub(crate) version_is_at_least_{{ version | replace(from=".", to="_") }}: bool,
{%- endfor -%}
{%- endif -%}
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Language {
if Self::SUPPORTED_VERSIONS.binary_search(&version).is_ok() {
Ok(Self {
{%- if not rendering_in_stubs -%}
{%- for version in model.referenced_versions %}
{%- for version in model.parser.referenced_versions %}
version_is_at_least_{{ version | replace(from=".", to="_") }}: Version::new({{ version | split(pat=".") | join(sep=", ") }}) <= version,
{%- endfor -%}
{%- endif -%}
Expand All @@ -89,12 +89,12 @@ impl Language {
* Parser Functions
********************************************/

{% for parser_name, parser_code in model.parser_functions %}
{% for parser_name, parser_code in model.parser.parser_functions %}
#[allow(unused_assignments, unused_parens)]
fn {{ parser_name | snake_case }}(&self, input: &mut ParserContext<'_>) -> ParserResult { {{ parser_code }} }
{% endfor %}

{% for parser_name, parser_code in model.trivia_parser_functions %}
{% for parser_name, parser_code in model.parser.trivia_parser_functions %}
#[allow(unused_assignments, unused_parens)]
fn {{ parser_name | snake_case }}(&self, input: &mut ParserContext<'_>) -> ParserResult { {{ parser_code }} }
{% endfor %}
Expand All @@ -103,12 +103,12 @@ impl Language {
* Scanner Functions
********************************************/

{% for scanner_name, scanner_code in model.scanner_functions %}
{% for scanner_name, scanner_code in model.parser.scanner_functions %}
#[allow(unused_assignments, unused_parens)]
fn {{ scanner_name | snake_case }}(&self, input: &mut ParserContext<'_>) -> bool { {{ scanner_code }} }
{% endfor %}

{%- for keyword_name, keyword_code in model.keyword_compound_scanners %}
{%- for keyword_name, keyword_code in model.parser.keyword_compound_scanners %}
#[inline]
fn {{ keyword_name | snake_case }}(&self, input: &mut ParserContext<'_>, ident: &str) -> KeywordScan { {{ keyword_code }} }
{% endfor %}
Expand All @@ -120,7 +120,7 @@ impl Language {
unreachable!("Attempting to parse in stubs: {kind}: {input}")
{%- else -%}
match kind {
{%- for parser_name, _ in model.parser_functions -%}
{%- for parser_name, _ in model.parser.parser_functions -%}
NonTerminalKind::{{ parser_name }} => Self::{{ parser_name | snake_case }}.parse(self, input),
{%- endfor -%}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Lexer for Language {
unreachable!("Invoking delimiters in stubs.")
{%- else -%}
match LexCtx::value() {
{%- for context_name, context in model.scanner_contexts %}
{%- for context_name, context in model.parser.scanner_contexts %}
LexicalContext::{{ context_name }} => &[
{%- for open, close in context.delimiters %}
(TerminalKind::{{ open }}, TerminalKind::{{ close }}),
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Lexer for Language {
}

match LexCtx::value() {
{%- for context_name, context in model.scanner_contexts %}
{%- for context_name, context in model.parser.scanner_contexts %}
LexicalContext::{{ context_name }} => {
if let Some(kind) = {{ context.literal_scanner }} {
furthest_position = input.position();
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/runtime/generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ publish = false

[dependencies]
anyhow = { workspace = true }
codegen_grammar = { workspace = true }
codegen_language_definition = { workspace = true }
indexmap = { workspace = true }
Inflector = { workspace = true }
Expand All @@ -17,6 +16,7 @@ proc-macro2 = { workspace = true }
quote = { workspace = true }
semver = { workspace = true }
serde = { workspace = true }
strum_macros = { workspace = true }

[lints]
workspace = true
8 changes: 1 addition & 7 deletions crates/codegen/runtime/generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(clippy::too_many_lines)]

use std::path::{Path, PathBuf};
use std::rc::Rc;

Expand All @@ -12,12 +10,8 @@ use serde::Serialize;
use crate::model::RuntimeModel;

mod ast;
mod keyword_scanner_definition;
mod model;
mod parser_definition;
mod precedence_parser_definition;
mod scanner_definition;
mod trie;
mod parser;

pub enum OutputLanguage {
Cargo,
Expand Down
Loading

0 comments on commit d1b348e

Please sign in to comment.