Пример #1
0
// Parse a class definition.
//
//    class-declaration:
//      'class' identifier [':']            class-body
//      'class' identifier [':' type]       class-body
//      'class' identifier [':' extension]  class-body
//
// NOTE: The parser currently allows the omission of the ':' because it
// looks weird when the kind is not given explicitly omitted.
//
// TODO: We could use '=' notation in bodies to create new derived types.
Decl&
Parser::class_declaration()
{

  Match_token_pred end_kind(*this, lbrace_tok);

  require(class_tok);
  Name& name = identifier();

  // Match the metatype.
  Type* kind;
  if (match_if(colon_tok)) {
    if (match_if(extension_tok))
      return extension_declaration(name, &cxt.get_type_type());
    if (next_token_is(lbrace_tok))
      kind = &cxt.get_type_type();
    else
      kind = &unparsed_type(end_kind);
  } else {
    kind = &cxt.get_type_type();
  }

  // Point of declaration.
  Decl& decl = start_class_declaration(name, *kind);
  Enter_scope scope(cxt, cxt.saved_scope(decl));

  // Match the class body.
  Def& def = class_body();
  
  return finish_class_definition(decl, def);
};
Пример #2
0
// Parse a base class (super) declaration.
//
//    super-declaration:
//      super [identifier] : type;
Decl&
Parser::super_declaration()
{
  Match_token_pred end_type(*this, tk::semicolon_tok);

  require(tk::super_tok);

  // Match the optional identifier.
  Name* name;
  if (next_token_is(tk::identifier_tok))
    name = &identifier();
  else
    name = &build.get_id();

  // Match type type.
  match(tk::colon_tok);
  Type& type = unparsed_type(end_type);
  match(tk::semicolon_tok);

  return on_super_declaration(*name, type);
}