~ubuntu-branches/ubuntu/trusty/styx/trusty

« back to all changes in this revision

Viewing changes to libbase/scn_io.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederik Schüler
  • Date: 2007-07-01 23:06:53 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070701230653-8tcr3ow0d49alwj2
Tags: 1.7.5-1
* New upstream version.
* Bump standard to 2.7.2, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#include "scn_imp.h"
17
17
#include "pathes.h"
18
18
#include "sink.h"
 
19
#include "line_scn.h"
19
20
 
20
21
/*!!
21
22
   Beside copying and disposing of scanner definitions 
340
341
      fprintf(fp,"  /* %3d */", i);
341
342
      for (j = 0; j < ed; j++)
342
343
      { long bgn = scn->StaEdg[i];
343
 
        fprintf(fp,"  %9ld",scn->EdgeC[bgn+j]);
 
344
        fprintf(fp,"  %9ld",(long int)scn->EdgeC[bgn+j]);
344
345
        if (             bgn+j+1 < SCN_Edges(scn)) fprintf(fp,",");
345
346
        if (j%6 == 5 && bgn+j+1 < SCN_Edges(scn) && j+1 < ed)
346
347
          fprintf(fp,"\n           ");
524
525
}
525
526
 
526
527
 
527
 
static void Scn_Src_SourceScanner(FILE* fp, Scn_T x)
 
528
static void Scn_Src_SourceScanner
 
529
            (
 
530
              FILE* fp, Scn_T x, c_string prefix, c_string suffix
 
531
            )
