Skip to content

Commit

Permalink
Fix COPY REPLACING and REPLACE
Browse files Browse the repository at this point in the history
Current implementation does not conform to standard (COPY REPLACING
and REPLACE are supposed to be in two successive passes instead of
applied together), and has various bugs.

This version is probably less efficient, but better conforms to the
standard.

Limitations:

* This first version only modify the replacements during
  preprocessing, not the ones during the listing printing.

* Since REPLACE are interpreted *before* the COPY REPLACING, they
  cannot be modified by them. A conforming implementation would
  interpret REPLACE strictly after COPY REPLACING.
  • Loading branch information
lefessan committed Apr 12, 2023
1 parent 7981d8a commit 01db7f9
Show file tree
Hide file tree
Showing 8 changed files with 839 additions and 257 deletions.
10 changes: 10 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@
* reserved.c (SEQUENCE): setting CB_CS_ALPHABET to allow code-name parsing
* parser.y, reserved.c: changed reference from 202x to 2023

2023-01-05 Fabrice Le Fessant <[email protected]>

* cobc.c.c: add `cobc_plex_stradd` and `cobc_plex_strsub` allocation
functions for the pplex phase.
* replace.c: new file containing the two-phase COPY-REPLACING and REPLACE
mechanism, conforming to COBOL standard.
* pplex.l: remove former `pplex_echo` and `pplex_replace` code. The
`alt_space` parameter is not used anymore, leading to different listing
code in some cases.

2023-01-16 Simon Sobisch <[email protected]>

* parser.y (occurs_index): only set VALUE 1 for defaultbyte == INIT
Expand Down
3 changes: 2 additions & 1 deletion cobc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
bin_PROGRAMS = cobc
cobc_SOURCES = cobc.c cobc.h ppparse.y pplex.c parser.y scanner.c config.c \
reserved.c error.c tree.c tree.h field.c typeck.c codegen.c help.c \
config.def flag.def warning.def codeoptim.def ppparse.def codeoptim.c
config.def flag.def warning.def codeoptim.def ppparse.def \
codeoptim.c replace.h replace.c

#cobc_SOURCES = cobc.c cobc.h ppparse.y pplex.l parser.y scanner.l config.c

Expand Down
58 changes: 56 additions & 2 deletions cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,8 @@ cobc_strdup (const char *dupstr)
return p;
}

#if defined (_WIN32) || defined (__CYGWIN__)
static char *
#if defined (_WIN32) || defined (__CYGWIN__)
char *
cobc_stradd_dup (const char *str1, const char *str2)
{
char *p;
Expand Down Expand Up @@ -1315,6 +1315,59 @@ cobc_plex_strdup (const char *dupstr)
return p;
}

char *
cobc_plex_stradd (const char *str1, const char *str2)
{
char *p;
size_t m, n;

/* LCOV_EXCL_START */
if (unlikely (!str1 || !str2)) {
cobc_err_msg (_("call to %s with NULL pointer"),
"cobc_plex_stradd");
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
m = strlen (str1);
n = strlen (str2);
p = cobc_plex_malloc (m + n + 1);
memcpy (p, str1, m);
memcpy (p + m, str2, n);
return p;
}

void *
cobc_plex_strsub (const char *s, const int len)
{
void *p;
int n;

#ifdef COB_TREE_DEBUG
/* LCOV_EXCL_START */
if (unlikely (!s)) {
cobc_err_msg (_("call to %s with NULL pointer"),
"cobc_plex_strsub");
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
#endif
n = strlen (s);

#ifdef COB_TREE_DEBUG
/* LCOV_EXCL_START */
if ( len>n ) {
cobc_err_msg (_("call to %s with bad argument len=%d>%d=strlen(s)"),
"cobc_plex_strsub", len, n);
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
#endif

p = cobc_plex_malloc (len + 1);
memcpy (p, s, len);
return p;
}

/* variant of strcpy which copies max 'max_size' bytes from 'src' to 'dest',
if the size of 'src' is too long only its last/last bytes are copied and an
eliding "..." is placed in front or at end depending on 'elide_at_end' */
Expand Down Expand Up @@ -6013,6 +6066,7 @@ print_program_trailer (void)

/* Print file/symbol tables if requested */
if (cb_listing_symbols) {

if (cb_listing_with_header) {
set_listing_header_symbols ();
}
Expand Down
5 changes: 5 additions & 0 deletions cobc/cobc.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ extern struct reserved_word_list *cob_user_res_list;
extern void *cobc_malloc (const size_t);
extern void cobc_free (void *);
extern void *cobc_strdup (const char *);
extern char *cobc_stradd_dup (const char *str1,
const char *str2);
extern void *cobc_realloc (void *, const size_t);

extern void *cobc_main_malloc (const size_t);
Expand All @@ -506,6 +508,9 @@ extern void cobc_parse_free (void *);

extern void *cobc_plex_malloc (const size_t);
extern void *cobc_plex_strdup (const char *);
extern void *cobc_plex_strsub (const char *, const int);
extern char *cobc_plex_stradd (const char *str1,
const char *str2);

extern void *cobc_check_string (const char *);
extern void cobc_err_msg (const char *, ...) COB_A_FORMAT12;
Expand Down
Loading

0 comments on commit 01db7f9

Please sign in to comment.