parsePrimary method

Expression parsePrimary()

// EXPRESSIONS //////

Implementation

Expression parsePrimary() {
  int? start = token!.startOffset;
  switch (token!.type) {
    case Token.NAME:
      switch (token!.text) {
        case 'this':
          Token tok = next();
          return ThisExpression()
            ..start = start
            ..end = endOffset
            ..line = tok.line;
        case 'true':
          Token tok = next();
          return LiteralExpression(true, 'true')
            ..start = start
            ..end = endOffset
            ..line = tok.line;
        case 'false':
          Token tok = next();
          return LiteralExpression(false, 'false')
            ..start = start
            ..end = endOffset
            ..line = tok.line;
        case 'null':
          Token tok = next();
          return LiteralExpression(null, 'null')
            ..start = start
            ..end = endOffset
            ..line = tok.line;
        case 'function':
          return FunctionExpression(parseFunction());
      }
      Name name = parseName();
      return NameExpression(name)
        ..start = start
        ..end = endOffset
        ..line = name.line;

    case Token.NUMBER:
      Token tok = next();
      return LiteralExpression(num.parse(tok.text!), tok.text)
        ..start = start
        ..end = endOffset
        ..line = tok.line;

    case Token.STRING:
      Token tok = next();
      return LiteralExpression(tok.value, tok.text)
        ..start = start
        ..end = endOffset
        ..line = tok.line;

    case Token.LBRACKET:
      return parseArrayLiteral();

    case Token.LBRACE:
      return parseObjectLiteral();

    case Token.LPAREN:
      next();
      Expression? exp;
      if (token!.type != Token.RPAREN) {
        exp = parseExpression();
      }
      consume(Token.RPAREN);
      if (token!.type == Token.ARROW) {
        return FunctionExpression(parseArrowFunction(exp));
      } else {
        return exp!;
      }

    case Token.BINARY:
    case Token.ASSIGN:
      if (token!.text == '/' || token!.text == '/=') {
        Token regexTok = lexer.scanRegexpBody(token);
        token = lexer.scan();
        endOffset = regexTok.endOffset;
        return RegexpExpression(regexTok.text)
          ..start = regexTok.startOffset
          ..end = regexTok.endOffset
          ..line = regexTok.line;
      }
      throw fail();

    default:
      throw fail();
  }
}