Skip to content

Commit

Permalink
add a few missed keywords, WordKinds is now u128
Browse files Browse the repository at this point in the history
  • Loading branch information
minestarks committed Oct 11, 2024
1 parent 93c3360 commit 3c5b548
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
13 changes: 9 additions & 4 deletions compiler/qsc_parse/src/completion/word_kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ bitflags! {
/// possible names are hardcoded into the language, e.g. "EntryPoint", "Qubit".
///
/// IF UPDATING: If new values are added before the keyword range,
/// [`KEYWORDS_START`] *must* be udpated.
/// [`KEYWORDS_START`] *must* be updated.
///
#[repr(transparent)]
#[derive(Default, PartialEq, Debug, Clone, Copy)]
pub struct WordKinds: u64 {
pub struct WordKinds: u128 {

//
// Begin names.
Expand Down Expand Up @@ -63,6 +63,8 @@ bitflags! {
const Attr = 1 << 8;
/// The word `Qubit`.
const Qubit = 1 << 9;
/// The word `size`.
const Size = 1 << 10;

//
// End hardcoded identifiers.
Expand Down Expand Up @@ -129,8 +131,8 @@ bitflags! {
}
}

const KEYWORDS_START: u8 = 10;
const fn keyword_bit(k: Keyword) -> u64 {
const KEYWORDS_START: u8 = 11;
const fn keyword_bit(k: Keyword) -> u128 {
1 << (k as u8 + KEYWORDS_START)
}

Expand Down Expand Up @@ -161,6 +163,7 @@ impl WordKinds {
self.iter().filter_map(|p| match p {
WordKinds::Attr => Some(HardcodedIdentKind::Attr),
WordKinds::Qubit => Some(HardcodedIdentKind::Qubit),
WordKinds::Size => Some(HardcodedIdentKind::Size),
_ => None,
})
}
Expand All @@ -180,6 +183,8 @@ pub enum HardcodedIdentKind {
Attr,
/// The word `Qubit`.
Qubit,
/// The word `size`.
Size,
}

/// A name (see: [`Predictions`])
Expand Down
4 changes: 4 additions & 0 deletions compiler/qsc_parse/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub(super) fn is_stmt_final(kind: &ExprKind) -> bool {

fn expr_op(s: &mut ParserContext, context: OpContext) -> Result<Box<Expr>> {
let lo = s.peek().span.lo;

s.expect(WordKinds::AdjointUpper | WordKinds::ControlledUpper | WordKinds::Not);
let mut lhs = if let Some(op) = prefix_op(op_name(s)) {
s.advance();
let rhs = expr_op(s, OpContext::Precedence(op.precedence))?;
Expand All @@ -117,6 +119,7 @@ fn expr_op(s: &mut ParserContext, context: OpContext) -> Result<Box<Expr>> {
OpContext::Stmt => 0,
};

s.expect(WordKinds::And | WordKinds::Or);
while let Some(op) = mixfix_op(op_name(s)) {
if op.precedence < min_precedence {
break;
Expand Down Expand Up @@ -346,6 +349,7 @@ fn expr_array_core(s: &mut ParserContext) -> Result<Box<ExprKind>> {
return Ok(Box::new(ExprKind::Array(vec![first].into_boxed_slice())));
}

s.expect(WordKinds::Size);
let second = expr(s)?;
if is_ident("size", &second.kind) && token(s, TokenKind::Eq).is_ok() {
let size = expr(s)?;
Expand Down
6 changes: 6 additions & 0 deletions language_service/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ fn collect_hardcoded_words(expected: WordKinds) -> Vec<Completion> {
Completion::new("Config".to_string(), CompletionItemKind::Interface),
]);
}
HardcodedIdentKind::Size => {
completions.push(Completion::new(
"size".to_string(),
CompletionItemKind::Keyword,
));
}
}
}

Expand Down
94 changes: 94 additions & 0 deletions language_service/src/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3310,3 +3310,97 @@ fn notebook_top_level_path_part_in_type() {
"#]],
);
}

#[test]
fn prefix_ops() {
check(
"namespace Test { function Main() : Unit { let x = ↘ ; } }",
&["and", "or", "not", "Adjoint"],
&expect![[r#"
[
None,
None,
Some(
CompletionItem {
label: "not",
kind: Keyword,
sort_text: Some(
"0000not",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "Adjoint",
kind: Keyword,
sort_text: Some(
"0000Adjoint",
),
detail: None,
additional_text_edits: None,
},
),
]
"#]],
);
}

#[test]
fn binary_ops() {
check(
"namespace Test { function Main() : Unit { let x = 1 ↘ ; } }",
&["and", "or", "not"],
&expect![[r#"
[
Some(
CompletionItem {
label: "and",
kind: Keyword,
sort_text: Some(
"0000and",
),
detail: None,
additional_text_edits: None,
},
),
Some(
CompletionItem {
label: "or",
kind: Keyword,
sort_text: Some(
"0000or",
),
detail: None,
additional_text_edits: None,
},
),
None,
]
"#]],
);
}

#[test]
fn array_size() {
check(
"namespace Test { function Main() : Unit { let x = [0, ↘] ; } }",
&["size"],
&expect![[r#"
[
Some(
CompletionItem {
label: "size",
kind: Keyword,
sort_text: Some(
"0000size",
),
detail: None,
additional_text_edits: None,
},
),
]
"#]],
);
}

0 comments on commit 3c5b548

Please sign in to comment.