Patch 8.2.1826
Problem:    Vim9: cannot use a {} block at script level.
Solution:   Recognize a {} block.
Files:      src/ex_docmd.c, src/ex_cmds.h, src/ex_cmdidxs.h, src/ex_eval.c,
            src/structs.h, src/proto/ex_eval.pro, src/errors.h,
            src/testdir/test_vim9_script.vim


*** ../vim-8.2.1825/src/ex_docmd.c      2020-10-10 15:05:19.970305397 +0200
--- src/ex_docmd.c      2020-10-10 21:20:17.845204378 +0200
***************
*** 3222,3228 ****
                *p == '('
                    || (p == eap->cmd
                        ? (
!                           // "{..." is an dict expression.
                            *eap->cmd == '{'
                            // "'string'->func()" is an expression.
                         || *eap->cmd == '\''
--- 3222,3228 ----
                *p == '('
                    || (p == eap->cmd
                        ? (
!                           // "{..." is a dict expression or block start.
                            *eap->cmd == '{'
                            // "'string'->func()" is an expression.
                         || *eap->cmd == '\''
***************
*** 3234,3239 ****
--- 3234,3245 ----
                            // "varname->func()" is an expression.
                        : (*p == '-' && p[1] == '>')))
            {
+               if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
+               {
+                   // "{" by itself is the start of a block.
+                   eap->cmdidx = CMD_block;
+                   return eap->cmd + 1;
+               }
                eap->cmdidx = CMD_eval;
                return eap->cmd;
            }
***************
*** 3355,3361 ****
        }
  
        // check for non-alpha command
!       if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
            ++p;
        len = (int)(p - eap->cmd);
        if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
--- 3361,3367 ----
        }
  
        // check for non-alpha command
!       if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
            ++p;
        len = (int)(p - eap->cmd);
        if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
*** ../vim-8.2.1825/src/ex_cmds.h       2020-09-26 15:08:52.877779920 +0200
--- src/ex_cmds.h       2020-10-10 20:53:29.248720066 +0200
***************
*** 1812,1817 ****
--- 1812,1823 ----
  EXCMD(CMD_at,         "@",            ex_at,
        EX_RANGE|EX_WHOLEFOLD|EX_EXTRA|EX_TRLBAR|EX_CMDWIN|EX_LOCK_OK,
        ADDR_LINES),
+ EXCMD(CMD_block,      "{{{{{{{{",     ex_block,  // not found normally
+       0,
+       ADDR_NONE),
+ EXCMD(CMD_endblock,   "}",            ex_endblock,
+       EX_TRLBAR|EX_CMDWIN|EX_LOCK_OK,
+       ADDR_NONE),
  EXCMD(CMD_tilde,      "~",            ex_substitute,
        EX_RANGE|EX_WHOLEFOLD|EX_EXTRA|EX_CMDWIN|EX_LOCK_OK|EX_MODIFY,
        ADDR_LINES),
*** ../vim-8.2.1825/src/ex_cmdidxs.h    2020-09-26 15:08:52.885779899 +0200
--- src/ex_cmdidxs.h    2020-10-10 20:50:28.189257994 +0200
***************
*** 69,72 ****
    /* z */ {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  
0,  0,  0,  0,  0,  0,  0,  0,  0,  0 }
  };
  
! static const int command_count = 571;
--- 69,72 ----
    /* z */ {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  
0,  0,  0,  0,  0,  0,  0,  0,  0,  0 }
  };
  
