Skip to content

Commit

Permalink
feat: Migrate the parser codegen to DSL v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Nov 10, 2023
1 parent 8d46847 commit 08a20b6
Show file tree
Hide file tree
Showing 357 changed files with 24,496 additions and 12,758 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ fn check_precedence(analysis: &mut Analysis, item: &PrecedenceItem, enablement:
let enablement = update_enablement(analysis, &enablement, &enabled);

for precedence_expression in precedence_expressions {
let PrecedenceExpression { name: _, operators } = &**precedence_expression;
let PrecedenceExpression {
name: _,
rule_name: _,
operators,
} = &**precedence_expression;

for operator in operators {
let PrecedenceOperator {
Expand Down
65 changes: 42 additions & 23 deletions crates/codegen/language/definition/src/model/types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
pub use self::wrapper::*;
pub use indexmap::{IndexMap, IndexSet};

/// We want to generate all Spanned types in a single module.
/// Unfortunately, module-level (inner) attribute macros are not supported yet:
/// This is why we put the attribute on a wrapper module containing all of them, then re-export its inner contents.
/// More information: https://github.com/rust-lang/rust/issues/54726
#[codegen_language_internal_macros::derive_internals]
mod wrapper {
use super::{IndexMap, IndexSet};

use crate::model::Identifier;
use indexmap::{IndexMap, IndexSet};
use semver::Version;
use serde::Serialize;
use std::{path::PathBuf, rc::Rc};
Expand Down Expand Up @@ -61,7 +63,23 @@ mod wrapper {
Fragment { item: FragmentItem },
}

#[derive(Debug, Eq, PartialEq, Serialize)]
impl Item {
pub fn name(&self) -> &Identifier {
match self {
Item::Struct { item } => &item.name,
Item::Enum { item } => &item.name,
Item::Repeated { item } => &item.name,
Item::Separated { item } => &item.name,
Item::Precedence { item } => &item.name,
Item::Trivia { item } => &item.name,
Item::Keyword { item } => &item.name,
Item::Token { item } => &item.name,
Item::Fragment { item } => &item.name,
}
}
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct StructItem {
pub name: Identifier,

Expand All @@ -71,7 +89,7 @@ mod wrapper {
pub fields: IndexMap<Identifier, Field>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct EnumItem {
pub name: Identifier,

Expand All @@ -80,7 +98,7 @@ mod wrapper {
pub variants: Vec<EnumVariant>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct EnumVariant {
pub name: Identifier,

Expand All @@ -89,7 +107,7 @@ mod wrapper {
pub reference: Identifier,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct RepeatedItem {
pub name: Identifier,
pub repeated: Identifier,
Expand All @@ -99,7 +117,7 @@ mod wrapper {
pub allow_empty: Option<bool>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct SeparatedItem {
pub name: Identifier,
pub separated: Identifier,
Expand All @@ -110,7 +128,7 @@ mod wrapper {
pub allow_empty: Option<bool>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PrecedenceItem {
pub name: Identifier,

Expand All @@ -120,14 +138,15 @@ mod wrapper {
pub primary_expressions: Vec<PrimaryExpression>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PrecedenceExpression {
pub name: Identifier,
pub rule_name: Identifier,

pub operators: Vec<PrecedenceOperator>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PrecedenceOperator {
pub model: OperatorModel,

Expand All @@ -137,34 +156,34 @@ mod wrapper {
pub fields: IndexMap<Identifier, Field>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
pub enum OperatorModel {
Prefix,
Postfix,
BinaryLeftAssociative,
BinaryRightAssociative,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PrimaryExpression {
pub expression: Identifier,

pub enabled: Option<VersionSpecifier>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct FieldsErrorRecovery {
pub terminator: Option<Identifier>,
pub delimiters: Option<FieldDelimiters>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct FieldDelimiters {
pub open: Identifier,
pub close: Identifier,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum Field {
Required {
kind: FieldKind,
Expand All @@ -176,7 +195,7 @@ mod wrapper {
},
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum FieldKind {
NonTerminal { item: Identifier },
Terminal { items: IndexSet<Identifier> },
Expand All @@ -201,45 +220,45 @@ mod wrapper {
pub scanner: Scanner,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct KeywordItem {
pub name: Identifier,
pub identifier: Identifier,

pub definitions: Vec<KeywordDefinition>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct KeywordDefinition {
pub enabled: Option<VersionSpecifier>,
pub reserved: Option<VersionSpecifier>,

pub value: KeywordValue,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum KeywordValue {
Sequence { values: Vec<KeywordValue> },
Optional { value: Box<KeywordValue> },
Choice { values: Vec<KeywordValue> },
Atom { atom: String },
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct TokenItem {
pub name: Identifier,

pub definitions: Vec<TokenDefinition>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct TokenDefinition {
pub enabled: Option<VersionSpecifier>,

pub scanner: Scanner,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct FragmentItem {
pub name: Identifier,

Expand All @@ -248,7 +267,7 @@ mod wrapper {
pub scanner: Scanner,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum Scanner {
Sequence {
scanners: Vec<Scanner>,
Expand Down Expand Up @@ -284,7 +303,7 @@ mod wrapper {
},
}

#[derive(Debug, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum VersionSpecifier {
Never,
From { from: Version },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ codegen_language_macros::compile!(Language(
Precedence(
name = Bar,
precedence_expressions = [
PrecedenceExpression(name = Expression1, operators = []),
PrecedenceExpression(name = Expression2, operators = []),
PrecedenceExpression(name = Expression1, operators = [])
PrecedenceExpression(name = Expression1, rule_name = X, operators = []),
PrecedenceExpression(name = Expression2, rule_name = X, operators = []),
PrecedenceExpression(name = Expression1, rule_name = X, operators = [])
],
primary_expressions = [PrimaryExpression(expression = Baz)]
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: An expression with the name 'Expression1' already exists.
--> src/fail/definitions/duplicate_expression_name/test.rs:19:53
|
19 | PrecedenceExpression(name = Expression1, operators = [])
19 | PrecedenceExpression(name = Expression1, rule_name = X, operators = [])
| ^^^^^^^^^^^
Loading

0 comments on commit 08a20b6

Please sign in to comment.