528
532
/* writes source for ( group ) scanner definition 'x' to file 'fp'
529
533
*/
530
534
{ int i; scn_t scn = Scn_IMP(x);
531
 
  fprintf(fp,"Name=%s\n",scn->Name);
 
535
  c_string nl = ( !STR_EMPTY(prefix) && !STR_EMPTY(suffix) ) ? "\\n" : "";
 
536
  fprintf(fp,"%sName=%s%s%s\n",prefix,scn->Name,nl,suffix);
532
537
  if( scn->Groups == 0 )
533
538
  {
534
 
    fprintf(fp,"StaEdg\n");
 
539
    fprintf(fp,"%sStaEdg%s%s\n",prefix,nl,suffix);
535
540
    for (i = 0; i < scn->States+1; i++)
536
541
    {
537
 
      fprintf(fp,"%ld\n",scn->StaEdg[i]);
538
 
    }
539
 
    fprintf(fp,"StaFin\n");
540
 
    for (i = 0; i < scn->States; i++)
541
 
    {
542
 
      fprintf(fp,"%d\n",scn->StaFin[i]);
543
 
    }
544
 
    fprintf(fp,"EdgeC\n");
545
 
    for (i = 0; i < scn->States; i++)
546
 
    { long j, ed = scn->StaEdg[i+1] - scn->StaEdg[i];
547
 
      for (j = 0; j < ed; j++)
548
 
      { long bgn = scn->StaEdg[i];
549
 
        fprintf(fp,"%ld\n",scn->EdgeC[bgn+j]);
550
 
      }
551
 
    }
552
 
    fprintf(fp,"EdgeS\n");
553
 
    for (i = 0; i < scn->States; i++)
554
 
    { long j, ed = scn->StaEdg[i+1] - scn->StaEdg[i];
555
 
      for (j = 0; j < ed; j++)
556
 
      { long bgn = scn->StaEdg[i];
557
 
        fprintf(fp,"%ld\n",scn->EdgeS[bgn+j]);
558
 
      }
559
 
    }
560
 
    fprintf(fp,"TokId\n");
561
 
    for (i = 0; i < scn->Tokens; i++)
562
 
      fprintf(fp," %s\n",scn->TokId[i]);
563
 
    fprintf(fp,"Flags\n");
564
 
    for (i = 0; i < scn->Tokens; i++)
565
 
    {
566
 
      fprintf(fp,"%d\n", (int)scn->Flags[i]);
 
542
      fprintf(fp,"%s%ld%s%s\n",prefix,scn->StaEdg[i],nl,suffix);
 
543
    }
 
544
    fprintf(fp,"%sStaFin%s%s\n",prefix,nl,suffix);
 
545
    for (i = 0; i < scn->States; i++)
 
546
    {
 
547
      fprintf(fp,"%s%d%s%s\n",prefix,scn->StaFin[i],nl,suffix);
 
548
    }
 
549
    fprintf(fp,"%sEdgeC%s%s\n",prefix,nl,suffix);
 
550
    for (i = 0; i < scn->States; i++)
 
551
    { long j, ed = scn->StaEdg[i+1] - scn->StaEdg[i];
 
552
      for (j = 0; j < ed; j++)
 
553
      { long bgn = scn->StaEdg[i];
 
554
        fprintf(fp,"%s%ld%s%s\n",prefix,(long int)scn->EdgeC[bgn+j],nl,suffix);
 
555
      }
 
556
    }
 
557
    fprintf(fp,"%sEdgeS%s%s\n",prefix,nl,suffix);
 
558
    for (i = 0; i < scn->States; i++)
 
559
    { long j, ed = scn->StaEdg[i+1] - scn->StaEdg[i];
 
560
      for (j = 0; j < ed; j++)
 
561
      { long bgn = scn->StaEdg[i];
 
562
        fprintf(fp,"%s%ld%s%s\n",prefix,scn->EdgeS[bgn+j],nl,suffix);
 
563
      }
 
564
    }
 
565
    fprintf(fp,"%sTokId%s%s\n",prefix,nl,suffix);
 
566
    for (i = 0; i < scn->Tokens; i++)
 
567
      fprintf(fp,"%s %s%s%s\n",prefix,scn->TokId[i],nl,suffix);
 
568
    fprintf(fp,"%sFlags%s%s\n",prefix,nl,suffix);
 
569
    for (i = 0; i < scn->Tokens; i++)
 
570
    {
 
571
      fprintf(fp,"%s%d%s%s\n", prefix,(int)scn->Flags[i],nl,suffix);
567
572
    }
568
573
    if( scn->Switch != (short*)NULL )
569
574
    {
570
 
      fprintf(fp,"Switch\n");
 
575
      fprintf(fp,"%sSwitch%s%s\n",prefix,nl,suffix);
571
576
      for (i = 0; i < scn->Tokens; i++)
572
577
      {
573
 
        fprintf(fp,"%d\n",scn->Switch[i]);
 
578
        fprintf(fp,"%s%d%s%s\n",prefix,scn->Switch[i],nl,suffix);
574
579
      }
575
580
    }
576
581
  }
578
583
  {
579
584
    for( i=0; i < scn->dyckcnt; ++i )
580
585
    {
581
 
      fprintf(fp,"# dyck scanner '%s'\n\nDyck\n",scn->dyckpat[i]->Name);
582
 
      Scn_Src_SourceScanner(fp,scn->dyckpat[i]);
 
586
      fprintf
 
587
      (
 
588
        fp,"%s# dyck scanner '%s'%s%s%s\n\n%sDyck%s%s\n",
 
589
        prefix,scn->dyckpat[i]->Name,nl,nl,suffix,prefix,nl,suffix
 
590
      );
 
591
      Scn_Src_SourceScanner(fp,scn->dyckpat[i],prefix,suffix);
583
592
    }
584
 
    fprintf(fp,"DyckToken\n");
 
593
    fprintf(fp,"%sDyckToken%s%s\n",prefix,nl,suffix);
585
594
    for( i=0; i < scn->dyckcnt; ++i )
586
595
    {
587
 
      fprintf(fp,"%d\n",scn->dyckidx[i]);
 
596
      fprintf(fp,"%s%d%s%s\n",prefix,scn->dyckidx[i],nl,suffix);
588
597
    }
589
598
  }
590
599
}
591
600
 
592
601
 
593
 
void Scn_Src_Source(FILE* fp, c_string fid, Scn_T x)
 
602
void Scn_Src_Source
 
603
     (
 
604
       FILE* fp, c_string fid, Scn_T x, c_string prefix, c_string suffix
 
605
     )
