Example #1
0
LEElement *LEElementPool::parseExpression(const char * line) {

	LEElement *first = NULL;
	LEElement *last = NULL;

	while (true) {
		line = getNextToken(line, parsingBuffer);

		if (line == NULL) {
			/**
			 * No more tokens in this line
			 */
			return first;
		}

		LEElement *n = next();

		if (isNumeric(parsingBuffer)) {
			n->init(LE_NUMERIC_VALUE, atoff(parsingBuffer));
		} else {
			le_action_e action = parseAction(parsingBuffer);
			if (action == LE_UNDEFINED) {
				/**
				 * Cannot recognize token
				 */
				warning((obd_code_e) 0, "unrecognized [%s]", parsingBuffer);
				return NULL;
			}
			n->init(action);
		}

		if (first == NULL) {
			first = n;
			last = n;
		} else {
			last->next = n;
			last = last->next;
		}
	}
	return first;
}
Example #2
0
static PSmmTypeInfo parseType(PSmmParser parser) {
	PSmmTypeInfo typeInfo = NULL;
	if (parser->curToken->kind != tkSmmIdent) {
		if (parser->curToken->kind != tkSmmErr) {
			char gotBuf[4];
			const char* got = smmTokenToString(parser->curToken, gotBuf);
			smmPostMessage(parser->msgs, errSmmGotUnexpectedToken, parser->curToken->filePos, "type", got);
		}
		typeInfo = &builtInTypes[tiSmmUnknown];
	} else {
		PSmmAstNode typeInfoNode = ibsDictGet(parser->idents, parser->curToken->repr);
		if (!typeInfoNode || typeInfoNode->kind != nkSmmType) {
			smmPostMessage(parser->msgs, errSmmUnknownType, parser->curToken->filePos, parser->curToken->repr);
			typeInfo = &builtInTypes[tiSmmUnknown];
		} else {
			typeInfo = typeInfoNode->type;
		}
		getNextToken(parser);
	}
	return typeInfo;
}
Example #3
0
int main()
{
	int n = 0; char c;
	f = fopen("9.txt", "r");
	if (f == NULL)
		err("Eroare la fisier!");
	while ((c = fgetc(f)) != EOF)
		text[n++] = c;
	fclose(f);
	pointer_urmator = text;
	do
		getNextToken();
	while (lastToken->code != END);
	showAtoms();
	initSymbols(&symbols);
	AddExtFunctions();
	currentToken = tokens;
	unit();
	afisareSymbols(&symbols);
	return 0;
}
Example #4
0
//-----------------------------------------
// This function not used in R1
// getToken(i) returns ith token without advancing
// in token stream.  getToken(0) returns 
// previousToken.  getToken(1) returns currentToken.
// getToken(2) returns next token, and so on.
//
TOKEN *getToken(int i)
{
    TOKEN *t;
    int j;
    if (i <= 0)
      return previousToken;

    t = currentToken;
    for (j = 1; j < i; j++)  // loop to ith token
    {
      // if next token is on token list, move t to it
      if (t -> next != NULL)
        t = (t -> next);

      // Otherwise, get next token from token mgr and 
      // put it on the list.
      else
        t = (t -> next) = getNextToken();
    }
    return t;
}
Example #5
0
/// top ::= definition | external | expression | ';'
static void MainLoop() {
  while (1) {
    fprintf(stderr, "ready> ");
    switch (CurTok) {
    case tok_eof:
      return;
    case ';': // ignore top-level semicolons.
      getNextToken();
      break;
    case tok_def:
      HandleDefinition();
      break;
    case tok_extern:
      HandleExtern();
      break;
    default:
      HandleTopLevelExpression();
      break;
    }
  }
}
Example #6
0
char *OpenDDLParser::parseProperty( char *in, char *end, Property **prop ) {
    *prop = ddl_nullptr;
    if( ddl_nullptr == in || in == end ) {
        return in;
    }

    in = lookForNextToken( in, end );
    Text *id( ddl_nullptr );
    in = parseIdentifier( in, end, &id );
    if( ddl_nullptr != id ) {
        in = lookForNextToken( in, end );
        if( *in == '=' ) {
            ++in;
            in = getNextToken( in, end );
            Value *primData( ddl_nullptr );
            if( isInteger( in, end ) ) {
                in = parseIntegerLiteral( in, end, &primData );
                createPropertyWithData( id, primData, prop );
            } else if( isFloat( in, end ) ) {
                in = parseFloatingLiteral( in, end, &primData );
                createPropertyWithData( id, primData, prop );
            } else if( isStringLiteral( *in ) ) { // string data
                in = parseStringLiteral( in, end, &primData );
                createPropertyWithData( id, primData, prop );
            } else {                              // reference data
                std::vector<Name*> names;
                in = parseReference( in, end, names );
                if( !names.empty() ) {
                    Reference *ref = new Reference( names.size(), &names[ 0 ] );
                    ( *prop ) = new Property( id );
                    ( *prop )->m_ref = ref;
                }
            }
        } else {
            delete id;
        }
    }

    return in;
}
Example #7
0
//start programu. PROGRAM-> FUNKCE . eof
tChyba PROGRAM () {					
	//inicializace labelu pro if a while na 0
	lab = 0;	
	lab_while = 0;						//nacitani prvniho tokenu je v komentu, protoze na konci je pomocna funkce, kde se uz nacita token, pri vyslednem kodu, bude toto nacteni tokenu odkomentovano
	/*token = getNextToken();
	if(token.stav == s_lex_error) {			
		return S_LEXIKALNI_CHYBA;
	}*/
	//lokalni promenna, do ni se nacita vystup funkci	
	int analyza;					
	//vytvoreni globalni a lokalni(main) hash tabulky
	int ret = initFrames();
	byla_funkce = false;
	pocitani_parametru = 0;

	if(ret != S_BEZ_CHYB ) {
		return S_INTERNI_CHYBA;
	}

	if(token.stav == s_klicove && (!strcmp(token.data, "function") || !strcmp(token.data, "var") || !strcmp(token.data, "begin"))) {
							//podminka kde se kontroluje, zda nacteny token je klicove slovo a nasledne se rozhodne, ktere konkretne. Pote dojde k zavolani funkce FUNKCE, kde jsou 3 pravidla, ktere zacinaji terminala vyse
		analyza = FUNKCE ();
		if (analyza != S_BEZ_CHYB) {		//pokud se vrati po zavolani FUNKCE() s_bez_chyb pokracuje se dal, jinak se vraci chybovy kod, ktery funkce vratila

		return analyza;
		}

		if(token.stav == s_tecka) {		//porovnava se ciselna hodnota tokenu s ciselnou hodnotu tecky (tecka neni primo nadefinovana jako tecka, ale je pro ni vyhrazen unikatni identifikator ciselny, ktery porovnavam s id prvku v nactenem tokenu)
			token = getNextToken();
			if(token.stav == s_lex_error) {	//jakmile nacitam token okamzite kontroluju, jestli se nevyskytla lexikalni chyba
				return S_LEXIKALNI_CHYBA;
			}

			if(token.stav == s_eof) {	//jakmile dojdeme ke konci souboru, vrati funkce syntakticka analyza ok a mame vyhrano
				return S_BEZ_CHYB;
			}
		}
	}
		return S_SYNTAKTICKA_CHYBA;		//pokud nejaky token neodpovida predem danym terminalum z pravidel, vraci chybu
}
Example #8
0
XmlAttribute * XmlReader::parseAttribute () {
    char * attr = getNextToken ();
    if (attr == NULL || *attr == 0) {
        free (attr);
        return NULL; // something unexpected: not an iden nor '/' or '>'
    }
    if (skipSpaces () != '=') {
        free (attr);
        MESSAGE ("parseAttributes: '=' expected after %s at line #%d\n", attr, m_nbLines);
        m_failure = true;
        return NULL;
    }
    getNextChar (); // eat '='
    char * value = getString (); // getString will eat spaces if needed
    if (value == NULL) {
        free (attr);
        MESSAGE ("parseAttributes: malformed quoted string after %s=\n", attr);
        m_failure = true;
        return NULL;
    }
    return new XmlAttribute (attr, value);
}
Example #9
0
static int
parseOrder(char *orderReq, struct resVal *resVal, struct lsInfo *lsInfo)
{
    int i, m, entry;
    char *token, negate;

    if ((i=strlen(orderReq)) == 0)
        return PARSE_OK;

    for(m=0; m<i; m++)
        if (orderReq[m] != ' ')
            break;

    if (m == i)
        return PARSE_OK;

    resVal->nphase = 0;
    while ((token = getNextToken(&orderReq)) != NULL) {
        negate = FALSE;
        if (token[0] == '-') {
            negate = TRUE;
            token++;
        }
        entry = getResEntry(token);
        if ((entry < 0) || (lsInfo->resTable[entry].orderType == NA))
            return PARSE_BAD_NAME;

        if (resVal->nphase > NBUILTINDEX)
            break;

        if (negate)
            resVal->order[resVal->nphase] = -(entry +1);
        else
            resVal->order[resVal->nphase] =  entry + 1;
        resVal->nphase++;
    }
    resVal->options |= PR_ORDER;
    return PARSE_OK;
}
Example #10
0
int
getValPair(char **resReq, int *val1, int *val2)
{
    char *token, *cp, *wd1 = NULL, *wd2 = NULL;
    int len;

    *val1 = INFINIT_INT;
    *val2 = INFINIT_INT;

    token = getNextToken (resReq);
    if (!token)
        return 0;
    len = strlen (token);
    if (len == 0)
        return 0;
    cp = token;
    while (*cp != '\0' && *cp != ',' && *cp != '/')
        cp++;
    if (*cp != '\0') {
        *cp = '\0';
        if (cp - token > 0)
            wd1 = token;
        if (cp - token < len - 1)
            wd2 = ++cp;
    } else
        wd1 = token;
    if (wd1 && !isint_(wd1))
        return -1;
    if (wd2 && !isint_(wd2))
        return -1;
    if (!wd1 && !wd2)
        return -1;
    if (wd1)
        *val1 = atoi(wd1);
    if (wd2)
        *val2 = atoi(wd2);

    return 0;
}
Example #11
0
static PSmmToken expect(PSmmParser parser, uint32_t kind) {
	PSmmToken token = parser->curToken;
	struct SmmFilePos filePos = token->filePos;
	if (token->kind != kind) {
		if (token->kind != tkSmmErr && token->filePos.lineNumber != parser->lastErrorLine) {
			// If it is smmErr, lexer already reported the error
			char expBuf[4];
			char tmpRepr[2] = { (char)kind, 0 };
			struct SmmToken tmpToken = { kind };
			tmpToken.repr = tmpRepr;
			const char* expected = smmTokenToString(&tmpToken, expBuf);
			if (token->isFirstOnLine && parser->prevToken) {
				filePos = parser->prevToken->filePos;
			}
			smmPostMessage(parser->msgs, errSmmNoExpectedToken, filePos, expected);
		}
		parser->lastErrorLine = filePos.lineNumber;
		return NULL;
	}
	getNextToken(parser);
	return token;
}
Example #12
0
void MeshParser::parseTokens()
{
	std::string currentToken;
	MeshParser::TokenType currentTokenType;

	while(tokens.size() != 0){

		//get next token
		currentToken = getNextToken();

		//detrmine token type
		currentTokenType = getTokenType(currentToken);
		
		//void (MeshParser::*TokenFunc) (std::string&) = StateFunction[currentTokenType];
		//(this->*TokenFunc)(currentToken);

		switch(currentTokenType){
		case positionsKeyword:
			gotPositionsToken(currentToken);
			break;
		case normalsKeyword:
			gotNormalsToken(currentToken);
			break;
		case skinWeightsKeyword:
			gotSkinWeightsToken(currentToken);
			break;
		case trianglesKeyword:
			gotTrianglesToken(currentToken);
			break;
		case bindingsKeyword:
			gotBindingsToken(currentToken);
			break;
		default:
			gotBadToken(currentToken);
			break;
		}
	}
}
Example #13
0
void
MainLoop()
{
    for (;;) {
        std::cout << "ready> ";
        switch(CurTok) {
        case tok_eof:
            return;
        case ';':
            getNextToken();
            break; // ignore at top level
        case tok_def:
            HandleDefinition();
            break;
        case tok_extern:
            HandleExtern();
            break;
        default:
            HandleTopLevelExpression();
            break;
        }
    }
}
Example #14
0
void read(string token){
	if(NT == 4000 && token != keyword){
		cout << "Error: Token mismatch expected : " << keyword << "\n";
		exit(1);
	}
	if (NT >= 1000 && NT < 4000){
		string s = "";
		if(NT == 1000){	
			if(token == "id"){
				s = "<ID:" + keyword + ">";					
			}else{
				cout << "Error: Token mismatch expected: " << keyword << "\n";
				exit(1);
			}			
		}else if(NT == 2000){			
			if(token == "int"){
				s = "<INT:" + keyword + ">";						
			}else{
				cout << "Error: Token mismatch expected: " << keyword << "\n";
				exit(1);
			}				
		}else if(NT == 3000){
			if(token == "str"){
				s = "<STR:" + keyword + ">";			
			}else{
				cout << "Error: Token mismatch expected: " << keyword << "\n";
				exit(1);
			}			
		}else{
			cout << ("Error!");
			exit(1);			
		}

		buildTree(s, 0);
	}
	NT = getNextToken();
}
pdf::Reference PdfReader::readCrt(unsigned crtOffset, pdf::CrossReferenceTable& crt){
  TR_USE(PDF_Reader);
  mReader->jumpTo(crtOffset);
  ASSERT_TOKEN("xref");
  crt.sections.push_back(pdf::CrossReferenceSection());
  pdf::CrossReferenceSection& sect = crt.sections.back();
  sect.object = getNextInt();
  sect.numObjects = getNextInt();
  for (unsigned i = 0; i < sect.numObjects; ++i){
    sect.entries.push_back(pdf::CrtEntry());
    pdf::CrtEntry& entry = sect.entries.back();
    entry.offset = getNextInt();
    entry.generation = getNextInt();
    const char* tok = getNextToken();
    entry.used = strcmp(tok, "n") == 0;
    crt.entries.push_back(entry);
  }
  //read trailer section
  int numObjects = 0;
  pdf::Reference root;
  ASSERT_TOKEN("trailer");
  ASSERT_TOKEN("<<");
  const char* key = getNextDictEntry();
  while(key != NULL){
    if (strcmp(key, "Size") == 0){
      numObjects = getNextInt();
    }
    else if (strcmp(key, "Root") == 0){
      root = getNextReference();
    }
    else
      TR_WARN("%s unexpected in trailer", key);
    key = getNextDictEntry();
  }
  return root;
}
Example #16
0
/*
* Handle the rhs of a binary operation, where
* binoprhs ::= (binop primary)*
*/
static std::unique_ptr<ExprAST> ParseBinOpRHS(int minExprPrec,
                                              std::unique_ptr<ExprAST> lhs)
{
    while (1) {
        int tokPrec = GetTokPrecedence();

        // token's precedence is too low to do anything with so we're done
        if (tokPrec < minExprPrec) {
            return lhs;
        }

        // otherwise, precedence it high enough so we should handle the binop
        int binOp = CurTok;
        getNextToken(); // advance past the binop

        // parse the primary expression after the operator
        auto rhs = ParsePrimary();

        if (!rhs) {
            return nullptr;
        }

        // determine the direction of associativity
        int nextPrec = GetTokPrecedence();

        if (tokPrec < nextPrec) {
            rhs = ParseBinOpRHS(tokPrec + 1, std::move(rhs));
            if (!rhs) {
                return nullptr;
            }
        }

        // merge the lhs and rhs
        lhs = llvm::make_unique<BinaryExprAST>(binOp, std::move(lhs), std::move(rhs));
    }
}
Example #17
0
  void addOp () {

    std::cout << "\nIn addOp\n";

    nextToken = getNextToken ();

    switch (nextToken) {

      case (scanner::PlusOp) :
        match (scanner::PlusOp);
        break;

      case (scanner::MinusOp) :
        match (scanner::MinusOp);
        break;

      default:
        syntaxError(nextToken);

    }

    std::cout << "\nEnd addOp\n";

  }
