
//
//  BnfParser おためし
//
//  bnf.bnf をルールとして読み込み，同じ bnf.bnf を解析します．
//  ルール "rule" にマッチする文字列を出力します．
//

#include <fstream>
#include <string>
#include <cstdio>

#include "../bnf.hpp"   // 適当なパスに書き換えてください


using namespace std;

bool matched_callback(void * _str,
    char const * rulename,
    int start,
    int length)
{
  string& str = *(static_cast<string*>(_str));
  
  if (length < 0)
  {
    // メタ情報は，このサンプルでは無視します
  }
  else if (string(rulename) == "rule")
  {
    printf("%s => %s\n",
        rulename,
        str.substr(start, length).c_str());
  }
  
  return true;
}

// おためし
int main()
{
  try
  {
    ifstream file ("bnf.bnf");
    string buf;
    buf.assign(istreambuf_iterator<char>(file),
        istreambuf_iterator<char>());
    file.close();
    
    bnf::BnfParser c = buf.c_str();

    c.parse(
        buf.c_str(),
        "rules",
        &buf,
        matched_callback);

    printf("\ncurrent matchers: %d\n", bnf::get_matchers_count());
  }
  catch(...)
  {
    puts("something wrong");
  }
  
  printf("\nlast matchers: %d\n", bnf::get_matchers_count());
}



