/********************************************************************
  APG - an ABNF Parser Generator
  Copyright  (C) 2007 Coast to Coast Research, Inc.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see
  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
  or write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    author: Lowell Thomas
            lowell@coasttocoastresearch.com
            http://www.coasttocoastresearch.com

   filename: APG\Samples\MyParser\main.cpp
   purpose: Tests the C++ wrapper for"MyParser".

*********************************************************************/
#include "Apg.h"
#include "ApgUtilities.h"
#include "MyParser.h"


int main(int argc, char** argv)
{
//  FILE*     spRedirectOut = NULL;
  APG_BSTR  sSrc;
  char*     cpInput;
  ulong     ulTest;
  void*     vpSemanticData = NULL; // they need not be the same

  // CONSTRUCTOR
/* *** uncomment, if you want to apply semantic analysis  
  ApgMyParser::SEMANTIC_DATA sSemanticData;  

// ... and insert the following code into the class.post section of MyParser.h
//
//   //{{class.post
//     typedef struct 
//     {
//       uchar* ucpSrc;
//       ulong  ulSrcLen;
//     } SEMANTIC_DATA;
//   //}}class.post  
//
//  
*** */  
  ApgMyParser oMyParser;

  cpInput = "List.txt";

  // GPL and copyright notice
  vLicenseNotice();

  // set the custom alert handler
  pfnSetAlertHandler(vTerminalAlert);

  // get the input string
  sSrc.ulLen = ulGetFileSize(cpInput);
  sSrc.ucpStr = (uchar*)new char[sSrc.ulLen + 10];
  sSrc.ulLen = ulGetFile(cpInput, (void*)sSrc.ucpStr);
  sSrc.ucpStr[sSrc.ulLen] = 0;

  // SYNTAX ANALYSIS
  ulTest = oMyParser.ulSyntaxAnalysis(ApgMyParser::ID_LIST, sSrc.ucpStr, sSrc.ulLen);

  // print the state and stats
  vPrintState(&oMyParser.m_sState, "Test");
  vPrintStats(oMyParser.m_vpParserCtx);
  
/* *** uncomment, if you want to apply semantic analysis  
  // SEMANTIC ANALYSIS
  if(oMyParser.m_sState.ulState == MATCH)
  {
    sSemanticData.ucpSrc = sSrc.ucpStr;
    sSemanticData.ulSrcLen = sSrc.ulLen;
    vpSemanticData = (void*)&sSemanticData;
    ulTest = oMyParser.ulSemanticAnalysis(vpSemanticData);
  }
*** */

APG_EXIT:
  if(oMyParser.m_vpMemCtx)
  {
    // display any error messages
    vPrintErrors(oMyParser.m_vpMemCtx);
  }

  return 0;
}

