Changeset 640ffe6 in mainline for uspace/app/sbi/src/parse.c


Ignore:
Timestamp:
2010-05-08T08:15:57Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4039c77
Parents:
1317380 (diff), 051bc69a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jsvoboda/helenos/sysel. New: cspan printing, boolean ops, enums, constructors etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/parse.c

    r1317380 r640ffe6  
    4848
    4949/*
    50  * Module members
     50 * Module and CSI members
    5151 */
    5252static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass,
    5353    stree_csi_t *outer_csi);
    5454static stree_csimbr_t *parse_csimbr(parse_t *parse, stree_csi_t *outer_csi);
     55
     56static stree_ctor_t *parse_ctor(parse_t *parse, stree_csi_t *outer_csi);
     57
     58static stree_enum_t *parse_enum(parse_t *parse, stree_csi_t *outer_csi);
     59static stree_embr_t *parse_embr(parse_t *parse, stree_enum_t *outer_enum);
    5560
    5661static stree_deleg_t *parse_deleg(parse_t *parse, stree_csi_t *outer_csi);
     
    7681static stree_for_t *parse_for(parse_t *parse);
    7782static stree_raise_t *parse_raise(parse_t *parse);
     83static stree_break_t *parse_break(parse_t *parse);
    7884static stree_return_t *parse_return(parse_t *parse);
    7985static stree_wef_t *parse_wef(parse_t *parse);
     
    123129{
    124130        stree_csi_t *csi;
     131        stree_enum_t *enum_d;
    125132        stree_modm_t *modm;
    126133
     
    136143                        list_append(&parse->cur_mod->members, modm);
    137144                        break;
     145                case lc_enum:
     146                        enum_d = parse_enum(parse, NULL);
     147                        modm = stree_modm_new(mc_enum);
     148                        modm->u.enum_d = enum_d;
     149
     150                        list_append(&parse->cur_mod->members, modm);
     151                        break;
    138152                default:
    139153                        lunexpected_error(parse);
     
    223237 *
    224238 * @param parse         Parser object.
    225  * @param outer_csi     CSI containing this declaration or @c NULL if global.
     239 * @param outer_csi     CSI containing this declaration.
    226240 * @return              New syntax tree node. In case of parse error,
    227241 *                      @c NULL may (but need not) be returned.
     
    232246
    233247        stree_csi_t *csi;
     248        stree_ctor_t *ctor;
    234249        stree_deleg_t *deleg;
     250        stree_enum_t *enum_d;
    235251        stree_fun_t *fun;
    236252        stree_var_t *var;
     
    245261                csimbr->u.csi = csi;
    246262                break;
     263        case lc_new:
     264                ctor = parse_ctor(parse, outer_csi);
     265                csimbr = stree_csimbr_new(csimbr_ctor);
     266                csimbr->u.ctor = ctor;
     267                break;
    247268        case lc_deleg:
    248269                deleg = parse_deleg(parse, outer_csi);
     
    250271                csimbr->u.deleg = deleg;
    251272                break;
     273        case lc_enum:
     274                enum_d = parse_enum(parse, outer_csi);
     275                csimbr = stree_csimbr_new(csimbr_enum);
     276                csimbr->u.enum_d = enum_d;
     277                break;
    252278        case lc_fun:
    253279                fun = parse_fun(parse, outer_csi);
     
    273299
    274300        return csimbr;
     301}
     302
     303/** Parse constructor.
     304 *
     305 * @param parse         Parser object.
     306 * @param outer_csi     CSI containing this declaration or @c NULL if global.
     307 * @return              New syntax tree node.
     308 */
     309static stree_ctor_t *parse_ctor(parse_t *parse, stree_csi_t *outer_csi)
     310{
     311        stree_ctor_t *ctor;
     312        stree_symbol_t *symbol;
     313        stree_symbol_attr_t *attr;
     314
     315        ctor = stree_ctor_new();
     316        symbol = stree_symbol_new(sc_ctor);
     317
     318        symbol->u.ctor = ctor;
     319        symbol->outer_csi = outer_csi;
     320        ctor->symbol = symbol;
     321
     322        lmatch(parse, lc_new);
     323
     324        /* Fake identifier. */
     325        ctor->name = stree_ident_new();
     326        ctor->name->sid = strtab_get_sid(CTOR_IDENT);
     327        ctor->name->cspan = lprev_span(parse);
     328
     329#ifdef DEBUG_PARSE_TRACE
     330        printf("Parsing constructor of CSI '");
     331        symbol_print_fqn(csi_to_symbol(outer_csi));
     332        printf("'.\n");
     333#endif
     334        ctor->sig = parse_fun_sig(parse);
     335        if (ctor->sig->rtype != NULL) {
     336                printf("Error: Constructor of CSI '");
     337                symbol_print_fqn(csi_to_symbol(outer_csi));
     338                printf("' has a return type.\n");
     339                parse_note_error(parse);
     340        }
     341
     342        list_init(&symbol->attr);
     343
     344        /* Parse attributes. */
     345        while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
     346                lskip(parse);
     347                attr = parse_symbol_attr(parse);
     348                list_append(&symbol->attr, attr);
     349        }
     350
     351        ctor->proc = stree_proc_new();
     352        ctor->proc->outer_symbol = symbol;
     353
     354        if (lcur_lc(parse) == lc_scolon) {
     355                lskip(parse);
     356
     357                /* This constructor has no body. */
     358                printf("Error: Constructor of CSI '");
     359                symbol_print_fqn(csi_to_symbol(outer_csi));
     360                printf("' has no body.\n");
     361                parse_note_error(parse);
     362
     363                ctor->proc->body = NULL;
     364        } else {
     365                lmatch(parse, lc_is);
     366                ctor->proc->body = parse_block(parse);
     367                lmatch(parse, lc_end);
     368        }
     369
     370        return ctor;
     371}
     372
     373/** Parse @c enum declaration.
     374 *
     375 * @param parse         Parser object.
     376 * @param outer_csi     CSI containing this declaration or @c NULL if global.
     377 * @return              New syntax tree node.
     378 */
     379static stree_enum_t *parse_enum(parse_t *parse, stree_csi_t *outer_csi)
     380{
     381        stree_enum_t *enum_d;
     382        stree_symbol_t *symbol;
     383        stree_embr_t *embr;
     384
     385        enum_d = stree_enum_new();
     386        symbol = stree_symbol_new(sc_enum);
     387
     388        symbol->u.enum_d = enum_d;
     389        symbol->outer_csi = outer_csi;
     390        enum_d->symbol = symbol;
     391
     392        lmatch(parse, lc_enum);
     393        enum_d->name = parse_ident(parse);
     394        list_init(&enum_d->members);
     395
     396#ifdef DEBUG_PARSE_TRACE
     397        printf("Parse enum '%s'.\n", strtab_get_str(enum_d->name->sid));
     398#endif
     399        lmatch(parse, lc_is);
     400
     401        /* Parse enum members. */
     402        while (lcur_lc(parse) != lc_end && !parse_is_error(parse)) {
     403                embr = parse_embr(parse, enum_d);
     404                if (embr == NULL)
     405                        break;
     406
     407                list_append(&enum_d->members, embr);
     408        }
     409
     410        if (list_is_empty(&enum_d->members)) {
     411                printf("Error: Enum type '%s' has no members.\n",
     412                    strtab_get_str(enum_d->name->sid));
     413                parse_note_error(parse);
     414        }
     415
     416        lmatch(parse, lc_end);
     417
     418        return enum_d;
     419}
     420
     421/** Parse enum member.
     422 *
     423 * @param parse         Parser object.
     424 * @param outer_enum    Enum containing this declaration.
     425 * @return              New syntax tree node. In case of parse error,
     426 *                      @c NULL may (but need not) be returned.
     427 */
     428static stree_embr_t *parse_embr(parse_t *parse, stree_enum_t *outer_enum)
     429{
     430        stree_embr_t *embr;
     431
     432        embr = stree_embr_new();
     433        embr->outer_enum = outer_enum;
     434        embr->name = parse_ident(parse);
     435
     436        lmatch(parse, lc_scolon);
     437
     438        return embr;
    275439}
    276440
     
    681845        stree_for_t *for_s;
    682846        stree_raise_t *raise_s;
     847        stree_break_t *break_s;
    683848        stree_return_t *return_s;
    684849        stree_wef_t *wef_s;
     
    714879                stat->u.raise_s = raise_s;
    715880                break;
     881        case lc_break:
     882                break_s = parse_break(parse);
     883                stat = stree_stat_new(st_break);
     884                stat->u.break_s = break_s;
     885                break;
    716886        case lc_return:
    717887                return_s = parse_return(parse);
     
    777947{
    778948        stree_if_t *if_s;
     949        stree_if_clause_t *if_c;
    779950
    780951#ifdef DEBUG_PARSE_TRACE
     
    782953#endif
    783954        if_s = stree_if_new();
    784 
     955        list_init(&if_s->if_clauses);
     956
     957        /* Parse @c if clause. */
    785958        lmatch(parse, lc_if);
    786         if_s->cond = parse_expr(parse);
     959
     960        if_c = stree_if_clause_new();
     961        if_c->cond = parse_expr(parse);
    787962        lmatch(parse, lc_then);
    788         if_s->if_block = parse_block(parse);
    789 
     963        if_c->block = parse_block(parse);
     964
     965        list_append(&if_s->if_clauses, if_c);
     966
     967        /* Parse @c elif clauses. */
     968        while (lcur_lc(parse) == lc_elif) {
     969                lskip(parse);
     970                if_c = stree_if_clause_new();
     971                if_c->cond = parse_expr(parse);
     972                lmatch(parse, lc_then);
     973                if_c->block = parse_block(parse);
     974
     975                list_append(&if_s->if_clauses, if_c);
     976        }
     977
     978        /* Parse @c else clause. */
    790979        if (lcur_lc(parse) == lc_else) {
    791980                lskip(parse);
     
    8671056}
    8681057
     1058/** Parse @c break statement.
     1059 *
     1060 * @param parse         Parser object.
     1061 * @return              New syntax tree node.
     1062 */
     1063static stree_break_t *parse_break(parse_t *parse)
     1064{
     1065        stree_break_t *break_s;
     1066
     1067#ifdef DEBUG_PARSE_TRACE
     1068        printf("Parse 'break' statement.\n");
     1069#endif
     1070        break_s = stree_break_new();
     1071
     1072        lmatch(parse, lc_break);
     1073        lmatch(parse, lc_scolon);
     1074
     1075        return break_s;
     1076}
     1077
    8691078/** Parse @c return statement.
    8701079 *
     
    8821091
    8831092        lmatch(parse, lc_return);
    884         return_s->expr = parse_expr(parse);
     1093
     1094        if (lcur_lc(parse) != lc_scolon)
     1095                return_s->expr = parse_expr(parse);
     1096
    8851097        lmatch(parse, lc_scolon);
    8861098
     
    9961208        ident = stree_ident_new();
    9971209        ident->sid = lcur(parse)->u.ident.sid;
     1210        ident->cspan = lcur_span(parse);
    9981211        lskip(parse);
    9991212
     
    10591272/** Return current lem lclass.
    10601273 *
    1061  * @param parse         Parser object.
    1062  * @return              Lclass of the current lem.
     1274 * @param parse         Parser object
     1275 * @return              Lclass of the current lem
    10631276 */
    10641277lclass_t lcur_lc(parse_t *parse)
     
    10811294}
    10821295
     1296/** Return coordinate span of current lem.
     1297 *
     1298 * @param parse         Parser object
     1299 * @return              Coordinate span of current lem or @c NULL if a
     1300 *                      parse error is active
     1301 */
     1302cspan_t *lcur_span(parse_t *parse)
     1303{
     1304        lem_t *lem;
     1305
     1306        if (parse_is_error(parse))
     1307                return NULL;
     1308
     1309        lem = lcur(parse);
     1310        return lem->cspan;
     1311}
     1312
     1313/** Return coordinate span of previous lem.
     1314 *
     1315 * @param parse         Parser object
     1316 * @return              Coordinate span of previous lem or @c NULL if
     1317 *                      parse error is active or previous lem is not
     1318 *                      available.
     1319 */
     1320cspan_t *lprev_span(parse_t *parse)
     1321{
     1322        lem_t *lem;
     1323
     1324        if (parse_is_error(parse))
     1325                return NULL;
     1326
     1327        lem = lex_peek_prev(parse->lex);
     1328        if (lem == NULL)
     1329                return NULL;
     1330
     1331        return lem->cspan;
     1332}
     1333
    10831334/** Skip to next lem.
    10841335 *
     
    11701421{
    11711422        switch (lclass) {
     1423        case lc_elif:
    11721424        case lc_else:
    11731425        case lc_end:
Note: See TracChangeset for help on using the changeset viewer.