comparison libinterp/parse-tree/oct-parse.yy @ 28509:06bc2f0bf760 stable

refactor some classdef parsing rules * oct-parse.yy: (attr_list1, attr_list): Rename from attr_list and opt_attr_list. Accept an optional trailing separator. (superclass_list1, superclass_list): Rename from superclass_list and opt_attr_list. Accept optional trailing separator. (class_body, property_list, methods_list, events_list, enum_list): Use new non-terminal to define list. Allow list to be empty and to handle an optional trailing separator. (classdef, properties_block, methods_block, events_block, enum_block): Eliminate separate rules for handling empty elements.
author John W. Eaton <jwe@octave.org>
date Thu, 28 May 2020 12:00:24 -0400
parents 71c34141cc2d
children 59dfd9ed72a3
comparison
equal deleted inserted replaced
28508:f4604e382ab0 28509:06bc2f0bf760
269 %type <tree_statement_type> statement function_end 269 %type <tree_statement_type> statement function_end
270 %type <tree_statement_list_type> simple_list simple_list1 list list1 270 %type <tree_statement_list_type> simple_list simple_list1 list list1
271 %type <tree_statement_list_type> opt_list 271 %type <tree_statement_list_type> opt_list
272 %type <tree_statement_list_type> opt_fcn_list fcn_list fcn_list1 272 %type <tree_statement_list_type> opt_fcn_list fcn_list fcn_list1
273 %type <tree_classdef_attribute_type> attr 273 %type <tree_classdef_attribute_type> attr
274 %type <tree_classdef_attribute_list_type> attr_list opt_attr_list 274 %type <tree_classdef_attribute_list_type> attr_list attr_list1
275 %type <tree_classdef_superclass_type> superclass 275 %type <tree_classdef_superclass_type> superclass
276 %type <tree_classdef_superclass_list_type> superclass_list opt_superclass_list 276 %type <tree_classdef_superclass_list_type> superclass_list superclass_list1
277 %type <tree_classdef_body_type> class_body 277 %type <tree_classdef_body_type> class_body class_body1
278 %type <tree_classdef_property_type> class_property 278 %type <tree_classdef_property_type> class_property
279 %type <tree_classdef_property_list_type> property_list 279 %type <tree_classdef_property_list_type> property_list property_list1
280 %type <tree_classdef_properties_block_type> properties_block 280 %type <tree_classdef_properties_block_type> properties_block
281 %type <tree_classdef_methods_list_type> methods_list 281 %type <tree_classdef_methods_list_type> methods_list methods_list1
282 %type <tree_classdef_methods_block_type> methods_block 282 %type <tree_classdef_methods_block_type> methods_block
283 %type <tree_classdef_event_type> class_event 283 %type <tree_classdef_event_type> class_event
284 %type <tree_classdef_events_list_type> events_list 284 %type <tree_classdef_events_list_type> events_list events_list1
285 %type <tree_classdef_events_block_type> events_block 285 %type <tree_classdef_events_block_type> events_block
286 %type <tree_classdef_enum_type> class_enum 286 %type <tree_classdef_enum_type> class_enum
287 %type <tree_classdef_enum_list_type> enum_list 287 %type <tree_classdef_enum_list_type> enum_list enum_list1
288 %type <tree_classdef_enum_block_type> enum_block 288 %type <tree_classdef_enum_block_type> enum_block
289 %type <tree_function_def_type> method_decl method 289 %type <tree_function_def_type> method_decl method
290 %type <octave_user_function_type> method_decl1 290 %type <octave_user_function_type> method_decl1
291 291
292 // Precedence and associativity. 292 // Precedence and associativity.
1720 1720
1721 // Create invalid parent scope. 1721 // Create invalid parent scope.
1722 lexer.m_symtab_context.push (octave::symbol_scope ()); 1722 lexer.m_symtab_context.push (octave::symbol_scope ());
1723 lexer.m_parsing_classdef = true; 1723 lexer.m_parsing_classdef = true;
1724 lexer.m_parsing_classdef_decl = true; 1724 lexer.m_parsing_classdef_decl = true;
1725
1725 $$ = $1; 1726 $$ = $1;
1726 } 1727 }
1727 ; 1728 ;
1728 1729
1729 classdef : classdef_beg stash_comment opt_attr_list identifier opt_superclass_list opt_sep class_body opt_sep END 1730 classdef : classdef_beg stash_comment attr_list identifier opt_sep superclass_list class_body END
1730 { 1731 {
1731 YYUSE ($6); 1732 YYUSE ($5);
1732 YYUSE ($8);
1733 1733
1734 lexer.m_parsing_classdef = false; 1734 lexer.m_parsing_classdef = false;
1735 1735
1736 if (! ($$ = parser.make_classdef ($1, $3, $4, $5, $7, $9, $2))) 1736 if (! ($$ = parser.make_classdef ($1, $3, $4, $6, $7, $8, $2)))
1737 { 1737 {
1738 // make_classdef deleted $3, $4, $5, and $7. 1738 // make_classdef deleted $3, $4, $6, and $7.
1739 YYABORT; 1739 YYABORT;
1740 } 1740 }
1741 } 1741 }
1742 | classdef_beg stash_comment opt_attr_list identifier opt_superclass_list opt_sep END 1742 ;
1743 { 1743
1744 YYUSE ($6); 1744 attr_list : // empty
1745
1746 lexer.m_parsing_classdef = false;
1747
1748 if (! ($$ = parser.make_classdef ($1, $3, $4, $5, nullptr,
1749 $7, $2)))
1750 {
1751 // make_classdef deleted $3, $4, and $5.
1752 YYABORT;
1753 }
1754 }
1755 ;
1756
1757 opt_attr_list : // empty
1758 { $$ = nullptr; } 1745 { $$ = nullptr; }
1759 | '(' attr_list ')' 1746 | '(' attr_list1 ')' opt_sep
1760 { 1747 {
1761 YYUSE ($1); 1748 YYUSE ($1);
1762 YYUSE ($3); 1749 YYUSE ($3);
1750 YYUSE ($4);
1763 1751
1764 $$ = $2; 1752 $$ = $2;
1765 } 1753 }
1766 ; 1754 ;
1767 1755
1768 attr_list : attr 1756 attr_list1 : attr
1769 { $$ = new octave::tree_classdef_attribute_list ($1); } 1757 { $$ = new octave::tree_classdef_attribute_list ($1); }
1770 | attr_list ',' attr 1758 | attr_list1 ',' attr
1771 { 1759 {
1772 YYUSE ($2); 1760 YYUSE ($2);
1773 1761
1774 $1->append ($3); 1762 $1->append ($3);
1775 $$ = $1; 1763 $$ = $1;
1791 1779
1792 $$ = new octave::tree_classdef_attribute ($2, false); 1780 $$ = new octave::tree_classdef_attribute ($2, false);
1793 } 1781 }
1794 ; 1782 ;
1795 1783
1796 opt_superclass_list 1784 superclass_list : // empty
1797 : // empty
1798 { 1785 {
1799 lexer.m_parsing_classdef_decl = false; 1786 lexer.m_parsing_classdef_decl = false;
1800 lexer.m_parsing_classdef_superclass = false; 1787 lexer.m_parsing_classdef_superclass = false;
1788
1801 $$ = nullptr; 1789 $$ = nullptr;
1802 } 1790 }
1803 | superclass_list 1791 | superclass_list1 opt_sep
1804 { 1792 {
1793 YYUSE ($2);
1794
1805 lexer.m_parsing_classdef_decl = false; 1795 lexer.m_parsing_classdef_decl = false;
1806 lexer.m_parsing_classdef_superclass = false; 1796 lexer.m_parsing_classdef_superclass = false;
1797
1807 $$ = $1; 1798 $$ = $1;
1808 } 1799 }
1809 ; 1800 ;
1810 1801
1811 superclass_list : EXPR_LT superclass 1802 superclass_list1
1803 : EXPR_LT superclass
1812 { 1804 {
1813 YYUSE ($1); 1805 YYUSE ($1);
1814 1806
1815 $$ = new octave::tree_classdef_superclass_list ($2); 1807 $$ = new octave::tree_classdef_superclass_list ($2);
1816 } 1808 }
1817 | superclass_list EXPR_AND superclass 1809 | superclass_list1 EXPR_AND superclass
1818 { 1810 {
1819 YYUSE ($2); 1811 YYUSE ($2);
1820 1812
1821 $1->append ($3); 1813 $1->append ($3);
1822 $$ = $1; 1814 $$ = $1;
1825 1817
1826 superclass : FQ_IDENT 1818 superclass : FQ_IDENT
1827 { $$ = new octave::tree_classdef_superclass ($1->text ()); } 1819 { $$ = new octave::tree_classdef_superclass ($1->text ()); }
1828 ; 1820 ;
1829 1821
1830 class_body : properties_block 1822 class_body : // empty
1823 { $$ = nullptr; }
1824 | class_body1 opt_sep
1825 {
1826 YYUSE ($2);
1827
1828 $$ = $1;
1829 }
1830 ;
1831
1832 class_body1 : properties_block
1831 { $$ = new octave::tree_classdef_body ($1); } 1833 { $$ = new octave::tree_classdef_body ($1); }
1832 | methods_block 1834 | methods_block
1833 { $$ = new octave::tree_classdef_body ($1); } 1835 { $$ = new octave::tree_classdef_body ($1); }
1834 | events_block 1836 | events_block
1835 { $$ = new octave::tree_classdef_body ($1); } 1837 { $$ = new octave::tree_classdef_body ($1); }
1836 | enum_block 1838 | enum_block
1837 { $$ = new octave::tree_classdef_body ($1); } 1839 { $$ = new octave::tree_classdef_body ($1); }
1838 | class_body opt_sep properties_block 1840 | class_body1 opt_sep properties_block
1839 { 1841 {
1840 YYUSE ($2); 1842 YYUSE ($2);
1841 1843
1842 $1->append ($3); 1844 $1->append ($3);
1843 $$ = $1; 1845 $$ = $1;
1844 } 1846 }
1845 | class_body opt_sep methods_block 1847 | class_body1 opt_sep methods_block
1846 { 1848 {
1847 YYUSE ($2); 1849 YYUSE ($2);
1848 1850
1849 $1->append ($3); 1851 $1->append ($3);
1850 $$ = $1; 1852 $$ = $1;
1851 } 1853 }
1852 | class_body opt_sep events_block 1854 | class_body1 opt_sep events_block
1853 { 1855 {
1854 YYUSE ($2); 1856 YYUSE ($2);
1855 1857
1856 $1->append ($3); 1858 $1->append ($3);
1857 $$ = $1; 1859 $$ = $1;
1858 } 1860 }
1859 | class_body opt_sep enum_block 1861 | class_body1 opt_sep enum_block
1860 { 1862 {
1861 YYUSE ($2); 1863 YYUSE ($2);
1862 1864
1863 $1->append ($3); 1865 $1->append ($3);
1864 $$ = $1; 1866 $$ = $1;
1865 } 1867 }
1866 ; 1868 ;
1867 1869
1868 properties_block 1870 properties_block
1869 : PROPERTIES stash_comment opt_attr_list opt_sep property_list opt_sep END 1871 : PROPERTIES opt_sep stash_comment attr_list property_list END
1870 { 1872 {
1871 YYUSE ($4); 1873 YYUSE ($2);
1872 YYUSE ($6);
1873 1874
1874 if (! ($$ = parser.make_classdef_properties_block 1875 if (! ($$ = parser.make_classdef_properties_block
1875 ($1, $3, $5, $7, $2))) 1876 ($1, $4, $5, $6, $3)))
1876 { 1877 {
1877 // make_classdef_properties_block delete $3 and $5. 1878 // make_classdef_properties_block deleted $4 and $5.
1878 YYABORT; 1879 YYABORT;
1879 } 1880 }
1880 } 1881 }
1881 | PROPERTIES stash_comment opt_attr_list opt_sep END 1882 ;
1882 { 1883
1883 YYUSE ($4); 1884 property_list : // empty
1884 1885 { $$ = nullptr; }
1885 if (! ($$ = parser.make_classdef_properties_block 1886 | property_list1 opt_sep
1886 ($1, $3, nullptr, $5, $2))) 1887 {
1887 { 1888 YYUSE ($2);
1888 // make_classdef_properties_block delete $3. 1889
1889 YYABORT; 1890 $$ = $1;
1890 } 1891 }
1891 } 1892 ;
1892 ; 1893
1893 1894 property_list1
1894 property_list
1895 : class_property 1895 : class_property
1896 { $$ = new octave::tree_classdef_property_list ($1); } 1896 { $$ = new octave::tree_classdef_property_list ($1); }
1897 | property_list sep class_property 1897 | property_list1 sep class_property
1898 { 1898 {
1899 YYUSE ($2); 1899 YYUSE ($2);
1900 1900
1901 $1->append ($3); 1901 $1->append ($3);
1902 $$ = $1; 1902 $$ = $1;
1912 lexer.m_looking_at_initializer_expression = false; 1912 lexer.m_looking_at_initializer_expression = false;
1913 $$ = new octave::tree_classdef_property ($1, $4); 1913 $$ = new octave::tree_classdef_property ($1, $4);
1914 } 1914 }
1915 ; 1915 ;
1916 1916
1917 methods_block : METHODS stash_comment opt_attr_list opt_sep methods_list opt_sep END 1917 methods_block : METHODS opt_sep stash_comment attr_list methods_list END
1918 { 1918 {
1919 YYUSE ($4); 1919 YYUSE ($2);
1920 YYUSE ($6);
1921 1920
1922 if (! ($$ = parser.make_classdef_methods_block 1921 if (! ($$ = parser.make_classdef_methods_block
1923 ($1, $3, $5, $7, $2))) 1922 ($1, $4, $5, $6, $3)))
1924 { 1923 {
1925 // make_classdef_methods_block deleted $3 and $5. 1924 // make_classdef_methods_block deleted $4 and $5.
1926 YYABORT; 1925 YYABORT;
1927 } 1926 }
1928 } 1927 }
1929 | METHODS stash_comment opt_attr_list opt_sep END
1930 {
1931 YYUSE ($4);
1932
1933 if (! ($$ = parser.make_classdef_methods_block
1934 ($1, $3, nullptr, $5, $2)))
1935 {
1936 // make_classdef_methods_block deleted $3.
1937 YYABORT;
1938 }
1939 }
1940 ;
1941 ; 1928 ;
1942 1929
1943 method_decl1 : identifier 1930 method_decl1 : identifier
1944 { 1931 {
1945 if (! ($$ = parser.start_classdef_external_method ($1, nullptr))) 1932 if (! ($$ = parser.start_classdef_external_method ($1, nullptr)))
1973 { $$ = $1; } 1960 { $$ = $1; }
1974 | function 1961 | function
1975 { $$ = $1; } 1962 { $$ = $1; }
1976 ; 1963 ;
1977 1964
1978 methods_list : method 1965 methods_list : // empty
1966 { $$ = nullptr; }
1967 | methods_list1 opt_sep
1968 {
1969 YYUSE ($2);
1970
1971 $$ = $1;
1972 }
1973 ;
1974
1975 methods_list1 : method
1979 { 1976 {
1980 octave_value fcn; 1977 octave_value fcn;
1981 if ($1) 1978 if ($1)
1982 fcn = $1->function (); 1979 fcn = $1->function ();
1983 delete $1; 1980 delete $1;
1984 $$ = new octave::tree_classdef_methods_list (fcn); 1981 $$ = new octave::tree_classdef_methods_list (fcn);
1985 } 1982 }
1986 | methods_list opt_sep method 1983 | methods_list1 opt_sep method
1987 { 1984 {
1988 YYUSE ($2); 1985 YYUSE ($2);
1989 1986
1990 octave_value fcn; 1987 octave_value fcn;
1991 if ($3) 1988 if ($3)
1995 $1->append (fcn); 1992 $1->append (fcn);
1996 $$ = $1; 1993 $$ = $1;
1997 } 1994 }
1998 ; 1995 ;
1999 1996
2000 events_block : EVENTS stash_comment opt_attr_list opt_sep events_list opt_sep END 1997 events_block : EVENTS opt_sep stash_comment attr_list events_list END
2001 { 1998 {
2002 YYUSE ($4); 1999 YYUSE ($2);
2003 YYUSE ($6);
2004 2000
2005 if (! ($$ = parser.make_classdef_events_block 2001 if (! ($$ = parser.make_classdef_events_block
2006 ($1, $3, $5, $7, $2))) 2002 ($1, $4, $5, $6, $3)))
2007 { 2003 {
2008 // make_classdef_events_block deleted $3 and $5. 2004 // make_classdef_events_block deleted $4 and $5.
2009 YYABORT; 2005 YYABORT;
2010 } 2006 }
2011 } 2007 }
2012 | EVENTS stash_comment opt_attr_list opt_sep END 2008 ;
2013 { 2009
2014 YYUSE ($4); 2010 events_list : // empty
2015 2011 { $$ = nullptr; }
2016 if (! ($$ = parser.make_classdef_events_block 2012 | events_list1 opt_sep
2017 ($1, $3, nullptr, $5, $2))) 2013 {
2014 YYUSE ($2);
2015
2016 $$ = $1;
2017 }
2018 ;
2019
2020 events_list1 : class_event
2021 { $$ = new octave::tree_classdef_events_list ($1); }
2022 | events_list1 opt_sep class_event
2023 {
2024 YYUSE ($2);
2025
2026 $1->append ($3);
2027 $$ = $1;
2028 }
2029 ;
2030
2031 class_event : identifier
2032 { $$ = new octave::tree_classdef_event ($1); }
2033 ;
2034
2035 enum_block : ENUMERATION opt_sep stash_comment attr_list enum_list END
2036 {
2037 YYUSE ($2);
2038
2039 if (! ($$ = parser.make_classdef_enum_block
2040 ($1, $4, $5, $6, $3)))
2018 { 2041 {
2019 // make_classdef_events_block deleted $3. 2042 // make_classdef_enum_block deleted $3 and $4.
2020 YYABORT; 2043 YYABORT;
2021 } 2044 }
2022 } 2045 }
2023 ; 2046 ;
2024 2047
2025 events_list : class_event 2048 enum_list : // empty
2026 { $$ = new octave::tree_classdef_events_list ($1); } 2049 { $$ = nullptr; }
2027 | events_list opt_sep class_event 2050 | enum_list1 opt_sep
2028 { 2051 {
2029 YYUSE ($2); 2052 YYUSE ($2);
2030 2053
2031 $1->append ($3);
2032 $$ = $1; 2054 $$ = $1;
2033 } 2055 }
2034 ; 2056 ;
2035 2057
2036 class_event : identifier 2058 enum_list1 : class_enum
2037 { $$ = new octave::tree_classdef_event ($1); }
2038 ;
2039
2040 enum_block : ENUMERATION stash_comment opt_attr_list opt_sep enum_list opt_sep END
2041 {
2042 YYUSE ($4);
2043 YYUSE ($6);
2044
2045 if (! ($$ = parser.make_classdef_enum_block
2046 ($1, $3, $5, $7, $2)))
2047 {
2048 // make_classdef_enum_block deleted $3 and $5.
2049 YYABORT;
2050 }
2051 }
2052 | ENUMERATION stash_comment opt_attr_list opt_sep END
2053 {
2054 YYUSE ($4);
2055
2056 if (! ($$ = parser.make_classdef_enum_block
2057 ($1, $3, nullptr, $5, $2)))
2058 {
2059 // make_classdef_enum_block deleted $3.
2060 YYABORT;
2061 }
2062 }
2063 ;
2064
2065 enum_list : class_enum
2066 { $$ = new octave::tree_classdef_enum_list ($1); } 2059 { $$ = new octave::tree_classdef_enum_list ($1); }
2067 | enum_list opt_sep class_enum 2060 | enum_list1 opt_sep class_enum
2068 { 2061 {
2069 YYUSE ($2); 2062 YYUSE ($2);
2070 2063
2071 $1->append ($3); 2064 $1->append ($3);
2072 $$ = $1; 2065 $$ = $1;