From 0eff5368b70b52167ff96ef6ada57459408eeac5 Mon Sep 17 00:00:00 2001
From: "James K. Lowden" <jklowden@symas.com>
Date: Thu, 9 May 2024 10:05:45 -0400
Subject: [PATCH] support MF $IF CDF syntax

---
 gcc/cobol/cdf_text.h  | 13 +++++++++----
 gcc/cobol/scan.l      |  7 +++++++
 gcc/cobol/scan_ante.h |  6 ++++++
 gcc/cobol/symbols.cc  |  3 +++
 gcc/cobol/symbols.h   |  1 +
 5 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/gcc/cobol/cdf_text.h b/gcc/cobol/cdf_text.h
index aae8c138f7fe..d93b82df32c6 100644
--- a/gcc/cobol/cdf_text.h
+++ b/gcc/cobol/cdf_text.h
@@ -256,6 +256,8 @@ cdftext::map_file( int fd ) {
   return mfile;
 }
 
+bool lexio_dialect_mf();
+
 filespan_t
 cdftext::free_form_reference_format( int input ) {
   filespan_t source_buffer = map_file(input);
@@ -358,12 +360,15 @@ cdftext::free_form_reference_format( int input ) {
           erase_source(indcol, mfile.eol);
         }
         continue;
+      case '$':
+        if( lexio_dialect_mf() ) {
+          break;
+        }
+        __attribute__ ((fallthrough));
       default: // ignore other characters in indicator area
         if( isspace(indcol[0]))  break;
-        if( yy_flex_debug ) {
-          warnx("--stray indicator 0x%0x \"%s\", line %zu",
-                indcol[0], indcol, mfile.lineno);
-        }
+        warnx("warning: stray indicator 0x%0x \"%s\" %zu",
+              indcol[0], indcol, mfile.lineno);
         *indcol = SPACE;
         break;
       }
diff --git a/gcc/cobol/scan.l b/gcc/cobol/scan.l
index 8670e6ca907d..acd7c9f95924 100644
--- a/gcc/cobol/scan.l
+++ b/gcc/cobol/scan.l
@@ -1845,6 +1845,13 @@ COPY		{
   ^[ ]*>>{OSPC}ELSE	 	{ return CDF_ELSE; }
   ^[ ]*>>{OSPC}END-IF	 	{ return CDF_END_IF; }
 
+ ^[ ]*[$]{OSPC}IF		{ if( ! dialect_mf() ) dialect_error( yytext, "mf");
+				  return CDF_IF; }
+  ^[ ]*[$]{OSPC}ELSE	 	{ if( ! dialect_mf() ) dialect_error( yytext, "mf");
+				  return CDF_ELSE; }
+  ^[ ]*[$]{OSPC}END	 	{ if( ! dialect_mf() ) dialect_error( yytext, "mf");
+				  return CDF_END_IF; }
+
   ^[ ]*>>{OSPC}EVALUATE		{ return CDF_EVALUATE; }
   ^[ ]*>>{OSPC}WHEN		{ return CDF_WHEN; }
   ^[ ]*>>{OSPC}END-EVALUATE	{ return CDF_END_EVALUATE; }
diff --git a/gcc/cobol/scan_ante.h b/gcc/cobol/scan_ante.h
index c2b734902c42..a24222114533 100644
--- a/gcc/cobol/scan_ante.h
+++ b/gcc/cobol/scan_ante.h
@@ -105,6 +105,12 @@ lexer_echo() {
   return echo_on;
 }
 
+static void
+dialect_error( const char term[], const char dialect[] ) {
+  yyerrorv("error: %s is not ISO syntax, requires -dialect %s",
+	   term, dialect);
+}
+
 // IBM says a picture can be up to 50 bytes, not 1000 words.
 // ISO says a picture can be up to 63 bytes.  We allow for a NUL terminator.
 static char orig_picture[PICTURE_MAX];
diff --git a/gcc/cobol/symbols.cc b/gcc/cobol/symbols.cc
index 4d62198cc931..886e502696f5 100644
--- a/gcc/cobol/symbols.cc
+++ b/gcc/cobol/symbols.cc
@@ -49,6 +49,9 @@
 
 #pragma GCC diagnostic ignored "-Wunused-result"
 
+bool
+lexio_dialect_mf() { return dialect_mf(); }
+
 class symbol_pair_t
 {
   const symbol_elem_t *first, *last;
diff --git a/gcc/cobol/symbols.h b/gcc/cobol/symbols.h
index 6c1fcc7c7907..d20f18598486 100644
--- a/gcc/cobol/symbols.h
+++ b/gcc/cobol/symbols.h
@@ -394,6 +394,7 @@ const char * time_is_now(void);
 
 void yyerrorv( const char fmt[], ... );
 void yyerrorvl( int line, const char fmt[], ... );
+void yywarnv( const char fmt[], ... );
 
 struct cbl_upsi_mask_t {
   bool on_off;
-- 
GitLab