Skip to content

Commit

Permalink
Improve code completion
Browse files Browse the repository at this point in the history
Top level identifiers are no longer parsed as rules, which improves
suggestions, as the following rules are now parsed correctly.
  • Loading branch information
0x2a-42 committed Jan 2, 2025
1 parent aacbace commit 63bad5d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/frontend/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<'a> Parser<'a> {
Token::Token => {
self.r#token_list(diags);
}
Token::Id => {
Token::Id if self.predicate_decl_1() => {
self.r#rule_decl(diags);
}
Token::Start => {
Expand All @@ -431,6 +431,9 @@ impl<'a> Parser<'a> {
Token::Skip => {
self.r#skip_decl(diags);
}
Token::Id => {
self.advance_with_error(diags, err![self.span(),]);
}
_ => {
self.error(
diags,
Expand Down Expand Up @@ -878,4 +881,5 @@ trait PredicatesAndActions {
/// Called when a new syntax tree node is created
#[allow(clippy::ptr_arg)]
fn build(&mut self, _rule: Rule, _node: NodeRef, _diags: &mut Vec<Diagnostic>) {}
fn predicate_decl_1(&self) -> bool;
}
2 changes: 1 addition & 1 deletion src/frontend/lelwel.llw
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ skip Comment DocComment Whitespace;
file: decl*;
decl^:
token_list
| rule_decl
| ?1 rule_decl
| start_decl
| right_decl
| skip_decl
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,9 @@ pub fn tokenize(

include!("./generated.rs");

impl PredicatesAndActions for Parser<'_> {}
impl PredicatesAndActions for Parser<'_> {
fn predicate_decl_1(&self) -> bool {
let peek = self.peek(1);
peek == Token::Colon || (peek == Token::Hat && self.peek(2) == Token::Colon)
}
}
11 changes: 11 additions & 0 deletions tests/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ fn syntax_error() {
assert_eq!(lines.next(), None);
}

#[test]
#[rustfmt::skip]
fn top_level_ident() {
let diags = gen_diags("tests/frontend/top_level_ident.llw");
let mut lines = diags.lines();

assert_eq!(lines.next().unwrap(), "tests/frontend/top_level_ident.llw:3:1: error: invalid syntax");
assert_eq!(lines.next().unwrap(), "tests/frontend/top_level_ident.llw:5:1: warning[W005]: empty rule");
assert_eq!(lines.next(), None);
}

#[test]
#[rustfmt::skip]
fn undefined() {
Expand Down

0 comments on commit 63bad5d

Please sign in to comment.