|
1534 | 1534 | " # https://docs.python.org/3/reference/lexical_analysis.html#identifiers\n",
|
1535 | 1535 | "\n",
|
1536 | 1536 | " # Function Calls\n",
|
1537 |
| - " '<Call>': [ 'Call(func=<func>, args=<expr_list>, keywords=<keyword_list>)' ],\n", |
1538 |
| - " '<func>': [ '<expr>' ], # Actually <Expr>, but this is more readable and parses 90%\n", |
| 1537 | + " '<Call>': [ 'Call(func=<func><args_param><keywords_param>)' ],\n", |
| 1538 | + " '<args_param>': [ ', args=<expr_list>' ],\n", |
| 1539 | + " '<keywords_param>': [ ', keywords=<keyword_list>' ],\n", |
| 1540 | + " '<func>': [ '<expr>' ], # Actually <Expr>, but this is more readable and parses 90%\n", |
1539 | 1541 | " '<keyword_list>': [ '[<keywords>?]' ],\n",
|
1540 | 1542 | " '<keywords>': [ '<keyword>', '<keyword>, <keywords>' ],\n",
|
1541 | 1543 | " '<keyword>': [ 'keyword(arg=<identifier>, value=<expr>)' ]\n",
|
1542 | 1544 | "})"
|
1543 | 1545 | ]
|
1544 | 1546 | },
|
| 1547 | + { |
| 1548 | + "cell_type": "code", |
| 1549 | + "execution_count": null, |
| 1550 | + "id": "4505b2fe", |
| 1551 | + "metadata": {}, |
| 1552 | + "outputs": [], |
| 1553 | + "source": [ |
| 1554 | + "# do import this unconditionally\n", |
| 1555 | + "if sys.version_info >= (3, 13):\n", |
| 1556 | + " PYTHON_AST_IDS_GRAMMAR: Grammar = extend_grammar(PYTHON_AST_IDS_GRAMMAR, {\n", |
| 1557 | + " # As of 3.13, args and keywords parameters are optional\n", |
| 1558 | + " '<Call>': [ 'Call(func=<func><args_param>?<keywords_param>?)' ],\n", |
| 1559 | + " })" |
| 1560 | + ] |
| 1561 | + }, |
1545 | 1562 | {
|
1546 | 1563 | "cell_type": "code",
|
1547 | 1564 | "execution_count": null,
|
|
1615 | 1632 | "metadata": {},
|
1616 | 1633 | "outputs": [],
|
1617 | 1634 | "source": [
|
1618 |
| - "call_str = ast.dump(ast.parse('open()').body[0].value) # type: ignore\n", |
| 1635 | + "call_str = ast.dump(ast.parse('open(\"foo.txt\", \"r\")').body[0].value) # type: ignore\n", |
| 1636 | + "print(call_str)\n", |
1619 | 1637 | "call_solver = ISLaSolver(ast_ids_grammar)\n",
|
1620 | 1638 | "assert call_solver.check(call_str)"
|
1621 | 1639 | ]
|
|
1880 | 1898 | " ],\n",
|
1881 | 1899 | "\n",
|
1882 | 1900 | " '<If>': [\n",
|
1883 |
| - " 'If(test=<expr>, body=<nonempty_stmt_list>, orelse=<stmt_list>)'\n", |
| 1901 | + " 'If(test=<expr>, body=<nonempty_stmt_list><orelse_param>)'\n", |
| 1902 | + " ],\n", |
| 1903 | + " '<orelse_param>': [\n", |
| 1904 | + " ', orelse=<stmt_list>'\n", |
1884 | 1905 | " ],\n",
|
1885 | 1906 | "\n",
|
1886 | 1907 | " '<With>': [\n",
|
|
1909 | 1930 | " '<Break>': [ 'Break()' ],\n",
|
1910 | 1931 | " '<Continue>': [ 'Continue()']\n",
|
1911 | 1932 | "\n",
|
1912 |
| - " # FIXME: A few more: AsyncFor, AsyncWith, Match, Try, TryStar, With\n", |
| 1933 | + " # FIXME: A few more: AsyncFor, AsyncWith, Match, Try, TryStar\n", |
1913 | 1934 | " # Import, ImportFrom, Global, Nonlocal...\n",
|
1914 | 1935 | "})"
|
1915 | 1936 | ]
|
1916 | 1937 | },
|
| 1938 | + { |
| 1939 | + "cell_type": "code", |
| 1940 | + "execution_count": null, |
| 1941 | + "id": "5db1c800", |
| 1942 | + "metadata": {}, |
| 1943 | + "outputs": [], |
| 1944 | + "source": [ |
| 1945 | + "# do import this unconditionally\n", |
| 1946 | + "if sys.version_info >= (3, 13):\n", |
| 1947 | + " PYTHON_AST_STMTS_GRAMMAR: Grammar = \\\n", |
| 1948 | + " extend_grammar(PYTHON_AST_STMTS_GRAMMAR, {\n", |
| 1949 | + " # As of 3.13, orelse is optional\n", |
| 1950 | + " '<If>': [\n", |
| 1951 | + " 'If(test=<expr>, body=<nonempty_stmt_list><orelse_param>?)'\n", |
| 1952 | + " ],\n", |
| 1953 | + " })" |
| 1954 | + ] |
| 1955 | + }, |
1917 | 1956 | {
|
1918 | 1957 | "cell_type": "code",
|
1919 | 1958 | "execution_count": null,
|
|
1969 | 2008 | "source": [
|
1970 | 2009 | "python_ast_stmts_grammar = convert_ebnf_grammar(PYTHON_AST_STMTS_GRAMMAR)\n",
|
1971 | 2010 | "with_tree_str = ast.dump(with_tree.body[0]) # get the `With(...)` subtree\n",
|
| 2011 | + "print(with_tree_str)\n", |
1972 | 2012 | "with_solver = ISLaSolver(python_ast_stmts_grammar)\n",
|
1973 | 2013 | "assert with_solver.check(with_tree_str)"
|
1974 | 2014 | ]
|
|
2031 | 2071 | " '<stmt>': PYTHON_AST_STMTS_GRAMMAR['<stmt>'] + [ '<FunctionDef>' ],\n",
|
2032 | 2072 | "\n",
|
2033 | 2073 | " '<FunctionDef>': [\n",
|
2034 |
| - " 'FunctionDef(name=<identifier>, args=<arguments>, body=<nonempty_stmt_list>, decorator_list=<expr_list><returns>?<type_comment>?)'\n", |
| 2074 | + " 'FunctionDef(name=<identifier>, args=<arguments>, body=<nonempty_stmt_list><decorator_list_param><returns>?<type_comment>?)'\n", |
| 2075 | + " ],\n", |
| 2076 | + " '<decorator_list_param>': [\n", |
| 2077 | + " ', decorator_list=<expr_list>'\n", |
2035 | 2078 | " ],\n",
|
| 2079 | + "\n", |
2036 | 2080 | " '<arguments>': [\n",
|
2037 |
| - " 'arguments(posonlyargs=<arg_list>, args=<arg_list><vararg>?, kwonlyargs=<arg_list>, kw_defaults=<expr_list><kwarg>?, defaults=<expr_list>)'\n", |
| 2081 | + " 'arguments(<posonlyargs_param>args=<arg_list><vararg>?<kwonlyargs_param><kw_defaults_param><kwarg>?<defaults_param>)'\n", |
| 2082 | + " ],\n", |
| 2083 | + " '<posonlyargs_param>': [\n", |
| 2084 | + " 'posonlyargs=<arg_list>, '\n", |
| 2085 | + " ],\n", |
| 2086 | + " '<kwonlyargs_param>': [\n", |
| 2087 | + " ', kwonlyargs=<arg_list>'\n", |
| 2088 | + " ],\n", |
| 2089 | + " '<kw_defaults_param>': [\n", |
| 2090 | + " ', kw_defaults=<expr_list>'\n", |
2038 | 2091 | " ],\n",
|
| 2092 | + " '<defaults_param>': [\n", |
| 2093 | + " ', defaults=<expr_list>'\n", |
| 2094 | + " ],\n", |
| 2095 | + "\n", |
2039 | 2096 | "\n",
|
2040 | 2097 | " '<arg_list>': [ '[<args>?]' ],\n",
|
2041 | 2098 | " '<args>': [ '<arg>', '<arg>, <arg>' ],\n",
|
|
2057 | 2114 | "In Python 3.12 and later, function definitions also have a `type_param` field:"
|
2058 | 2115 | ]
|
2059 | 2116 | },
|
2060 |
| - { |
2061 |
| - "cell_type": "code", |
2062 |
| - "execution_count": null, |
2063 |
| - "id": "ac5c79cf", |
2064 |
| - "metadata": {}, |
2065 |
| - "outputs": [], |
2066 |
| - "source": [ |
2067 |
| - "import sys" |
2068 |
| - ] |
2069 |
| - }, |
2070 | 2117 | {
|
2071 | 2118 | "cell_type": "code",
|
2072 | 2119 | "execution_count": null,
|
|
2078 | 2125 | "if sys.version_info >= (3, 12):\n",
|
2079 | 2126 | " PYTHON_AST_DEFS_GRAMMAR: Grammar = extend_grammar(PYTHON_AST_DEFS_GRAMMAR, {\n",
|
2080 | 2127 | " '<FunctionDef>': [\n",
|
2081 |
| - " 'FunctionDef(name=<identifier>, args=<arguments>, body=<nonempty_stmt_list>, decorator_list=<expr_list><returns>?<type_comment>?<type_params>?)'\n", |
| 2128 | + " 'FunctionDef(name=<identifier>, args=<arguments>, body=<nonempty_stmt_list><decorator_list_param><returns>?<type_comment>?<type_params>?)'\n", |
2082 | 2129 | " ],\n",
|
2083 | 2130 | " '<type_params>': [\n",
|
2084 | 2131 | " ', type_params=<type_param_list>',\n",
|
|
2097 | 2144 | " })"
|
2098 | 2145 | ]
|
2099 | 2146 | },
|
| 2147 | + { |
| 2148 | + "cell_type": "markdown", |
| 2149 | + "id": "02959d01", |
| 2150 | + "metadata": {}, |
| 2151 | + "source": [ |
| 2152 | + "In Python 3.13 and later, several `<FunctionDef>` and `<arguments>` attributes are optional:" |
| 2153 | + ] |
| 2154 | + }, |
| 2155 | + { |
| 2156 | + "cell_type": "code", |
| 2157 | + "execution_count": null, |
| 2158 | + "id": "ee08d09d", |
| 2159 | + "metadata": {}, |
| 2160 | + "outputs": [], |
| 2161 | + "source": [ |
| 2162 | + "# do import this unconditionally\n", |
| 2163 | + "if sys.version_info >= (3, 13):\n", |
| 2164 | + " PYTHON_AST_DEFS_GRAMMAR: Grammar = extend_grammar(PYTHON_AST_DEFS_GRAMMAR, {\n", |
| 2165 | + " '<FunctionDef>': [\n", |
| 2166 | + " 'FunctionDef(name=<identifier>, args=<arguments>, body=<nonempty_stmt_list><decorator_list_param>?<returns>?<type_comment>?<type_params>?)'\n", |
| 2167 | + " ],\n", |
| 2168 | + " '<arguments>': [\n", |
| 2169 | + " 'arguments(<posonlyargs_param>?args=<arg_list><vararg>?<kwonlyargs_param>?<kw_defaults_param>?<kwarg>?<defaults_param>?)'\n", |
| 2170 | + " ],\n", |
| 2171 | + " })" |
| 2172 | + ] |
| 2173 | + }, |
2100 | 2174 | {
|
2101 | 2175 | "cell_type": "code",
|
2102 | 2176 | "execution_count": null,
|
|
2155 | 2229 | "PYTHON_AST_MODULE_GRAMMAR: Grammar = extend_grammar(PYTHON_AST_DEFS_GRAMMAR, {\n",
|
2156 | 2230 | " '<start>': [ '<mod>' ],\n",
|
2157 | 2231 | " '<mod>': [ '<Module>' ],\n",
|
2158 |
| - " '<Module>': [ 'Module(body=<nonempty_stmt_list>, type_ignores=<type_ignore_list>)'],\n", |
| 2232 | + " '<Module>': [ 'Module(body=<nonempty_stmt_list><type_ignore_param>)'],\n", |
2159 | 2233 | "\n",
|
| 2234 | + " '<type_ignore_param>': [ ', type_ignores=<type_ignore_list>' ],\n", |
2160 | 2235 | " '<type_ignore_list>': [ '[<type_ignores>?]' ],\n",
|
2161 | 2236 | " '<type_ignores>': [ '<type_ignore>', '<type_ignore>, <type_ignore>' ],\n",
|
2162 | 2237 | " '<type_ignore>': [ 'TypeIgnore(lineno=<integer>, tag=<string>)' ],\n",
|
2163 | 2238 | "})"
|
2164 | 2239 | ]
|
2165 | 2240 | },
|
| 2241 | + { |
| 2242 | + "cell_type": "code", |
| 2243 | + "execution_count": null, |
| 2244 | + "id": "ef453b00", |
| 2245 | + "metadata": {}, |
| 2246 | + "outputs": [], |
| 2247 | + "source": [ |
| 2248 | + "# do import this unconditionally\n", |
| 2249 | + "if sys.version_info >= (3, 13):\n", |
| 2250 | + " PYTHON_AST_MODULE_GRAMMAR: Grammar = \\\n", |
| 2251 | + " extend_grammar(PYTHON_AST_MODULE_GRAMMAR, {\n", |
| 2252 | + " # As of 3.13, the type_ignore parameter is optional\n", |
| 2253 | + " '<Module>': [ 'Module(body=<nonempty_stmt_list><type_ignore_param>?)'],\n", |
| 2254 | + " })" |
| 2255 | + ] |
| 2256 | + }, |
2166 | 2257 | {
|
2167 | 2258 | "cell_type": "code",
|
2168 | 2259 | "execution_count": null,
|
|
2751 | 2842 | "outputs": [],
|
2752 | 2843 | "source": [
|
2753 | 2844 | "solver = ISLaSolver(python_ast_grammar)\n",
|
2754 |
| - "solver.check(sum_str)" |
| 2845 | + "assert solver.check(sum_str)" |
2755 | 2846 | ]
|
2756 | 2847 | },
|
2757 | 2848 | {
|
|
4180 | 4271 | ],
|
4181 | 4272 | "metadata": {
|
4182 | 4273 | "kernelspec": {
|
4183 |
| - "display_name": "Python 3 (ipykernel)", |
| 4274 | + "display_name": "python3.11", |
4184 | 4275 | "language": "python",
|
4185 | 4276 | "name": "python3"
|
4186 | 4277 | },
|
|
4194 | 4285 | "name": "python",
|
4195 | 4286 | "nbconvert_exporter": "python",
|
4196 | 4287 | "pygments_lexer": "ipython3",
|
4197 |
| - "version": "3.12.1" |
| 4288 | + "version": "3.11.10" |
4198 | 4289 | }
|
4199 | 4290 | },
|
4200 | 4291 | "nbformat": 4,
|
|
0 commit comments