Skip to content

Commit f20c3f9

Browse files
committed
refactor(syntax): Group SyntaxKind and unify token/terminal kinds under LexemeKind.
SyntaxKind's flat token/terminal/missing variants are grouped into nested enums. A terminal node and the token that backs it share one lexical identity, so both reuse a single `LexemeKind` instead of duplicated `TokenKind`/`TerminalKind` lists; trivia tokens (whitespace, newlines, comments, skipped) move to `TriviaKind`, and enum missing-variants to `MissingKind`: enum SyntaxKind { Token(LexemeKind), TriviaToken(TriviaKind), Terminal(LexemeKind), Missing(MissingKind), // ...other node kinds stay flat } The parser/lexer layer now operates on `LexemeKind` directly (LexerTerminal, peek().kind, Terminal::KIND, MissingToken, operator-precedence and skip predicates), reserving SyntaxKind for green-tree node kinds. Debug/Display preserve the historical flat names (TerminalIdentifier, TokenWhitespace, ExprMissing) so diagnostics and golden tests are unaffected by the grouping; the lone visible change is the friendlier "Missing token Identifier." (was "TerminalIdentifier"), since kind_to_string now renders a LexemeKind. colored_printer::set_color matches exhaustively over LexemeKind/TriviaKind, removing its panicking catch-all. All kind enums are generated from the spec via a single classifier, so the duplication can't drift.
1 parent 0077583 commit f20c3f9

23 files changed

Lines changed: 4159 additions & 3530 deletions

File tree

crates/cairo-lang-doc/src/db.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cairo_lang_filesystem::db::FilesGroup;
66
use cairo_lang_filesystem::ids::{CrateId, FileId, Tracked};
77
use cairo_lang_filesystem::span::{TextOffset, TextSpan, TextWidth};
88
use cairo_lang_syntax::node::SyntaxNode;
9-
use cairo_lang_syntax::node::kind::SyntaxKind;
9+
use cairo_lang_syntax::node::kind::{SyntaxKind, TriviaKind};
1010
use itertools::{Itertools, intersperse};
1111
use salsa::Database;
1212

@@ -256,7 +256,9 @@ fn get_embedded_markdown_links<'db>(
256256
node: SyntaxNode<'db>,
257257
) -> Vec<MarkdownLink> {
258258
match node.kind(db) {
259-
SyntaxKind::TokenSingleLineDocComment | SyntaxKind::TokenSingleLineInnerComment => {
259+
SyntaxKind::TriviaToken(
260+
TriviaKind::SingleLineDocComment | TriviaKind::SingleLineInnerComment,
261+
) => {
260262
let content = node.get_text(db);
261263
let base_span = node.span(db);
262264
parse_embedded_markdown_links(content, base_span.start)

crates/cairo-lang-doc/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use cairo_lang_semantic::items::us::UseSemantic;
1212
use cairo_lang_semantic::items::visibility::Visibility;
1313
use cairo_lang_semantic::{ConcreteTypeId, GenericParam, TypeId, TypeLongId};
1414
use cairo_lang_syntax::attribute::structured::Attribute;
15-
use cairo_lang_syntax::node::kind::SyntaxKind;
15+
use cairo_lang_syntax::node::kind::{LexemeKind, SyntaxKind};
1616
use cairo_lang_syntax::node::{SyntaxNode, TypedStablePtr, TypedSyntaxNode, green};
1717
use itertools::Itertools;
1818
use salsa::Database;
@@ -106,14 +106,14 @@ pub fn get_syntactic_evaluation<'db>(
106106
for child in syntax_node.get_children(db).iter() {
107107
let kind = child.kind(db);
108108
if !matches!(kind, SyntaxKind::Trivia) {
109-
if matches!(kind, SyntaxKind::TerminalSemicolon) {
109+
if matches!(kind, SyntaxKind::Terminal(LexemeKind::Semicolon)) {
110110
buff.push(';');
111111
return Ok(buff);
112112
}
113113
if is_after_evaluation_value {
114114
buff.push_str(&SyntaxNode::get_text_without_all_comment_trivia(child, db));
115115
};
116-
if matches!(kind, SyntaxKind::TerminalEq) {
116+
if matches!(kind, SyntaxKind::Terminal(LexemeKind::Eq)) {
117117
is_after_evaluation_value = true;
118118
}
119119
}

crates/cairo-lang-doc/src/tests/test_documentation_comment_parser_links.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use cairo_lang_defs::db::DefsGroup;
22
use cairo_lang_defs::ids::ModuleId;
33
use cairo_lang_parser::db::ParserGroup;
44
use cairo_lang_syntax::node::SyntaxNode;
5-
use cairo_lang_syntax::node::kind::SyntaxKind;
5+
use cairo_lang_syntax::node::kind::{SyntaxKind, TriviaKind};
66
use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
77
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
88
use salsa::Database;
@@ -24,7 +24,7 @@ fn collect_doc_comments<'db>(
2424
node: SyntaxNode<'db>,
2525
comments: &mut Vec<SyntaxNode<'db>>,
2626
) {
27-
if node.kind(db) == SyntaxKind::TokenSingleLineDocComment {
27+
if node.kind(db) == SyntaxKind::TriviaToken(TriviaKind::SingleLineDocComment) {
2828
comments.push(node);
2929
}
3030
for &child in node.get_children(db) {

crates/cairo-lang-formatter/src/formatter_impl.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1616
use cairo_lang_utils::smol_str::SmolStr;
1717
use itertools::{Itertools, chain};
1818
use salsa::Database;
19-
use syntax::node::kind::SyntaxKind;
19+
use syntax::node::kind::{LexemeKind, SyntaxKind, TriviaKind};
2020

2121
use crate::FormatterConfig;
2222

@@ -1145,8 +1145,9 @@ impl<'a> FormatterImpl<'a> {
11451145
return;
11461146
}
11471147
// Split list into `use` path parts and TokenComma.
1148-
let (mut sorted_elements, commas): (Vec<_>, Vec<_>) =
1149-
children.drain(..).partition(|node| node.kind(self.db) != SyntaxKind::TerminalComma);
1148+
let (mut sorted_elements, commas): (Vec<_>, Vec<_>) = children
1149+
.drain(..)
1150+
.partition(|node| node.kind(self.db) != SyntaxKind::Terminal(LexemeKind::Comma));
11501151

11511152
// Sort the filtered nodes by comparing their `UsePath`.
11521153
sorted_elements.sort_by(|a_node, b_node| {
@@ -1262,7 +1263,7 @@ impl<'a> FormatterImpl<'a> {
12621263
self.line_state.line_buffer.push_space();
12631264
}
12641265
self.line_state.prevent_next_space = syntax_node.force_no_space_after(self.db);
1265-
if syntax_node.kind(self.db) != SyntaxKind::TokenWhitespace {
1266+
if syntax_node.kind(self.db) != SyntaxKind::TriviaToken(TriviaKind::Whitespace) {
12661267
self.is_current_line_whitespaces = false;
12671268
}
12681269
let node_break_points = syntax_node.get_wrapping_break_line_point_properties(self.db);

0 commit comments

Comments
 (0)