示例#1
0
文件: parser.cpp 项目: helgefmi/Toy
Statement *ParserContext::parse_statement() {
    Statement *statement = 0;
    switch (curtok()->type()) {
        case tok_while: {
            statement = parse_while();
            break;
        }
        case tok_if: {
            statement = parse_if();
            break;
        }
        case tok_return: {
            statement = parse_return();
            eat_token(tok_semicolon);
            break;
        }
        case tok_def: {
            statement = parse_def();
            break;
        }
        default: {
            Expression *expression = parse_expression();
            if (expression) {
                statement = new ExpressionStatement(expression);
                eat_token(tok_semicolon);
            } else {
                throw UnexpectedToken("parse_statement", curtok());
            }
            break;
        }
    }
    return statement;
}
示例#2
0
Status Parser::ConsumeToken(int type) {
  ASSIGN_OR_RETURN(auto token, GetNextToken());

  if (!token->IsType(type)) {
    return UnexpectedToken(*token);
  }

  return Status();
}
示例#3
0
文件: parser.cpp 项目: helgefmi/Toy
Expression *ParserContext::parse_primary() {
    switch (curtok()->type()) {
        case tok_word: return parse_word_expression();
        case tok_number: return parse_number();
        case tok_string: return parse_string();
        case tok_paren_start: return parse_paren_expression();
        default: throw UnexpectedToken("parse_primary", curtok());
    }
}
示例#4
0
 inline void FileParse::checkComment(std::ifstream& fin) {
   char c;
   fin.get(c);
   // Make sure we are not at eof
   if (fin.eof()) return;
   // Check what kind of comment this is
   if (c=='/')      passComment(fin, false);
   else if (c=='*') passComment(fin, true);
   else             throw UnexpectedToken("In checkComment: Token: ["+toStr(c)+"].");
 }
示例#5
0
StatusOr<std::unique_ptr<Node>> Parser::Parse() {
  if (!look_ahead_token_) {
    ASSIGN_OR_RETURN(auto token, GetNextToken());
  }

  // Check for end of input.
  if (look_ahead_token_->IsType(Lexer::TYPE_END_OF_INPUT)) {
    return UnexpectedToken(*look_ahead_token_);
  }

  // Parse expression.
  return ParseExpression(0);
}
示例#6
0
 inline void FileParse::getHeading(std::ifstream& fin, string& heading, bool& found) {
   char c;
   fin.get(c);
   bool whitespace = false;
   while (!fin.eof()) {
     if (c=='{') {
       found = true;
       return;
     }
     else if (c==':') {
       found = false;
       return;
     }
     else if (c=='\n' || c=='\r' || c==' ') whitespace = true;
     else { // Just a regular character. Could be a whitespace
       if (whitespace) { // Error
         throw UnexpectedToken("In getHeading: Found whitespace.");
       }
       else heading.push_back(c);
     }
     // Get the next character
     fin.get(c);
   }
 }
示例#7
0
StatusOr<std::unique_ptr<Node>> Parser::ParseInfixToken(
    std::unique_ptr<const Token> token, std::unique_ptr<const Node> left) {
  return UnexpectedToken(*token);
}
示例#8
0
Status Parser::ExpectToken(const Token& token, int type) {
  return token.IsType(type) ? Status() : UnexpectedToken(token);
}