! static const int command_count = 573;
*** ../vim-8.2.1825/src/ex_eval.c       2020-10-10 20:31:33.360640043 +0200
--- src/ex_eval.c       2020-10-10 21:11:22.054516428 +0200
***************
*** 1002,1008 ****
      did_endif = TRUE;
      if (cstack->cs_idx < 0
            || (cstack->cs_flags[cstack->cs_idx]
!                                          & (CSF_WHILE | CSF_FOR | CSF_TRY)))
        eap->errmsg = _(e_endif_without_if);
      else
      {
--- 1002,1008 ----
      did_endif = TRUE;
      if (cstack->cs_idx < 0
            || (cstack->cs_flags[cstack->cs_idx]
!                               & (CSF_WHILE | CSF_FOR | CSF_TRY | CSF_BLOCK)))
        eap->errmsg = _(e_endif_without_if);
      else
      {
***************
*** 1043,1049 ****
  
      if (cstack->cs_idx < 0
            || (cstack->cs_flags[cstack->cs_idx]
!                                          & (CSF_WHILE | CSF_FOR | CSF_TRY)))
      {
        if (eap->cmdidx == CMD_else)
        {
--- 1043,1049 ----
  
      if (cstack->cs_idx < 0
            || (cstack->cs_flags[cstack->cs_idx]
!                               & (CSF_WHILE | CSF_FOR | CSF_TRY | CSF_BLOCK)))
      {
        if (eap->cmdidx == CMD_else)
        {
***************
*** 1375,1380 ****
--- 1375,1411 ----
      }
  }
  
+ /*
+  * "{" start of a block in Vim9 script
+  */
+     void
+ ex_block(exarg_T *eap)
+ {
+     cstack_T  *cstack = eap->cstack;
+ 
+     if (cstack->cs_idx == CSTACK_LEN - 1)
+       eap->errmsg = _("E579: block nesting too deep");
+     else
+     {
+       enter_block(cstack);
+       cstack->cs_flags[cstack->cs_idx] = CSF_BLOCK | CSF_ACTIVE | CSF_TRUE;
+     }
+ }
+ 
+ /*
+  * "}" end of a block in Vim9 script
+  */
+     void
+ ex_endblock(exarg_T *eap)
+ {
+     cstack_T  *cstack = eap->cstack;
+ 
+     if (cstack->cs_idx < 0
+           || (cstack->cs_flags[cstack->cs_idx] & CSF_BLOCK) == 0)
+       eap->errmsg = _(e_endblock_without_block);
+     else
+       leave_block(cstack);
+ }
  
  /*
   * ":throw expr"
*** ../vim-8.2.1825/src/structs.h       2020-10-10 19:07:04.187713866 +0200
--- src/structs.h       2020-10-10 19:38:37.583617226 +0200
***************
*** 907,912 ****
--- 907,913 ----
  # define CSF_ELSE     0x0004  // ":else" has been passed
  # define CSF_WHILE    0x0008  // is a ":while"
  # define CSF_FOR      0x0010  // is a ":for"
+ # define CSF_BLOCK    0x0020  // is a "{" block
  
  # define CSF_TRY      0x0100  // is a ":try"
  # define CSF_FINALLY  0x0200  // ":finally" has been passed
*** ../vim-8.2.1825/src/proto/ex_eval.pro       2020-01-26 15:52:33.023833239 
+0100
--- src/proto/ex_eval.pro       2020-10-10 20:54:11.848593518 +0200
***************
*** 20,25 ****
--- 20,27 ----
  void ex_continue(exarg_T *eap);
  void ex_break(exarg_T *eap);
  void ex_endwhile(exarg_T *eap);
+ void ex_block(exarg_T *eap);
+ void ex_endblock(exarg_T *eap);
  void ex_throw(exarg_T *eap);
  void do_throw(cstack_T *cstack);
  void ex_try(exarg_T *eap);
*** ../vim-8.2.1825/src/errors.h        2020-10-03 13:41:49.959173003 +0200
--- src/errors.h        2020-10-10 20:55:11.524416259 +0200
***************
*** 278,281 ****
--- 278,283 ----
        INIT(= N_("E1126: Cannot use :let in Vim9 script"));
  EXTERN char e_missing_name_after_dot[]
        INIT(= N_("E1127: Missing name after dot"));
+ EXTERN char e_endblock_without_block[]
+       INIT(= N_("E1128: } without {"));
  #endif
*** ../vim-8.2.1825/src/testdir/test_vim9_script.vim    2020-10-10 
19:07:04.187713866 +0200
--- src/testdir/test_vim9_script.vim    2020-10-10 21:13:30.854213701 +0200
***************
*** 2733,2738 ****
--- 2733,2759 ----
        echo one
    END
    CheckScriptFailure(lines, 'E121:', 6)
+ 
+   lines =<< trim END
+       vim9script
+       {
+         var one = 'one'
+         assert_equal('one', one)
+       }
+       assert_false(exists('one'))
+       assert_false(exists('s:one'))
+   END
+   CheckScriptSuccess(lines)
+ 
+   lines =<< trim END
+       vim9script
+       {
+         var one = 'one'
+         echo one
+       }
+       echo one
+   END
+   CheckScriptFailure(lines, 'E121:', 6)
  enddef
  
  " Keep this last, it messes up highlighting.
*** ../vim-8.2.1825/src/version.c       2020-10-10 20:31:33.360640043 +0200
--- src/version.c       2020-10-10 21:19:36.013310796 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     1826,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
57. You begin to wonder how on earth your service provider is allowed to call
    200 hours per month "unlimited."

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202010101934.09AJYG0r3802160%40masaka.moolenaar.net.

Raspunde prin e-mail lui