From c5799690b8b1148931e54c3f66e49617eb1d2ca1 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 21 Feb 2020 07:58:24 +0000 Subject: [PATCH] added @pragma striprecspc on/off --- hawk/lib/hawk.c | 2 +- hawk/lib/misc.c | 4 +-- hawk/lib/parse.c | 72 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/hawk/lib/hawk.c b/hawk/lib/hawk.c index ad6604e7..6d39653d 100644 --- a/hawk/lib/hawk.c +++ b/hawk/lib/hawk.c @@ -379,7 +379,7 @@ void hawk_clear (hawk_t* awk) awk->parse.depth.loop = 0; awk->parse.depth.expr = 0; awk->parse.depth.incl = 0; - awk->parse.pragma.trait = (awk->opt.trait & HAWK_IMPLICIT); /* implicit on if you didn't mask it off in awk->opt.trait with hawk_setopt */ + awk->parse.pragma.trait = (awk->opt.trait & (HAWK_IMPLICIT | HAWK_STRIPRECSPC)); /* implicit on if you didn't mask it off in awk->opt.trait with hawk_setopt */ awk->parse.pragma.rtx_stack_limit = 0; awk->parse.pragma.entry[0] = '\0'; diff --git a/hawk/lib/misc.c b/hawk/lib/misc.c index f1628fa0..f275d4dd 100644 --- a/hawk/lib/misc.c +++ b/hawk/lib/misc.c @@ -281,7 +281,7 @@ hawk_ooch_t* hawk_rtx_strxntokbyrex ( cursub.ptr++; cursub.len--; } - else if (rtx->gbl.striprecspc > 0 || (rtx->gbl.striprecspc < 0 && (rtx->awk->opt.trait & HAWK_STRIPRECSPC))) + else if (rtx->gbl.striprecspc > 0 || (rtx->gbl.striprecspc < 0 && (rtx->awk->parse.pragma.trait & HAWK_STRIPRECSPC))) { /* match at the beginning of the input string */ if (match.ptr == substr) @@ -329,7 +329,7 @@ exit_loop: } /* the match is all spaces */ - if (rtx->gbl.striprecspc > 0 || (rtx->gbl.striprecspc < 0 && (rtx->awk->opt.trait & HAWK_STRIPRECSPC))) + if (rtx->gbl.striprecspc > 0 || (rtx->gbl.striprecspc < 0 && (rtx->awk->parse.pragma.trait & HAWK_STRIPRECSPC))) { /* if the match reached the last character in the input string, * it returns HAWK_NULL to terminate tokenization. */ diff --git a/hawk/lib/parse.c b/hawk/lib/parse.c index eeccdfa0..e4154c05 100644 --- a/hawk/lib/parse.c +++ b/hawk/lib/parse.c @@ -870,7 +870,7 @@ static int begin_include (hawk_t* awk, int once) arg->pragma_trait = awk->parse.pragma.trait; /* but don't change awk->parse.pragma.trait. it means the included file inherits * the existing progma values. - awk->parse.pragma.trait = (awk->opt.trait & HAWK_IMPLICIT); + awk->parse.pragma.trait = (awk->opt.trait & (HAWK_IMPLICIT | HAWK_STRIPRECSPC)); */ /* i update the current pointer after opening is successful */ @@ -983,7 +983,34 @@ static int parse_progunit (hawk_t* awk) name.len = HAWK_OOECS_LEN(awk->tok.name); name.ptr = HAWK_OOECS_PTR(awk->tok.name); - if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("implicit")) == 0) + if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("entry")) == 0) + { + if (get_token(awk) <= -1) return -1; + if (!MATCH(awk, TOK_IDENT)) + { + hawk_seterrfmt (awk, &awk->ptok.loc, HAWK_EIDENT, HAWK_T("function name expected for 'entry'")); + return -1; + } + + if (HAWK_OOECS_LEN(awk->tok.name) >= HAWK_COUNTOF(awk->parse.pragma.entry)) + { + hawk_seterrfmt (awk, &awk->tok.loc, HAWK_EFUNNAM, HAWK_T("entry function name too long")); + return -1; + } + + if (awk->sio.inp == &awk->sio.arg) + { + /* only the top level source */ + if (awk->parse.pragma.entry[0] != '\0') + { + hawk_seterrfmt (awk, &awk->tok.loc, HAWK_EEXIST, HAWK_T("@pragma entry already set")); + return -1; + } + + hawk_copy_oochars_to_oocstr (awk->parse.pragma.entry, HAWK_COUNTOF(awk->parse.pragma.entry), HAWK_OOECS_PTR(awk->tok.name), HAWK_OOECS_LEN(awk->tok.name)); + } + } + else if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("implicit")) == 0) { /* @pragma implicit on * @pragma implicit off */ @@ -1029,31 +1056,46 @@ static int parse_progunit (hawk_t* awk) if (sl > awk->parse.pragma.rtx_stack_limit) awk->parse.pragma.rtx_stack_limit = sl; } - else if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("entry")) == 0) + else if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("striprecspc")) == 0) { + /* @pragma striprecspc on + * @pragma striprecspc off + * + * Take note the global STRIPRECSPC is available for context based change. + * STRIPRECSPC takes precedence over this pragma. + */ + int is_on; + if (get_token(awk) <= -1) return -1; if (!MATCH(awk, TOK_IDENT)) { - hawk_seterrfmt (awk, &awk->ptok.loc, HAWK_EIDENT, HAWK_T("function name expected for 'entry'")); + error_ident_on_off_expected_for_striprecspc: + hawk_seterrfmt (awk, &awk->ptok.loc, HAWK_EIDENT, HAWK_T("identifier 'on' or 'off' expected for 'striprecspc'")); return -1; } - if (HAWK_OOECS_LEN(awk->tok.name) >= HAWK_COUNTOF(awk->parse.pragma.entry)) + name.len = HAWK_OOECS_LEN(awk->tok.name); + name.ptr = HAWK_OOECS_PTR(awk->tok.name); + if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("on")) == 0) { - hawk_seterrfmt (awk, &awk->tok.loc, HAWK_EFUNNAM, HAWK_T("entry function name too long")); - return -1; + is_on = 1; + } + else if (hawk_comp_oochars_oocstr(name.ptr, name.len, HAWK_T("off")) == 0) + { + is_on = 0; + } + else + { + goto error_ident_on_off_expected_for_striprecspc; } if (awk->sio.inp == &awk->sio.arg) { - /* only the top level source */ - if (awk->parse.pragma.entry[0] != '\0') - { - hawk_seterrfmt (awk, &awk->tok.loc, HAWK_EEXIST, HAWK_T("@pragma entry already set")); - return -1; - } - - hawk_copy_oochars_to_oocstr (awk->parse.pragma.entry, HAWK_COUNTOF(awk->parse.pragma.entry), HAWK_OOECS_PTR(awk->tok.name), HAWK_OOECS_LEN(awk->tok.name)); + /* only the top level source. ignore striprecspc pragma in other levels */ + if (is_on) + awk->parse.pragma.trait |= HAWK_STRIPRECSPC; + else + awk->parse.pragma.trait &= ~HAWK_STRIPRECSPC; } } else