bool Read() { bool result = false; EatSpace(); if (index < length) { result = true; char c = Current(); if (IsAlpha(c) || c == '_') ParseIdentifier(); else if (IsDigit(c)) ParseIntegerLiteral(); else if (c == '"') ParseStringLiteral(); else if (c == '\'') ParseCodePointLiteral(); else if (IsSymbol(c)) ParseSymbols(); else result = false; } return result; }
bool Lexer::Next() { assert(_currentCursor >= 0); assert(_currentCursor <= _text.length()); EatSpace(); if (_currentCursor == _text.length()) { _lexeme.reset(); return false; } _beginCursor = _currentCursor; _beginLine = _currentLine; _beginRow = _currentRow; const std::function<LexemeSptr()> readers[] = { [&] { return TryReadSimple(); }, [&] { return TryReadSymbol(); }, [&] { return TryReadString(); }, [&] { return TryReadNumber(); } }; for (const auto &reader : readers) { auto nextLexeme = reader(); if (nextLexeme) { _lexeme = nextLexeme; return !_lexeme->IsError(); } } _lexeme = NewErrorLexeme(LexerErrorCode::UnknownLexeme); return false; }
/* parse the program options */ int parsop(pScene sc,pMesh mesh) { FILE *in; pMaterial pm; double dd; float r,g,b,ca,cb,cc,na,nb,nc; int k,i,m,n,xs,ys,ret,ref,nbmat; char *ptr,ub,data[128],key[256],buf[256],pscol[32]; /* check if user-specified parameters */ iniopt(sc,mesh); strcpy(data,mesh->name); ptr = (char *)strstr(data,".mesh"); if ( ptr ) *ptr = '\0'; in = 0; if ( !strstr(data,".medit") ) { strcat(data,".medit"); in = fopen(data,"r"); } if ( !in ) { sprintf(data,"%s",DEFAULT_FILE); in = fopen(data,"r"); if ( !in ) { fprintf(stdout," Loading default options\n"); sc->par.nbmat = MAX_MATERIAL; matInit(sc); return(1); } } if ( !quiet ) fprintf(stdout," Reading %s\n",data); m = n = 0; while ( !feof(in) ) { fscanf(in,"%s",key); if ( feof(in) ) break; for (i=0; i<strlen(key); i++) key[i] = tolower(key[i]); if ( key[0] == '#' ) { fgets(key,255,in); } else if ( !strcmp(key,"backgroundcolor") ) { fscanf(in,"%f %f %f",&r,&g,&b); sc->par.back[0] = r; sc->par.back[1] = g; sc->par.back[2] = b; sc->par.back[3] = 1.0f; } else if ( !strcmp(key,"boundingbox") ) { fscanf(in,"%f %f %f %f %f %f",&ca,&cb,&cc,&na,&nb,&nc); mesh->xmin = ca; mesh->ymin = cb; mesh->zmin = cc; mesh->xmax = na; mesh->ymax = nb; mesh->zmax = nc; } else if ( !strcmp(key,"clipplane") ) { fscanf(in,"%f %f %f %f %f %f",&ca,&cb,&cc,&na,&nb,&nc); sc->par.clip[0] = ca - mesh->xtra; sc->par.clip[1] = cb - mesh->ytra; sc->par.clip[2] = cc - mesh->ztra; dd = sqrt(na*na + nb*nb + nc*nc); if ( dd > EPS ) { sc->par.clip[3] = na / dd; sc->par.clip[4] = nb / dd; sc->par.clip[5] = nc / dd; } } else if ( !strcmp(key,"linecolor") ) { fscanf(in,"%f %f %f",&r,&g,&b); sc->par.line[0] = r; sc->par.line[1] = g; sc->par.line[2] = b; sc->par.linc = 1; } else if ( !strcmp(key,"linewidth") ) { fscanf(in,"%f",&r); sc->par.linewidth = max(1.0,min(10.0,r)); sc->par.linc = 1; } else if ( !strcmp(key,"pointsize") ) { fscanf(in,"%f",&r); sc->par.pointsize = max(1.0,min(10.0,r)); sc->par.linc = 1; } else if ( !strcmp(key,"edgecolor") ) { fscanf(in,"%f %f %f",&r,&g,&b); sc->par.edge[0] = r; sc->par.edge[1] = g; sc->par.edge[2] = b; sc->par.linc = 1; } else if ( !strcmp(key,"sunposition") ) { fscanf(in,"%f %f %f",&r,&g,&b); sc->dmax = mesh->xmax - mesh->xmin; sc->dmax = max(sc->dmax,mesh->ymax - mesh->ymin); sc->dmax = max(sc->dmax,mesh->zmax - mesh->zmin); sc->dmax = fabs(sc->dmax); sc->par.sunpos[0] = 2.0*sc->dmax*r; sc->par.sunpos[1] = 2.0*sc->dmax*g; sc->par.sunpos[2] = 2.0*sc->dmax*b; sc->par.sunp = 1; } else if ( !strcmp(key,"windowsize") ) { fscanf(in,"%d %d",&xs,&ys); sc->par.xs = (short)xs; sc->par.ys = (short)ys; } else if ( !strcmp(key,"rendermode") ) { fscanf(in,"%s",buf); for (i=0; i<strlen(buf); i++) buf[i] = tolower(buf[i]); if ( strstr(buf,"hidden") ) sc->mode = HIDDEN; else if ( strstr(buf,"fill") ) sc->mode = FILL; else if ( strstr(buf,"colorshadingline") ) sc->mode = SHADED + S_MATERIAL; else if ( strstr(buf,"colorshading") ) sc->mode = S_FILL + S_COLOR + S_MATERIAL; else if ( strstr(buf,"shading") ) sc->mode = SHADED; } else if ( strstr(key,"palette") ) { sc->iso.palette = 1; if ( !strcmp(key,"palettet") ) sc->iso.palette = 1; else if ( !strcmp(key,"paletteb") ) sc->iso.palette = 2; else if ( !strcmp(key,"palettel") ) sc->iso.palette = 3; else if ( !strcmp(key,"paletter") ) sc->iso.palette = 4; for (k=0; k<MAXISO; k++) ret = fscanf(in,"%f",&sc->iso.val[k]); if ( sc->iso.val[MAXISO-1] < sc->iso.val[0] ) sc->iso.palette = 0; } else if ( !strcmp(key,"postscript") ) { fscanf(in,"%f %f %s %s",&sc->par.cm,&sc->par.dpi, buf,pscol); strncpy(sc->par.pscolor,pscol,10); sc->par.coeff = atof(buf); if ( sc->par.coeff < 0.0f ) sc->par.coeff = 0.0f; if ( sc->par.coeff > 1.0f ) sc->par.coeff = 1.0f; } else if ( !strcmp(key,"time") ) { ret = fscanf(in,"%f %f %f",&sc->par.maxtime,&sc->par.pertime,&sc->par.dt); if ( !EatSpace(in) ) { fscanf(in,"%c",&ub); sc->par.nbpart = max(atoi(&ub),1); } } else if ( !strcmp(key,"nbmaterial") ) { fscanf(in,"%d",&nbmat); sc->par.nbmat = max(2,nbmat); matInit(sc); } else if ( !strcmp(key,"material") ) { if ( sc->par.nbmat == -1 ) { sc->par.nbmat = MAX_MATERIAL; matInit(sc); } fgets(buf,255,in); if ( n > sc->par.nbmat ) continue; ret = sscanf(buf,"%s %d",buf,&ref); ptr = strstr(buf,"DEFAULT"); pm = ptr ? &sc->material[DEFAULT_MAT] : &sc->material[++n]; strcpy(pm->name,buf); if ( ret < 2 ) ref = 0; pm->ref = ref ? ref : n; fscanf(in,"%f %f %f %f",&pm->amb[0],&pm->amb[1],&pm->amb[2],&pm->amb[3]); fscanf(in,"%f %f %f %f",&pm->dif[0],&pm->dif[1],&pm->dif[2],&pm->dif[3]); fscanf(in,"%f %f %f %f",&pm->spe[0],&pm->spe[1],&pm->spe[2],&pm->spe[3]); fscanf(in,"%f %f %f %f",&pm->emi[0],&pm->emi[1],&pm->emi[2],&pm->emi[3]); fscanf(in,"%f",&pm->shininess); if ( pm->amb[3] == 0.0 ) pm->amb[3] = 1.0; if ( pm->dif[3] == 0.0 ) pm->dif[3] = 1.0; if ( pm->spe[3] == 0.0 ) pm->spe[3] = 1.0; if ( pm->emi[3] == 0.0 ) pm->emi[3] = 1.0; pm->shininess = min(fabs(pm->shininess),128.0f); pm->shininess = max(pm->shininess,3.0f); ++m; } /* stereo mode */ else if ( !strcmp(key,"eyesep") ) { fscanf(in,"%f",&sc->par.eyesep); } } fclose(in); if ( sc->par.nbmat < 0 ) { sc->par.nbmat = MAX_MATERIAL; matInit(sc); } else if ( m == n ) sc->par.nbmat++; if ( !sc->par.linc ) { sc->par.line[0] = 1.0 - sc->par.back[0]; sc->par.line[1] = 1.0 - sc->par.back[1]; sc->par.line[2] = 1.0 - sc->par.back[2]; } if ( ddebug ) fprintf(stdout," Materials %8d\n",sc->par.nbmat); return(1); }
int bbfile(pMesh mesh) { FILE *in; pSolution ps; double a,b,c,lambda[3],eigv[3][3],m[6],vp[2][2]; float dummy; int j,k,l,dim,np,nfield,nf,i1,i2,i3,i4,err,iord; char *ptr,data[128],tmp[128]; ubyte bigbb; /* default */ strcpy(tmp,mesh->name); ptr = (char *)strstr(tmp,".mesh"); if ( ptr ) *ptr = '\0'; sprintf(data,"%s.bb",tmp); in = fopen(data,"r"); bigbb = 0; if ( !in ) { sprintf(data,"%s.pbb",tmp); in = fopen(data,"r"); } if ( !in ) { bigbb = 1; sprintf(data,"%s.BB",tmp); in = fopen(data,"r"); if ( !in ) { /* hack FH pour le mac */ sprintf(data,"%s.gbb",tmp); in = fopen(data,"r"); } } if ( !in ) return(0); /* if ( !quiet ) fprintf(stdout," Reading %s\n",data); */ i1=i2=i3=i4=-1; /* read file format */ err=0; fscanf(in,"%d",&dim); if(EatSpace(in)) err++; fscanf(in,"%d",&i1); if(EatSpace(in)) err++; fscanf(in,"%d",&i2); if(EatSpace(in)) err++; fscanf(in,"%d",&i3); bigbb= (EatSpace(in)==0); /* not nl after the 4 integer => BB */ if ( !quiet ) if(bigbb) fprintf(stdout," Reading BB file %s\n",data); else fprintf(stdout," Reading bb file %s\n",data); if ( dim < 2 || dim > 3 || err ) { fprintf(stderr," %%%% Wrong file (dim=%d) (err=%d). Ignored\n",dim,err); return(0); } /* read number of field(s) */ nf = 0; if ( bigbb ) { /* get only 1st field */ /* fscanf(in,"%d",&nfield);*/ nfield=i1; /*fscanf(in,"%d",&mesh->nfield);*/ mesh->nfield = i2; if (nfield>1) { nf += i3; for (k=1; k<nfield-1; k++) { fscanf(in,"%d",&np); nf += np; } fscanf(in,"%d",&np); } else np = i3; /* read file type */ fscanf(in,"%d",&mesh->typage); printf(" np= %d, type= %d\n",np,mesh->typage); } else { /* fscanf(in,"%d",&mesh->nfield); fscanf(in,"%d",&np);*/ /* read file type */ /* fscanf(in,"%d",&mesh->typage);*/ mesh->nfield=i1; np=i2; mesh->typage=i3; } if ( mesh->typage == 2 ) { if ( np < mesh->np ) { fprintf(stderr," %%%% Wrong solution number (%d , %d). Ignored\n",np,mesh->np); fclose(in); return(0); } mesh->nbb = mesh->np; } else if ( mesh->typage == 1 ) { if ( np < mesh->ne ) { fprintf(stderr," %%%% Wrong solution number (%d , %d). Ignored\n",np,mesh->ne); fclose(in); return(0); } mesh->nbb = mesh->ne; } else { fprintf(stderr," %%%% Wrong typage (%d). Ignored\n",mesh->typage); fclose(in); return(0); } /* read solutions */ mesh->bbmin = 1.e10; mesh->bbmax = -1.e10; /* allocate memory */ if ( !zaldy2(mesh) ) { mesh->nbb = 0; fclose(in); return(0); } /* scalar field */ if ( mesh->nfield == 1 ) { if ( ddebug ) printf(" scalar (isotropic) field\n"); for (k=1; k<=mesh->nbb; k++) { ps = &mesh->sol[k]; ps->bb = 0.0; if ( fscanf(in,"%s",data) != 1 ) continue; if ( ptr = strpbrk(data,"dD") ) *ptr = 'E'; sscanf(data,"%f",&ps->bb); if ( ps->bb < mesh->bbmin ) mesh->bbmin = ps->bb; if ( ps->bb > mesh->bbmax ) mesh->bbmax = ps->bb; for (j=1; j<=nf; j++) fscanf(in,"%f",&dummy); } } /* vector field */ else if ( mesh->nfield == mesh->dim ) { if ( ddebug ) fprintf(stdout," vector field \n"); for (k=1; k<=mesh->nbb; k++) { ps = &mesh->sol[k]; ps->bb = 0.0; for (l=0; l<mesh->dim; l++) { if ( fscanf(in,"%s",data) != 1 ) continue; if ( ptr = strpbrk(data,"dD") ) *ptr = 'E'; sscanf(data,"%f",&ps->m[l]); ps->bb += ps->m[l]*ps->m[l]; } ps->bb = sqrt(ps->bb); if ( ps->bb < mesh->bbmin ) mesh->bbmin = ps->bb; if ( ps->bb > mesh->bbmax ) mesh->bbmax = ps->bb; for (j=1; j<nf; j++) fscanf(in,"%f",&dummy); } fclose(in); return(0); } else if ( dim == 2 && mesh->nfield == 3 ) { if ( ddebug ) fprintf(stdout," 2D metric field\n"); for (k=1; k<=mesh->np; k++) { ps = &mesh->sol[k]; fscanf(in,"%lf %lf %lf",&a,&b,&c); ps->m[0] = a; ps->m[1] = b; ps->m[2] = c; m[0] = a; m[1] = b; m[2] = c; eigen2(m,lambda,vp); ps->bb = MEDIT_MIN(lambda[0],lambda[1]); if ( ps->bb < mesh->bbmin ) mesh->bbmin = ps->bb; if ( ps->bb > mesh->bbmax ) mesh->bbmax = ps->bb; for (j=1; j<nf; j++) fscanf(in,"%f",&dummy); } } else if ( dim == 3 && mesh->nfield == 6 ) { if ( ddebug ) fprintf(stdout," 3D metric field\n"); for (k=1; k<=mesh->np; k++) { ps = &mesh->sol[k]; ps->bb = 0.0f; for (l=0; l<6; l++) { if ( fscanf(in,"%s",data) != 1 ) continue; if ( ptr = strpbrk(data,"dD") ) *ptr = 'E'; sscanf(data,"%f",&dummy); m[l] = dummy; } ps->m[0] = m[0]; ps->m[1] = m[1]; ps->m[2] = m[3]; ps->m[3] = m[2]; ps->m[4] = m[4]; ps->m[5] = m[5]; m[2] = ps->m[2]; m[3] = ps->m[3]; iord = eigenv(1,m,lambda,eigv); if ( iord ) { ps->bb = lambda[0]; ps->bb = MEDIT_MAX(ps->bb,lambda[1]); ps->bb = MEDIT_MAX(ps->bb,lambda[2]); if ( ps->bb < mesh->bbmin ) mesh->bbmin = ps->bb; if ( ps->bb > mesh->bbmax ) mesh->bbmax = ps->bb; } else { fprintf(stdout," ## Eigenvalue problem.\n"); } for (j=1; j<nf; j++) fscanf(in,"%f",&dummy); } } else { fprintf(stderr," %%%% Solution not suitable. Ignored\n"); mesh->nbb = 0; } fclose(in); return(np); }