Beispiel #1
0
int main(int argc, char *argv[]) {
	QString lang_in("lang.strings"), lang_out("lang");
	for (int i = 0; i < argc; ++i) {
		if (string("-lang_in") == argv[i]) {
			if (++i < argc) lang_in = argv[i];
		} else if (string("-lang_out") == argv[i]) {
			if (++i < argc) lang_out = argv[i];
		}
	}
#ifdef Q_OS_MAC
    if (QDir(QString()).absolutePath() == "/") {
        QString first = argc ? QString::fromLocal8Bit(argv[0]) : QString();
        if (!first.isEmpty()) {
            QFileInfo info(first);
            if (info.exists()) {
                QDir result(info.absolutePath() + "/../../..");
                QString basePath = result.absolutePath() + '/';
                lang_in = basePath + lang_in;
                lang_out = basePath + lang_out;
            }
        }
    }
#endif
	QObject *taskImpl = new GenLang(lang_in, lang_out);

	QCoreApplication a(argc, argv);

	QObject::connect(taskImpl, SIGNAL(finished()), &a, SLOT(quit()));
	QTimer::singleShot(0, taskImpl, SLOT(run()));

	return a.exec();
}
Beispiel #2
0
void Process(StrLen file_name)
 {
  SplitPath dev_name(file_name);
  SplitName path_name(dev_name.path);
  SplitExt name_ext(path_name.name);

  String in_name;

  if( !name_ext )
    {
     in_name=StringCat(dev_name.dev,path_name.path,name_ext.name,".lang");
    }
  else
    {
     in_name=file_name;
    }

  TrackStage("Load file #.q;",StrLen(Range(in_name)));

  CondLang clang(Range(in_name));

  TrackStage("Build top lang");

  TopLang top(clang);

  TrackStage("Run good test on top lang");

  if( !RunGoodTest(top) ) return;

  TrackStage("Build bottom lang");

  BottomLang bottom(clang);

  TrackStage("Extend bottom lang");

  ExtLang ext_bottom(bottom);

  TrackStage("Process top lang");

  ExtLang ext_top(top);
  LangDiagram diagram(ext_top);
  LangStateMachine<LR1Estimate,LR1MapContext> machine(ext_top,diagram,ext_bottom);

  StateCompress<LR1Estimate> compress(machine);

  TrackStage("LR1) #;",PrintCompressCounts(compress));

  {
   ulen conflicts=0;

   for(auto &est : compress.getProps() ) conflicts+=est.hasConflict();

   if( conflicts )
     {
      String out_name=StringCat(dev_name.dev,path_name.path,name_ext.name,".bad.txt");
      PrintFile out(Range(out_name));

      Putobj(out,clang);

      PrintBad(out,ext_top,compress);

      Printf(Exception,"#; CONFLICTs detected. Not LR1 language.",conflicts);
     }
   else
     {
      TrackStage("No conflicts. LR1 language.");
     }
  }

  StateCompress<LR1Estimate,LR1PropNonEmpty> compress_ne(machine);

  TrackStage("NonEmpty) #;",PrintCompressCounts(compress_ne));

#if 0

  StateCompress<LR1Estimate,LR1PropShiftSet> compress_shift(machine);

  TrackStage("Shift) #;",PrintCompressCounts(compress_shift));

  StateCompress<LR1Estimate,LR1PropValidSet> compress_valid(machine);

  TrackStage("Valid) #;",PrintCompressCounts(compress_valid));

  StateCompress<LR1Estimate,LR1PropRuleSet> compress_rules(machine);

  TrackStage("Rules) #;",PrintCompressCounts(compress_rules));

  StateMap map(compress,compress_ne);

  String out_name=StringCat(dev_name.dev,path_name.path,"Result.txt");
  PrintFile out(Range(out_name));

  PrintFibres(out,compress,compress_ne,map);

  Putobj(out,BindOpt(ext_top,compress_ne));

#endif

  {
   String out_name=StringCat(dev_name.dev,path_name.path,name_ext.name,".txt");
   PrintFile out(Range(out_name));

   Putobj(out,clang);

   Putobj(out,BindOpt(ext_top,compress));
  }

  {
   String out_name=StringCat(dev_name.dev,path_name.path,name_ext.name,".ddl");

   PosPrint<PrintFile> out(Range(out_name));

   Printf(out,"/* #;.ddl */\n\n",name_ext.name);

   Putobj(out,"//include <LangTypes.ddl>\n\n");

   Putobj(out,"Lang lang=\n");

   // lang
   {
    ListPrint<decltype(out)> lang_out(out);

    // atoms
    {
     ListPrint<decltype(lang_out)> atom_out(lang_out);

     for(auto &atom : clang.getAtoms() )
       Printf(atom_out,"{ #; , #; , lang.elements+#; }#;",atom.index,StrLen(atom.name.inner(2,1)),atom.index,EndItem());

     Putobj(atom_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // synts
    {
     ListPrint<decltype(lang_out)> synt_out(lang_out);

     ulen element=clang.getAtomCount();
     ulen top_index=0;

     for(auto &synt : clang.getSynts() )
       {
        Printf(synt_out,"{ #; , #.q; ,",synt.index,synt.name);

        auto kinds=synt.kinds;

        if( !kinds )
          {
           Indent indent(synt_out.getCol());

           Printf(synt_out," { { #; , 0 , \"\" , lang.synts+#; , lang.elements+#; ,#;",top_index,synt.index,element++,AutoIndent());

           auto &top_synt=top.getSynts()[top_index++];

           ListPrint<decltype(synt_out)> rule_out(synt_out);

           for(auto &top_rule : top_synt.rules )
             Printf(rule_out,"lang.top_rules+#;#;",top_rule.index,EndItem());

           Putobj(rule_out,EndList());

           Printf(synt_out,"#;   }#; } ,#;",indent,indent,indent);
          }
        else
          {
           Indent indent(synt_out.getCol());

           Putobj(synt_out,indent);

           ListPrint<decltype(synt_out)> kind_out(synt_out);

           for(auto &kind : kinds )
             {
              Printf(kind_out,"{ #; , #; , #.q; , lang.synts+#; , lang.elements+#; ,#;",top_index,kind.index,kind.name,synt.index,element++,AutoIndent());

              auto &top_synt=top.getSynts()[top_index++];

              ListPrint<decltype(kind_out)> rule_out(kind_out);

              for(auto &top_rule : top_synt.rules )
                Printf(rule_out,"lang.top_rules+#;#;",top_rule.index,EndItem());

              Putobj(rule_out,EndList());

              Printf(kind_out,"\n}#;",EndItem());
             }

           Putobj(kind_out,EndList());

           Putobj(synt_out," ,",indent);
          }

        ListPrint<decltype(synt_out)> rule_out(synt_out);

        for(auto &rule : synt.rules )
          Printf(rule_out,"lang.rules+#;#;",rule.index,EndItem());

        Putobj(rule_out,EndList());

        Putobj(synt_out,"\n}",EndItem());
       }

     Putobj(synt_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // lang
    {
     ListPrint<decltype(lang_out)> synt_out(lang_out);

     for(auto &synt : clang.getSynts() )
       if( synt.is_lang )
         {
          Printf(synt_out,"lang.synts+#;",synt.index);
         }

     Putobj(synt_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // elements
    {
     ListPrint<decltype(lang_out)> elem_out(lang_out);

     ulen element=0;

     for(auto &atom : clang.getAtoms() )
       Printf(elem_out,"{ #; , lang.atoms+#; }#;",element++,atom.index,EndItem());

     for(auto &synt : clang.getSynts() )
       {
        auto len=synt.kinds.len;

        if( !len ) len=1;

        for(ulen i=0; i<len ;i++)
          {
           Printf(elem_out,"{ #; , lang.synts[#;].kinds+#; }#;",element++,synt.index,i,EndItem());
          }
       }

     Putobj(elem_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // rules
    {
     ListPrint<decltype(lang_out)> rule_out(lang_out);

     for(auto &rule : clang.getRules() )
       {
        Printf(rule_out,"{ #; , #.q; , lang.synts[#;].kinds+#; ,#;",rule.index,rule.name,rule.ret->index,rule.getKindIndex(),AutoIndent());

        ListPrint<decltype(rule_out)> arg_out(rule_out);

        for(auto &element : rule.args )
          element.apply( [&] (const CondLangBase::AtomDesc *desc)
                             {
                              Printf(arg_out,"lang.atoms+#;#;",desc->index,EndItem());
                             } ,
                         [&] (const CondLangBase::SyntDesc *desc)
                             {
                              Printf(arg_out,"lang.synts+#;#;",desc->index,EndItem());
                             }
                       );

        Putobj(arg_out,EndList());

        Putobj(rule_out,"\n}",EndItem());
       }

     Putobj(rule_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // top rules
    {
     ListPrint<decltype(lang_out)> rule_out(lang_out);

     for(auto &rule : top.getRules() )
       {
        Printf(rule_out,"{ #; , #.q; , lang.rules+#; , lang.synts[#;].kinds+#; ,#;",
          rule.index,rule.name,rule.map_index,rule.ret->map_index,rule.ret->kind_index,AutoIndent());

        ListPrint<decltype(rule_out)> arg_out(rule_out);

        for(auto &element : rule.args )
          element.apply( [&] (const LangBase::AtomDesc *desc)
                             {
                              Printf(arg_out,"lang.atoms+#;#;",desc->index,EndItem());
                             } ,
                         [&] (const LangBase::SyntDesc *desc)
                             {
                              Printf(arg_out,"lang.synts[#;].kinds+#;#;",desc->map_index,desc->kind_index,EndItem());
                             }
                       );

        Putobj(arg_out,EndList());

        Putobj(rule_out,"\n}",EndItem());
       }

     Putobj(rule_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // states
    {
     ListPrint<decltype(lang_out)> state_out(lang_out);

     for(auto &state : compress.getStateTable() )
       {
        Printf(state_out,"{ #; , lang.finals+#; ,#;",state.index,state.prop_index,AutoIndent());

        ListPrint<decltype(state_out)> trans_out(state_out);

        for(auto &trans : state.transitions )
          Printf(trans_out,"{ lang.elements+#; , lang.states+#; }#;",trans.element,trans.dst->index,EndItem());

        Putobj(trans_out,EndList());

        Putobj(state_out,"\n}",EndItem());
       }

     Putobj(state_out,EndList());
    }

    Putobj(lang_out,EndItem());

    // finals
    {
     ListPrint<decltype(lang_out)> final_out(lang_out);

     ulen atom_count=clang.getAtomCount();

     ulen index=0;

     for(auto &final : compress.getProps() )
       {
        Printf(final_out,"{ #; ,#;",index++,AutoIndent());

        ListPrint<decltype(final_out)> action_out(final_out);

        if( final.hasNull() )
          {
           Printf(action_out,"{ null , null }#;",EndItem());
          }
        else
          {
           auto &alpha=final.getAlpha();

           if( alpha.nonEmpty() )
             {
              Printf(action_out,"{ null , lang.rules+#; }#;",alpha.getPtr()->getIndex()-atom_count,EndItem());
             }
          }

        for(auto &rec : Range(final.getBeta()) )
          {
           if( rec.object.shift )
             {
              Printf(action_out,"{ lang.atoms+#; , null }#;",rec.index.getIndex(),EndItem());
             }
           else
             {
              auto &rules=rec.object.rules;

              if( rules.nonEmpty() )
                {
                 Printf(action_out,"{ lang.atoms+#; , lang.rules+#; }#;",rec.index.getIndex(),rules.getPtr()->getIndex()-atom_count,EndItem());
                }
             }
          }

        Putobj(action_out,EndList());

        Putobj(final_out,"\n}",EndItem());
       }

     Putobj(final_out,EndList());
    }

    Putobj(lang_out,EndItem(),EndList());
   }

   Putobj(out,";\n\n");
  }