/** * Called when we hit a backslash. * If there is nothing but whitespace until the newline, then this is a * backslash newline */ static bool parse_bs_newline(tok_ctx& ctx, chunk_t& pc) { ctx.save(); ctx.get(); /* skip the '\' */ int ch; while (ctx.more() && unc_isspace(ch = ctx.peek())) { ctx.get(); if ((ch == '\r') || (ch == '\n')) { if (ch == '\r') { ctx.expect('\n'); } pc.str = "\\"; pc.type = CT_NL_CONT; pc.nl_count = 1; return(true); } } ctx.restore(); return(false); }
/** * Count the number of whitespace characters. * * @param pc The structure to update, str is an input. * @return Whether whitespace was parsed */ static bool parse_whitespace(tok_ctx& ctx, chunk_t& pc) { int nl_count = 0; int ch = -2; /* REVISIT: use a better whitespace detector? */ while (ctx.more() && unc_isspace(ctx.peek())) { ch = ctx.get(); /* throw away the whitespace char */ switch (ch) { case '\r': if (ctx.expect('\n')) { /* CRLF ending */ cpd.le_counts[LE_CRLF]++; } else { /* CR ending */ cpd.le_counts[LE_CR]++; } nl_count++; pc.orig_prev_sp = 0; break; case '\n': /* LF ending */ cpd.le_counts[LE_LF]++; nl_count++; pc.orig_prev_sp = 0; break; case '\t': pc.orig_prev_sp += calc_next_tab_column(cpd.column, cpd.settings[UO_input_tab_size].n) - cpd.column; break; case ' ': pc.orig_prev_sp++; break; default: break; } } if (ch != -2) { pc.str.clear(); pc.nl_count = nl_count; pc.type = nl_count ? CT_NEWLINE : CT_WHITESPACE; pc.after_tab = (ctx.c.last_ch == '\t'); return(true); } return(false); } // parse_whitespace
/** * Parses any number of tab or space chars followed by a newline. * Does not change pc.len if a newline isn't found. * This is not the same as parse_whitespace() because it only consumes until * a single newline is encountered. */ static bool parse_newline(tok_ctx& ctx) { ctx.save(); /* Eat whitespace */ while ((ctx.peek() == ' ') || (ctx.peek() == '\t')) { ctx.get(); } if ((ctx.peek() == '\r') || (ctx.peek() == '\n')) { if (!ctx.expect('\n')) { ctx.get(); ctx.expect('\n'); } return(true); } ctx.restore(); return(false); }