comparison src/vim9class.c @ 33942:3bba09502b8d v9.0.2167

patch 9.0.2167: Vim9: not consistently using :var for declarations Commit: https://github.com/vim/vim/commit/74da0ee0a24799a312a3a8a65858237185ef7a23 Author: Doug Kearns <dougkearns@gmail.com> Date: Thu Dec 14 20:26:26 2023 +0100 patch 9.0.2167: Vim9: not consistently using :var for declarations Problem: Vim9-script object/class variable declarations use syntax that is inconsistent with the rest of the language. Solution: Use :var to declare object and class variables. closes: #13670 Signed-off-by: Doug Kearns <dougkearns@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Thu, 14 Dec 2023 20:30:06 +0100
parents bdd408288d95
children 45a50fd59a73
comparison
equal deleted inserted replaced
33941:8845f55a6c20 33942:3bba09502b8d
1556 break; 1556 break;
1557 } 1557 }
1558 has_public = TRUE; 1558 has_public = TRUE;
1559 p = skipwhite(line + 6); 1559 p = skipwhite(line + 6);
1560 1560
1561 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0) 1561 if (STRNCMP(p, "var", 3) != 0 && STRNCMP(p, "static", 6) != 0)
1562 { 1562 {
1563 emsg(_(e_public_must_be_followed_by_this_or_static)); 1563 emsg(_(e_public_must_be_followed_by_var_or_static));
1564 break; 1564 break;
1565 } 1565 }
1566 } 1566 }
1567 1567
1568 int abstract_method = FALSE; 1568 int abstract_method = FALSE;
1613 emsg(_(e_static_member_not_supported_in_interface)); 1613 emsg(_(e_static_member_not_supported_in_interface));
1614 break; 1614 break;
1615 } 1615 }
1616 has_static = TRUE; 1616 has_static = TRUE;
1617 p = skipwhite(ps + 6); 1617 p = skipwhite(ps + 6);
1618
1619 if (STRNCMP(p, "var", 3) != 0 && STRNCMP(p, "def", 3) != 0)
1620 {
1621 emsg(_(e_static_must_be_followed_by_var_or_def));
1622 break;
1623 }
1618 } 1624 }
1619 1625
1620 // object members (public, read access, private): 1626 // object members (public, read access, private):
1621 // "this._varname" 1627 // "var _varname"
1622 // "this.varname" 1628 // "var varname"
1623 // "public this.varname" 1629 // "public var varname"
1624 if (STRNCMP(p, "this", 4) == 0) 1630 // class members (public, read access, private):
1625 { 1631 // "static var _varname"
1626 if (p[4] != '.' || !eval_isnamec1(p[5])) 1632 // "static var varname"
1627 { 1633 // "public static var varname"
1628 semsg(_(e_invalid_object_variable_declaration_str), p); 1634 if (checkforcmd(&p, "var", 3))
1629 break; 1635 {
1630 } 1636 char_u *varname = p;
1631 if (has_static)
1632 {
1633 emsg(_(e_static_cannot_be_followed_by_this));
1634 break;
1635 }
1636 char_u *varname = p + 5;
1637 char_u *varname_end = NULL; 1637 char_u *varname_end = NULL;
1638 type_T *type = NULL; 1638 type_T *type = NULL;
1639 char_u *init_expr = NULL; 1639 char_u *init_expr = NULL;
1640 int has_type = FALSE; 1640 int has_type = FALSE;
1641
1642 if (!eval_isnamec1(*p))
1643 {
1644 if (has_static)
1645 semsg(_(e_invalid_class_variable_declaration_str), line);
1646 else
1647 semsg(_(e_invalid_object_variable_declaration_str), line);
1648 break;
1649 }
1641 1650
1642 if (!is_class && *varname == '_') 1651 if (!is_class && *varname == '_')
1643 { 1652 {
1644 // private variables are not supported in an interface 1653 // private variables are not supported in an interface
1645 semsg(_(e_protected_variable_not_supported_in_interface), 1654 semsg(_(e_protected_variable_not_supported_in_interface),
1660 varname_end)) 1669 varname_end))
1661 { 1670 {
1662 vim_free(init_expr); 1671 vim_free(init_expr);
1663 break; 1672 break;
1664 } 1673 }
1665 if (add_member(&objmembers, varname, varname_end, 1674 if (add_member(has_static ? &classmembers : &objmembers, varname, varname_end,
1666 has_public, has_type, type, init_expr) == FAIL) 1675 has_public, has_type, type, init_expr) == FAIL)
1667 { 1676 {
1668 vim_free(init_expr); 1677 vim_free(init_expr);
1669 break; 1678 break;
1670 } 1679 }
1762 ++fgap->ga_len; 1771 ++fgap->ga_len;
1763 } 1772 }
1764 } 1773 }
1765 } 1774 }
1766 1775
1767 // class members
1768 else if (has_static)
1769 {
1770 // class members (public, read access, private):
1771 // "static _varname"
1772 // "static varname"
1773 // "public static varname"
1774 char_u *varname = p;
1775 char_u *varname_end = NULL;
1776 int has_type = FALSE;
1777 type_T *type = NULL;
1778 char_u *init_expr = NULL;
1779
1780 if (parse_member(eap, line, varname, has_public,
1781 &varname_end, &has_type, &type_list, &type,
1782 &init_expr) == FAIL)
1783 break;
1784 if (is_reserved_varname(varname, varname_end))
1785 {
1786 vim_free(init_expr);
1787 break;
1788 }
1789 if (is_duplicate_variable(&classmembers, &objmembers, varname,
1790 varname_end))
1791 {
1792 vim_free(init_expr);
1793 break;
1794 }
1795 if (add_member(&classmembers, varname, varname_end,
1796 has_public, has_type, type, init_expr) == FAIL)
1797 {
1798 vim_free(init_expr);
1799 break;
1800 }
1801 }
1802
1803 else 1776 else
1804 { 1777 {
1805 if (is_class) 1778 if (is_class)
1806 semsg(_(e_not_valid_command_in_class_str), line); 1779 semsg(_(e_not_valid_command_in_class_str), line);
1807 else 1780 else