Skip to content

Commit

Permalink
Use model::KeywordValue instead of KeywordScannerDefinitionNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed May 22, 2024
1 parent e84435d commit 7c72891
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub struct KeywordItem {
#[derive_spanned_type(Clone, Debug, ParseInputTokens, WriteOutputTokens)]
pub struct KeywordDefinition {
pub enabled: Option<VersionSpecifier>,
/// When the keyword is reserved, i.e. can't be used in other position (e.g. as a name)
pub reserved: Option<VersionSpecifier>,

// Underlying keyword scanner (i.e. identifier scanner)
pub value: KeywordValue,
}

Expand Down
33 changes: 7 additions & 26 deletions crates/codegen/runtime/generator/src/parser/grammar/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use once_cell::sync::Lazy;

use crate::parser::grammar::{
DelimitedRecoveryTokenThreshold, Grammar, GrammarElement, KeywordScannerDefinition,
KeywordScannerDefinitionNode, KeywordScannerDefinitionVersionedNode, Labeled, ParserDefinition,
ParserDefinitionNode, PrecedenceParserDefinition, PrecedenceParserDefinitionNode,
ScannerDefinition, ScannerDefinitionNode, TriviaParserDefinition, VersionQuality,
VersionQualityRange,
KeywordScannerDefinitionVersionedNode, Labeled, ParserDefinition, ParserDefinitionNode,
PrecedenceParserDefinition, PrecedenceParserDefinitionNode, ScannerDefinition,
ScannerDefinitionNode, TriviaParserDefinition, VersionQuality, VersionQualityRange,
};

impl Grammar {
Expand Down Expand Up @@ -389,13 +388,10 @@ fn resolve_grammar_element(ident: &Identifier, ctx: &mut ResolveCtx<'_>) -> Gram
.definitions
.iter()
.cloned()
.map(|def| {
let value = resolve_keyword_value(def.value);
KeywordScannerDefinitionVersionedNode {
value,
enabled: enabled_to_range(def.enabled),
reserved: enabled_to_range(def.reserved),
}
.map(|def| KeywordScannerDefinitionVersionedNode {
value: def.value,
enabled: enabled_to_range(def.enabled),
reserved: enabled_to_range(def.reserved),
})
.collect();

Expand Down Expand Up @@ -487,21 +483,6 @@ fn resolve_token(token: model::TokenItem, ctx: &mut ResolveCtx<'_>) -> ScannerDe
}
}

fn resolve_keyword_value(value: model::KeywordValue) -> KeywordScannerDefinitionNode {
match value {
model::KeywordValue::Sequence { values } => KeywordScannerDefinitionNode::Sequence(
values.into_iter().map(resolve_keyword_value).collect(),
),
model::KeywordValue::Choice { values } => KeywordScannerDefinitionNode::Choice(
values.into_iter().map(resolve_keyword_value).collect(),
),
model::KeywordValue::Optional { value } => {
KeywordScannerDefinitionNode::Optional(Box::new(resolve_keyword_value(*value)))
}
model::KeywordValue::Atom { atom } => KeywordScannerDefinitionNode::Atom(atom),
}
}

fn resolve_trivia(
parser: model::TriviaParser,
kind: TriviaKind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::rc::Rc;

use codegen_language_definition::model::Identifier;
use codegen_language_definition::model::{self, Identifier};

use crate::parser::grammar::{GrammarVisitor, VersionQualityRange, Visitable};

Expand Down Expand Up @@ -85,34 +85,25 @@ impl Visitable for KeywordScannerDefinitionRef {
#[derive(Debug)]
pub struct KeywordScannerDefinitionVersionedNode {
// Underlying keyword scanner (i.e. identifier scanner)
pub value: KeywordScannerDefinitionNode,
pub value: model::KeywordValue,
/// When the keyword scanner is enabled
pub enabled: Vec<VersionQualityRange>,
/// When the keyword is reserved, i.e. can't be used in other position (e.g. as a name)
pub reserved: Vec<VersionQualityRange>,
}

#[derive(Clone, Debug)]
pub enum KeywordScannerDefinitionNode {
Optional(Box<Self>),
Sequence(Vec<Self>),
Choice(Vec<Self>),
Atom(String),
// No repeatable combinators, because keywords are assumed to be finite
}

impl From<KeywordScannerDefinitionNode> for ScannerDefinitionNode {
fn from(val: KeywordScannerDefinitionNode) -> Self {
impl From<model::KeywordValue> for ScannerDefinitionNode {
fn from(val: model::KeywordValue) -> Self {
match val {
KeywordScannerDefinitionNode::Optional(node) => {
ScannerDefinitionNode::Optional(Box::new((*node).into()))
model::KeywordValue::Optional { value } => {
ScannerDefinitionNode::Optional(Box::new((*value).into()))
}
KeywordScannerDefinitionNode::Sequence(nodes) => {
ScannerDefinitionNode::Sequence(nodes.into_iter().map(Into::into).collect())
model::KeywordValue::Sequence { values } => {
ScannerDefinitionNode::Sequence(values.into_iter().map(Into::into).collect())
}
KeywordScannerDefinitionNode::Atom(string) => ScannerDefinitionNode::Literal(string),
KeywordScannerDefinitionNode::Choice(nodes) => {
ScannerDefinitionNode::Choice(nodes.into_iter().map(Into::into).collect())
model::KeywordValue::Atom { atom } => ScannerDefinitionNode::Literal(atom),
model::KeywordValue::Choice { values } => {
ScannerDefinitionNode::Choice(values.into_iter().map(Into::into).collect())
}
}
}
Expand All @@ -131,7 +122,7 @@ impl KeywordScannerAtomic {
pub fn try_from_def(def: &KeywordScannerDefinitionRef) -> Option<Self> {
match def.definitions() {
[KeywordScannerDefinitionVersionedNode {
value: KeywordScannerDefinitionNode::Atom(_),
value: model::KeywordValue::Atom { .. },
..
}] => Some(Self(Rc::clone(def))),
_ => None,
Expand All @@ -155,7 +146,7 @@ impl KeywordScannerAtomic {
pub fn value(&self) -> &str {
match self.definition() {
KeywordScannerDefinitionVersionedNode {
value: KeywordScannerDefinitionNode::Atom(atom),
value: model::KeywordValue::Atom { atom },
..
} => atom,
_ => unreachable!("KeywordScannerAtomic should have a single atom value"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use codegen_language_definition::model;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::parser::grammar::{
KeywordScannerDefinitionNode, KeywordScannerDefinitionRef, ScannerDefinitionNode,
};
use crate::parser::grammar::{KeywordScannerDefinitionRef, ScannerDefinitionNode};
use crate::parser::parser_definition::VersionQualityRangeVecExtensions;
use crate::parser::scanner_definition::ScannerDefinitionNodeExtensions;

Expand Down Expand Up @@ -80,7 +79,7 @@ impl KeywordScannerDefinitionExtensions for KeywordScannerDefinitionRef {
}
}

impl KeywordScannerDefinitionExtensions for KeywordScannerDefinitionNode {
impl KeywordScannerDefinitionExtensions for model::KeywordValue {
fn to_scanner_code(&self) -> TokenStream {
// This is a subset; let's reuse that
ScannerDefinitionNode::from(self.clone()).to_scanner_code()
Expand Down

0 comments on commit 7c72891

Please sign in to comment.