return ok;} /* decorator: * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */static intvalidate_decorator(node *tree){ int ok; int nch = NCH(tree); ok = (validate_ntype(tree, decorator) && (nch == 3 || nch == 5 || nch == 6) && validate_at(CHILD(tree, 0)) && validate_dotted_name(CHILD(tree, 1)) && validate_newline(RCHILD(tree, -1))); if (ok && nch != 3) { ok = (validate_lparen(CHILD(tree, 2)) && validate_rparen(RCHILD(tree, -2))); if (ok && nch == 6) ok = validate_arglist(CHILD(tree, 3)); } return ok;} /* decorators: * decorator+ */static intvalidate_decorators(node *tree){ int i, nch, ok; nch = NCH(tree); ok = validate_ntype(tree, decorators) && nch >= 1; for (i = 0; ok && i < nch; ++i) ok = validate_decorator(CHILD(tree, i)); return ok;} /* funcdef: * * -6 -5 -4 -3 -2 -1 * [decorators] 'def' NAME parameters ':' suite */static intvalidate_funcdef(node *tree){ int nch = NCH(tree); int ok = (validate_ntype(tree, funcdef) && ((nch == 5) || (nch == 6)) && validate_name(RCHILD(tree, -5), "def") && validate_ntype(RCHILD(tree, -4), NAME) && validate_colon(RCHILD(tree, -2)) && validate_parameters(RCHILD(tree, -3)) && validate_suite(RCHILD(tree, -1))); if (ok && (nch == 6)) ok = validate_decorators(CHILD(tree, 0)); return ok;} static intvalidate_lambdef(node *tree){ int nch = NCH(tree); int res = (validate_ntype(tree, lambdef) && ((nch == 3) || (nch == 4)) && validate_name(CHILD(tree, 0), "lambda") && validate_colon(CHILD(tree, nch - 2)) && validate_test(CHILD(tree, nch - 1))); if (res && (nch == 4)) res = validate_varargslist(CHILD(tree, 1)); else if (!res && !PyErr_Occurred()) (void) validate_numnodes(tree, 3, "lambdef"); return (res);} static intvalidate_old_lambdef(node *tree){ int nch = NCH(tree); int res = (validate_ntype(tree, old_lambdef) && ((nch == 3) || (nch == 4)) && validate_name(CHILD(tree, 0), "lambda") && validate_colon(CHILD(tree, nch - 2)) && validate_test(CHILD(tree, nch - 1))); if (res && (nch == 4)) res = validate_varargslist(CHILD(tree, 1)); else if (!res && !PyErr_Occurred()) (void) validate_numnodes(tree, 3, "old_lambdef"); return (res);} /* arglist: * * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test) */static intvalidate_arglist(node *tree){ int nch = NCH(tree); int i = 0; int ok = 1; if (nch <= 0) /* raise the right error from having an invalid number of children */ return validate_numnodes(tree, nch + 1, "arglist"); if (nch > 1) { for (i=0; i<nch; i++) { if (TYPE(CHILD(tree, i)) == argument) { node *ch = CHILD(tree, i); if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) { err_string("need '(', ')' for generator expression"); return 0; } } } } while (ok && nch-i >= 2) { /* skip leading (argument ',') */ ok = (validate_argument(CHILD(tree, i)) && validate_comma(CHILD(tree, i+1))); if (ok) i += 2; else PyErr_Clear(); } ok = 1; if (nch-i > 0) { /* * argument | '*' test [',' '**' test] | '**' test */ int sym = TYPE(CHILD(tree, i)); if (sym == argument) { ok = validate_argument(CHILD(tree, i)); if (ok && i+1 != nch) { err_string("illegal arglist specification" " (extra stuff on end)"); ok = 0; } } else if (sym == STAR) { ok = validate_star(CHILD(tree, i)); if (ok && (nch-i == 2)) ok = validate_test(CHILD(tree, i+1)); else if (ok && (nch-i == 5)) ok = (validate_test(CHILD(tree, i+1)) && validate_comma(CHILD(tree, i+2)) && validate_doublestar(CHILD(tree, i+3)) && validate_test(CHILD(tree, i+4))); else { err_string("illegal use of '*' in arglist"); ok = 0; } } else if (sym == DOUBLESTAR) { if (nch-i == 2) ok = (validate_doublestar(CHILD(tree, i)) && validate_test(CHILD(tree, i+1))); else { err_string("illegal use of '**' in arglist"); ok = 0; } } else { err_string("illegal arglist specification"); ok = 0; } } return (ok);} /* argument: * * [test '='] test [gen_for] */static intvalidate_argument(node *tree){ int nch = NCH(tree); int res = (validate_ntype(tree, argument) && ((nch == 1) || (nch == 2) || (nch == 3)) && validate_test(CHILD(tree, 0))); if (res && (nch == 2)) res = validate_gen_for(CHILD(tree, 1)); else if (res && (nch == 3)) res = (validate_equal(CHILD(tree, 1)) && validate_test(CHILD(tree, 2))); return (res);} /* trailer: * * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */static intvalidate_trailer(node *tree){ int nch = NCH(tree); int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3)); if (res) { switch (TYPE(CHILD(tree, 0))) { case LPAR: res = validate_rparen(CHILD(tree, nch - 1)); if (res && (nch == 3)) res = validate_arglist(CHILD(tree, 1)); break; case LSQB: res = (validate_numnodes(tree, 3, "trailer") && validate_subscriptlist(CHILD(tree, 1)) && validate_ntype(CHILD(tree, 2), RSQB)); break; case DOT: res = (validate_numnodes(tree, 2, "trailer") && validate_ntype(CHILD(tree, 1), NAME)); break; default: res = 0; break; } } else { (void) validate_numnodes(tree, 2, "trailer"); } return (res);} /* subscriptlist: * * subscript (',' subscript)* [','] */static intvalidate_subscriptlist(node *tree){ return (validate_repeating_list(tree, subscriptlist, validate_subscript, "subscriptlist"));} /* subscript: * * '.' '.' '.' | test | [test] ':' [test] [sliceop] */static intvalidate_subscript(node *tree){ int offset = 0; int nch = NCH(tree); int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4); if (!res) { if (!PyErr_Occurred()) err_string("invalid number of arguments for subscript node"); return (0); } if (TYPE(CHILD(tree, 0)) == DOT) /* take care of ('.' '.' '.') possibility */ return (validate_numnodes(tree, 3, "subscript") && validate_dot(CHILD(tree, 0)) && validate_dot(CHILD(tree, 1)) && validate_dot(CHILD(tree, 2))); if (nch == 1) { if (TYPE(CHILD(tree, 0)) == test) res = validate_test(CHILD(tree, 0)); else res = validate_colon(CHILD(tree, 0)); return (res); } /* Must be [test] ':' [test] [sliceop], * but at least one of the optional components will * be present, but we don't know which yet. */ if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) { res = validate_test(CHILD(tree, 0)); offset = 1; } if (res) res = validate_colon(CHILD(tree, offset)); if (res) { int rem = nch - ++offset; if (rem) { if (TYPE(CHILD(tree, offset)) == test) { res = validate_test(CHILD(tree, offset)); ++offset; --rem; } if (res && rem) res = validate_sliceop(CHILD(tree, offset)); } } return (res);} static intvalidate_sliceop(node *tree){ int nch = NCH(tree); int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop")) && validate_ntype(tree, sliceop); if (!res && !PyErr_Occurred()) { res = validate_numnodes(tree, 1, "sliceop"); } if (res) res = validate_colon(CHILD(tree, 0)); if (res && (nch == 2)) res = validate_test(CHILD(tree, 1)); return (res);} static intvalidate_exprlist(node *tree){ return (validate_repeating_list(tree, exprlist, validate_expr, "exprlist"));} static intvalidate_dictmaker(node *tree){ int nch = NCH(tree); int res = (validate_ntype(tree, dictmaker) && (nch >= 3) && validate_test(CHILD(tree, 0)) && validate_colon(CHILD(tree, 1)) && validate_test(CHILD(tree, 2))); if (res && ((nch % 4) == 0)) res = validate_comma(CHILD(tree, --nch)); else if (res) res = ((nch % 4) == 3); if (res && (nch > 3)) { int pos = 3; /* ( ',' test ':' test )* */ while (res && (pos < nch)) { res = (validate_comma(CHILD(tree, pos)) && validate_test(CHILD(tree, pos + 1)) && validate_colon(CHILD(tree, pos + 2)) && validate_test(CHILD(tree, pos + 3))); pos += 4; } } return (res);} static intvalidate_eval_input(node *tree){ int pos; int nch = NCH(tree); int res = (validate_ntype(tree, eval_input) && (nch >= 2) && validate_testlist(CHILD(tree, 0)) && validate_ntype(CHILD(tree, nch - 1), ENDMARKER)); for (pos = 1; res && (pos < (nch - 1)); ++pos) res = validate_ntype(CHILD(tree, pos), NEWLINE); return (res);} static intvalidate_node(node *tree){ int nch = 0; /* num. children on current node */ int res = 1; /* result value */ node* next = 0; /* node to process after this one */ while (res && (tree != 0)) { nch = NCH(tree); next = 0; switch (TYPE(tree)) { /* * Definition nodes. */ case funcdef: res = validate_funcdef(tree); break; case classdef: res = validate_class(tree); break; /* * "Trivial" parse tree nodes. * (Why did I call these trivial?) */ case stmt: res = validate_stmt(tree); break; case small_stmt: /* * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt * | import_stmt | global_stmt | exec_stmt | assert_stmt */ res = validate_small_stmt(tree); break; case flow_stmt: res = (validate_numnodes(tree, 1, "flow_stmt") && ((TYPE(CHILD(tree, 0)) == break_stmt) || (TYPE(CHILD(tree, 0)) == continue_stmt) || (TYPE(CHILD(tree, 0)) == yield_stmt) || (TYPE(CHILD(tree, 0)) == return_stmt) || (TYPE(CHILD(tree, 0)) == raise_stmt))); if (res) next = CHILD(tree, 0); else if (nch == 1) err_string("illegal flow_stmt type"); break; case yield_stmt: res = validate_yield_stmt(tree); break; /* * Compound statements. */ case simple_stmt: res = validate_simple_stmt(tree); break; case compound_stmt: res = validate_compound_stmt(tree); break; /* * Fundamental statements. */ case expr_stmt: res = validate_expr_stmt(tree); break; case print_stmt: res = validate_print_stmt(tree); break; case del_stmt: res = validate_del_stmt(tree); break; case pass_stmt: res = (validate_numnodes(tree, 1, "pass") && validate_name(CHILD(tree, 0), "pass")); break; case break_stmt: res = (validate_numnodes(tree, 1, "break") && validate_name(CHILD(tree, 0), "break")); break; case continue_stmt: res = (validate_numnodes(tree, 1, "continue") && validate_name(CHILD(tree, 0), "continue")); break; case return_stmt: res = validate_return_stmt(tree); break; case raise_stmt: res = validate_raise_stmt(tree); break; case import_stmt: res = validate_import_stmt(tree); break; case import_name: res = validate_import_name(tree); break; case import_from: res = validate_import_from(tree); break; case global_stmt: res = validate_global_stmt(tree); break; case exec_stmt: res = validate_exec_stmt(tree); break; case assert_stmt: res = validate_assert_stmt(tree); break; case if_stmt: res = validate_if(tree); break;Lines 2501–3000 of 3279. Previous lines Load more
return ok; } /* decorator: * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ static int validate_decorator(node *tree) { int ok; int nch = NCH(tree); ok = (validate_ntype(tree, decorator) && (nch == 3 || nch == 5 || nch == 6) && validate_at(CHILD(tree, 0)) && validate_dotted_name(CHILD(tree, 1)) && validate_newline(RCHILD(tree, -1))); if (ok && nch != 3) { ok = (validate_lparen(CHILD(tree, 2)) && validate_rparen(RCHILD(tree, -2))); if (ok && nch == 6) ok = validate_arglist(CHILD(tree, 3)); } return ok; } /* decorators: * decorator+ */ static int validate_decorators(node *tree) { int i, nch, ok; nch = NCH(tree); ok = validate_ntype(tree, decorators) && nch >= 1; for (i = 0; ok && i < nch; ++i) ok = validate_decorator(CHILD(tree, i)); return ok; } /* funcdef: * * -6 -5 -4 -3 -2 -1 * [decorators] 'def' NAME parameters ':' suite */ static int validate_funcdef(node *tree) { int nch = NCH(tree); int ok = (validate_ntype(tree, funcdef) && ((nch == 5) || (nch == 6)) && validate_name(RCHILD(tree, -5), "def") && validate_ntype(RCHILD(tree, -4), NAME) && validate_colon(RCHILD(tree, -2)) && validate_parameters(RCHILD(tree, -3)) && validate_suite(RCHILD(tree, -1))); if (ok && (nch == 6)) ok = validate_decorators(CHILD(tree, 0)); return ok; } static int validate_lambdef(node *tree) { int nch = NCH(tree); int res = (validate_ntype(tree, lambdef) && ((nch == 3) || (nch == 4)) && validate_name(CHILD(tree, 0), "lambda") && validate_colon(CHILD(tree, nch - 2)) && validate_test(CHILD(tree, nch - 1))); if (res && (nch == 4)) res = validate_varargslist(CHILD(tree, 1)); else if (!res && !PyErr_Occurred()) (void) validate_numnodes(tree, 3, "lambdef"); return (res); } static int validate_old_lambdef(node *tree) { int nch = NCH(tree); int res = (validate_ntype(tree, old_lambdef) && ((nch == 3) || (nch == 4)) && validate_name(CHILD(tree, 0), "lambda") && validate_colon(CHILD(tree, nch - 2)) && validate_test(CHILD(tree, nch - 1))); if (res && (nch == 4)) res = validate_varargslist(CHILD(tree, 1)); else if (!res && !PyErr_Occurred()) (void) validate_numnodes(tree, 3, "old_lambdef"); return (res); } /* arglist: * * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test) */ static int validate_arglist(node *tree) { int nch = NCH(tree); int i = 0; int ok = 1; if (nch <= 0) /* raise the right error from having an invalid number of children */ return validate_numnodes(tree, nch + 1, "arglist"); if (nch > 1) { for (i=0; i<nch; i++) { if (TYPE(CHILD(tree, i)) == argument) { node *ch = CHILD(tree, i); if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) { err_string("need '(', ')' for generator expression"); return 0; } } } } while (ok && nch-i >= 2) { /* skip leading (argument ',') */ ok = (validate_argument(CHILD(tree, i)) && validate_comma(CHILD(tree, i+1))); if (ok) i += 2; else PyErr_Clear(); } ok = 1; if (nch-i > 0) { /* * argument | '*' test [',' '**' test] | '**' test */ int sym = TYPE(CHILD(tree, i)); if (sym == argument) { ok = validate_argument(CHILD(tree, i)); if (ok && i+1 != nch) { err_string("illegal arglist specification" " (extra stuff on end)"); ok = 0; } } else if (sym == STAR) { ok = validate_star(CHILD(tree, i)); if (ok && (nch-i == 2)) ok = validate_test(CHILD(tree, i+1)); else if (ok && (nch-i == 5)) ok = (validate_test(CHILD(tree, i+1)) && validate_comma(CHILD(tree, i+2)) && validate_doublestar(CHILD(tree, i+3)) && validate_test(CHILD(tree, i+4))); else { err_string("illegal use of '*' in arglist"); ok = 0; } } else if (sym == DOUBLESTAR) { if (nch-i == 2) ok = (validate_doublestar(CHILD(tree, i)) && validate_test(CHILD(tree, i+1))); else { err_string("illegal use of '**' in arglist"); ok = 0; } } else { err_string("illegal arglist specification"); ok = 0; } } return (ok); } /* argument: * * [test '='] test [gen_for] */ static int validate_argument(node *tree) { int nch = NCH(tree); int res = (validate_ntype(tree, argument) && ((nch == 1) || (nch == 2) || (nch == 3)) && validate_test(CHILD(tree, 0))); if (res && (nch == 2)) res = validate_gen_for(CHILD(tree, 1)); else if (res && (nch == 3)) res = (validate_equal(CHILD(tree, 1)) && validate_test(CHILD(tree, 2))); return (res); } /* trailer: * * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */ static int validate_trailer(node *tree) { int nch = NCH(tree); int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3)); if (res) { switch (TYPE(CHILD(tree, 0))) { case LPAR: res = validate_rparen(CHILD(tree, nch - 1)); if (res && (nch == 3)) res = validate_arglist(CHILD(tree, 1)); break; case LSQB: res = (validate_numnodes(tree, 3, "trailer") && validate_subscriptlist(CHILD(tree, 1)) && validate_ntype(CHILD(tree, 2), RSQB)); break; case DOT: res = (validate_numnodes(tree, 2, "trailer") && validate_ntype(CHILD(tree, 1), NAME)); break; default: res = 0; break; } } else { (void) validate_numnodes(tree, 2, "trailer"); } return (res); } /* subscriptlist: * * subscript (',' subscript)* [','] */ static int validate_subscriptlist(node *tree) { return (validate_repeating_list(tree, subscriptlist, validate_subscript, "subscriptlist")); } /* subscript: * * '.' '.' '.' | test | [test] ':' [test] [sliceop] */ static int validate_subscript(node *tree) { int offset = 0; int nch = NCH(tree); int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4); if (!res) { if (!PyErr_Occurred()) err_string("invalid number of arguments for subscript node"); return (0); } if (TYPE(CHILD(tree, 0)) == DOT) /* take care of ('.' '.' '.') possibility */ return (validate_numnodes(tree, 3, "subscript") && validate_dot(CHILD(tree, 0)) && validate_dot(CHILD(tree, 1)) && validate_dot(CHILD(tree, 2))); if (nch == 1) { if (TYPE(CHILD(tree, 0)) == test) res = validate_test(CHILD(tree, 0)); else res = validate_colon(CHILD(tree, 0)); return (res); } /* Must be [test] ':' [test] [sliceop], * but at least one of the optional components will * be present, but we don't know which yet. */ if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) { res = validate_test(CHILD(tree, 0)); offset = 1; } if (res) res = validate_colon(CHILD(tree, offset)); if (res) { int rem = nch - ++offset; if (rem) { if (TYPE(CHILD(tree, offset)) == test) { res = validate_test(CHILD(tree, offset)); ++offset; --rem; } if (res && rem) res = validate_sliceop(CHILD(tree, offset)); } } return (res); } static int validate_sliceop(node *tree) { int nch = NCH(tree); int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop")) && validate_ntype(tree, sliceop); if (!res && !PyErr_Occurred()) { res = validate_numnodes(tree, 1, "sliceop"); } if (res) res = validate_colon(CHILD(tree, 0)); if (res && (nch == 2)) res = validate_test(CHILD(tree, 1)); return (res); } static int validate_exprlist(node *tree) { return (validate_repeating_list(tree, exprlist, validate_expr, "exprlist")); } static int validate_dictmaker(node *tree) { int nch = NCH(tree); int res = (validate_ntype(tree, dictmaker) && (nch >= 3) && validate_test(CHILD(tree, 0)) && validate_colon(CHILD(tree, 1)) && validate_test(CHILD(tree, 2))); if (res && ((nch % 4) == 0)) res = validate_comma(CHILD(tree, --nch)); else if (res) res = ((nch % 4) == 3); if (res && (nch > 3)) { int pos = 3; /* ( ',' test ':' test )* */ while (res && (pos < nch)) { res = (validate_comma(CHILD(tree, pos)) && validate_test(CHILD(tree, pos + 1)) && validate_colon(CHILD(tree, pos + 2)) && validate_test(CHILD(tree, pos + 3))); pos += 4; } } return (res); } static int validate_eval_input(node *tree) { int pos; int nch = NCH(tree); int res = (validate_ntype(tree, eval_input) && (nch >= 2) && validate_testlist(CHILD(tree, 0)) && validate_ntype(CHILD(tree, nch - 1), ENDMARKER)); for (pos = 1; res && (pos < (nch - 1)); ++pos) res = validate_ntype(CHILD(tree, pos), NEWLINE); return (res); } static int validate_node(node *tree) { int nch = 0; /* num. children on current node */ int res = 1; /* result value */ node* next = 0; /* node to process after this one */ while (res && (tree != 0)) { nch = NCH(tree); next = 0; switch (TYPE(tree)) { /* * Definition nodes. */ case funcdef: res = validate_funcdef(tree); break; case classdef: res = validate_class(tree); break; /* * "Trivial" parse tree nodes. * (Why did I call these trivial?) */ case stmt: res = validate_stmt(tree); break; case small_stmt: /* * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt * | import_stmt | global_stmt | exec_stmt | assert_stmt */ res = validate_small_stmt(tree); break; case flow_stmt: res = (validate_numnodes(tree, 1, "flow_stmt") && ((TYPE(CHILD(tree, 0)) == break_stmt) || (TYPE(CHILD(tree, 0)) == continue_stmt) || (TYPE(CHILD(tree, 0)) == yield_stmt) || (TYPE(CHILD(tree, 0)) == return_stmt) || (TYPE(CHILD(tree, 0)) == raise_stmt))); if (res) next = CHILD(tree, 0); else if (nch == 1) err_string("illegal flow_stmt type"); break; case yield_stmt: res = validate_yield_stmt(tree); break; /* * Compound statements. */ case simple_stmt: res = validate_simple_stmt(tree); break; case compound_stmt: res = validate_compound_stmt(tree); break; /* * Fundamental statements. */ case expr_stmt: res = validate_expr_stmt(tree); break; case print_stmt: res = validate_print_stmt(tree); break; case del_stmt: res = validate_del_stmt(tree); break; case pass_stmt: res = (validate_numnodes(tree, 1, "pass") && validate_name(CHILD(tree, 0), "pass")); break; case break_stmt: res = (validate_numnodes(tree, 1, "break") && validate_name(CHILD(tree, 0), "break")); break; case continue_stmt: res = (validate_numnodes(tree, 1, "continue") && validate_name(CHILD(tree, 0), "continue")); break; case return_stmt: res = validate_return_stmt(tree); break; case raise_stmt: res = validate_raise_stmt(tree); break; case import_stmt: res = validate_import_stmt(tree); break; case import_name: res = validate_import_name(tree); break; case import_from: res = validate_import_from(tree); break; case global_stmt: res = validate_global_stmt(tree); break; case exec_stmt: res = validate_exec_stmt(tree); break; case assert_stmt: res = validate_assert_stmt(tree); break; case if_stmt: res = validate_if(tree); break;