[d14593]: PTOparser / panoScanner.l  Maximize  Restore  History

Download this file

320 lines (244 with data), 9.3 kB

/*
 *  panoScanner.h
 *
 *  Copyright  Daniel M. German
 *  
 *  April 2007
 *
 *  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 software 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 software; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  Author: Daniel M German dmgerman at uvic doooot ca
 * 
 *  This lex input file implements the panotools and hugin input scripts
 *
 */

%option noyywrap

%{
/* need this for the call to atof() below */
#include <math.h>
#include <string.h>
#include "tparserdebug.h"
#include "tparser.h"
#include "tparserprivate.h"
#include "panoParser.h"

char strBuffer[PT_TOKEN_MAX_LEN+1];
char *strBuffer_ptr;

double ptTokenInt;
double ptTokenFloat;
char ptTokenLineType;

#define YY_INPUT(buf,result,max_size)  {\
    result = panoScriptScannerGetNextChar(buf, max_size); \
    if (  result <= 0  ) \
      result = YY_NULL; \
    }

%}

DIGIT    [0-9]
LINETYPE [a-zA-Z]
ID       [a-zA-Z]

%x str
%x parm
%x hugin


%%

  /* Simple rules for all purpose */

^[ \t]*\n    { /* Ignore empty lines */
              DEBUG_1("Empty line\n");
            }

    /* Simple rules to read numbers */

 
[-+]?{DIGIT}+    { /* Simple integer */
                DEBUG_3( "An integer: %s (%d)\n", yytext, atoi( yytext ) );
                panoScriptScannerTokenBegin(yytext); 
                yylval.fVal = atof( yytext );
                return PT_TOKEN_NUMBER;
               }

[-+]?{DIGIT}+"."{DIGIT}* { /* float point with period */
               /*printf( "A float: %s (%g)\n", yytext, atof( yytext ) );*/
                panoScriptScannerTokenBegin(yytext); 
                yylval.fVal = atof( yytext );
                return PT_TOKEN_NUMBER;
            }

[ \t]*\n    { /* END OF LINE is an important marker when processing a rule */
            DEBUG_2( "End of line", yytext );
            panoScriptScannerTokenBegin(yytext); 
            return PT_TOKEN_EOL;
            }

<<EOF>>     { /* We are done */
               DEBUG_1("End of file\n");
               panoScriptScannerTokenBegin(yytext); 
               return PT_TOKEN_EOF;
            }

[ \t]+      {  /* token separator */
               panoScriptScannerTokenBegin(yytext); 
               return PT_TOKEN_SEP;
            }


  /* Things we are currently ignoring */


[K][0-9A-Za-z.=-]+[ \t]* {  
               panoScriptScannerTokenBegin(yytext);
             }

^k.*\n       {  
               panoScriptScannerTokenBegin(yytext);
             }



  /****************************************/
  /* These are the types of lines supported */
   
^o           {   /* Output line */ 
                panoScriptScannerTokenBegin(yytext); 
                return PT_TOKEN_OUTPUT_LINE;
             }

^i           {  /* input line */
                panoScriptScannerTokenBegin(yytext); 
                return PT_TOKEN_INPUT_LINE;
             }

^p           {  /* panorama line */
                panoScriptScannerTokenBegin(yytext); 
                return PT_TOKEN_PANO_LINE;
             }

^m           {  /* Optimization options line */
                panoScriptScannerTokenBegin(yytext); 
                return PT_TOKEN_OPTIMIZE_OPT_LINE;
             }

^c  {
             panoScriptScannerTokenBegin(yytext); 
             return PT_TOKEN_CONTROL_PT_LINE;
             }

^C  {
             panoScriptScannerTokenBegin(yytext); 
             return PT_TOKEN_MORPH_PT_LINE;
             }

^v          {  
               panoScriptScannerTokenBegin(yytext); 
               return PT_TOKEN_OPTIMIZE_VARS_LINE;

            } 


^#" hugin project file, version" { /* hugin header file for PTO files */
              DEBUG_1("In PTo version line\n");
              panoScriptScannerTokenBegin(yytext); 
              return PT_TOKEN_HUGIN_VERSION;
            }

^#"hugin_ptoversion" { /* hugin header file for PTO files */
              DEBUG_1("In PTo version line\n");
             panoScriptScannerTokenBegin(yytext); 
              return PT_TOKEN_HUGIN_VERSION;
            }

^#"-hugin".*\n { /* hugin options*/
              DEBUG_1("Ignored hugin options\n");
            }

^#"hugin".*\n { /* hugin options*/
              DEBUG_1("Ignored hugin options\n");
            }


          /********************************/
          /* Main tokens in different libpano lines */
