static ParseResult ParseNotExpression(const char *expr, int start, int end) { if (start < end && expr[start] == '!') { ParseResult primres = ParsePrimary(expr, start + 1, end); if (primres.result) { Expression *res = xcalloc(1, sizeof(Expression)); res->op = NOT; res->val.not.arg = primres.result; return (ParseResult) { res, primres.position }; } else { return primres; } } else { return ParsePrimary(expr, start, end); } }
Json* Json::Parser::ParsePair(){ Json* jstring = ParsePrimary(); if(jstring->_kind != kString) JSON_ASSERT("json of string expected"); NextChar(); if(ch != ':') JSON_ASSERT("expect \":\""); Json* value = ParsePrimary(); Pair* pairs = new Pair(*(std::string*)(jstring->_data), value); return new Json(pairs); }
/// binoprhs /// ::= ('+' primary)* static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) { // If this is a binop, find its precedence. while (1) { int TokPrec = GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (TokPrec < ExprPrec) return LHS; // Okay, we know this is a binop. int BinOp = CurTok; getNextToken(); // eat binop // Parse the primary expression after the binary operator. auto RHS = ParsePrimary(); if (!RHS) return nullptr; // If BinOp binds less tightly with RHS than the operator after RHS, let // the pending operator take RHS as its LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) { RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS)); if (!RHS) return nullptr; } // Merge LHS/RHS. LHS = helper::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); } }
// binoplhs // ::= // ::= primary ExprAST *Parser::ParseBinOpLHS() { if (Lex.tokStr == "-" || Lex.tokStr == "@" || Lex.tokStr == "!") { return 0; } else { return ParsePrimary(); } }
/// binoprhs /// ::= ('+' primary)* static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) { // If this is a binop, find its precedence. while (1) { int TokPrec = GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (TokPrec < ExprPrec) return LHS; // Okay, we know this is a binop. int BinOp = CurTok; getNextToken(); // eat binop // Parse the primary expression after the binary operator. ExprAST *RHS = ParsePrimary(); if (!RHS) return 0; // If BinOp binds less tightly with RHS than the operator after RHS, let // the pending operator take RHS as its LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) { RHS = ParseBinOpRHS(TokPrec+1, RHS); if (RHS == 0) return 0; } // Merge LHS/RHS. LHS = new BinaryExprAST(BinOp, LHS, RHS); } }
/// expression /// ::= primary binoprhs /// static std::unique_ptr<ExprAST> ParseExpression() { auto LHS = ParsePrimary(); if (!LHS) return nullptr; return ParseBinOpRHS(0, std::move(LHS)); }
static NodeRef ParseTerm( char **spec, int *status ) { NodeRef node = NIL; node = ParsePrimary( spec, status ); while ( (curtok == TOK_TIMES) && _ok(status) ) { NextToken( spec, status ); node = NewNode( TOK_TIMES, 0, node, ParsePrimary(spec, status), status ); } return node; }
/// expression /// ::= primary binoprhs /// std::unique_ptr<ExprAST> UDFParser::ParseExpression() { auto lhs = ParsePrimary(); if (!lhs) { return nullptr; } return ParseBinOpRHS(0, std::move(lhs)); }
/* expression * ::= primary binoprhs * */ static shared_ptr<ExprAST> ParseExpression() { auto LHS = ParsePrimary(); if (!LHS) return 0; if (LHS->getType() == Ty_string) return LHS; return ParseBinOpRHS(1, LHS); }
ExprAST * FAO::ParseExpression() { ExprAST * lhs = ParsePrimary(); if (lhs == nullptr){ return nullptr; } return ParseBinOpRHS(0, lhs); }
/* ParseLet - parse the 'LET' statement */ static void ParseLet(ParseContext *c) { ParseTreeNode *lvalue; PVAL pv; lvalue = ParsePrimary(c); code_lvalue(c, lvalue, &pv); FRequire(c, '='); ParseRValue(c); (*pv.fcn)(c, PV_STORE, &pv); FRequire(c, T_EOL); }
//create a JsonPair object while pair found sgdm::StackGuard<JsonValue>&& JsonParser::ParsePair(){ sgdm::StackGuard<JsonValue> pairName(new JsonName(idString)); getNextTok(); if(currentTok != ':'){ return Error("Expecting :"); } else{ getNextTok(); //eat : sgdm::StackGuard<JsonValue> pairValue(std::move(ParsePrimary())); return std::move(new JsonPair(std::move(pairName),std::move(pairValue))); } }
sptr<ExprNode> Parser::ParseUnary() { if (Match(PLUS, MINUS, NOT, -1)) { sptr<Token> buff = currToken; NextToken(); return sptr<UnOpNode>(new UnOpNode(buff, ParseUnary())); } else { return ParsePrimary(); } }
Json* Json::Parser::ParseArray(){ NextChar(); Array* array = new Array; Json* member; while(ch != ']'){ BackCharIndex(); member = ParsePrimary(); array->push_back(member); NextChar(); if(ch == ',') NextChar(); } return new Json(array); }
// unary // ::= primary // ::= '!' unary std::unique_ptr<CExprAST> CParser::ParseUnary() { // If current token is not an operator, it must be a primary expr if (!isascii(m_CurrentToken) || m_CurrentToken == '(' || m_CurrentToken == ',') return ParsePrimary(); // If this is an unary operator, read it int opcode = m_CurrentToken; GetNextToken(); auto operand = ParseUnary(); if (operand) return std::make_unique<CUnaryExprAST>(opcode, std::move(operand)); return nullptr; }
/* binoprhs * ::= ('+' primary)* * */ static shared_ptr<ExprAST> ParseBinOpRHS(int lasprec, shared_ptr<ExprAST> LHS) { while (1) { int curprec = getTokPrecedence(); if (curprec < lasprec) return LHS; int op = CurTok; getNextToken(); //eat op auto RHS = ParsePrimary(); if (!RHS) return 0; int nextprec = getTokPrecedence(); if (curprec < nextprec) { RHS = ParseBinOpRHS(curprec + 1, RHS); if (!RHS) return 0; } LHS = shared_ptr<ExprAST>(new BinaryExprAST(op, LHS, RHS)); } }
//create a JsonArray object while array found sgdm::StackGuard<JsonValue>&& JsonParser::ParseArray(){ if(currentTok != '['){ return Error("Expecting [ at the beginning"); } sgdm::StackGuard<JsonValue> array(new JsonArray()); getNextTok(); while(currentTok != ']'){ if (currentTok == ','){ getNextTok(); continue; } else{ array->push(std::move(ParsePrimary())); } } getNextTok(); //eat ] return std::move(array); }
/* ParseImpliedLetOrFunctionCall - parse an implied let statement or a function call */ static void ParseImpliedLetOrFunctionCall(ParseContext *c) { ParseTreeNode *expr; Token tkn; PVAL pv; expr = ParsePrimary(c); switch (tkn = GetToken(c)) { case '=': code_lvalue(c, expr, &pv); ParseRValue(c); (*pv.fcn)(c, PV_STORE, &pv); break; default: SaveToken(c, tkn); code_rvalue(c, expr); putcbyte(c, OP_DROP); break; } FRequire(c, T_EOL); }
//top level parse function sgdm::StackGuard<JsonValue>&& JsonParser::ParsePrimary(){ switch(currentTok){ case tok_endl: return std::move(Error("primary fail")); case tok_identifier: return std::move(ParseName()); case tok_integer: return std::move(ParseInteger()); case tok_double: return std::move(ParseDouble()); case '[': return std::move(ParseArray(alloc)); case '{': return std::move(ParseObject(alloc)); default: return std::move(Error("unknown token found near place" + std::to_string(indexCount))); } sgdm::StackGuard<JsonValue>&& JsonParser::Parse(){ getNextTok(); return std::move(ParsePrimary(alloc)); }
/// binoprhs /// ::= ('+' primary)* std::unique_ptr<ExprAST> UDFParser::ParseBinOpRHS( int expr_prec, std::unique_ptr<ExprAST> lhs) { // If this is a binop, find its precedence. while (true) { int tok_prec = GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (tok_prec < expr_prec) { return lhs; } // Okay, we know this is a binop. int bin_op = cur_tok_; GetNextToken(); // eat binop // Parse the primary expression after the binary operator. auto rhs = ParsePrimary(); if (!rhs) { return nullptr; } // If BinOp binds less tightly with rhs than the operator after rhs, let // the pending operator take rhs as its lhs. int next_prec = GetTokPrecedence(); if (tok_prec < next_prec) { rhs = ParseBinOpRHS(tok_prec + 1, std::move(rhs)); if (!rhs) { return nullptr; } } // Merge lhs/rhs. lhs = llvm::make_unique<BinaryExprAST>(bin_op, std::move(lhs), std::move(rhs)); } }
ExprAST * FAO::ParseBinOpRHS(int Expc, ExprAST * lhs) { // If this is a binary operator,find its precedence while (true){ int TokPrec = GetPrecedence(); // If this is a binary operator,find its precedence at least as tightly as the current binary operator // consume it,otherwise we are done. if (TokPrec < Expc){ return lhs; } // Okay,we know this is binary operator char BinOp = cur_token_.GetOperation(); Advance(); // eat binary operator // Parse the primary expression after the binary operator ExprAST * rhs = ParsePrimary(); if (rhs == nullptr){ return nullptr; } // If binary operator less tightly with rhs than the operator after rhs,let // the pending operator than rhs as its lhs int NextPrec = GetPrecedence(); if (TokPrec < NextPrec){ rhs = ParseBinOpRHS(TokPrec + 1, rhs); if (rhs == nullptr){ return nullptr; } } // Merge lhs/rhs lhs = new BinaryOpExpr(BinOp, lhs, rhs); } return lhs; }
/* * 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)); } }
/// expression /// ::= primary binoprhs /// static ExprAST *ParseExpression() { ExprAST *LHS = ParsePrimary(); if (!LHS) return 0; return ParseBinOpRHS(0, LHS); }
std::unique_ptr<ExprAST> UDFParser::ParseReturn() { GetNextToken(); // eat the return return ParsePrimary(); }