Example #18
0
void CRehldsRuntimeConfig::parseFromCommandLine(const char* cmdLine) {
	char localBuf[2048];
	if (strlen(cmdLine) >= sizeof(localBuf)) rehlds_syserror("%s: too long cmdline", __func__);

	strcpy(localBuf, cmdLine);
	char* cpos = localBuf;

	getNextToken(&cpos); //skip executable path

	const char* token = getNextToken(&cpos);
	while (token != NULL) {
		if (!strcmp(token, "--rehlds-test-record"))
		{
			const char* fname = getNextToken(&cpos);
			if (fname == NULL) rehlds_syserror("%s: usage: --rehlds-test-record <filename>", __func__);
			strncpy(testRecordingFileName, fname, sizeof(testRecordingFileName));
			testRecordingFileName[sizeof(testRecordingFileName) - 1] = 0;
			testPlayerMode = TPM_RECORD;
		}
		else if (!strcmp(token, "--rehlds-test-play"))
		{
			const char* fname = getNextToken(&cpos);
			if (fname == NULL) rehlds_syserror("%s: usage: --rehlds-test-play <filename>", __func__);
			strncpy(testRecordingFileName, fname, sizeof(testRecordingFileName));
			testRecordingFileName[sizeof(testRecordingFileName) - 1] = 0;
			testPlayerMode = TPM_PLAY;
		}
		else if (!strcmp(token, "--rehlds-test-anon"))
		{
			const char* fname = getNextToken(&cpos);
			if (fname == NULL) rehlds_syserror("%s: usage: --rehlds-test-anon <filename>", __func__);
			strncpy(testRecordingFileName, fname, sizeof(testRecordingFileName));
			testRecordingFileName[sizeof(testRecordingFileName) - 1] = 0;
			testPlayerMode = TPM_ANONYMIZE;
		}
		else if (!strcmp(token, "--rehlds-disable-all-hooks"))
		{
			disableAllHooks = true;
		}

		token = getNextToken(&cpos);
	}
}
/// external ::= 'extern' prototype
static shared_ptr<PrototypeAST> ParseExtern() {
    getNextToken();  // eat extern.
    return ParsePrototype();
}
/* returnexpr
 * */
