Skip to content

Commit

Permalink
xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
lefessan committed Apr 23, 2024
1 parent 7d5ab1c commit 1c35279
Showing 1 changed file with 55 additions and 63 deletions.
118 changes: 55 additions & 63 deletions cobc/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ static void do_replace (WITH_DEPTH struct cb_replacement_state* repls);
static void check_replace_after_match (WITH_DEPTH struct cb_replacement_state *repls);
static void check_replace_all (WITH_DEPTH struct cb_replacement_state *repls,
const struct cb_text_list *new_text,
int matched,
const struct cb_text_list *src,
const struct cb_replace_list *replace_list);

Expand Down Expand Up @@ -562,7 +561,6 @@ check_replace (WITH_DEPTH struct cb_replacement_state* repls,
* replacement */
check_replace_all(MORE_DEPTH repls,
new_text,
0,
src->text_list,
replace_list);
}
Expand All @@ -575,20 +573,18 @@ is_space_or_nl (const char c)
return c == ' ' || c == '\n';
}

/* `check_replace_all( repls, new_text, matched, src, replace_list )`:
/* `check_replace_all( repls, new_text, src, replace_list )`:
* checks whether a particular replacement is possible on the current
* list of texts.
* * `repls` is the current stream state
* * `new_text` is the text by which the texts should be replace in case of match
* * `matched` is the number of already matched text tokens found in the source, and matched by previous values
* * `src` is the list of texts from the replacement to be matched
* * `replace_list` is the next replacements to try in case of failure
*/
static void
check_replace_all (WITH_DEPTH
struct cb_replacement_state *repls,
const struct cb_text_list *new_text,
int matched,
const struct cb_text_list *src,
const struct cb_replace_list *replace_list)
{
Expand All @@ -603,69 +599,65 @@ check_replace_all (WITH_DEPTH
string_of_text_list(src));
fprintf (stderr, "%s)\n", DEPTH);
#endif
int matched = 0;

while (1){
const char* src_text;
const char *text;

if (src==NULL){
/* MATCH */
/* pass the new text to the next stream */
ppecho_switch_text_list (MORE_DEPTH repls, new_text) ;
/* keep only in this stream the remaining texts that have not
* been matched */
token_queue_remove (repls->token_queue, matched);
/* restart replacements on the stream */
check_replace_after_match(MORE_DEPTH repls);
break;
}

src_text = src->text;

if (src==NULL){
/* MATCH */
/* pass the new text to the next stream */
ppecho_switch_text_list (MORE_DEPTH repls, new_text) ;
/* keep only in this stream the remaining texts that have not
* been matched */
token_queue_remove (repls->token_queue, matched);
/* restart replacements on the stream */
check_replace_after_match (MORE_DEPTH repls);
} else {
const char* src_text = src->text;
if (is_space_or_nl(src_text[0])) {
/* skip spaces in replacement */
check_replace_all(MORE_DEPTH repls, new_text,
matched,
src->next, replace_list);
} else {
if ( token_queue_length (repls->token_queue) == matched){
/* PARTIAL MATCH, we have emptied the
* list of texts, but there are still
* texts in the replacement, so wait
* for more texts to be added on the
* stream */
src = src->next;
continue;
}

if ( token_queue_length (repls->token_queue) == matched){
/* PARTIAL MATCH, we have emptied the
* list of texts, but there are still
* texts in the replacement, so wait
* for more texts to be added on the
* stream */
#ifdef DEBUG_REPLACE_TRACE
fprintf (stderr, "%s check_replace_all --> PARTIAL MATCH\n", DEPTH);
fprintf (stderr, "%s check_replace_all --> PARTIAL MATCH\n", DEPTH);
#endif
} else {
const char *text;
token_queue_get(repls->token_queue, matched, &text, NULL);
matched++;
if (is_space_or_nl(text[0])) {
/* skip spaces in texts */
check_replace_all(MORE_DEPTH repls,
new_text, matched,
src,
replace_list);
} else {
if (!strcasecmp (src_text,text)){
/* We could match one
* text from the
* stream with a text
* from the
* replacement, so
* move on to the next
* text */
check_replace_all (
MORE_DEPTH repls,
new_text,
matched, src->next,
replace_list);
} else {
/* match failed, move
* on to the next
* potential
* replacement */
check_replace (
MORE_DEPTH repls,
replace_list);
}
}
}
}
return;
}

token_queue_get(repls->token_queue, matched, &text, NULL);
matched++;

if (is_space_or_nl(text[0])) {
/* skip spaces in texts */
continue;
}

if (!strcasecmp (src_text,text)){
/* We could match one text from the stream
* with a text from the replacement, so move
* on to the next text */
src = src->next;
continue;
}

/* match failed, move on to the next potential
* replacement */
return check_replace (
MORE_DEPTH repls,
replace_list);
}
}

Expand Down

0 comments on commit 1c35279

Please sign in to comment.