@@ -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+
8393impl 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 ) ]
19321949pub struct Parser < ' a > {
19331950 lexer : & ' a Lexer ,
19341951 token_buffer : RefCell < VecDeque < Token < ' a > > > ,
1952+
1953+ mode : RefCell < ParserMode > ,
19351954}
19361955
19371956impl < ' 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\n var x = true\n " ;
0 commit comments