594
606
/* writes source for scanner ( group ) definition 'x' to file 'fp';
595
 
   produces a source header if filename 'fid' is not empty
 
607
   produces a source header if filename 'fid' is not empty;
 
608
   encapsulates output lines with 'prefix' and 'suffix'
596
609
*/
597
610
{ int i; scn_t scn = Scn_IMP(x);
 
611
  c_string nl = ( !STR_EMPTY(prefix) && !STR_EMPTY(suffix) ) ? "\\n" : "";
598
612
  if( !STR_EMPTY(fid) )
599
 
    fprintf(fp,"# [%s] Scanner table for '%s'\n\n", fid, scn->Name);
 
613
    fprintf
 
614
    (
 
615
      fp,"%s# [%s] Scanner table for '%s'%s%s%s\n\n",
 
616
      prefix,fid,scn->Name,nl,nl,suffix
 
617
    );
600
618
  for( i=0; i < scn->Groups; ++i )
601
619
  {
602
 
    fprintf(fp,"# group scanner '%s'\n\nGroup\n",scn->GrpScn[i]->Name);
603
 
    Scn_Src_SourceScanner(fp,scn->GrpScn[i]);
 
620
    fprintf
 
621
    (
 
622
      fp,"%s# group scanner '%s'%s%s%s\n\n%sGroup%s%s\n",
 
623
      prefix,scn->GrpScn[i]->Name,nl,nl,suffix,prefix,nl,suffix
 
624
    );
 
625
    Scn_Src_SourceScanner(fp,scn->GrpScn[i],prefix,suffix);
604
626
  }
605
 
  fprintf(fp,"# scanner group '%s'\n\nLanguage\n",scn->Name);
606
 
  Scn_Src_SourceScanner(fp, x);
607
 
  fprintf(fp,"\n");
 
627
  fprintf
 
628
  (
 
629
    fp,"%s# scanner group '%s'%s%s%s\n\n%sLanguage%s%s\n",
 
630
    prefix,scn->Name,nl,nl,suffix,prefix,nl,suffix
 
631
  );
 
632
  Scn_Src_SourceScanner(fp,x,prefix,suffix);
 
633
  fprintf(fp,"%s%s%s\n",prefix,nl,suffix);
608
634
}
609
635
 
610
 
void Scn_Src_Modul(c_string Environ, Scn_T x)
 
636
void Scn_Src_Modul(c_string Environ, Scn_T x, string Template)
611
637
/* creates source [$'Environ'/]<Language>.ltbl
612
 
   for scanner ( group ) definition 'x'
 
638
   for scanner ( group ) definition 'x';
 
639
   it uses the template file for the export, if 'Template' is a regular file 
613
640
*/
614
 
{ FILE *tf; c_string fid;
 
641
{ FILE *tf = NULL; c_string fid;
615
642
  scn_t scn = Scn_IMP(x);
616
643
  fid = Str_printf("%s.ltbl",scn->Name);
617
644
  tf = OpenPath(Environ,fid,"","wt");
618
 
  Scn_Src_Source(tf,fid,x);
 
645
  if( STR_EMPTY(Template) ) Scn_Src_Source(tf,fid,x,"","");
 
646
  else
 
647
  { FILE* pf = NULL; c_string line;
 
648
    pf = OpnFile(Template,"rt");
 
649
    while( (line=Line_get(pf)) != (c_string)NULL )
 
650
    { c_string prefix, suffix;
 
651
      if( Line_split(line,"@TABLE@",&prefix,&suffix) )
 
652
      {
 
653
        fprintf(tf,"%sltbl%s%s\n",prefix?prefix:"",scn->Name,suffix?suffix:"");
 
654
      }
 
655
      else
 
656
      if( Line_split(line,"@LINE@",&prefix,&suffix) )
 
657
      {
 
658
        if( prefix != (c_string)NULL && suffix != (c_string)NULL )
 
659
          Scn_Src_Source(tf,fid,x,prefix,suffix);
 
660
        else
 
661
        if( prefix != (c_string)NULL )
 
662
          Scn_Src_Source(tf,fid,x,prefix,"");
 
663
        else
 
664
        if( suffix != (c_string)NULL )
 
665
          Scn_Src_Source(tf,fid,x,"",suffix);
 
666
        else
 
667
          Scn_Src_Source(tf,fid,x,"","");
 
668
      }
 
669
      else fprintf(tf,"%s\n",line);
 
670
      if( prefix != (c_string)NULL ) FreeMem(prefix);
 
671
      if( suffix != (c_string)NULL ) FreeMem(suffix);
 
672
      FreeMem(line);
 
673
    }
 
674
    fclose(pf);
 
675
  }
619
676
  fclose(tf);
620
677
  FreeMem(fid);
621
678
}