","           { 
              panoScriptScannerTokenBegin(yytext); 
              BEGIN(parm);
              return PT_TOKEN_COMMA;  
              }

<parm>=      {  /* Reference to a variable */

              panoScriptScannerTokenBegin(yytext); 
              return PT_TOKEN_REFERENCE;  
              }


[CS]        {
             /* These are the C and S parameters that contain multiple values */
               strncpy(yylval.strVal, yytext, PT_TOKEN_MAX_LEN);
               BEGIN(parm);
               return PT_TOKEN_KEYWORD_CROPPING;
            } 


<parm>[+-]?{DIGIT}+   { /* Simple integer */
                DEBUG_3( "An integer: %s (%d)\n", yytext, atoi( yytext ) );
                /* move back to default mode */
                BEGIN(INITIAL);
                panoScriptScannerTokenBegin(yytext); 
                yylval.fVal = atof( yytext );
                return PT_TOKEN_NUMBER;
               }

<parm>[+-]?{DIGIT}+"."{DIGIT}*   { /* a float */
                DEBUG_3( "An float: %s (%f)\n", yytext, atof( yytext ) );
                /* move back to default mode */
                BEGIN(INITIAL);
                panoScriptScannerTokenBegin(yytext); 
                yylval.fVal = atof( yytext );
                return PT_TOKEN_NUMBER;
               }



<parm>a-zA-Z[0-9a-zA-Z]+ {
                  /* Read the rest of the parameter as a string by default */
                   /* move back to default mode */
                   BEGIN(INITIAL);
                   panoScriptScannerTokenBegin(yytext); 
                   strncat(strBuffer, yytext, PT_TOKEN_MAX_LEN -1);
                   strncpy(yylval.strVal, strBuffer, PT_TOKEN_MAX_LEN);
                   return PT_TOKEN_STRING;
                }


  /* Some variables now have are now more than 1 char long */

Tr[XYZ]|E[erb]v?|V[mabcdxy]|R[abcde]     {
                  panoScriptScannerTokenBegin(yytext); 
                  strncpy(yylval.strVal, yytext, PT_TOKEN_MAX_LEN);
                  BEGIN(parm);
                  return PT_TOKEN_KEYWORD_MULTICHAR;
            }


   /* The default is only one character */

{ID}        {
                DEBUG_2( "An identifier for a variable: %s\n", yytext );
                /* move into parameter mode */
                BEGIN(parm);
                panoScriptScannerTokenBegin(yytext); 
                strncpy(yylval.strVal, yytext, PT_TOKEN_MAX_LEN);
                return PT_TOKEN_KEYWORD;
             }


  /********************************/
   /* From this on process strings */
<parm>\"       { /* Begin of string */
                 panoScriptScannerTokenBegin(yytext); 
                 strBuffer_ptr = strBuffer; 
                 BEGIN(str);
                 //printf("Beginning of string\n");
               }

<str>\"        { /* saw closing quote - all done */
                   BEGIN(INITIAL);
                   *strBuffer_ptr = '\0';
                   /* return string constant token type and
                    * value to parser
                    */
              //     printf("A string %s\n", strBuffer);
                   strncpy(yylval.strVal, strBuffer, PT_TOKEN_MAX_LEN);
                   return PT_TOKEN_STRING;
                }

<str>\n        {
                 /* error - unterminated string constant */
                 /* generate error message */
                 panoScriptParserError(1,"non-terminated string: %s\n", yytext ); 
                 return -0;
               }

<str>\\n  *strBuffer_ptr++ = '\n';
<str>\\t  *strBuffer_ptr++ = '\t';
<str>\\r  *strBuffer_ptr++ = '\r';
<str>\\b  *strBuffer_ptr++ = '\b';
<str>\\f  *strBuffer_ptr++ = '\f';

<str>\\(.|\n)  *strBuffer_ptr++ = yytext[1];

<str>[^\\\n\"]+    {
                     int currentLen = strlen(strBuffer);
                     char *yptr = yytext;
                     while ( *yptr && ++currentLen < PT_TOKEN_MAX_LEN)
                           *strBuffer_ptr++ = *yptr++;
                     panoScriptScannerTokenBegin(yytext); 
                   }

  /* Ignore comments. If the line starts with #h or #- then it is a hugin-option line... ugly */

^#[^h-].*\n     {
                  DEBUG_1("Comment\n");
                }




  /********************************/
  /* Catch errors */
.           { 
               panoScriptParserError(1,"Unrecognized character: [%s]\n", yytext );
               return PT_TOKEN_ERROR;
            }



%%