Skip to content

Commit f91894b

Browse files
committed
fix: more lenient parsing for non-vim9script code
1 parent b219b6e commit f91894b

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let mapleader = "\\<Space>"

crates/vim9-gen/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,9 @@ mod test {
18831883
snapshot!(test_export, "../testdata/snapshots/export.vim");
18841884
// snapshot!(test_matchparen, "../../shared/snapshots/matchparen.vim");
18851885

1886+
// Issues #41
1887+
snapshot!(test_mapleader, "../../shared/snapshots/mapleader.vim");
1888+
18861889
#[test]
18871890
fn test_simple_def() {
18881891
let contents = r#"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: crates/vim9-gen/src/lib.rs
3+
expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unwrap().lua"
4+
---
5+
----------------------------------------
6+
-- This file is generated via github.com/tjdevries/vim9jit
7+
-- For any bugs, please first consider reporting there.
8+
----------------------------------------
9+
10+
-- Ignore "value assigned to a local variable is unused" because
11+
-- we can't guarantee that local variables will be used by plugins
12+
-- luacheck: ignore 311
13+
14+
local vim9 = require('_vim9script')
15+
local M = {}
16+
pcall(vim.cmd, [[ let mapleader = "\<Space>" ]])
17+
return M
18+

crates/vim9-parser/src/lib.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ pub struct TokenOwned {
8080
pub span: Span,
8181
}
8282

83+
impl TokenOwned {
84+
pub fn literal(&self) -> String {
85+
match self.kind {
86+
TokenKind::SingleQuote => format!("'{}'", self.text),
87+
TokenKind::DoubleQuoteString => format!("\"{}\"", self.text).replace("\\\\", "\\"),
88+
_ => self.text.clone(),
89+
}
90+
}
91+
}
92+
8393
impl Debug for TokenOwned {
8494
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8595
write!(
@@ -418,11 +428,12 @@ impl SharedCommand {
418428
let tok = parser.pop();
419429

420430
if prev_end > tok.span.start_col {
421-
panic!("failed to make shared command: {parser:#?}");
431+
// panic!("failed to make shared command: {parser:#?}");
432+
continue;
422433
}
423434

424435
contents += " ".repeat(tok.span.start_col - prev_end).as_str();
425-
contents += tok.text.as_str();
436+
contents += &tok.literal();
426437

427438
prev_end = tok.span.end_col;
428439
}
@@ -1928,10 +1939,18 @@ impl PeekInfo {
19281939
}
19291940
}
19301941

1942+
#[derive(Debug, PartialEq, Eq)]
1943+
pub enum ParserMode {
1944+
PreVim9Script,
1945+
PostVim9Script,
1946+
}
1947+
19311948
#[derive(Debug)]
19321949
pub struct Parser<'a> {
19331950
lexer: &'a Lexer,
19341951
token_buffer: RefCell<VecDeque<Token<'a>>>,
1952+
1953+
mode: RefCell<ParserMode>,
19351954
}
19361955

19371956
impl<'a> Parser<'a> {
@@ -1941,6 +1960,7 @@ impl<'a> Parser<'a> {
19411960
tokens.push_back(lexer.next_token().unwrap());
19421961

19431962
Self {
1963+
mode: RefCell::new(ParserMode::PreVim9Script),
19441964
token_buffer: RefCell::new(tokens),
19451965
lexer,
19461966
}
@@ -2418,6 +2438,15 @@ impl<'a> Parser<'a> {
24182438
pub fn parse_command(&self) -> Result<ExCommand> {
24192439
use TokenKind::*;
24202440

2441+
if *self.mode.borrow() == ParserMode::PreVim9Script
2442+
&& !self.command_match("vim9script")
2443+
&& !self.command_match("def")
2444+
{
2445+
if self.command_match("let") {
2446+
return SharedCommand::parse(&self);
2447+
}
2448+
}
2449+
24212450
// If the line starts with a colon, then just skip over it.
24222451
if self.front_kind() == Colon {
24232452
self.next_token();
@@ -2453,6 +2482,9 @@ impl<'a> Parser<'a> {
24532482

24542483
Identifier => {
24552484
if self.command_match("vim9script") {
2485+
// We've now seen vim9script, go ahead and update
2486+
*self.mode.borrow_mut() = ParserMode::PostVim9Script;
2487+
24562488
ExCommand::Vim9Script(Vim9ScriptCommand::parse(self)?)
24572489
} else if self.command_match("execute") {
24582490
ExecuteCommand::parse(self)?
@@ -2810,6 +2842,9 @@ mod test {
28102842
snap!(test_fileselect, "../../shared/snapshots/lsp_fileselect.vim");
28112843
snap!(test_startup, "../../shared/snapshots/startup9.vim");
28122844

2845+
// Issues #41
2846+
snap!(test_mapleader, "../../shared/snapshots/mapleader.vim");
2847+
28132848
#[test]
28142849
fn test_peek_n() {
28152850
let input = "vim9script\nvar x = true\n";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/vim9-parser/src/lib.rs
3+
expression: snapshot_parsing(contents)
4+
---
5+
[
6+
SharedCommand(
7+
SharedCommand {
8+
contents: "let mapleader = \\\\<Space>",
9+
eol: Token(EndOfLine, (0,27)->(0,27)),
10+
},
11+
),
12+
]

scratch/vimcmd.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vim.cmd([[
2+
if 1
3+
finish
4+
endif
5+
]])

0 commit comments

Comments
 (0)