home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GRECODE / MERGELEX.AWK < prev    next >
Text File  |  1993-12-10  |  5KB  |  192 lines

  1. # Conversion of files between different charsets and usages.
  2. # Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  3. # Francois Pinard <pinard@iro.umontreal.ca>, 1990.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. # This awk script merges several lex sources intended for GNU recode.
  20.  
  21. # At beginning, one temporary file is initialized for each section.
  22.  
  23. BEGIN {
  24.   SECTION1 = "merged1.tmp"
  25.   SECTION2 = "merged2.tmp"
  26.   SECTION3 = "merged3.tmp"
  27.  
  28.   print "/* This file is generated automatically by mergelex.awk.  */"
  29.  
  30.   print ""                        >SECTION1
  31.   print "%{"                        >SECTION1
  32.   print "#include \"recode.h\""                >SECTION1
  33.   print "#ifdef USE_FPUTC"                >SECTION1
  34.   print "#define output(ch) fputc (ch, yyout)"        >SECTION1
  35.   print "#else"                        >SECTION1
  36.   print "#define output(ch) putc (ch, yyout)"        >SECTION1
  37.   print "#endif"                    >SECTION1
  38.   print "%}"                        >SECTION1
  39.  
  40.   print "%%"                        >SECTION2
  41.  
  42.   print "%%"                        >SECTION3
  43.   print ""                        >SECTION3
  44.   print "int"                        >SECTION3
  45.   print "yywrap (void)"                    >SECTION3
  46.   print "{"                        >SECTION3
  47.   print "  return 1;"                    >SECTION3
  48.   print "}"                        >SECTION3
  49. }
  50.  
  51. # A `/* Step name: NAME.  */' line indicates the start of a new file.
  52. # Generate an interface routine.  The step name is saved for broketed
  53. # prefixes in rules.
  54.  
  55. $1 == "/*" && $2 == "Step" && $3 == "name:" && $5 == "*/" {
  56.   section = 1
  57.   step_name = $4
  58.   sub (".$", "", step_name)
  59.   print ""                        >SECTION3
  60.   print "static void"                    >SECTION3
  61.   print "file_" step_name " (const STEP *step, " \
  62.     "FILE *input_file, FILE *output_file)"         >SECTION3
  63.   print "{"                        >SECTION3
  64.   print "  yy_init = 1;"                >SECTION3
  65.   print "  yyin = input_file;"                >SECTION3
  66.   print "  yyout = output_file;"            >SECTION3
  67.   print "  BEGIN X_" step_name ";"            >SECTION3
  68.   print "  yylex ();"                    >SECTION3
  69.   print "}"                        >SECTION3
  70.   next
  71. }
  72.  
  73. # Remove block C comments in section 1, except the very first.  It is
  74. # assumed that, when a /* comment starts in column 1, there is no code
  75. # following the closing */ on its line.  Also, remove all white lines.
  76.  
  77. /^\/\*/ && section == 1 {
  78.   while (!match ($0, /\*\//)) {
  79.     if (!comment_done) {
  80.       print
  81.     }
  82.     getline
  83.   }
  84.   if (!comment_done) {
  85.     print
  86.   }
  87.   comment_done = 1
  88.   next
  89. }
  90.  
  91. /^[     ]*$/ && section == 1 {
  92.   next
  93. }
  94.  
  95. # A %% in column 1 signals the beginning of lex rules.
  96.  
  97. /^%%/ && section == 1 {
  98.   print "%x X_" step_name                >SECTION1
  99.   section = 2
  100.   print ""                        >SECTION2
  101.   next
  102. }
  103.  
  104. # A %{ comment in column 1 signals the start of a C code section ended
  105. # by a %} line.
  106.  
  107. /^%\{/ {
  108.   c_code = 1
  109.   print ""                        >SECTION1
  110.   print                            >SECTION1
  111.   next
  112. }
  113.  
  114. /^%\}/ {
  115.   print                            >SECTION1
  116.   print ""                        >SECTION1
  117.   c_code = 0
  118.   next
  119. }
  120.  
  121. c_code {
  122.   print                            >SECTION1
  123.   next
  124. }
  125.  
  126. # Section 1 declarations are studied and copied, once each.
  127. # Conflicting declaractions are reported at beginning of output.
  128.  
  129. /^[^     ]/ && section == 1 {
  130.   if ($1 in rules) {
  131.     if ($0 != rules[$1]) {
  132.       print "** Conflicting definition: " $0
  133.     }
  134.   }
  135.   else {
  136.     rules[$1] = $0
  137.     print                        >SECTION1
  138.   }
  139.   next
  140. }
  141.  
  142. # In section 2, every non-empty line which does not start with white
  143. # space has to be a lex rule, which is then prefixed by a start name
  144. # derived from the step name.  However, a %% in column 1 starts
  145. # section 3, which contains pure C code, which is copied verbatim.
  146.  
  147. /^$/ && section == 2 {
  148.   print                            >SECTION2
  149.   next
  150. }
  151.  
  152. /^[     ]/ && section == 2 {
  153.   print                            >SECTION2
  154.   next
  155. }
  156.  
  157. /^%%/ && section == 2 {
  158.   section = 3
  159.   next
  160. }
  161.  
  162. section == 2 {
  163.   print "<X_" step_name ">" $0                >SECTION2
  164.   next
  165. }
  166.  
  167. section == 3 {
  168.   print                            >SECTION3
  169.   next
  170. }
  171.  
  172. # At end, all three temporary files are reread and output, followed by
  173. # the generated interfaces: one routine for each step name.
  174.  
  175. END {
  176.   close (SECTION1)
  177.   while (getline <SECTION1)
  178.     print
  179.   close (SECTION1)
  180.  
  181.   close (SECTION2)
  182.   while (getline <SECTION2)
  183.     print
  184.   close (SECTION2)
  185.  
  186.   close (SECTION3)
  187.   while (getline <SECTION3)
  188.     print
  189.   close (SECTION3)
  190. }
  191.  
  192.