Ejemplo n.º 1
0
void UnwrappedLineParser::parseIfThenElse() {
  assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
  nextToken();
  parseParens();
  bool NeedsUnwrappedLine = false;
  if (FormatTok.Tok.is(tok::l_brace)) {
    parseBlock();
    NeedsUnwrappedLine = true;
  } else {
    addUnwrappedLine();
    ++Line.Level;
    parseStatement();
    --Line.Level;
  }
  if (FormatTok.Tok.is(tok::kw_else)) {
    nextToken();
    if (FormatTok.Tok.is(tok::l_brace)) {
      parseBlock();
      addUnwrappedLine();
    } else if (FormatTok.Tok.is(tok::kw_if)) {
      parseIfThenElse();
    } else {
      addUnwrappedLine();
      ++Line.Level;
      parseStatement();
      --Line.Level;
    }
  } else if (NeedsUnwrappedLine) {
    addUnwrappedLine();
  }
}
Ejemplo n.º 2
0
Symbol parseForStatement(CharacterSource* source)
{
    static String forKeyword("for");
    static String doneKeyword("done");
    Span span;
    if (!Space::parseKeyword(source, forKeyword, &span))
        return Symbol();
    Span span2;
    Space::assertCharacter(source, '(', &span2);
    Symbol preStatement = parseStatement(source);
    if (!preStatement.valid())
        Space::assertCharacter(source, ';', &span2);
    Symbol expression = parseExpression(source);
    Space::assertCharacter(source, ';', &span2);
    Symbol postStatement = parseStatement(source);
    Space::parseCharacter(source, ')', &span2);
    Symbol statement = parseStatementOrFail(source);
    Symbol doneStatement;
    span2 = spanOf(statement);
    if (Space::parseKeyword(source, doneKeyword, &span2)) {
        doneStatement = parseStatementOrFail(source);
        span2 = spanOf(doneStatement);
    }
    return Symbol(atomForStatement, preStatement, expression, postStatement,
        statement, doneStatement, newSpan(span + span2));
}
Ejemplo n.º 3
0
/*** Parse an if statement ***/
If* Parser::parseIf()
{
	if(!hasTokens() || peekToken().getType() != T_IF)
		throw ParserSyntaxException(getToken(), "Expected \"if\".");
	Token tok = getToken();

	// Parse the condition
	std::auto_ptr<Object> expr(parseExpression());

	if(!hasTokens() || peekToken().getType() != T_THEN)
		throw ParserSyntaxException(getToken(), "Expected \"then\"!");
	getToken();

	// Parse any newlines
	while(hasTokens() && peekToken().getType() == T_EOL)
		getToken();

	// Parse if statement body
	std::auto_ptr<NodeList> ifStmts(new NodeList());
	while(hasTokens() && !(peekToken().getType() == T_ELSE || peekToken().getType() == T_FI))
	{
		ifStmts->pushNode(parseStatement());
		parseSeparation();
	}

	// Create the new if object
	std::auto_ptr<If> result(new If());
	result->setLineNumber(tok.getLineNumber());
	result->setColumnNumber(tok.getColumnNumber());

	// Parse an else statement body if present
	if(hasTokens() && peekToken().getType() == T_ELSE)
	{
		getToken();
		std::auto_ptr<NodeList> elseStmts(new NodeList());

		// Parse any newlines
		while(hasTokens() && peekToken().getType() == T_EOL)
			getToken();

		while(hasTokens() && peekToken().getType() != T_FI)
		{
			elseStmts->pushNode(parseStatement());
			parseSeparation();
		}

		result->setElseStatements(elseStmts.release());
	}

	if(!hasTokens() || peekToken().getType() != T_FI)
		throw ParserSyntaxException(getToken(), "Expected \"fi\"!");
	getToken();

	result->setIfCondition(expr.release());
	result->setIfStatements(ifStmts.release());
	return result.release();
}
Ejemplo n.º 4
0
void Parser::parseStatement(){
	if(isNext(tc_ID)){
		SymbolTableEntry* entry = m_currentToken->getSymTabEntry();
		match(tc_ID);
		parseStatementPrime(entry);
	}
	else if(isNext(tc_IF)){
		match(tc_IF);
		SymbolTableEntry* exp = parseExpression();
		SymbolTableEntry* f = newLabel();
		SymbolTableEntry* end = newLabel();
		m_code->generate(cd_EQ, exp, m_symbolTable->lookup(CodeFalse), f);
		match(tc_THEN);
		if(m_parserError){
			TokenCode synch[] = {tc_ID, tc_BEGIN, tc_IF, tc_WHILE, tc_NONE};
			recover(synch);
//			if(isNext(tc_THEN)){
//				match(tc_THEN);
//			}
		}
		parseStatement();
		m_code->generate(cd_GOTO, NULL, NULL, end);
		m_code->generate(cd_LABEL, NULL, NULL, f);
		match(tc_ELSE);
		if(m_parserError){
			TokenCode synch[] = {tc_ID, tc_BEGIN, tc_IF, tc_WHILE, tc_NONE};
			recover(synch);
//			if(isNext(tc_ELSE)){
//				match(tc_ELSE);
//			}
		}
		parseStatement();
		m_code->generate(cd_LABEL, NULL, NULL, end);
	}
	else if(isNext(tc_WHILE)){
		match(tc_WHILE);
		SymbolTableEntry* start = newLabel();
		m_code->generate(cd_LABEL, NULL, NULL, start);
		SymbolTableEntry* exp = parseExpression();
		SymbolTableEntry* end = newLabel();
		m_code->generate(cd_EQ, exp, m_symbolTable->lookup(CodeFalse), end);
		match(tc_DO);
		if(m_parserError){
			TokenCode synch[] = {tc_ID, tc_BEGIN, tc_IF, tc_WHILE, tc_NONE};
			recover(synch);
//			if(isNext(tc_DO)){
//				match(tc_DO);
//			}
		}
		parseStatement();
		m_code->generate(cd_GOTO, NULL, NULL, start);
		m_code->generate(cd_LABEL, NULL, NULL, end);
	}
	else{
		parseCompoundStatement();
	}
}
Ejemplo n.º 5
0
ContextStatement WASMFunctionParser::parseIfElseStatement(Context& context)
{
    parseExpressionI32(context);
    PROPAGATE_ERROR();
    parseStatement(context);
    PROPAGATE_ERROR();
    parseStatement(context);
    // FIXME: Implement this instruction.
    return UNUSED;
}
Ejemplo n.º 6
0
/***************************************************************
* Function: CodeParser::parseForStmt()
* Purpose : Parse the XML code of a for loop statement
* Initial : Maxime Chevalier-Boisvert on November 8, 2008
****************************************************************
Revisions and bug fixes:
*/
Statement* CodeParser::parseForStmt(const XML::Element* pElement)
{
  unsigned loopDepth = ++maxLoopDepth;

	// Parse the assignment statement
	Statement* pAssignStmt = parseStatement(pElement->getChildElement(0));

	// Ensure that this statement really is an assignment statement
	if (pAssignStmt->getStmtType() != Statement::ASSIGN)
		throw XML::ParseError("Invalid statement type", pElement->getChildElement(0)->getTextPos());

	// Parse the loop body
	StmtSequence* pLoopBody = parseStmtList(pElement->getChildElement(1));

    unsigned annotations = 0;
    // reset maxLoopDepth
    if (loopDepth == maxLoopDepth) {
      // isInnermost loop is true;
      annotations |= Statement::INNERMOST;
    }
    if (loopDepth == 1) {
      // isOutermost loop is true;
      annotations |= Statement::OUTERMOST;
      // reset the counters
      maxLoopDepth = 0;
    }

	// return the new for statement object
    return new ForStmt((AssignStmt*)pAssignStmt, pLoopBody, annotations);
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
Symbol parseStatementOrFail(CharacterSource* source)
{
    Symbol statement = parseStatement(source);
    if (!statement.valid())
        source->location().throwError("Expected statement");
    return statement;
}
Ejemplo n.º 9
0
	shared_ptr<Statement> Parser::parseElse()
	{
		shared_ptr<ElseStmt> elseStmt = make_shared<ElseStmt>();
		elseStmt->value = *it;
		it++;
		if (match(IF))//match if
		{
			shared_ptr<IfStmt> elseifnode = static_pointer_cast<IfStmt>(parseIf());
			elseStmt->addNode(elseifnode);
		}
		else if (match(BEGIN))//match begin Compound Statement 复合语句
		{
			shared_ptr<Statement> body = parseStmtList(END);
			elseStmt->body = body;
			match(END);
			it++;
			match(SEMI);
		}
		else//单行语句
		{
			shared_ptr<Statement> body1 = parseStatement();
			elseStmt->addNode(body1);
		}
		return elseStmt;
	}
Ejemplo n.º 10
0
/***************************************************************
* Function: CodeParser::parseStmtList()
* Purpose : Parse the XML code of a statement list
* Initial : Maxime Chevalier-Boisvert on November 7, 2008
****************************************************************
Revisions and bug fixes:
*/
StmtSequence* CodeParser::parseStmtList(const XML::Element* pElement)
{
	// Create a statement vector to store the statements
	StmtSequence::StmtVector stmtVector;

	// For each child element
	for (size_t i = 0; i < pElement->getNumChildren(); ++i)
	{
		// Get this child element
		XML::Element* pChildElement = pElement->getChildElement(i);

		// If this is a variable declaration
		if (pChildElement->getName() == "VariableDecl")
		{
			// Ignore for now
			continue;
		}

		// Parse this statement
		Statement* pStmt = parseStatement(pChildElement);

		// Add the statement to the vector
		stmtVector.push_back(pStmt);
	}

	// Return a new sequence statement
	return new StmtSequence(stmtVector);
}
Ejemplo n.º 11
0
	shared_ptr<Statement> Parser::parseIf()
	{
		shared_ptr<IfStmt> ifstmt = make_shared<IfStmt>();
		ifstmt->value = *it;
		currNode = ifstmt;
		it++;
		auto cond = parseExpression();
		ifstmt->condition = cond;
		match(THEN);
		it++;
		if (match(BEGIN))
		{
			currNode = ifstmt;
			shared_ptr<Statement> body = parseStmtList(END);
			ifstmt->thenBody=body;
			match(END);
			it++;
			match(SEMI);
		}
		else
		{
			shared_ptr<Statement> body1 = parseStatement();
			ifstmt->thenBody = body1;
		}
		ifstmt->haveElse = false;
		while (advance(ELSE))
		{
			ifstmt->haveElse = true;
			it++;
			shared_ptr<ElseStmt> elseStmt = static_pointer_cast<ElseStmt>(parseElse());
			ifstmt->elseBody = elseStmt;
		}
		return ifstmt;
	}
Ejemplo n.º 12
0
void Parser::parseStatementListPrime(){
	if(isNext(tc_SEMICOL)){
		match(tc_SEMICOL);
		parseStatement();
		parseStatementListPrime();
	}
}
Ejemplo n.º 13
0
	shared_ptr<Statement> Parser::parseWhile()
	{
		shared_ptr<WhileStmt> whStmt = make_shared<WhileStmt>();
		whStmt->value = *it;
		currNode = whStmt;
		it++;
		shared_ptr<Expression> cond = parseExpression();
		whStmt->condition = cond;
		//it++;
		match(DO);
		it++;
		if (match(BEGIN))
		{
			shared_ptr<Statement> body = parseStmtList(END);
			whStmt->body=body;
			match(END);
			it++;
			match(SEMI);
		}
		else
		{
			shared_ptr<Statement> body1 = parseStatement();
			whStmt->body = body1;
		}
		return whStmt;
	}
Ejemplo n.º 14
0
	shared_ptr<Statement> Parser::parseStmtList(Tag teminator)
	{
		auto tmpNode = currNode;
		shared_ptr<Statement> stList = make_shared<Statement>();
		currNode = stList;//所有子节点将加入到block上

		stList->value = *it;
		it++;
		while (it->tag != teminator)
		{
			if (it->tag == END && teminator != END)//处理begin..end的匹配情况
			{
				shared_ptr<Statement> st1 = make_shared<Statement>();
				st1->value = *it;
				stList->addNode(st1);
				it++;
			}
			shared_ptr<Statement> st = parseStatement();
			stList->addNode(st);
			it++;
		}
		//恢复currNode
		currNode = tmpNode;
		return stList;
	}
Ejemplo n.º 15
0
/*** Parse from zero-level ***/
void Parser::parse()
{
	if(hasTokens() && peekToken().getType() == T_EOL)
		parseSeparation();

	while(hasTokens())
	{
		

	//		if(peekToken().getType() == T_PROC)
	//			parseProcedureDefinition();
	//	else	if(peekToken().getType() == T_FUN)
	//			parseFunctionDefinition();
	//	else
			if(peekToken().getType() == T_PROC)
			{
				parseProcedure();
				parseSeparation();
			}
			else if(peekToken().getType() == T_FUN)
			{
				parseFunction();
				parseSeparation();
			}
			else
			{
			list->pushNode(parseStatement());
			parseSeparation();
			}
	}
//	VariableManager manager;
}
Ejemplo n.º 16
0
static void expectAndEatStatement(MemoryStack* stack, LexerCarriage* carriage, Array<SyntaxError>* errors, ASTNode* result)
{
    if (!parseStatement(stack, carriage, errors, result))
    {
        pushBack(errors, SyntaxError{ SET_STATEMENT_EXPECTED, carriage->currentLineNumber });
    }
}
Ejemplo n.º 17
0
ContextStatement WASMFunctionParser::parseSwitchStatement(Context& context)
{
    uint32_t numberOfCases;
    READ_COMPACT_UINT32_OR_FAIL(numberOfCases, "Cannot read the number of cases.");
    parseExpressionI32(context);
    PROPAGATE_ERROR();

    m_breakScopeDepth++;
    for (uint32_t i = 0; i < numberOfCases; ++i) {
        WASMSwitchCase switchCase;
        READ_SWITCH_CASE_OR_FAIL(switchCase, "Cannot read the switch case.");
        switch (switchCase) {
        case WASMSwitchCase::CaseWithNoStatements:
        case WASMSwitchCase::CaseWithStatement:
        case WASMSwitchCase::CaseWithBlockStatement: {
            uint32_t value;
            READ_COMPACT_INT32_OR_FAIL(value, "Cannot read the value of the switch case.");
            if (switchCase == WASMSwitchCase::CaseWithStatement) {
                parseStatement(context);
                PROPAGATE_ERROR();
            } else if (switchCase == WASMSwitchCase::CaseWithBlockStatement) {
                parseBlockStatement(context);
                PROPAGATE_ERROR();
            }
            break;
        }
        case WASMSwitchCase::DefaultWithNoStatements:
        case WASMSwitchCase::DefaultWithStatement:
        case WASMSwitchCase::DefaultWithBlockStatement: {
            FAIL_IF_FALSE(i == numberOfCases - 1, "The default case must be the last case.");
            if (switchCase == WASMSwitchCase::DefaultWithStatement) {
                parseStatement(context);
                PROPAGATE_ERROR();
            } else if (switchCase == WASMSwitchCase::DefaultWithBlockStatement) {
                parseBlockStatement(context);
                PROPAGATE_ERROR();
            }
            break;
        }
        default:
            ASSERT_NOT_REACHED();
        }
    }
    m_breakScopeDepth--;
    // FIXME: Implement this instruction.
    return UNUSED;
}
Ejemplo n.º 18
0
ContextStatement WASMFunctionParser::parseLabelStatement(Context& context)
{
    m_labelDepth++;
    parseStatement(context);
    PROPAGATE_ERROR();
    m_labelDepth--;
    // FIXME: Implement this instruction.
    return UNUSED;
}
Ejemplo n.º 19
0
ast::Node *parseTopLevel(TokenIt &it, const TokenIt &end)
{
  switch((*it)->type)
  {
    case Token::FUNCTION:   return parseFunction(it, end, false);
    case Token::SUB:        return parseFunction(it, end, true);
    default:                return parseStatement(it, end);
  }
}
Ejemplo n.º 20
0
std::list<PARSENODE_PTR > SQLParser::parseSQL() {
    std::list<PARSENODE_PTR > statements;
    readToken();
    while (startsStatement(nowReading)) {
        statements.push_back(parseStatement());
    }
    expect(NULLTOKEN);
    return statements;
}
Ejemplo n.º 21
0
ParseStatus Parser::parseStatementList() {
  // Keep parsing statements until we see a right brace.
  while(lex[0].type != TT_DELIMITER ||
        lex[0].value != DL_RBRACE) {
    ParseStatus error = parseStatement();
    if (error) return error;
  }

  return PARSE_OK;
}
Ejemplo n.º 22
0
ContextStatement WASMFunctionParser::parseBlockStatement(Context& context)
{
    uint32_t numberOfStatements;
    READ_COMPACT_UINT32_OR_FAIL(numberOfStatements, "Cannot read the number of statements.");
    for (uint32_t i = 0; i < numberOfStatements; ++i) {
        parseStatement(context);
        PROPAGATE_ERROR();
    }
    return UNUSED;
}
Ejemplo n.º 23
0
SymbolArray parseStatementSequence(CharacterSource* source)
{
    SymbolList list;
    do {
        Symbol statement = parseStatement(source);
        if (!statement.valid())
            return list;
        list.add(statement);
    } while (true);
}
Ejemplo n.º 24
0
	shared_ptr<Statement> Parser::parseCaseBranch()
	{
		shared_ptr<Statement> caseBranch = make_shared<Statement>();
		shared_ptr<Expression> constNode = parseCaseConst();
		caseBranch->addNode(constNode);
		match(Colon);
		shared_ptr<Statement> st = parseStatement();
		caseBranch->addNode(st);
		return caseBranch;
	}
Ejemplo n.º 25
0
static ast* parseS (parserCtx* ctx) {
    ast* node = parseStatement(ctx);

    if (see_kind(ctx, tokenEOF))
        accept(ctx);

    else
        error(ctx)("Expected end of text, found '%s'\n", ctx->current.buffer);

    return node;
}
Ejemplo n.º 26
0
Ptr<AST::Program> Parser::parseProgram ()
{
	AST::Program *program = new AST::Program;
	AST::Statement *statement = nullptr;

	while ((statement = parseStatement()) != nullptr) {
		program->statements.push_back(statement);
	}

	return program;
}
Ejemplo n.º 27
0
toSQLParse::statement toSQLParse::parseStatement(tokenizer &tokens)
{
	statement cur(statement::Statement);
	cur = parseStatement(tokens, false, false);
	if (cur.Type == statement::List)
	{
		QMessageBox::warning(QApplication::activeWindow(), "Sqliteman",
							 "toSQLparse: Unbalanced parenthesis (Too many ')')");
	}
	return cur;
}
Ejemplo n.º 28
0
static void parseStatements(MemoryStack* stack, LexerCarriage* carriage, Array<SyntaxError>* errors, LinkedList<ASTNode>* result)
{
    ASTNode statement;
    do
    {
        if (parseStatement(stack, carriage, errors, &statement))
            *pushElement(result, stack) = statement;
        else
            break;
    } while (true);
}
Ejemplo n.º 29
0
void UnwrappedLineParser::parseDoWhile() {
  assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
  nextToken();
  if (FormatTok.Tok.is(tok::l_brace)) {
    parseBlock();
  } else {
    addUnwrappedLine();
    ++Line.Level;
    parseStatement();
    --Line.Level;
  }

  // FIXME: Add error handling.
  if (!FormatTok.Tok.is(tok::kw_while)) {
    addUnwrappedLine();
    return;
  }

  nextToken();
  parseStatement();
}
Ejemplo n.º 30
0
std::list<toSQLParse::statement> toSQLParse::parse(tokenizer &tokens)
{
	std::list<toSQLParse::statement> ret;
	statement cur(statement::Statement);
	for (cur = parseStatement(tokens, false, false);
			cur.subTokens().begin() != cur.subTokens().end();
			cur = parseStatement(tokens, false, false))
	{
		if (cur.Type == statement::List)
		{
			QMessageBox::warning(QApplication::activeWindow(), "Sqliteman",
								 "toSQLparse: Unbalanced parenthesis (Too many ')')");
		}
		ret.insert(ret.end(), cur);
	}
	QString str = tokens.remaining(false);
	if (!str.isEmpty())
		ret.insert(ret.end(), statement(statement::Raw,
										str, tokens.line()));
	return ret;
}