static shared_ptr<ExprAST> ParseReturnExpr() {
    getNextToken();//eat return
    return shared_ptr<ExprAST>(new ReturnAST(ParseExpression()));
}
/* numberexpr
 * ::= number
 * */
static shared_ptr<ExprAST> ParseNumberExpr() {
    shared_ptr<ExprAST> result = shared_ptr<ExprAST>(new NumberExprAST(NumVal));
    getNextToken();
    return result;
}
Example #22
0
parseTree parseInputSourceCode(char *testcaseFile, table T, grammar G)
{
    FILE *fp = fopen(testcaseFile,"r");
    parseTree pt = (parseTree)calloc(1,sizeof(parseTree_t));
    pt->parent = NULL;
    stack st = (stack)calloc(1,sizeof(stack_t));
    st->size = 0;
    st = push(DOLLAR,st);
    pt->tk = (tokenInfo)calloc(1,sizeof(token_struct));
    st = push(mainFunction,st);
    pt->tk->sym = mainFunction;

    parseTree curNode = pt;
    tokenInfo tk = getNextToken(fp);
    int t;
    while(st->size != 1)
    {

        if(tk == NULL)
        {
            tk = getNextToken(fp);
            while(tk == NULL || tk->sym != EPSILON)
                tk = getNextToken(fp);
            return NULL;
        }
        symbol_name symbol = tk->sym;
        t = (int)symbol - nNonTerminals;
        if(st->head->sym == symbol)
        {
            curNode->tk = tk;
            while(curNode->right == NULL && curNode->parent != NULL)
                curNode = curNode->parent;
            curNode = curNode->right;
            st = pop(st);
            tk = getNextToken(fp);
        }
        else if(st->head->sym == EPSILON)
        {
        	st = pop(st);
        	while(curNode->right == NULL)
        		curNode = curNode->parent;
            curNode = curNode->right;
        }
        else if(st->head->sym >= nNonTerminals)
        {
            parseError(tk, st, G, &T);
            return NULL;
        }
        else if((*T)->array[st->head->sym][t] == -1)
        {
            parseError(tk, st, G, &T);
            return NULL;
        }
        else
        {
            int i = (*T)->array[st->head->sym][t];
            nodeptr n1 = G->rules[i]->rhs;
            while(n1->next != NULL)
            {
                n1 = n1->next;
            }
            st = pop(st);
            curNode->child = (parseTree)calloc(1,sizeof(parseTree_t));
            curNode->child->parent = curNode;
            curNode = curNode->child;
            curNode->tk = (tokenInfo)calloc(1,sizeof(token_struct));
            curNode->tk->sym = n1->sym;
            st = push(n1->sym,st);
            n1 = n1->prev;
            while(n1!=NULL)
            {
                curNode->left = (parseTree)calloc(1,sizeof(parseTree_t));
                curNode->left->right = curNode;
                curNode->left->parent = curNode->parent;
                curNode = curNode->left;
                curNode->tk = (tokenInfo)calloc(1,sizeof(token_struct));
                curNode->tk->sym = n1->sym;
                //strcpy(curNode->tk->stringValue, "----");
                st = push(n1->sym,st);
                n1 = n1->prev;
            }
        }
    }
    if((tk = getNextToken(fp))->sym != EPSILON)
    {
        parseError(tk, st, G, &T);
        return NULL;
    }
    printf("Compiled Successfully: Input source code is syntactically correct!!\n");
    return pt;
}
Example #23
0
ExprAST *Parser::parseBinOpRHS (int exprPrecedence, ExprAST *LHS)
{
	while (1)
	{
		// This should be either an operator, a close-paren, comma,
		// or the end of the formula.
		if (currentToken != TOKEN_OPERATOR &&
		    currentToken != TOKEN_PAREN_CLOSE &&
		    currentToken != TOKEN_COMMA &&
		    currentToken != TOKEN_END)
		{
			error (boost::format ("Attempting to parse the RHS of a binary operator, "
			                      "and got %1%!") % formatToken ());
			return NULL;
		}
			
		// Get the precedence
		int tokenPrecedence = -1;
			
		for (int i = 0 ; i < NUM_OPERATORS ; i++)
		{
			if (strToken == operators[i].name)
			{
				tokenPrecedence = operators[i].precedence;
				break;
			}
		}
			
		// We're done if we run into an equivalent or lower-precedence binary
		// operator (or, notably, something that's not an operator at all, like
		// the end of the formula)
		if (tokenPrecedence < exprPrecedence)
			return LHS;
			
		// Pull off the operator
		std::string binOp = strToken;
		getNextToken ();
			
		// Parse the primary after the operator
		ExprAST *RHS = parsePrimary ();
		if (!RHS)
			return NULL;
			
		// We've now parsed a full binary operator pair, "A op B".  If the next
		// thing is a binary operator (which it should be, or it should be
		// nothing at all), we need to see whether we want "(A op B) op2 ..." or
		// "A op (B op2 ...)" by precedence rules.  Look ahead:
		int nextPrecedence = -1;
		for (int i = 0 ; i < NUM_OPERATORS ; i++)
		{
			if (strToken == operators[i].name)
			{
				nextPrecedence = operators[i].precedence;
				break;
			}
		}
			
		if (tokenPrecedence < nextPrecedence)
		{
			// Next precedence is greater, we want "A op (B op2 ...)".  Do that by
			// parsing B op2 ... into a new RHS.
			RHS = parseBinOpRHS (tokenPrecedence + 1, RHS);
			if (RHS == NULL)
				return NULL;
		}
			
		// Next precedence is lower, we want "(A op B) op2 ...", so make a new LHS
		LHS = new BinaryExprAST (binOp, LHS, RHS);
			
		// And loop back to the beginning
	}
}
Example #24
0
//! creates a surface from the file
IImage* CImageLoaderPPM::loadImage(io::IReadFile* file) const
{
	IImage* image;

	if (file->getSize() < 12)
		return 0;

	c8 id[2];
	file->read(&id, 2);

	if (id[0]!='P' || id[1]<'1' || id[1]>'6')
		return 0;

	const u8 format = id[1] - '0';
	const bool binary = format>3;

	core::stringc token;
	getNextToken(file, token);
	const u32 width = core::strtol10(token.c_str());

	getNextToken(file, token);
	const u32 height = core::strtol10(token.c_str());

	u8* data = 0;
	const u32 size = width*height;
	if (format==1 || format==4)
	{
		skipToNextToken(file); // go to start of data

		const u32 bytesize = size/8+(size & 3)?1:0;
		if (binary)
		{
			if (file->getSize()-file->getPos() < (long)bytesize)
				return 0;
			data = new u8[bytesize];
			file->read(data, bytesize);
		}
		else
		{
			if (file->getSize()-file->getPos() < (long)(2*size)) // optimistic test
				return 0;
			data = new u8[bytesize];
			memset(data, 0, bytesize);
			u32 shift=0;
			for (u32 i=0; i<size; ++i)
			{
				getNextToken(file, token);
				if (token == "1")
					data[i/8] |= (0x01 << shift);
				if (++shift == 8)
					shift=0;
			}
		}
		image = new CImage(ECF_A1R5G5B5, core::dimension2d<u32>(width, height));
		if (image)
			CColorConverter::convert1BitTo16Bit(data, (s16*)image->lock(), width, height);
	}
	else
	{
		getNextToken(file, token);
		const u32 maxDepth = core::strtol10(token.c_str());
		if (maxDepth > 255) // no double bytes yet
			return 0;

		skipToNextToken(file); // go to start of data

		if (format==2 || format==5)
		{
			if (binary)
			{
				if (file->getSize()-file->getPos() < (long)size)
					return 0;
				data = new u8[size];
				file->read(data, size);
				image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height));
				if (image)
				{
					u8* ptr = (u8*)image->lock();
					for (u32 i=0; i<size; ++i)
					{
						*ptr++ = data[i];
						*ptr++ = data[i];
						*ptr++ = data[i];
						*ptr++ = 255;
					}
				}
			}
			else
			{
				if (file->getSize()-file->getPos() < (long)(2*size)) // optimistic test
					return 0;
				image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height));
				if (image)
				{
					u8* ptr = (u8*)image->lock();
					for (u32 i=0; i<size; ++i)
					{
						getNextToken(file, token);
						const u8 num = (u8)core::strtol10(token.c_str());
						*ptr++ = num;
						*ptr++ = num;
						*ptr++ = num;
						*ptr++ = 255;
					}
				}
			}
		}
		else
		{
			const u32 bytesize = 3*size;
			if (binary)
			{
				if (file->getSize()-file->getPos() < (long)bytesize)
					return 0;
				data = new u8[bytesize];
				file->read(data, bytesize);
				image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height));
				if (image)
				{
					u8* ptr = (u8*)image->lock();
					for (u32 i=0; i<size; ++i)
					{
						*ptr++ = data[3*i];
						*ptr++ = data[3*i+1];
						*ptr++ = data[3*i+2];
						*ptr++ = 255;
					}
				}
			}
			else
			{
				if (file->getSize()-file->getPos() < (long)(2*bytesize)) // optimistic test
					return 0;
				image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height));
				if (image)
				{
					u8* ptr = (u8*)image->lock();
					for (u32 i=0; i<size; ++i)
					{
						getNextToken(file, token);
						*ptr++ = (u8)core::strtol10(token.c_str());
						getNextToken(file, token);
						*ptr++ = (u8)core::strtol10(token.c_str());
						getNextToken(file, token);
						*ptr++ = (u8)core::strtol10(token.c_str());
						*ptr++ = 255;
					}
				}
			}
		}
	}

	if (image)
		image->unlock();

	delete [] data;

	return image;
}
Example #25
0
/// external ::= 'extern' prototype
static PrototypeAST *ParseExtern() {
  getNextToken();  // eat extern.
  return ParsePrototype();
}
bool CsvConfigFileReader::getNextConfigItem(std::string &itemName, Variant &value)
{
    enum ParsingState
    {
        NeedNameField,
        NeedTypeFieldSep,
        NeedTypeField,
        NeedValueFieldSep,
        NeedValueField,
        NeedEOL,
        Done
    };
    
    ++lastLineProcessed;
    
    ParsingState state = NeedNameField;

    std::string token;
    
    tokens.clear();
    
    TokenType tokenType;
    
    while (state != Done)
    {
        tokenType = getNextToken(token);
        
        switch (state)
        {
        case NeedNameField:
            // Hit the end of file?
            if (tokenType == EndOfFile)
            {
                return false;
            }
                
            // Skip comment lines and blank lines.
            if ((tokenType == Comment) || (tokenType == EndOfLine))
            {
                ++lastLineProcessed;
                continue;
            }
            
            if (tokenType == ColumnContents)
            {
                tokens.push_back(token);
                state = NeedTypeFieldSep;
            }
            else
            {
                // Any other token is an error.
                raiseErrorException("Missing item name", filename, lastLineProcessed);
            }
            break;
                
        case NeedTypeFieldSep:
        case NeedValueFieldSep:
            if (tokenType == Comma)
            {
                state = (state == NeedTypeFieldSep) ? NeedTypeField : NeedValueField;
            }
            else
            {
                // Error should have received a comma.
                if (state == NeedTypeFieldSep)
                {
                    raiseErrorException("Missing comma separator after type field", filename, lastLineProcessed);
                }
                else
                {
                    raiseErrorException("Missing comma separator after value field", filename, lastLineProcessed);
                }
            }
            break;
                
        case NeedTypeField:
            if (tokenType == ColumnContents)
            {
                tokens.push_back(token);
                state = NeedValueFieldSep;
            }
            else
            {
                // Any other token is an error.
                raiseErrorException("Expected item type data column", filename, lastLineProcessed);
            }
            break;
                
        case NeedValueField:
            if ((tokenType == ColumnContents) && !token.empty())
            {
                tokens.push_back(token);
                state = NeedEOL;
            }
            else
            {
                raiseErrorException("Expected item value column", filename, lastLineProcessed);
            }
            break;
                
        case NeedEOL:
            if (tokenType == EndOfLine)
            {
                state = Done;
            }
            else
            {
                raiseErrorException("Expected eol after reading value field. Extraneous information.", filename, lastLineProcessed);
            }
            break;
                
        default:  // This should never happen.
            throw ConfigFileException("Unexpected Error.\n");
        }
    }
    
    if (state == Done)
    {

    	Logger::GetInstance()->Log(ConfigurationId, Logger::kTRACE, "CSVConfigFileReader: Parsed Line into \"%s\", \"%s\", \"%s\"",
    			                   tokens[0].c_str(), tokens[1].c_str(), tokens[2].c_str());

        setVariantFromData(value,
                           tokens[1],           // Type is in index one of the tokens array.
                           tokens[2],           // Value is in index two of the tokens array;
                           filename,
                           lastLineProcessed);
        
        itemName = tokens[0];
    }
    
    return true;
}
void MD5Model::readMeshEl(std::ifstream &fin) {
	// make sure numMeshes has been set
	if (numMeshes <= 0)
		throw Exception("MD5Model::readMeshesEl(): numMeshes needs to be set before 'mesh' block");

	TOKEN t = getNextToken(fin);

	// expect an opening brace { to begin block of joints
	if (t != TOKEN_LBRACE)
		throw Exception("MD5Model::readMeshEl(): expected { to follow 'mesh'");

	Mesh *mesh = new Mesh;

	// read in all mesh information in block until '}' is hit
	// (if EOF is reached before this, then the read*() methods
	//  will close the file and throw an exception)
	std::string str;
	t = getNextToken(fin, &str);
	while (t != TOKEN_RBRACE) {
		if (str == "vert") {
			// syntax: vert (vertex index) '(' (x) (y) (z) ')' (weight index) (weight count)
			Vertex vert;

			int index = readInt(fin);
			readVec(fin, vert.tc, 2);
			vert.weightIndex = readInt(fin);
			vert.weightCount = readInt(fin);

			// make sure index is within bounds of vector of vertices
			if (size_t(index) >= mesh->verts.size())
				throw Exception("MD5Model::readMeshEl(): vertex index >= numverts");

			mesh->verts[index] = vert;
		}
		else if (str == "tri") {
			// syntax: tri (triangle index) (v0 index) (v1 index) (v2 index)
			Tri tri;

			int index = readInt(fin);

			// make sure index is within bounds of vector of triangles
			if (size_t(index) >= mesh->tris.size())
				throw Exception("MD5Model::readMeshEl(): triangle index >= numtris");

			tri.v[0] = readInt(fin);
			tri.v[1] = readInt(fin);
			tri.v[2] = readInt(fin);
			mesh->tris[index] = tri;
		}
		else if (str == "weight") {
			Weight weight;

			int weightIndex = readInt(fin);
			weight.joint = readInt(fin);
			weight.w = readFloat(fin);
			readVec(fin, weight.pos, 3);

			if (size_t(weightIndex) >= mesh->weights.size())
				throw Exception("MD5Model::readMeshEl(): weight index >= numweights");

			mesh->weights[weightIndex] = weight;
		}
		else if (str == "shader") {
			// 没有使用纹理
			std::string shader;
			if (getNextToken(fin, &shader) != TOKEN_STRING)
				throw Exception("MD5Model::readMeshEl(): expected string to follow 'shader'");
		}
		else if (str == "numverts") {
			if (mesh->verts.size() > 0)
				throw Exception("MD5Model::readMeshEl(): numverts has already been set");

			int n = readInt(fin);

			if (n <= 0)
				throw Exception("MD5Model::readMeshEl(): numverts must be greater than 0");

			mesh->verts.resize(n);
		}
		else if (str == "numtris") {
			if (mesh->tris.size() > 0)
				throw Exception("MD5Model::readMeshEl(): numtris has already been set");

			int n = readInt(fin);

			if (n <= 0)
				throw Exception("MD5Model::readMeshEl(): numtris must be greater than 0");

			mesh->tris.resize(n);
		}
		else if (str == "numweights") {
			if (mesh->weights.size() > 0)
				throw Exception("MD5Model::readMeshEl(): numweights has already been set");

			int n = readInt(fin);

			if (n <= 0)
				throw Exception("MD5Model::readMeshEl(): numweights must be greater than 0");

			mesh->weights.resize(n);
		}

		// read next token
		t = getNextToken(fin, &str);
	}

	meshes.push_back(mesh);
}
void MD5Model::readCommandLineEl(std::ifstream &fin) {
	// commandline elements can be ignored, but make sure the syntax is correct
	if (getNextToken(fin) != TOKEN_STRING)
		throw Exception("MD5Model::readCommandLineEl(): expected string");
}
Example #29
0
Lexer::TokenType
Lexer::getNextToken()
{
  static const char* delims = "\"();";

  try {
    while(isspace(*c)) {
      if(*c == '\n')
        ++linenumber;
      nextChar();
    };

    token_length = 0;

    switch(*c) {
      case ';': // comment
        while(true) {
          nextChar();
          if(*c == '\n') {
            ++linenumber;
            break;
          }
        }
        return getNextToken(); // and again
      case '(':
        nextChar();
        return TOKEN_OPEN_PAREN;
      case ')':
        nextChar();
        return TOKEN_CLOSE_PAREN;
      case '"': {  // string
        int startline = linenumber;
        try {
          while(1) {
            nextChar();
            if(*c == '"')
              break;
            else if (*c == '\r') // XXX this breaks with pure \r EOL
              continue;
            else if(*c == '\n')
              linenumber++;
            else if(*c == '\\') {
              nextChar();
              switch(*c) {
                case 'n':
                  *c = '\n';
                  break;
                case 't':
                  *c = '\t';
                  break;
              }
            }
            if(token_length < MAX_TOKEN_LENGTH)
              token_string[token_length++] = *c;
          }
          token_string[token_length] = 0;
        } catch(EOFException& ) {
          std::stringstream msg;
          msg << "Parse error in line " << startline << ": "
            << "EOF while parsing string.";
          throw std::runtime_error(msg.str());
        }
        nextChar();
        return TOKEN_STRING;
      }
      case '#': // constant
        try {
          nextChar();

          while(isalnum(*c) || *c == '_') {
            if(token_length < MAX_TOKEN_LENGTH)
              token_string[token_length++] = *c;
            nextChar();
          }
          token_string[token_length] = 0;
        } catch(EOFException& ) {
          std::stringstream msg;
          msg << "Parse Error in line " << linenumber << ": "
            << "EOF while parsing constant.";
          throw std::runtime_error(msg.str());
        }

        if(strcmp(token_string, "t") == 0)
          return TOKEN_TRUE;
        if(strcmp(token_string, "f") == 0)
          return TOKEN_FALSE;

        // we only handle #t and #f constants at the moment...

        {
          std::stringstream msg;
          msg << "Parse Error in line " << linenumber << ": "
            << "Unknown constant '" << token_string << "'.";
          throw std::runtime_error(msg.str());
        }

      default:
        if(isdigit(*c) || *c == '-') {
          bool have_nondigits = false;
          bool have_digits = false;
          int have_floating_point = 0;

          do {
            if(isdigit(*c))
              have_digits = true;
            else if(*c == '.')
              ++have_floating_point;
            else if(isalnum(*c) || *c == '_')
              have_nondigits = true;

            if(token_length < MAX_TOKEN_LENGTH)
              token_string[token_length++] = *c;

            nextChar();
          } while(!isspace(*c) && !strchr(delims, *c));

          token_string[token_length] = 0;

          // no nextChar

          if(have_nondigits || !have_digits || have_floating_point > 1)
            return TOKEN_SYMBOL;
          else if(have_floating_point == 1)
            return TOKEN_REAL;
          else
            return TOKEN_INTEGER;
        } else {
          do {
            if(token_length < MAX_TOKEN_LENGTH)
              token_string[token_length++] = *c;
            nextChar();
          } while(!isspace(*c) && !strchr(delims, *c));
          token_string[token_length] = 0;

          // no nextChar

          return TOKEN_SYMBOL;
        }
    }
  } catch(EOFException& ) {
    return TOKEN_EOF;
  }
}
Example #30
0
/// numberexpr ::= number
static ExprAST *ParseNumberExpr() {
  ExprAST *Result = new NumberExprAST(NumVal);
  getNextToken(); // consume the number
  return Result;
}