// Parse a record declaration. // // record-decl -> 'struct' identifier record-body // // record-body -> '{' field-seq '}' // // field-seq -> field-seq | field-seq field-seq Decl* Parser::record_decl(Specifier spec) { require(struct_kw); Token n = match(identifier_tok); const Type* t = nullptr; // Determine if it is inheriting from a base class if(match_if(colon_tok)){ // We have a base class t = type(); } // record-body and field-seq require(lbrace_tok); Decl_seq fs, ms; while (lookahead() != rbrace_tok) { Specifier spec = specifier_seq(); if (lookahead() == def_kw) { Decl* m = method_decl(spec); ms.push_back(m); } else if(lookahead() == identifier_tok) { Decl* f = field_decl(spec); fs.push_back(f); } else { throw Syntax_error(ts_.location(), "invalid member declaration"); } } match(rbrace_tok); // Need to replace nullptr with base record return on_record(spec, n, fs, ms, t); }
// Parse a record declaration. // // record-decl -> 'struct' identifier record-body // // record-body -> '{' field-seq '}' // // field-seq -> field-seq | field-seq field-seq Decl* Parser::record_decl(Specifier spec) { require(struct_kw); Token n = match(identifier_tok); // record-body and field-seq require(lbrace_tok); Decl_seq fs, ms; while (lookahead() != rbrace_tok) { Specifier spec = specifier_seq(); if (lookahead() == def_kw) { Decl* m = method_decl(spec); ms.push_back(m); } else if(lookahead() == identifier_tok) { Decl* f = field_decl(spec); fs.push_back(f); } else { throw Syntax_error(ts_.location(), "invalid member declaration"); } } match(rbrace_tok); return on_record(spec, n, fs, ms); }