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

Co-locate parser generator logic #972

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading