static int parser_compare_nodes(node *left, node *right) { int j; if (TYPE(left) < TYPE(right)) return (-1); if (TYPE(right) < TYPE(left)) return (1); if (ISTERMINAL(TYPE(left))) return (strcmp(STR(left), STR(right))); if (NCH(left) < NCH(right)) return (-1); if (NCH(right) < NCH(left)) return (1); for (j = 0; j < NCH(left); ++j) { int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j)); if (v != 0) return (v); } return (0); }
static Py_ssize_t sizeofchildren(node *n) { Py_ssize_t res = 0; int i; for (i = NCH(n); --i >= 0; ) res += sizeofchildren(CHILD(n, i)); if (n->n_child != NULL) /* allocated size of n->n_child array */ res += XXXROUNDUP(NCH(n)) * sizeof(node); if (STR(n) != NULL) res += strlen(STR(n)) + 1; return res; }
bool Jet::isGood() const { // bool passesPt = pt() > 30; // Bristol original value bool passesPt = pt() > 35; // 19.07.11 Chris's value bool passesEta = fabs(eta()) < 2.4; bool jetID = false; //if (usedAlgorithm == JetAlgorithm::ParticleFlow || usedAlgorithm == JetAlgorithm::PF2PAT) { if (usedAlgorithm == JetAlgorithm::CA08PF || usedAlgorithm == JetAlgorithm::PF2PAT) { bool passNOD = NOD() > 1; bool passCEF = CEF() < 0.99; bool passNHF = NHF() < 0.99; bool passNEF = NEF() < 0.99; bool passCHF = true; bool passNCH = true; if (fabs(eta()) < 2.4) { passCHF = CHF() > 0; passNCH = NCH() > 0; } jetID = passNOD && passCEF && passNHF && passNEF && passCHF && passNCH; } else{ bool passesEMF = emf() > 0.01; bool passesN90Hits = n90Hits() > 1; bool passesFHPD = fHPD() < 0.98; jetID = passesEMF && passesN90Hits && passesFHPD; } return passesPt && passesEta && jetID; }
static void freechildren(node *n) { int i; for (i = NCH(n); --i >= 0; ) freechildren(CHILD(n, i)); if (n->n_child != NULL) PyObject_FREE(n->n_child); if (STR(n) != NULL) PyObject_FREE(STR(n)); }
static void future_hack(parser_state *ps) { node *n = ps->p_stack.s_top->s_parent; node *ch; int i; if (strcmp(STR(CHILD(n, 0)), "from") != 0) return; ch = CHILD(n, 1); if (strcmp(STR(CHILD(ch, 0)), "__future__") != 0) return; for (i = 3; i < NCH(n); i += 2) { ch = CHILD(n, i); if (NCH(ch) >= 1 && TYPE(CHILD(ch, 0)) == NAME && strcmp(STR(CHILD(ch, 0)), "generators") == 0) { ps->p_generators = 1; break; } } }
int run_prompt() { Node *ptree; AstNode *stree; EmCodeObject *co; Environment *env; EmObject *retval; env = newenv(vm->topenv); while (1) { ptree = parse(); if (ptree) { if (ptree->type != MAGIC_COMMAND) { // printtree(ptree); stree = ast_from_ptree(ptree); // printstree(stree); co = compile_ast_tree(stree); INCREF(&nulobj); retval = run_codeobject(co, env, &nulobj); if (retval) DECREF(retval); vm_reset_for_prompt(); freetree(ptree); freestree(stree); } else { // MAGIC_COMMAND printf("got magic command %d\n", CHILD(ptree,0)->type); if (NCH(ptree) == 2) { printf("magic command arg = %s\n", CHILD(ptree,1)->lexeme); } if (CHILD(ptree,0)->type == MCA_EXIT) { freetree(ptree); // release memory before exit break; } // Always release memory of parse tree freetree(ptree); } } } env_free(env); return 1; }
/* Leaving this in as an example */ static void future_hack(parser_state *ps) { node *n = ps->p_stack.s_top->s_parent; node *ch, *cch; int i; /* from __future__ import ..., must have at least 4 children */ n = CHILD(n, 0); if (NCH(n) < 4) return; ch = CHILD(n, 0); if (STR(ch) == NULL || strcmp(STR(ch), "from") != 0) return; ch = CHILD(n, 1); if (NCH(ch) == 1 && STR(CHILD(ch, 0)) && strcmp(STR(CHILD(ch, 0)), "__future__") != 0) return; ch = CHILD(n, 3); /* ch can be a star, a parenthesis or import_as_names */ if (TYPE(ch) == STAR) return; if (TYPE(ch) == LPAR) ch = CHILD(n, 4); for (i = 0; i < NCH(ch); i += 2) { cch = CHILD(ch, i); if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME) { char *str_ch = STR(CHILD(cch, 0)); if (strcmp(str_ch, FUTURE_WITH_STATEMENT) == 0) { ps->p_flags |= CO_FUTURE_WITH_STATEMENT; } else if (strcmp(str_ch, FUTURE_PRINT_FUNCTION) == 0) { ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; } else if (strcmp(str_ch, FUTURE_UNICODE_LITERALS) == 0) { ps->p_flags |= CO_FUTURE_UNICODE_LITERALS; } } } }
static int push(stack *s, int type, dfa *d, int newstate, int lineno, int col_offset) { int err; node *n; n = s->s_top->s_parent; assert(!s_empty(s)); err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset); if (err) return err; s->s_top->s_state = newstate; return s_push(s, d, CHILD(n, NCH(n)-1)); }
static void freechildren(node *n) { int i; for (i = NCH(n); --i >= 0; ) freechildren(CHILD(n, i)); if (n->n_child != NULL) PyObject_FREE(n->n_child); /* Print statement modification : Free all nodes except those marked modified. Those are virtual nodes in the parse three that don't actually reside in the statement's memory, Freeing them is dangerous. */ if (STR(n) != NULL && (!n->n_wasModified)) PyObject_FREE(STR(n)); }
static int future_check_features(PyFutureFeatures *ff, node *n, const char *filename) { int i; char *feature; node *ch, *nn; REQ(n, import_from); nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR)); if (TYPE(nn) == STAR) { PyErr_SetString(PyExc_SyntaxError, FUTURE_IMPORT_STAR); PyErr_SyntaxLocation(filename, nn->n_lineno); return -1; } REQ(nn, import_as_names); for (i = 0; i < NCH(nn); i += 2) { ch = CHILD(nn, i); REQ(ch, import_as_name); feature = STR(CHILD(ch, 0)); if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { continue; } else if (strcmp(feature, FUTURE_GENERATORS) == 0) { continue; } else if (strcmp(feature, FUTURE_DIVISION) == 0) { ff->ff_features |= CO_FUTURE_DIVISION; } else if (strcmp(feature, "braces") == 0) { PyErr_SetString(PyExc_SyntaxError, "not a chance"); PyErr_SyntaxLocation(filename, CHILD(ch, 0)->n_lineno); return -1; } else { PyErr_Format(PyExc_SyntaxError, UNDEFINED_FUTURE_FEATURE, feature); PyErr_SyntaxLocation(filename, CHILD(ch, 0)->n_lineno); return -1; } } return 0; }
static void list1node(FILE *fp, node *n) { if (n == NULL) return; if (ISNONTERMINAL(TYPE(n))) { int i; for (i = 0; i < NCH(n); i++) list1node(fp, CHILD(n, i)); } else if (ISTERMINAL(TYPE(n))) { switch (TYPE(n)) { case INDENT: ++level; break; case DEDENT: --level; break; default: if (atbol) { int i; for (i = 0; i < level; ++i) fprintf(fp, "\t"); atbol = 0; } if (TYPE(n) == NEWLINE) { if (STR(n) != NULL) fprintf(fp, "%s", STR(n)); fprintf(fp, "\n"); atbol = 1; } else fprintf(fp, "%s ", STR(n)); break; } } else fprintf(fp, "? "); }
static int future_parse(PyFutureFeatures *ff, node *n, const char *filename) { int i, r; loop: switch (TYPE(n)) { case single_input: if (TYPE(CHILD(n, 0)) == simple_stmt) { n = CHILD(n, 0); goto loop; } return 0; case file_input: /* Check each statement in the file, starting with the first, and continuing until the first statement that isn't a future statement. */ for (i = 0; i < NCH(n); i++) { node *ch = CHILD(n, i); if (TYPE(ch) == stmt) { r = future_parse(ff, ch, filename); /* Need to check both conditions below to accomodate doc strings, which causes r < 0. */ if (r < 1 && !FUTURE_POSSIBLE(ff)) return r; } } return 0; case simple_stmt: if (NCH(n) == 2) { REQ(CHILD(n, 0), small_stmt); n = CHILD(n, 0); goto loop; } else { /* Deal with the special case of a series of small statements on a single line. If a future statement follows some other statement, the SyntaxError is raised here. In all other cases, the symtable pass raises the exception. */ int found = 0, end_of_future = 0; for (i = 0; i < NCH(n); i += 2) { if (TYPE(CHILD(n, i)) == small_stmt) { r = future_parse(ff, CHILD(n, i), filename); if (r < 1) end_of_future = 1; else { found = 1; if (end_of_future) { future_error(n, filename); return -1; } } } } /* If we found one and only one, then the current lineno is legal. */ if (found) ff->ff_last_lineno = n->n_lineno + 1; else ff->ff_last_lineno = n->n_lineno; if (end_of_future && found) return 1; else return 0; } case stmt: if (TYPE(CHILD(n, 0)) == simple_stmt) { n = CHILD(n, 0); goto loop; } else if (TYPE(CHILD(n, 0)) == expr_stmt) { n = CHILD(n, 0); goto loop; } else { REQ(CHILD(n, 0), compound_stmt); ff->ff_last_lineno = n->n_lineno; return 0; } case small_stmt: n = CHILD(n, 0); goto loop; case import_stmt: { node *name; n = CHILD(n, 0); if (TYPE(n) != import_from) { ff->ff_last_lineno = n->n_lineno; return 0; } name = CHILD(n, 1); if (strcmp(STR(CHILD(name, 0)), "__future__") != 0) return 0; if (future_check_features(ff, n, filename) < 0) return -1; ff->ff_last_lineno = n->n_lineno + 1; return 1; } /* The cases below -- all of them! -- are necessary to find and skip doc strings. */ case expr_stmt: case testlist: case test: case and_test: case not_test: case comparison: case expr: case xor_expr: case and_expr: case shift_expr: case arith_expr: case term: case factor: case power: if (NCH(n) == 1) { n = CHILD(n, 0); goto loop; } break; case atom: if (TYPE(CHILD(n, 0)) == STRING && ff->ff_found_docstring == 0) { ff->ff_found_docstring = 1; return 0; } ff->ff_last_lineno = n->n_lineno; return 0; default: ff->ff_last_lineno = n->n_lineno; return 0; } return 0; }
static node * parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, int *flags) { parser_state *ps; node *n; int started = 0; growable_int_array type_ignores; if (!growable_int_array_init(&type_ignores, 10)) { err_ret->error = E_NOMEM; Ta27Tokenizer_Free(tok); return NULL; } if ((ps = Ta27Parser_New(g, start)) == NULL) { fprintf(stderr, "no mem for new parser\n"); err_ret->error = E_NOMEM; Ta27Tokenizer_Free(tok); return NULL; } #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD if (*flags & PyPARSE_PRINT_IS_FUNCTION) { ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; } if (*flags & PyPARSE_UNICODE_LITERALS) { ps->p_flags |= CO_FUTURE_UNICODE_LITERALS; } #endif for (;;) { char *a, *b; int type; size_t len; char *str; int col_offset; type = Ta27Tokenizer_Get(tok, &a, &b); if (type == ERRORTOKEN) { err_ret->error = tok->done; break; } if (type == ENDMARKER && started) { type = NEWLINE; /* Add an extra newline */ started = 0; /* Add the right number of dedent tokens, except if a certain flag is given -- codeop.py uses this. */ if (tok->indent && !(*flags & PyPARSE_DONT_IMPLY_DEDENT)) { tok->pendin = -tok->indent; tok->indent = 0; } } else started = 1; len = b - a; /* XXX this may compute NULL - NULL */ str = (char *) PyObject_MALLOC(len + 1); if (str == NULL) { fprintf(stderr, "no mem for next token\n"); err_ret->error = E_NOMEM; break; } if (len > 0) strncpy(str, a, len); str[len] = '\0'; #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD #endif if (a >= tok->line_start) col_offset = a - tok->line_start; else col_offset = -1; if (type == TYPE_IGNORE) { if (!growable_int_array_add(&type_ignores, tok->lineno)) { err_ret->error = E_NOMEM; break; } continue; } if ((err_ret->error = Ta27Parser_AddToken(ps, (int)type, str, tok->lineno, col_offset, &(err_ret->expected))) != E_OK) { if (err_ret->error != E_DONE) { PyObject_FREE(str); err_ret->token = type; } break; } } if (err_ret->error == E_DONE) { n = ps->p_tree; ps->p_tree = NULL; if (n->n_type == file_input) { /* Put type_ignore nodes in the ENDMARKER of file_input. */ int num; node *ch; size_t i; num = NCH(n); ch = CHILD(n, num - 1); REQ(ch, ENDMARKER); for (i = 0; i < type_ignores.num_items; i++) { Ta27Node_AddChild(ch, TYPE_IGNORE, NULL, type_ignores.items[i], 0); } } growable_int_array_deallocate(&type_ignores); } else n = NULL; #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD *flags = ps->p_flags; #endif Ta27Parser_Delete(ps); if (n == NULL) { if (tok->lineno <= 1 && tok->done == E_EOF) err_ret->error = E_EOF; err_ret->lineno = tok->lineno; if (tok->buf != NULL) { char *text = NULL; size_t len; assert(tok->cur - tok->buf < INT_MAX); err_ret->offset = (int)(tok->cur - tok->buf); len = tok->inp - tok->buf; #ifdef Py_USING_UNICODE text = Ta27Tokenizer_RestoreEncoding(tok, len, &err_ret->offset); #endif if (text == NULL) { text = (char *) PyObject_MALLOC(len + 1); if (text != NULL) { if (len > 0) strncpy(text, tok->buf, len); text[len] = '\0'; } } err_ret->text = text; } } else if (tok->encoding != NULL) { /* 'nodes->n_str' uses PyObject_*, while 'tok->encoding' was * allocated using PyMem_ */ node* r = Ta27Node_New(encoding_decl); if (r) r->n_str = PyObject_MALLOC(strlen(tok->encoding)+1); if (!r || !r->n_str) { err_ret->error = E_NOMEM; if (r) PyObject_FREE(r); n = NULL; goto done; } strcpy(r->n_str, tok->encoding); PyMem_FREE(tok->encoding); tok->encoding = NULL; r->n_nchildren = 1; r->n_child = n; n = r; } done: Ta27Tokenizer_Free(tok); return n; }
static PyObject* node2tuple(node *n, /* node to convert */ SeqMaker mkseq, /* create sequence */ SeqInserter addelem, /* func. to add elem. in seq. */ int lineno, /* include line numbers? */ int col_offset) /* include column offsets? */ { PyObject *result = NULL, *w; if (n == NULL) { Py_RETURN_NONE; } if (ISNONTERMINAL(TYPE(n))) { int i; result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl)); if (result == NULL) goto error; w = PyLong_FromLong(TYPE(n)); if (w == NULL) goto error; (void) addelem(result, 0, w); for (i = 0; i < NCH(n); i++) { w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset); if (w == NULL) goto error; (void) addelem(result, i+1, w); } if (TYPE(n) == encoding_decl) { w = PyUnicode_FromString(STR(n)); if (w == NULL) goto error; (void) addelem(result, i+1, w); } } else if (ISTERMINAL(TYPE(n))) { result = mkseq(2 + lineno + col_offset); if (result == NULL) goto error; w = PyLong_FromLong(TYPE(n)); if (w == NULL) goto error; (void) addelem(result, 0, w); w = PyUnicode_FromString(STR(n)); if (w == NULL) goto error; (void) addelem(result, 1, w); if (lineno) { w = PyLong_FromLong(n->n_lineno); if (w == NULL) goto error; (void) addelem(result, 2, w); } if (col_offset) { w = PyLong_FromLong(n->n_col_offset); if (w == NULL) goto error; (void) addelem(result, 2 + lineno, w); } } else { PyErr_SetString(PyExc_SystemError, "unrecognized parse tree node type"); return ((PyObject*) NULL); } return result; error: Py_XDECREF(result); return NULL; }