static void parseSwitch (tokenInfo *const token) { /* * switch (expression) { * case value1: * statement; * break; * case value2: * statement; * break; * default : statement; * } */ readToken (token); if (isType (token, TOKEN_OPEN_PAREN)) { /* * Handle nameless functions, these will only * be considered methods. */ skipArgumentList(token, FALSE, NULL); } if (isType (token, TOKEN_OPEN_CURLY)) { parseBlock (token, token); } }
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { nextToken(); nextToken(); // interface name // @interface can be followed by either a base class, or a category. if (FormatTok->Tok.is(tok::colon)) { nextToken(); nextToken(); // base class name } else if (FormatTok->Tok.is(tok::l_paren)) // Skip category, if present. parseParens(); if (FormatTok->Tok.is(tok::less)) parseObjCProtocolList(); // If instance variables are present, keep the '{' on the first line too. if (FormatTok->Tok.is(tok::l_brace)) parseBlock(/*MustBeDeclaration=*/true); // With instance variables, this puts '}' on its own line. Without instance // variables, this ends the @interface line. addUnwrappedLine(); parseObjCUntilAtEnd(); }
static PSmmAstNode parseStatement(PSmmParser parser) { switch (parser->curToken->kind) { case tkSmmReturn: return parseReturnStmt(parser); case '{': return (PSmmAstNode)parseBlock(parser, parser->curScope->returnType, false); case tkSmmIdent: case '(': case '-': case '+': case tkSmmNot: case tkSmmInt: case tkSmmFloat: case tkSmmBool: return parseExpressionStmt(parser); case tkSmmIf: case tkSmmWhile: return parseIfWhileStmt(parser); case tkSmmErr: if (findToken(parser, ';')) getNextToken(parser); return NULL; case ';': return NULL; // Just skip empty statements default: if (parser->lastErrorLine != parser->curToken->filePos.lineNumber) { char gotBuf[4]; const char* got = smmTokenToString(parser->curToken, gotBuf); smmPostMessage(parser->msgs, errSmmGotUnexpectedToken, parser->curToken->filePos, "valid statement", got); } getNextToken(parser); // Skip the bad character if (findToken(parser, ';')) getNextToken(parser); return &errorNode; } }
static void parseSwitch (tokenInfo *const token) { /* * switch (expression) { * case value1: * statement; * break; * case value2: * statement; * break; * default : statement; * } */ readToken (token); if (isType (token, TOKEN_OPEN_PAREN)) { skipArgumentList(token, FALSE, NULL); } if (isType (token, TOKEN_OPEN_CURLY)) { parseBlock (token, token); } }
ExpPtr parseFor (Lexer& lex) { Span spStart, spEnd; ExpPtr val1, val2, body; spStart = lex.eat(tFor).span; auto var = lex.eat(tIdent).str; lex.eat(tColon); val1 = parseExp(lex); if (lex.current() == tArrow) { lex.advance(); val2 = parseExp(lex); } else val2 = nullptr; body = parseBlock(lex); spEnd = body->span; if (val2 == nullptr) return Exp::make(eForEach, var, { val1, body }, spStart + spEnd); else return Exp::make(eForRange, var, { val1, val2, body }, spStart + spEnd); }
static boolean findCmdTerm (tokenInfo *const token, boolean include_newlines, boolean include_commas) { /* * Read until we find either a semicolon or closing brace. * Any nested braces will be handled within. */ while (! isType (token, TOKEN_SEMICOLON) && ! isType (token, TOKEN_CLOSE_CURLY) && ! (include_commas && isType (token, TOKEN_COMMA)) && ! isType (token, TOKEN_EOF)) { /* Handle nested blocks */ if ( isType (token, TOKEN_OPEN_CURLY)) { parseBlock (token, token); readTokenFull (token, include_newlines, NULL); } else if ( isType (token, TOKEN_OPEN_PAREN) ) { skipArgumentList(token, include_newlines, NULL); } else if ( isType (token, TOKEN_OPEN_SQUARE) ) { skipArrayList(token, include_newlines); } else { readTokenFull (token, include_newlines, NULL); } } return isType (token, TOKEN_SEMICOLON); }
void Parser::parseProgram() { if (it->tag == PROGRAM) { it++; //根节点生成 shared_ptr<Node> beginNode = make_shared<Node>(); beginNode->value = *it; root = beginNode; currNode = root; it++; match(SEMI); parseBlock(); } else { //unexpected symbol error Error err("unexpected symbol error", *it); errList.push_back(err); } if (it->tag != FINISH) { //missing the finish symbol Error err("missing the finish symbol", *it); errList.push_back(err); } }
ExpPtr parseLambda (Lexer& lex) { Span spStart, spEnd; SigPtr sig; ExpPtr body; spStart = lex.current().span; if (lex.current() == tFunc) { lex.advance(); sig = parseSigParens(lex); body = parseBlock(lex); } else { lex.eat(tLambda); sig = parseSig(lex, true); lex.eat(tArrow); body = parseExp(lex); } spEnd = body->span; return Exp::make(eLambda, sig->toSigType(), "", { body }, spStart + spEnd); }
void parseBlockExp (Lexer& lex, ExpList& list) { switch (lex.current().tok) { case tSemicolon: lex.advance(); break; case tLet: list.push_back(parseLet(lex)); lex.eat(tSemicolon); break; case tLCurl: list.push_back(parseBlock(lex)); break; case tIf: list.push_back(parseCond(lex)); break; case tLoop: list.push_back(parseLoop(lex)); break; case tFor: list.push_back(parseFor(lex)); break; // everthing else does default: list.push_back(parseAssign(lex, parseExp(lex))); if (lex.current() != tSemicolon) lex.expect(tRCurl); else lex.eat(tSemicolon); break; } }
bool UnwrappedLineParser::parseLevel() { bool Error = false; do { switch (FormatTok.Tok.getKind()) { case tok::hash: parsePPDirective(); break; case tok::comment: nextToken(); addUnwrappedLine(); break; case tok::l_brace: Error |= parseBlock(); addUnwrappedLine(); break; case tok::r_brace: // Stray '}' is an error. return true; default: parseStatement(); break; } } while (!eof()); return Error; }
void GLECSVData::readBuffer(const char* buffer) { unsigned int size = strlen(buffer); m_buffer.resize(size + 1); memcpy(&m_buffer[0], buffer, size); m_buffer[size] = 0; parseBlock(); }
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { bool SwitchLabelEncountered = false; do { switch (FormatTok->Tok.getKind()) { case tok::comment: nextToken(); addUnwrappedLine(); break; case tok::l_brace: // FIXME: Add parameter whether this can happen - if this happens, we must // be in a non-declaration context. parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); break; case tok::r_brace: if (HasOpeningBrace) return; StructuralError = true; nextToken(); addUnwrappedLine(); break; case tok::kw_default: case tok::kw_case: if (!SwitchLabelEncountered) Line->Level += Style.IndentCaseLabels; SwitchLabelEncountered = true; parseStructuralElement(); break; default: parseStructuralElement(); break; } } while (!eof()); }
void NewLanguagesController::updateNewLanguages() { emit beforeUpdateNewLanguages(); // remove prev. new languages clearAllNewLanguages(); // fill with new languages parseBlock(copyBetween(newLanguagesFile, "#COMMON{", "}")); }
Node* Parser::parseWhileStmt() { expect(Token::TYPE::WHILE); nextToken(); auto _condition = parseBinaryExpr(); auto _block = parseBlock(); return make_node<WhileStmtNode>(_condition, _block); }
ImportDecl parseImportDecl (Lexer& lex) { Span spStart, spEnd; spStart = lex.eat(tImport).span; auto nameTok = lex.eat(tIdent, tString); auto name = nameTok.str; spEnd = nameTok.span; return ImportDecl { .isPublic = false, .name = name, .span = spStart + spEnd }; } FuncDecl parseFuncDecl (Lexer& lex) { Span spStart, spEnd; spStart = lex.eat(tFunc).span; auto name = lex.eat(tIdent).str; auto sig = parseSigParens(lex); ExpPtr body; /* if (lex.current() == tEqual) { lex.advance(); body = parseExp(lex); } else */ body = parseBlock(lex); spEnd = sig->span; return FuncDecl { .isPublic = false, .name = name, .signature = sig, .body = body, .span = spStart + spEnd }; } static FuncDecl parseConstructor (Lexer& lex) { Span spStart, spEnd; FuncDecl result; result.name = lex.current().str; spStart = lex.eat(tIdent).span; result.signature = parseSigParens(lex); spEnd = result.signature->span; result.span = spStart + spEnd; result.body = nullptr; return result; }
static void parseLongestChain() { Block *blk = gNullBlock->next; start(blk, gMaxBlock); while(likely(0!=blk)) { parseBlock(blk); blk = blk->next; } }
void TGen::PropertyTreeParser::parseBlock(TGen::PropertyTree & node) { std::vector<std::string> line; // stack while (currentToken != endIter) { // std::cout << "loop '" << currentToken->second << "'" << std::endl; if (currentToken->first == TGen::PropertyTokenBlockStart) { step(); TGen::PropertyTree newNode(line.at(0)); // std::cout << "parsing block " << line.at(0) << std::endl; for (int i = 1; i < line.size(); ++i) newNode.addAttribute(line[i]); line.clear(); parseBlock(newNode); node.addNode(newNode); } else if (currentToken->first == TGen::PropertyTokenBlockEnd) { if (!line.empty()) { addLine(line, node); line.clear(); } step(); // std::cout << "leaving block" << std::endl; break; } else if (currentToken->first == PropertyTokenNodeEnd) { TGen::PropertyTree newNode(line.at(0)); for (int i = 1; i < line.size(); ++i) newNode.addAttribute(line[i]); line.clear(); node.addNode(newNode); } else if (currentToken->first == TGen::PropertyTokenEOL) { if (!line.empty()) { // std::cout << "finishing line " << line.size() << std::endl; addLine(line, node); line.clear(); } } else { line.push_back(currentToken->second); //std::cout << "adding param " << currentToken->second << std::endl; } if (currentToken != endIter) step(); } if (!line.empty()) addLine(line, node); }
static void parseLongestChain() { info( "pass 4 -- full blockchain analysis (with%s index)...", gNeedUpstream ? "" : "out" ); auto startTime = Timer::usecs(); gCallback->startLC(); uint64_t bytesSoFar = 0; auto blk = gNullBlock->next; start(blk, gMaxBlock); while(likely(0!=blk)) { if(0==(blk->height % 10)) { auto now = Timer::usecs(); static auto last = -1.0; auto elapsedSinceLastTime = now - last; auto elapsedSinceStart = now - startTime; auto progress = bytesSoFar/(double)gChainSize; auto bytesPerSec = bytesSoFar / (elapsedSinceStart*1e-6); auto bytesLeft = gChainSize - bytesSoFar; auto secsLeft = bytesLeft / bytesPerSec; if((1.0 * 1000 * 1000)<elapsedSinceLastTime) { fprintf( stderr, "block %6d/%6d, %.2f%% done, ETA = %.2fsecs, mem = %.3f Gig \r", (int)blk->height, (int)gMaxHeight, progress*100.0, secsLeft, getMem() ); fflush(stderr); last = now; } } if(parseBlock(blk)) { break; } bytesSoFar += blk->chunk->getSize(); blk = blk->next; } fprintf(stderr, " \r"); gCallback->wrapup(); info("pass 4 -- done."); }
static void findRustTags (void) { lexerState lexer; vString* scope = vStringNew(); initLexer(&lexer); parseBlock(&lexer, FALSE, K_NONE, scope); vStringDelete(scope); deInitLexer(&lexer); }
void UnwrappedLineParser::parseNamespace() { assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); nextToken(); if (FormatTok.Tok.is(tok::identifier)) nextToken(); if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(0); addUnwrappedLine(); } // FIXME: Add error handling. }
//===----------------------------------------------------------------------===// // Parsers based on grammar in Grammar.txt // Program* Parser::parseProgram() { Token program = match( PRGRM_T ); Token ID = match( IDENT_T ); match( SEMICOLON_T ); Block* block = parseBlock(); match( PERIOD_T ); Program* node = new Program( ID.lexeme, block, program.line, program.column ); return node; }
WhileNode* Parser::parseWhile() { uint32_t token = _currentTokenIndex; ensureKeyword("while"); ensureToken(tLPAREN); AstNode* whileExpr = parseExpression(); ensureToken(tRPAREN); BlockNode* loopBlock = parseBlock(true); return new WhileNode(token, whileExpr, loopBlock); }
TGen::PropertyTree TGen::PropertyTreeParser::parse(const char * code) { TGen::PropertyTreeTokenizer tokenizer; tokenizer.tokenizeString(code, tokens, false); currentToken = tokens.getFirstToken(); endIter = tokens.getEndToken(); TGen::PropertyTree root; parseBlock(root); return root; }
ProcDecl* Parser::parseProcDecl() { Token procedure = match( PROC_T ); Token ID = match( IDENT_T ); list<Param*> params = parseParamList(); match( SEMICOLON_T ); Block* block = parseBlock(); match( SEMICOLON_T ); ProcDecl* node = new ProcDecl( ID.lexeme, params, block, procedure.line, procedure.column ); return node; }
void UnwrappedLineParser::parseIfThenElse() { assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); bool NeedsUnwrappedLine = false; if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || Style.BreakBeforeBraces == FormatStyle::BS_GNU) { addUnwrappedLine(); } else { NeedsUnwrappedLine = true; } } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } if (FormatTok->Tok.is(tok::kw_else)) { nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else if (FormatTok->Tok.is(tok::kw_if)) { parseIfThenElse(); } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } } else if (NeedsUnwrappedLine) { addUnwrappedLine(); } }
/* Mod format: * "mod" <ident> "{" [<body>] "}" * "mod" <ident> ";"*/ static void parseMod (lexerState *lexer, vString *scope, int parent_kind) { advanceToken(lexer, TRUE); if (lexer->cur_token != TOKEN_IDENT) return; addTag(lexer->token_str, NULL, K_MOD, lexer->line, lexer->pos, scope, parent_kind); addToScope(scope, lexer->token_str); advanceToken(lexer, TRUE); parseBlock(lexer, TRUE, K_MOD, scope); }
void UnwrappedLineParser::parseLabel() { // FIXME: remove all asserts. assert(FormatTok.Tok.is(tok::colon) && "':' expected"); nextToken(); unsigned OldLineLevel = Line.Level; if (Line.Level > 0) --Line.Level; if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(); } addUnwrappedLine(); Line.Level = OldLineLevel; }
ExpPtr parseLoop (Lexer& lex) { Span spStart, spEnd; ExpPtr body; spStart = lex.eat(tLoop).span; ExpList sub; sub.reserve(2); if (lex.current() == tLCurl) { sub.push_back(body = parseBlock(lex)); } else { sub.push_back(parseExp(lex)); sub.push_back(body = parseBlock(lex)); } spEnd = body->span; return Exp::make(eLoop, std::move(sub), spStart + spEnd); }
bool LessParser::parseAtRuleOrVariable (LessStylesheet &stylesheet) { Token token; TokenList value, rule; AtRule* atrule = NULL; if (tokenizer->getTokenType() != Token::ATKEYWORD) return false; token = tokenizer->getToken(); tokenizer->readNextToken(); CssParser::skipWhitespace(); #ifdef WITH_LIBGLOG VLOG(2) << "Parse: keyword: " << token; #endif if (parseVariable(value)) { #ifdef WITH_LIBGLOG VLOG(2) << "Parse: variable"; #endif stylesheet.putVariable(token, value); } else { if (token == "@media") { parseLessMediaQuery(token, stylesheet); return true; } while(parseAny(rule)) {}; if (!parseBlock(rule)) { if (tokenizer->getTokenType() != Token::DELIMITER) { throw new ParseException(tokenizer->getToken(), "delimiter (';') at end of @-rule"); } tokenizer->readNextToken(); skipWhitespace(); } // parse import if (token == "@import" && rule.size() > 0) { if (parseImportStatement(rule, stylesheet)) return true; } atrule = stylesheet.createLessAtRule(token); atrule->setReference(reference); atrule->setRule(rule); } return true; }
bool CssParser::parseUnused(TokenList* tokens) { if (parseBlock(tokens)) { } else if (tokenizer->getTokenType() == Token::ATKEYWORD) { tokens->push(tokenizer->getToken()->clone()); tokenizer->readNextToken(); parseWhitespace(tokens); } else if (tokenizer->getTokenType() == Token::DELIMITER) { tokens->push(tokenizer->getToken()->clone()); tokenizer->readNextToken(); skipWhitespace(); } else return false; return true; }