Skip to content

Commit

Permalink
fix: more lenient parsing for non-vim9script code
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Mar 15, 2024
1 parent b219b6e commit f91894b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/shared/snapshots/mapleader.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let mapleader = "\\<Space>"
3 changes: 3 additions & 0 deletions crates/vim9-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,9 @@ mod test {
snapshot!(test_export, "../testdata/snapshots/export.vim");
// snapshot!(test_matchparen, "../../shared/snapshots/matchparen.vim");

// Issues #41
snapshot!(test_mapleader, "../../shared/snapshots/mapleader.vim");

#[test]
fn test_simple_def() {
let contents = r#"
Expand Down
18 changes: 18 additions & 0 deletions crates/vim9-gen/testdata/output/vim9_gen__test__mapleader.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/vim9-gen/src/lib.rs
expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unwrap().lua"
---
----------------------------------------
-- This file is generated via github.com/tjdevries/vim9jit
-- For any bugs, please first consider reporting there.
----------------------------------------

-- Ignore "value assigned to a local variable is unused" because
-- we can't guarantee that local variables will be used by plugins
-- luacheck: ignore 311

local vim9 = require('_vim9script')
local M = {}
pcall(vim.cmd, [[ let mapleader = "\<Space>" ]])
return M

39 changes: 37 additions & 2 deletions crates/vim9-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ pub struct TokenOwned {
pub span: Span,
}

impl TokenOwned {
pub fn literal(&self) -> String {
match self.kind {
TokenKind::SingleQuote => format!("'{}'", self.text),
TokenKind::DoubleQuoteString => format!("\"{}\"", self.text).replace("\\\\", "\\"),
_ => self.text.clone(),
}
}
}

impl Debug for TokenOwned {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
Expand Down Expand Up @@ -418,11 +428,12 @@ impl SharedCommand {
let tok = parser.pop();

if prev_end > tok.span.start_col {
panic!("failed to make shared command: {parser:#?}");
// panic!("failed to make shared command: {parser:#?}");
continue;
}

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

prev_end = tok.span.end_col;
}
Expand Down Expand Up @@ -1928,10 +1939,18 @@ impl PeekInfo {
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum ParserMode {
PreVim9Script,
PostVim9Script,
}

#[derive(Debug)]
pub struct Parser<'a> {
lexer: &'a Lexer,
token_buffer: RefCell<VecDeque<Token<'a>>>,

mode: RefCell<ParserMode>,
}

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

Self {
mode: RefCell::new(ParserMode::PreVim9Script),
token_buffer: RefCell::new(tokens),
lexer,
}
Expand Down Expand Up @@ -2418,6 +2438,15 @@ impl<'a> Parser<'a> {
pub fn parse_command(&self) -> Result<ExCommand> {
use TokenKind::*;

if *self.mode.borrow() == ParserMode::PreVim9Script
&& !self.command_match("vim9script")
&& !self.command_match("def")
{
if self.command_match("let") {
return SharedCommand::parse(&self);
}
}

// If the line starts with a colon, then just skip over it.
if self.front_kind() == Colon {
self.next_token();
Expand Down Expand Up @@ -2453,6 +2482,9 @@ impl<'a> Parser<'a> {

Identifier => {
if self.command_match("vim9script") {
// We've now seen vim9script, go ahead and update
*self.mode.borrow_mut() = ParserMode::PostVim9Script;

ExCommand::Vim9Script(Vim9ScriptCommand::parse(self)?)
} else if self.command_match("execute") {
ExecuteCommand::parse(self)?
Expand Down Expand Up @@ -2810,6 +2842,9 @@ mod test {
snap!(test_fileselect, "../../shared/snapshots/lsp_fileselect.vim");
snap!(test_startup, "../../shared/snapshots/startup9.vim");

// Issues #41
snap!(test_mapleader, "../../shared/snapshots/mapleader.vim");

#[test]
fn test_peek_n() {
let input = "vim9script\nvar x = true\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/vim9-parser/src/lib.rs
expression: snapshot_parsing(contents)
---
[
SharedCommand(
SharedCommand {
contents: "let mapleader = \\\\<Space>",
eol: Token(EndOfLine, (0,27)->(0,27)),
},
),
]
5 changes: 5 additions & 0 deletions scratch/vimcmd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vim.cmd([[
if 1
finish
endif
]])

0 comments on commit f91894b

Please sign in to comment.