Copyright (C) Kevin Larke 2009-2020

This file is part of libcm.

libcm 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 3 of the License, or (at your option) any later version.

libcm 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.

See the GNU General Public License distributed with the libcm package or look here: .


//
// cmLexTest() gives a simple cmLex example.
//
void cmLexTest( cmRpt_t* rpt)
{
  cmChar_t buf[] =
  "123ident0\n 123.456\nident0\n"
  "0xa12+.2\n"
  "// comment \n"
  "/* block \n"
  "comment */"
  "\"quoted string\""
  "ident1"
  "// last line comment";
  
  // initialize a lexer with a buffer of text
  cmLexH h = cmLexInit(buf,strlen(buf),
  kReturnSpaceLexFl | kReturnCommentsLexFl,rpt);
  
  // verify that the lexer initialization succeded.
  if( cmLexIsValid(h) == false )
  {
    cmRptPrintf(rpt,"Lexer initialization failed.");
    return;
  }
  
  // register some additional recoginizers
  cmLexRegisterToken(h,kUserLexTId+1,"+");
  cmLexRegisterToken(h,kUserLexTId+2,"-");
  
  unsigned tid;
  
  // ask for token id's 
  while( (tid = cmLexGetNextToken(h)) != kEofLexTId )
  {
    // print information about each token
    cmRptPrintf(rpt,"%i %i %s '%.*s' (%i) ", 
    cmLexCurrentLineNumber(h), 
    cmLexCurrentColumnNumber(h), 
    cmLexIdToLabel(h,tid), 
    cmLexTokenCharCount(h), 
    cmLexTokenText(h) , 
    cmLexTokenCharCount(h));
    
    // if the token is a number ...
    if( tid==kIntLexTId || tid==kRealLexTId || tid==kHexLexTId )
    {
      // ... then request the numbers value
      int    iv = cmLexTokenInt(h);
      double dv = cmLexTokenDouble(h);
      
      cmRptPrintf(rpt,"%i %f",iv,dv);
    }
    
    cmRptPrintf(rpt,"\n");
    
    // handle errors
    if( tid == kErrorLexTId )
    {
      cmRptPrintf(rpt,"Error:%i\n", cmLexErrorRC(h));
      break;
    }
    
  }
  
  // finalize the lexer 
  cmLexFinal(&h);
  
}