/** * Add an open paren after first and add a close paren before the last */ static void add_parens_between(chunk_t *first, chunk_t *last) { chunk_t pc; chunk_t *first_n; chunk_t *last_p; chunk_t *tmp; LOG_FMT(LPARADD, "%s: line %d between %.*s [lvl=%d] and %.*s [lvl=%d]\n", __func__, first->orig_line, first->len, first->str, first->level, last->len, last->str, last->level); /* Don't do anything if we have a bad sequence, ie "&& )" */ first_n = chunk_get_next_ncnl(first); if (first_n == last) { return; } memset(&pc, 0, sizeof(pc)); pc.type = CT_PAREN_OPEN; pc.str = "("; pc.len = 1; pc.flags = first_n->flags & PCF_COPY_FLAGS; pc.level = first_n->level; pc.pp_level = first_n->pp_level; pc.brace_level = first_n->brace_level; chunk_add_before(&pc, first_n); last_p = chunk_get_prev_ncnl(last, CNAV_PREPROC); pc.type = CT_PAREN_CLOSE; pc.str = ")"; pc.flags = last_p->flags & PCF_COPY_FLAGS; pc.level = last_p->level; pc.pp_level = last_p->pp_level; pc.brace_level = last_p->brace_level; chunk_add_after(&pc, last_p); for (tmp = first_n; tmp != last_p; tmp = chunk_get_next_ncnl(tmp)) { tmp->level++; } last_p->level++; }
static void add_parens_between(chunk_t *first, chunk_t *last) { LOG_FUNC_ENTRY(); LOG_FMT(LPARADD, "%s: line %zu between %s [lvl=%zu] and %s [lvl=%zu]\n", __func__, first->orig_line, first->text(), first->level, last->text(), last->level); // Don't do anything if we have a bad sequence, ie "&& )" chunk_t *first_n = chunk_get_next_ncnl(first); if (first_n == last) { return; } chunk_t pc; pc.type = CT_PAREN_OPEN; pc.str = "("; pc.flags = first_n->flags & PCF_COPY_FLAGS; pc.level = first_n->level; pc.pp_level = first_n->pp_level; pc.brace_level = first_n->brace_level; chunk_add_before(&pc, first_n); chunk_t *last_p = chunk_get_prev_ncnl(last, scope_e::PREPROC); pc.type = CT_PAREN_CLOSE; pc.str = ")"; pc.flags = last_p->flags & PCF_COPY_FLAGS; pc.level = last_p->level; pc.pp_level = last_p->pp_level; pc.brace_level = last_p->brace_level; chunk_add_after(&pc, last_p); for (chunk_t *tmp = first_n; tmp != last_p; tmp = chunk_get_next_ncnl(tmp)) { tmp->level++; } last_p->level++; } // add_parens_between
/** * This function parses or tokenizes the whole buffer into a list. * It has to do some tricks to parse preprocessors. * * If output_text() were called immediately after, two things would happen: * - trailing whitespace are removed. * - leading space & tabs are converted to the appropriate format. * * All the tokens are inserted before ref. If ref is NULL, they are inserted * at the end of the list. Line numbers are relative to the start of the data. */ void tokenize(const deque<int>& data, chunk_t *ref) { tok_ctx ctx(data); chunk_t chunk; chunk_t *pc = NULL; chunk_t *rprev = NULL; struct parse_frame frm; bool last_was_tab = false; int prev_sp = 0; memset(&frm, 0, sizeof(frm)); while (ctx.more()) { chunk.reset(); if (!parse_next(ctx, chunk)) { LOG_FMT(LERR, "%s:%d Bailed before the end?\n", cpd.filename, ctx.c.row); cpd.error_count++; break; } /* Don't create an entry for whitespace */ if (chunk.type == CT_WHITESPACE) { last_was_tab = chunk.after_tab; prev_sp = chunk.orig_prev_sp; continue; } chunk.orig_prev_sp = prev_sp; prev_sp = 0; if (chunk.type == CT_NEWLINE) { last_was_tab = chunk.after_tab; chunk.after_tab = false; chunk.str.clear(); } else if (chunk.type == CT_NL_CONT) { last_was_tab = chunk.after_tab; chunk.after_tab = false; chunk.str = "\\\n"; } else { chunk.after_tab = last_was_tab; last_was_tab = false; } /* Strip trailing whitespace (for CPP comments and PP blocks) */ while ((chunk.str.size() > 0) && ((chunk.str[chunk.str.size() - 1] == ' ') || (chunk.str[chunk.str.size() - 1] == '\t'))) { // If comment contains backslash '\' followed by whitespace chars, keep last one; // this will prevent it from turning '\' into line continuation. if ((chunk.str.size() > 1) && (chunk.str[chunk.str.size() - 2] == '\\')) { break; } chunk.str.pop_back(); } /* Store off the end column */ chunk.orig_col_end = ctx.c.col; /* Add the chunk to the list */ rprev = pc; if (rprev != NULL) { chunk_flags_set(pc, rprev->flags & PCF_COPY_FLAGS); /* a newline can't be in a preprocessor */ if (pc->type == CT_NEWLINE) { chunk_flags_clr(pc, PCF_IN_PREPROC); } } if (ref != NULL) { chunk.flags |= PCF_INSERTED; } else { chunk.flags &= ~PCF_INSERTED; } pc = chunk_add_before(&chunk, ref); /* A newline marks the end of a preprocessor */ if (pc->type == CT_NEWLINE) // || (pc->type == CT_COMMENT_MULTI)) { cpd.in_preproc = CT_NONE; cpd.preproc_ncnl_count = 0; } /* Special handling for preprocessor stuff */ if (cpd.in_preproc != CT_NONE) { chunk_flags_set(pc, PCF_IN_PREPROC); /* Count words after the preprocessor */ if (!chunk_is_comment(pc) && !chunk_is_newline(pc)) { cpd.preproc_ncnl_count++; } /* Figure out the type of preprocessor for #include parsing */ if (cpd.in_preproc == CT_PREPROC) { if ((pc->type < CT_PP_DEFINE) || (pc->type > CT_PP_OTHER)) { set_chunk_type(pc, CT_PP_OTHER); } cpd.in_preproc = pc->type; } } else { /* Check for a preprocessor start */ if ((pc->type == CT_POUND) && ((rprev == NULL) || (rprev->type == CT_NEWLINE))) { set_chunk_type(pc, CT_PREPROC); pc->flags |= PCF_IN_PREPROC; cpd.in_preproc = CT_PREPROC; } } } /* Set the cpd.newline string for this file */ if ((cpd.settings[UO_newlines].le == LE_LF) || ((cpd.settings[UO_newlines].le == LE_AUTO) && (cpd.le_counts[LE_LF] >= cpd.le_counts[LE_CRLF]) && (cpd.le_counts[LE_LF] >= cpd.le_counts[LE_CR]))) { /* LF line ends */ cpd.newline = "\n"; LOG_FMT(LLINEENDS, "Using LF line endings\n"); } else if ((cpd.settings[UO_newlines].le == LE_CRLF) || ((cpd.settings[UO_newlines].le == LE_AUTO) && (cpd.le_counts[LE_CRLF] >= cpd.le_counts[LE_LF]) && (cpd.le_counts[LE_CRLF] >= cpd.le_counts[LE_CR]))) { /* CRLF line ends */ cpd.newline = "\r\n"; LOG_FMT(LLINEENDS, "Using CRLF line endings\n"); } else { /* CR line ends */ cpd.newline = "\r"; LOG_FMT(LLINEENDS, "Using CR line endings\n"); } } // tokenize
static chunk_t *pawn_process_func_def(chunk_t *pc) { /* We are on a function definition */ chunk_t *clp; chunk_t *last; chunk_t *next; pc->type = CT_FUNC_DEF; /* If we don't have a brace open right after the close fparen, then * we need to add virtual braces around the function body. */ clp = chunk_get_next_str(pc, ")", 1, 0); last = chunk_get_next_ncnl(clp); if (last != NULL) { LOG_FMT(LPFUNC, "%s: %d] last is '%s' [%s]\n", __func__, last->orig_line, last->str.c_str(), get_token_name(last->type)); } /* See if there is a state clause after the function */ if ((last != NULL) && chunk_is_str(last, "<", 1)) { LOG_FMT(LPFUNC, "%s: %d] '%s' has state angle open %s\n", __func__, pc->orig_line, pc->str.c_str(), get_token_name(last->type)); last->type = CT_ANGLE_OPEN; last->parent_type = CT_FUNC_DEF; while (((last = chunk_get_next(last)) != NULL) && !chunk_is_str(last, ">", 1)) { } if (last != NULL) { LOG_FMT(LPFUNC, "%s: %d] '%s' has state angle close %s\n", __func__, pc->orig_line, pc->str.c_str(), get_token_name(last->type)); last->type = CT_ANGLE_CLOSE; last->parent_type = CT_FUNC_DEF; } last = chunk_get_next_ncnl(last); } if (last == NULL) { return(last); } if (last->type == CT_BRACE_OPEN) { last->parent_type = CT_FUNC_DEF; last = chunk_get_next_type(last, CT_BRACE_CLOSE, last->level); if (last != NULL) { last->parent_type = CT_FUNC_DEF; } } else { LOG_FMT(LPFUNC, "%s: %d] '%s' fdef: expected brace open: %s\n", __func__, pc->orig_line, pc->str.c_str(), get_token_name(last->type)); chunk_t chunk; chunk = *last; chunk.str.clear(); chunk.type = CT_VBRACE_OPEN; chunk.parent_type = CT_FUNC_DEF; chunk_t *prev = chunk_add_before(&chunk, last); last = prev; /* find the next newline at level 0 */ prev = chunk_get_next_ncnl(prev); do { LOG_FMT(LPFUNC, "%s:%d] check %s, level %d\n", __func__, prev->orig_line, get_token_name(prev->type), prev->level); if ((prev->type == CT_NEWLINE) && (prev->level == 0)) { next = chunk_get_next_ncnl(prev); if ((next != NULL) && (next->type != CT_ELSE) && (next->type != CT_WHILE_OF_DO)) { break; } } prev->level++; prev->brace_level++; last = prev; } while ((prev = chunk_get_next(prev)) != NULL); if (last != NULL) { LOG_FMT(LPFUNC, "%s:%d] ended on %s, level %d\n", __func__, last->orig_line, get_token_name(last->type), last->level); } chunk = *last; chunk.str.clear(); chunk.column += last->len(); chunk.type = CT_VBRACE_CLOSE; chunk.level = 0; chunk.brace_level = 0; chunk.parent_type = CT_FUNC_DEF; last = chunk_add_after(&chunk, last); } return(last); }
static chunk_t *mod_case_brace_add(chunk_t *cl_colon) { LOG_FUNC_ENTRY(); chunk_t *pc = cl_colon; chunk_t *last = NULL; chunk_t *next = chunk_get_next_ncnl(cl_colon, CNAV_PREPROC); LOG_FMT(LMCB, "%s: line %zu", __func__, pc->orig_line); while ((pc = chunk_get_next_ncnl(pc, CNAV_PREPROC)) != NULL) { if (pc->level < cl_colon->level) { LOG_FMT(LMCB, " - level drop\n"); return(next); } if ((pc->level == cl_colon->level) && ((pc->type == CT_CASE) || (pc->type == CT_BREAK))) { last = pc; //if (pc->type == CT_BREAK) //{ // /* Step past the semicolon */ // last = chunk_get_next_ncnl(chunk_get_next_ncnl(last)); //} break; } } if (last == NULL) { LOG_FMT(LMCB, " - NULL last\n"); return(next); } LOG_FMT(LMCB, " - adding before '%s' on line %zu\n", last->text(), last->orig_line); chunk_t chunk; chunk.type = CT_BRACE_OPEN; chunk.orig_line = cl_colon->orig_line; chunk.parent_type = CT_CASE; chunk.level = cl_colon->level; chunk.brace_level = cl_colon->brace_level; chunk.flags = pc->flags & PCF_COPY_FLAGS; chunk.str = "{"; chunk_t *br_open = chunk_add_after(&chunk, cl_colon); chunk.type = CT_BRACE_CLOSE; chunk.orig_line = last->orig_line; chunk.str = "}"; chunk_t *br_close = chunk_add_before(&chunk, last); newline_add_before(last); for (pc = chunk_get_next(br_open, CNAV_PREPROC); pc != br_close; pc = chunk_get_next(pc, CNAV_PREPROC)) { pc->level++; pc->brace_level++; } return(br_open); } // mod_case_brace_add