示例#1
0
void Parser::parsePrimary() {
  Token t = lex.peek();
  Token t2 = lex.peek(2);
  
  switch (t.type) {
    case Token::NUMERIC:
      lex.consume(Token::NUMERIC);
      break;
    case Token::STRINGCONSTANT:
      lex.consume(Token::STRINGCONSTANT);
      break;
    case Token::TEXTURE:
    case Token::ENVIRONMENT:
    case Token::SHADOW:
      parseTexture();
      break;
    case Token::LPAREN:
      lex.consume();
      parseExpression();
      lex.consume(Token::RPAREN);
      break;
    case Token::LBRACE:
      lex.consume();
      parseExpression();
      lex.consume(Token::RBRACE);
      break;
    case Token::IDENTIFIER:
      t2 = lex.peek(2);
      if (t2.type == Token::LPAREN)
        parseCallExpr();
      else if (t2.type == Token::LBRACKET)
        parseArrayExpr();
      else
        lex.consume(Token::IDENTIFIER);
      break;
    default:
      assert(0 && "Parse error, expected primary!");
  }
}
示例#2
0
文件: parser.cpp 项目: maximecb/bjrn
/**
Implements the parsing of expressions that are part of the core language

The core language supports:
- Functions (but not closures)
- Integer literals (decimal only)
- String literals
- Tuple literals [a,b]
- If-else expressions
- Sequencing blocks with {a;b}
- Function calls fun(a,b)
*/
Tuple coreParseExpr(Input* input)
{
    // Consume whitespace
    input->eatWS();

    // Numerical constant
    if (isdigit(input->peekCh()))
    {
        return parseInt(input);
    }

    // String literal
    if (input->matchCh('\''))
    {
        return parseStringLit(input, '\'');
    }
    if (input->matchCh('\"'))
    {
        return parseStringLit(input, '\"');
    }

    // Tuple expression
    if (input->matchCh('['))
    {
        return Tuple{
            Tuple("tuple"),
            parseExprList(input, ']')
        };
    }

    // Block/sequence expression (i.e { a; b; c }
    if (input->matchCh('{'))
    {
        return parseBlockExpr(input, '}');
    }

    // Keywords
    if (isalnum(input->peekCh()))
    {
        // If expression
        if (input->matchStr("if"))
            return parseIfExpr(input);

        // Let expression
        if (input->matchStr("let"))
            return parseLetExpr(input);

        // Function definition expression
        if (input->matchStr("fun"))
            return parseFunExpr(input);

        // Function call expression
        if (input->matchStr("call"))
            return parseCallExpr(input);
    }

    // Host/magic constants
    if (input->matchCh('$'))
    {
        return parseHostRef(input);
    }

    // Variable references
    if (input->peekCh() == '_' || isalpha(input->peekCh()))
    {
        return parseIdent(input);
    }

    // Parsing failed
    throw ParseError(input, "failed to parse expression");
}
示例#3
0
void Parser::parseCallStmt() {
  parseCallExpr();
  lex.consume(Token::SEMI);
}