Skip to content

Commit

Permalink
Introduce private keyword in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Sep 19, 2023
1 parent f7e079a commit 3b2e5e5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rust/parser/debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ where T: serde::Serialize + Reflect {
TextEnd::reflect(),
TextStart::reflect(),
Wildcard::reflect(),
Private::reflect(),
];
skip_tokens.into_iter().for_each(|token| to_s_expr.skip(rust_to_meta[&token.id]));
let ident_token = rust_to_meta[&Ident::reflect().id];
Expand Down
29 changes: 29 additions & 0 deletions lib/rust/parser/debug/tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,35 @@ fn pattern_match_auto_scope() {
test(&code.join("\n"), expected);
}

// === Private (project-private) keyword ===
#[test]
fn private_keyword() {
test("private", block![(Private)]);
}

#[test]
#[ignore]
fn private_is_first_statement() {
// Comments and empty lines are allowed before `private`.
#[rustfmt::skip]
let lines = vec![
"# Some comment",
"# Other comment",
"",
"private"
];
test(&lines.join("\n"), block![
() () () (Private)
]);

#[rustfmt::skip]
let lines = vec![
"type T",
"",
"private"
];
expect_invalid_node(&lines.join("\n"));
}

// === Array/tuple literals ===

Expand Down
19 changes: 19 additions & 0 deletions lib/rust/parser/src/macros/built_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ fn foreign<'s>() -> Definition<'s> {
crate::macro_definition! {("foreign", everything()) foreign_body}
}

fn private<'s>() -> Definition<'s> {
println!("Registering private macro def");
crate::macro_definition! {("private", everything()) private_keyword}
}

fn skip<'s>() -> Definition<'s> {
crate::macro_definition! {("SKIP", everything()) capture_expressions}
}
Expand All @@ -646,6 +651,15 @@ fn freeze<'s>() -> Definition<'s> {
crate::macro_definition! {("FREEZE", everything()) capture_expressions}
}

fn private_keyword(segments: NonEmptyVec<MatchedSegment>) -> syntax::Tree {
println!("private_keyword[begin] : Segments={:?}", segments);
let segment = segments.pop().0;
let keyword = into_private(segment.header);
let ret_tree = syntax::Tree::private(keyword);
println!("private_keyword[end]: ret_tree={:?}", ret_tree);
ret_tree
}

/// Macro body builder that just parses the tokens of each segment as expressions, and places them
/// in a [`MultiSegmentApp`].
fn capture_expressions(segments: NonEmptyVec<MatchedSegment>) -> syntax::Tree {
Expand Down Expand Up @@ -743,6 +757,11 @@ fn into_ident(token: syntax::token::Token) -> syntax::token::Ident {
syntax::token::ident(left_offset, code, false, 0, false, false, false)
}

fn into_private(token: syntax::token::Token) -> syntax::token::Private {
let syntax::token::Token { left_offset, code, .. } = token;
syntax::token::private(left_offset, code)
}


// === Validators ===

Expand Down
1 change: 1 addition & 0 deletions lib/rust/parser/src/syntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ macro_rules! with_token_definition { ($f:ident ($($args:tt)*)) => { $f! { $($arg
pub base: Option<Base>
},
NumberBase,
Private,
TextStart,
TextEnd,
TextSection,
Expand Down
5 changes: 5 additions & 0 deletions lib/rust/parser/src/syntax/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ macro_rules! with_ast_definition { ($f:ident ($($args:tt)*)) => { $f! { $($args)
Ident {
pub token: token::Ident<'s>,
},
/// A `private` keyword, marking associated expressions as project-private.
Private {
pub token: token::Private<'s>,
},
/// A numeric literal, like `10`.
Number {
pub base: Option<token::NumberBase<'s>>,
Expand Down Expand Up @@ -951,6 +955,7 @@ pub fn apply_unary_operator<'s>(opr: token::Operator<'s>, rhs: Option<Tree<'s>>)
pub fn to_ast(token: Token) -> Tree {
match token.variant {
token::Variant::Ident(ident) => token.with_variant(ident).into(),
token::Variant::Private(private) => Tree::private(token.with_variant(private)),
token::Variant::Digits(number) =>
Tree::number(None, Some(token.with_variant(number)), None),
token::Variant::NumberBase(base) =>
Expand Down

0 comments on commit 3b2e5e5

Please sign in to comment.