ASTNode* Parser::procedure() { matchKeyword("procedure"); if (_procTable->getIndex(_token) > -1) { throw ParseException(_stmtNum, _token, "Duplicate procedure names"); } std::string procedure = _token; ASTNode* procNode = new ASTNode(procedure, PROCEDURE, 0); matchName(); matchKeyword("{"); int fromStmtNum = _stmtNum + 1; ASTNode* stmtListNode = statementList(); matchKeyword("}"); int toStmtNum = _stmtNum; _procTable->addProcedure(procedure, fromStmtNum, toStmtNum); procNode->joinChild(stmtListNode); return procNode; }
ASTNode* Parser::callStmt() { matchKeyword("call"); ASTNode* callNode = new ASTNode(_token, CALL, _stmtNum); matchName(); matchKeyword(";"); return callNode; }
ASTNode* Parser::ifStmt() { matchKeyword("if"); ASTNode* ifNode = new ASTNode("", IF, _stmtNum); _varTable->addVariable(_token); ASTNode* varNode = new ASTNode(_token, VARIABLE, 0); matchName(); matchKeyword("then"); matchKeyword("{"); ASTNode* ifStmtListNode = statementList(); matchKeyword("}"); matchKeyword("else"); matchKeyword("{"); ASTNode* elseStmtListNode = statementList(); matchKeyword("}"); ifNode->joinChild(varNode); varNode->joinNext(ifStmtListNode); ifStmtListNode->joinNext(elseStmtListNode); return ifNode; }
ASTNode* Parser::assignStmt() { _varTable->addVariable(_token); ASTNode* varNode = new ASTNode(_token, VARIABLE, 0); matchName(); matchKeyword("="); ASTNode* assignNode = new ASTNode("", ASSIGN, _stmtNum); ASTNode* expNode = expression(); matchKeyword(";"); assignNode->joinChild(varNode); varNode->joinNext(expNode); return assignNode; }
ASTNode* Parser::whileStmt() { matchKeyword("while"); ASTNode* whileNode = new ASTNode("", WHILE, _stmtNum); _varTable->addVariable(_token); ASTNode* varNode = new ASTNode(_token, VARIABLE, 0); matchName(); matchKeyword("{"); ASTNode* stmtListNode = statementList(); matchKeyword("}"); whileNode->joinChild(varNode); varNode->joinNext(stmtListNode); return whileNode; }
inline bool isKeyword(TToken &tk,const wchar_t* name) { return matchKeyword(tk,name); }
static void parse_file(FILE *f, configuration *conf) { do { // read a line char *b = fgets(buffer, sizeof(buffer), f); if (!b) { break; } else if (b[0] == '#') { // comment - skip line } else if (b[0] == '\n') { // empty line } else if (matchKeyword(b, "GL_VENDOR")) { conf->vendor = stringValue(f, b + 10); } else if (matchKeyword(b, "GL_VERSION")) { conf->version = stringValue(f, b + 11); } else if (matchKeyword(b, "GL_EXTENSIONS")) { conf->extensions = stringValue(f, b + 14); } else if (matchKeyword(b, "GL_RENDERER")) { conf->renderer = stringValue(f, b + 12); } else if (matchKeyword(b, "GL_SHADING_LANGUAGE_VERSION")) { conf->glslVersion = stringValue(f, b + 28); } else if (matchKeyword(b, "GL_MAX_TEXTURE_SIZE")) { conf->maxTextureSize = intValue(f, b + 20); } else if (matchKeyword(b, "GL_MAJOR_VERSION")) { conf->versionMajor = intValue(f, b + 17); } else if (matchKeyword(b, "GL_MINOR_VERSION")) { conf->versionMinor = intValue(f, b + 17); } else if (matchKeyword(b, "GL_CONTEXT_PROFILE_MASK")) { char *maskStr = stringValue(f, b + 24); conf->profileMask = 0x0; if (strstr(maskStr, "GL_CONTEXT_CORE_PROFILE_BIT")) conf->profileMask |= GL_CONTEXT_CORE_PROFILE_BIT; if (strstr(maskStr, "GL_CONTEXT_COMPATIBILITY_PROFILE_BIT")) conf->profileMask |= GL_CONTEXT_COMPATIBILITY_PROFILE_BIT; free(maskStr); } else { std::cerr << "Unexpected config variable: " << b << ".\n"; break; } } while (!feof(f)); if (conf->version) { // String version was specified, compute integer major/minor versions conf->versionMajor = conf->version[0] - '0'; conf->versionMinor = conf->version[2] - '0'; assert(conf->versionMajor >= 1 && conf->versionMajor <= 4); assert(conf->versionMinor >= 0 && conf->versionMinor <= 9); } else if (conf->versionMajor) { // Numeric version was specified, update the string if (conf->version) { // if version string was specified too, override it conf->version[0] = '0' + conf->versionMajor; conf->version[2] = '0' + conf->versionMinor; } else { // allocate new version string conf->version = (char *) malloc(4); assert(conf->version); conf->version[0] = '0' + conf->versionMajor; conf->version[1] = '.'; conf->version[2] = '0' + conf->versionMinor; conf->version[3] = 0; } } if (conf->extensions) { create_extensions_list(conf); } }
yylex() { char c; for (;;) { c = input(); oldnewline = newline; newline = FALSE; switch (char_type[c]) { Case C_SLASH: if (oldnewline) { parseRawline(); return(Rawline); } c = input(); if (c == '*') comment(); else { unput(c); return(DIV); } Case C_ZERO: c = input(); if (c == 'x' || c == 'X') parseNumber(16, input()); else if (c == 'b' || c == 'B') parseNumber(2, input()); else if (c == 'q' || c == 'Q') parseNumber(4, input()); else parseNumber(8, c); return(Number); Case C_DIG: parseNumber(10, c); return(Number); Case C_ALPH: parseName(c); if ((yylval = matchKeyword(yytext)) != 0) return(yylval); yylval = lookupSymbol(yytext); /* if (debug) printf("lexer: Name '%s'\n", yytext);*/ return(Name); Case C_QUOTE: lexString('"'); string(); return(String); Case C_APOSTROPHE: lexString('\''); c = input(); if (c == 'b' || c == 'B') { bitString(); return(BitString); } else { string(); unput(c); return(String); } Case C_LIT: return(litCode[c]); Case C_NL: newline = TRUE; } } }