refactor: Reduce codegen burden for SyntaxNode and SyntaxToken

This commit is contained in:
Lukas Wirth 2025-03-21 10:54:38 +01:00
parent d6dc1bf05e
commit d31301cbe7
4 changed files with 2767 additions and 426 deletions

View file

@ -1008,7 +1008,7 @@ impl ::core::cmp::Eq for SyntaxKind {}
impl ::core::cmp::PartialOrd for SyntaxKind { impl ::core::cmp::PartialOrd for SyntaxKind {
#[inline] #[inline]
fn partial_cmp(&self, other: &Self) -> core::option::Option<core::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> core::option::Option<core::cmp::Ordering> {
(*self as u16).partial_cmp(&(*other as u16)) Some(self.cmp(other))
} }
} }
impl ::core::cmp::Ord for SyntaxKind { impl ::core::cmp::Ord for SyntaxKind {

File diff suppressed because it is too large Load diff

View file

@ -5,8 +5,7 @@ use crate::{
SyntaxKind::{self, *}, SyntaxKind::{self, *},
SyntaxToken, SyntaxToken,
}; };
use std::{fmt, hash};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Byte { pub struct Byte {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -26,8 +25,21 @@ impl AstToken for Byte {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for Byte {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Byte").field("syntax", &self.syntax).finish()
}
}
impl Clone for Byte {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for Byte {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for Byte {}
impl PartialEq for Byte {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct ByteString { pub struct ByteString {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -47,8 +59,21 @@ impl AstToken for ByteString {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for ByteString {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ByteString").field("syntax", &self.syntax).finish()
}
}
impl Clone for ByteString {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for ByteString {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for ByteString {}
impl PartialEq for ByteString {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct CString { pub struct CString {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -68,8 +93,21 @@ impl AstToken for CString {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for CString {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CString").field("syntax", &self.syntax).finish()
}
}
impl Clone for CString {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for CString {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for CString {}
impl PartialEq for CString {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct Char { pub struct Char {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -89,8 +127,21 @@ impl AstToken for Char {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for Char {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Char").field("syntax", &self.syntax).finish()
}
}
impl Clone for Char {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for Char {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for Char {}
impl PartialEq for Char {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct Comment { pub struct Comment {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -110,8 +161,21 @@ impl AstToken for Comment {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for Comment {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Comment").field("syntax", &self.syntax).finish()
}
}
impl Clone for Comment {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for Comment {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for Comment {}
impl PartialEq for Comment {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct FloatNumber { pub struct FloatNumber {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -131,8 +195,21 @@ impl AstToken for FloatNumber {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for FloatNumber {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FloatNumber").field("syntax", &self.syntax).finish()
}
}
impl Clone for FloatNumber {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for FloatNumber {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for FloatNumber {}
impl PartialEq for FloatNumber {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct Ident { pub struct Ident {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -152,8 +229,21 @@ impl AstToken for Ident {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for Ident {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ident").field("syntax", &self.syntax).finish()
}
}
impl Clone for Ident {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for Ident {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for Ident {}
impl PartialEq for Ident {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct IntNumber { pub struct IntNumber {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -173,8 +263,21 @@ impl AstToken for IntNumber {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for IntNumber {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IntNumber").field("syntax", &self.syntax).finish()
}
}
impl Clone for IntNumber {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for IntNumber {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for IntNumber {}
impl PartialEq for IntNumber {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct String { pub struct String {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -194,8 +297,21 @@ impl AstToken for String {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for String {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("String").field("syntax", &self.syntax).finish()
}
}
impl Clone for String {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for String {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for String {}
impl PartialEq for String {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
pub struct Whitespace { pub struct Whitespace {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -215,3 +331,18 @@ impl AstToken for Whitespace {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for Whitespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Whitespace").field("syntax", &self.syntax).finish()
}
}
impl Clone for Whitespace {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl hash::Hash for Whitespace {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for Whitespace {}
impl PartialEq for Whitespace {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}

View file

@ -67,7 +67,6 @@ fn generate_tokens(grammar: &AstSrc) -> String {
let name = format_ident!("{}", token); let name = format_ident!("{}", token);
let kind = format_ident!("{}", to_upper_snake_case(token)); let kind = format_ident!("{}", to_upper_snake_case(token));
quote! { quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name { pub struct #name {
pub(crate) syntax: SyntaxToken, pub(crate) syntax: SyntaxToken,
} }
@ -83,6 +82,29 @@ fn generate_tokens(grammar: &AstSrc) -> String {
} }
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
impl fmt::Debug for #name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(#token).field("syntax", &self.syntax).finish()
}
}
impl Clone for #name {
fn clone(&self) -> Self {
Self { syntax: self.syntax.clone() }
}
}
impl hash::Hash for #name {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.syntax.hash(state);
}
}
impl Eq for #name {}
impl PartialEq for #name {
fn eq(&self, other: &Self) -> bool {
self.syntax == other.syntax
}
}
} }
}); });
@ -90,7 +112,10 @@ fn generate_tokens(grammar: &AstSrc) -> String {
crate::flags::CodegenType::Grammar, crate::flags::CodegenType::Grammar,
reformat( reformat(
quote! { quote! {
use std::{fmt, hash};
use crate::{SyntaxKind::{self, *}, SyntaxToken, ast::AstToken}; use crate::{SyntaxKind::{self, *}, SyntaxToken, ast::AstToken};
#(#tokens)* #(#tokens)*
} }
.to_string(), .to_string(),
@ -104,6 +129,7 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
.nodes .nodes
.iter() .iter()
.map(|node| { .map(|node| {
let node_str_name = &node.name;
let name = format_ident!("{}", node.name); let name = format_ident!("{}", node.name);
let kind = format_ident!("{}", to_upper_snake_case(&node.name)); let kind = format_ident!("{}", to_upper_snake_case(&node.name));
let traits = node let traits = node
@ -149,7 +175,6 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
( (
quote! { quote! {
#[pretty_doc_comment_placeholder_workaround] #[pretty_doc_comment_placeholder_workaround]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name { pub struct #name {
pub(crate) syntax: SyntaxNode, pub(crate) syntax: SyntaxNode,
} }
@ -180,6 +205,31 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
#[inline] #[inline]
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl hash::Hash for #name {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.syntax.hash(state);
}
}
impl Eq for #name {}
impl PartialEq for #name {
fn eq(&self, other: &Self) -> bool {
self.syntax == other.syntax
}
}
impl Clone for #name {
fn clone(&self) -> Self {
Self { syntax: self.syntax.clone() }
}
}
impl fmt::Debug for #name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(#node_str_name).field("syntax", &self.syntax).finish()
}
}
}, },
) )
}) })
@ -265,6 +315,7 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
.sorted_by_key(|(name, _)| *name) .sorted_by_key(|(name, _)| *name)
.map(|(trait_name, nodes)| { .map(|(trait_name, nodes)| {
let name = format_ident!("Any{}", trait_name); let name = format_ident!("Any{}", trait_name);
let node_str_name = name.to_string();
let trait_name = format_ident!("{}", trait_name); let trait_name = format_ident!("{}", trait_name);
let kinds: Vec<_> = nodes let kinds: Vec<_> = nodes
.iter() .iter()
@ -274,13 +325,9 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
( (
quote! { quote! {
#[pretty_doc_comment_placeholder_workaround] #[pretty_doc_comment_placeholder_workaround]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name { pub struct #name {
pub(crate) syntax: SyntaxNode, pub(crate) syntax: SyntaxNode,
} }
impl ast::#trait_name for #name {}
},
quote! {
impl #name { impl #name {
#[inline] #[inline]
pub fn new<T: ast::#trait_name>(node: T) -> #name { pub fn new<T: ast::#trait_name>(node: T) -> #name {
@ -289,6 +336,9 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
} }
} }
} }
},
quote! {
impl ast::#trait_name for #name {}
impl AstNode for #name { impl AstNode for #name {
#[inline] #[inline]
fn can_cast(kind: SyntaxKind) -> bool { fn can_cast(kind: SyntaxKind) -> bool {
@ -304,6 +354,31 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
} }
} }
impl hash::Hash for #name {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.syntax.hash(state);
}
}
impl Eq for #name {}
impl PartialEq for #name {
fn eq(&self, other: &Self) -> bool {
self.syntax == other.syntax
}
}
impl Clone for #name {
fn clone(&self) -> Self {
Self { syntax: self.syntax.clone() }
}
}
impl fmt::Debug for #name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(#node_str_name).field("syntax", &self.syntax).finish()
}
}
#( #(
impl From<#nodes> for #name { impl From<#nodes> for #name {
#[inline] #[inline]
@ -346,6 +421,8 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
let ast = quote! { let ast = quote! {
#![allow(non_snake_case)] #![allow(non_snake_case)]
use std::{fmt, hash};
use crate::{ use crate::{
SyntaxNode, SyntaxToken, SyntaxKind::{self, *}, SyntaxNode, SyntaxToken, SyntaxKind::{self, *},
ast::{self, AstNode, AstChildren, support}, ast::{self, AstNode, AstChildren, support},
@ -613,7 +690,7 @@ fn generate_syntax_kinds(grammar: KindsSrc) -> String {
impl ::core::cmp::PartialOrd for SyntaxKind { impl ::core::cmp::PartialOrd for SyntaxKind {
#[inline] #[inline]
fn partial_cmp(&self, other: &Self) -> core::option::Option<core::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> core::option::Option<core::cmp::Ordering> {
(*self as u16).partial_cmp(&(*other as u16)) Some(self.cmp(other))
} }
} }
impl ::core::cmp::Ord for SyntaxKind { impl ::core::cmp::Ord for SyntaxKind {