aboutsummaryrefslogtreecommitdiffstats
path: root/awkgram.y
diff options
context:
space:
mode:
Diffstat (limited to 'awkgram.y')
-rw-r--r--awkgram.y625
1 files changed, 535 insertions, 90 deletions
diff --git a/awkgram.y b/awkgram.y
index 4eab3d6f..04e65ef2 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -57,6 +57,7 @@ static int include_source(INSTRUCTION *file);
static int load_library(INSTRUCTION *file);
static void next_sourcefile(void);
static char *tokexpand(void);
+static NODE *make_profile_number(double d, const char *str, size_t len);
#define instruction(t) bcalloc(t, 1, 0)
@@ -83,10 +84,18 @@ static void check_funcs(void);
static ssize_t read_one_line(int fd, void *buffer, size_t count);
static int one_line_close(int fd);
+static void split_comment(void);
+static void check_comment(void);
+static void add_sign_to_num(NODE *n, char sign);
static bool at_seen = false;
static bool want_source = false;
static bool want_regexp = false; /* lexical scanning kludge */
+static enum {
+ FUNC_HEADER,
+ FUNC_BODY,
+ DONT_CHECK
+} want_param_names = DONT_CHECK; /* ditto */
static char *in_function; /* parsing kludge */
static int rule = 0;
@@ -141,11 +150,23 @@ static INSTRUCTION *ip_atexit = NULL;
static INSTRUCTION *ip_end;
static INSTRUCTION *ip_endfile;
static INSTRUCTION *ip_beginfile;
+INSTRUCTION *main_beginfile;
+
+static INSTRUCTION *comment = NULL;
+static INSTRUCTION *prior_comment = NULL;
+static INSTRUCTION *comment_to_save = NULL;
+static INSTRUCTION *program_comment = NULL;
+static INSTRUCTION *function_comment = NULL;
+static INSTRUCTION *block_comment = NULL;
+
+static bool func_first = true;
+static bool first_rule = true;
static inline INSTRUCTION *list_create(INSTRUCTION *x);
static inline INSTRUCTION *list_append(INSTRUCTION *l, INSTRUCTION *x);
static inline INSTRUCTION *list_prepend(INSTRUCTION *l, INSTRUCTION *x);
static inline INSTRUCTION *list_merge(INSTRUCTION *l1, INSTRUCTION *l2);
+static inline INSTRUCTION *add_pending_comment(INSTRUCTION *stmt);
extern double fmod(double x, double y);
@@ -218,6 +239,7 @@ rule
: pattern action
{
(void) append_rule($1, $2);
+ first_rule = false;
}
| pattern statement_term
{
@@ -234,6 +256,7 @@ rule
{
in_function = NULL;
(void) mk_function($1, $2);
+ want_param_names = DONT_CHECK;
yyerrok;
}
| '@' LEX_INCLUDE source statement_term
@@ -282,9 +305,24 @@ library
pattern
: /* empty */
- { $$ = NULL; rule = Rule; }
+ {
+ rule = Rule;
+ if (comment != NULL) {
+ $$ = list_create(comment);
+ comment = NULL;
+ } else
+ $$ = NULL;
+ }
| exp
- { $$ = $1; rule = Rule; }
+ {
+ rule = Rule;
+ if (comment != NULL) {
+ $$ = list_prepend($1, comment);
+ comment = NULL;
+ } else
+ $$ = $1;
+ }
+
| exp ',' opt_nls exp
{
INSTRUCTION *tp;
@@ -308,41 +346,55 @@ pattern
($1->nexti + 1)->condpair_left = $1->lasti;
($1->nexti + 1)->condpair_right = $4->lasti;
}
- $$ = list_append(list_merge($1, $4), tp);
+ if (comment != NULL) {
+ $$ = list_append(list_merge(list_prepend($1, comment), $4), tp);
+ comment = NULL;
+ } else
+ $$ = list_append(list_merge($1, $4), tp);
rule = Rule;
}
| LEX_BEGIN
{
static int begin_seen = 0;
+
+ func_first = false;
if (do_lint_old && ++begin_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
$1->in_rule = rule = BEGIN;
$1->source_file = source;
+ check_comment();
$$ = $1;
}
| LEX_END
{
static int end_seen = 0;
+
+ func_first = false;
if (do_lint_old && ++end_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
$1->in_rule = rule = END;
$1->source_file = source;
+ check_comment();
$$ = $1;
}
| LEX_BEGINFILE
{
+ func_first = false;
$1->in_rule = rule = BEGINFILE;
$1->source_file = source;
+ check_comment();
$$ = $1;
}
| LEX_ENDFILE
{
+ func_first = false;
$1->in_rule = rule = ENDFILE;
$1->source_file = source;
+ check_comment();
$$ = $1;
}
;
@@ -350,10 +402,12 @@ pattern
action
: l_brace statements r_brace opt_semi opt_nls
{
+ INSTRUCTION *ip;
if ($2 == NULL)
- $$ = list_create(instruction(Op_no_op));
+ ip = list_create(instruction(Op_no_op));
else
- $$ = $2;
+ ip = $2;
+ $$ = ip;
}
;
@@ -381,16 +435,43 @@ lex_builtin
;
function_prologue
- : LEX_FUNCTION func_name '(' opt_param_list r_paren opt_nls
+ : LEX_FUNCTION func_name '(' { want_param_names = FUNC_HEADER; } opt_param_list r_paren opt_nls
{
+ /*
+ * treat any comments between BOF and the first function
+ * definition (with no intervening BEGIN etc block) as
+ * program comments. Special kludge: iff there are more
+ * than one such comments, treat the last as a function
+ * comment.
+ */
+ if (prior_comment != NULL) {
+ comment_to_save = prior_comment;
+ prior_comment = NULL;
+ } else if (comment != NULL) {
+ comment_to_save = comment;
+ comment = NULL;
+ } else
+ comment_to_save = NULL;
+
+ if (comment_to_save != NULL && func_first
+ && strstr(comment_to_save->memory->stptr, "\n\n") != NULL)
+ split_comment();
+
+ /* save any other pre-function comment as function comment */
+ if (comment_to_save != NULL) {
+ function_comment = comment_to_save;
+ comment_to_save = NULL;
+ }
+ func_first = false;
$1->source_file = source;
- if (install_function($2->lextok, $1, $4) < 0)
+ if (install_function($2->lextok, $1, $5) < 0)
YYABORT;
in_function = $2->lextok;
$2->lextok = NULL;
bcfree($2);
- /* $4 already free'd in install_function */
+ /* $5 already free'd in install_function */
$$ = $1;
+ want_param_names = FUNC_BODY;
}
;
@@ -440,19 +521,62 @@ a_slash
statements
: /* empty */
- { $$ = NULL; }
+ {
+ if (prior_comment != NULL) {
+ $$ = list_create(prior_comment);
+ prior_comment = NULL;
+ } else if (comment != NULL) {
+ $$ = list_create(comment);
+ comment = NULL;
+ } else
+ $$ = NULL;
+ }
| statements statement
{
- if ($2 == NULL)
- $$ = $1;
- else {
+ if ($2 == NULL) {
+ if (prior_comment != NULL) {
+ $$ = list_append($1, prior_comment);
+ prior_comment = NULL;
+ if (comment != NULL) {
+ $$ = list_append($$, comment);
+ comment = NULL;
+ }
+ } else if (comment != NULL) {
+ $$ = list_append($1, comment);
+ comment = NULL;
+ } else
+ $$ = $1;
+ } else {
add_lint($2, LINT_no_effect);
- if ($1 == NULL)
- $$ = $2;
- else
+ if ($1 == NULL) {
+ if (prior_comment != NULL) {
+ $$ = list_append($2, prior_comment);
+ prior_comment = NULL;
+ if (comment != NULL) {
+ $$ = list_append($$, comment);
+ comment = NULL;
+ }
+ } else if (comment != NULL) {
+ $$ = list_append($2, comment);
+ comment = NULL;
+ } else
+ $$ = $2;
+ } else {
+ if (prior_comment != NULL) {
+ list_append($2, prior_comment);
+ prior_comment = NULL;
+ if (comment != NULL) {
+ list_append($2, comment);
+ comment = NULL;
+ }
+ } else if (comment != NULL) {
+ list_append($2, comment);
+ comment = NULL;
+ }
$$ = list_merge($1, $2);
+ }
}
- yyerrok;
+ yyerrok;
}
| statements error
{ $$ = NULL; }
@@ -496,7 +620,7 @@ statement
} /* else
curr = NULL; */
- for(; curr != NULL; curr = nextc) {
+ for (; curr != NULL; curr = nextc) {
INSTRUCTION *caseexp = curr->case_exp;
INSTRUCTION *casestmt = curr->case_stmt;
@@ -782,6 +906,7 @@ regular_loop:
$$ = list_prepend($1, instruction(Op_exec_count));
else
$$ = $1;
+ $$ = add_pending_comment($$);
}
;
@@ -793,6 +918,7 @@ non_compound_stmt
_("`break' is not allowed outside a loop or switch"));
$1->target_jmp = NULL;
$$ = list_create($1);
+ $$ = add_pending_comment($$);
}
| LEX_CONTINUE statement_term
@@ -802,6 +928,7 @@ non_compound_stmt
_("`continue' is not allowed outside a loop"));
$1->target_jmp = NULL;
$$ = list_create($1);
+ $$ = add_pending_comment($$);
}
| LEX_NEXT statement_term
@@ -812,6 +939,7 @@ non_compound_stmt
_("`next' used in %s action"), ruletab[rule]);
$1->target_jmp = ip_rec;
$$ = list_create($1);
+ $$ = add_pending_comment($$);
}
| LEX_NEXTFILE statement_term
{
@@ -823,6 +951,7 @@ non_compound_stmt
$1->target_newfile = ip_newfile;
$1->target_endfile = ip_endfile;
$$ = list_create($1);
+ $$ = add_pending_comment($$);
}
| LEX_EXIT opt_exp statement_term
{
@@ -838,6 +967,7 @@ non_compound_stmt
$$->nexti->memory = dupnode(Nnull_string);
} else
$$ = list_append($2, $1);
+ $$ = add_pending_comment($$);
}
| LEX_RETURN
{
@@ -862,6 +992,7 @@ non_compound_stmt
$$ = list_append($3, $1);
}
+ $$ = add_pending_comment($$);
}
| simple_stmt statement_term
;
@@ -971,6 +1102,7 @@ regular_print:
}
}
}
+ $$ = add_pending_comment($$);
}
| LEX_DELETE NAME { sub_counter = 0; } delete_subscript_list
@@ -1005,6 +1137,7 @@ regular_print:
$1->expr_count = sub_counter;
$$ = list_append(list_append($4, $2), $1);
}
+ $$ = add_pending_comment($$);
}
| LEX_DELETE '(' NAME ')'
/*
@@ -1035,9 +1168,13 @@ regular_print:
else if ($3->memory == func_table)
fatal(_("`delete' is not allowed with FUNCTAB"));
}
+ $$ = add_pending_comment($$);
}
| exp
- { $$ = optimize_assignment($1); }
+ {
+ $$ = optimize_assignment($1);
+ $$ = add_pending_comment($$);
+ }
;
opt_simple_stmt
@@ -1100,14 +1237,19 @@ case_value
}
| '+' YNUMBER %prec UNARY
{
+ NODE *n = $2->lasti->memory;
bcfree($1);
+ add_sign_to_num(n, '+');
$$ = $2;
}
| YSTRING
{ $$ = $1; }
| regexp
{
- $1->opcode = Op_push_re;
+ if ($1->memory->type == Node_regex)
+ $1->opcode = Op_push_re;
+ else
+ $1->opcode = Op_push;
$$ = $1;
}
;
@@ -1184,7 +1326,7 @@ opt_param_list
: /* empty */
{ $$ = NULL; }
| param_list
- { $$ = $1 ; }
+ { $$ = $1; }
;
param_list
@@ -1255,6 +1397,47 @@ expression_list
}
;
+opt_fcall_expression_list
+ : /* empty */
+ { $$ = NULL; }
+ | fcall_expression_list
+ { $$ = $1; }
+ ;
+
+fcall_expression_list
+ : fcall_exp
+ { $$ = mk_expression_list(NULL, $1); }
+ | fcall_expression_list comma fcall_exp
+ {
+ $$ = mk_expression_list($1, $3);
+ yyerrok;
+ }
+ | error
+ { $$ = NULL; }
+ | fcall_expression_list error
+ {
+ /*
+ * Returning the expression list instead of NULL lets
+ * snode get a list of arguments that it can count.
+ */
+ $$ = $1;
+ }
+ | fcall_expression_list error fcall_exp
+ {
+ /* Ditto */
+ $$ = mk_expression_list($1, $3);
+ }
+ | fcall_expression_list comma error
+ {
+ /* Ditto */
+ $$ = $1;
+ }
+ ;
+
+fcall_exp
+ : exp { $$ = $1; }
+ ;
+
/* Expressions, not including the comma operator. */
exp
: variable assign_operator exp %prec ASSIGNOP
@@ -1275,6 +1458,7 @@ exp
_("regular expression on left of `~' or `!~' operator"));
if ($3->lasti == $3->nexti && $3->nexti->opcode == Op_match_rec) {
+ /* RHS is /.../ */
$2->memory = $3->nexti->memory;
bcfree($3->nexti); /* Op_match_rec */
bcfree($3); /* Op_list */
@@ -1364,10 +1548,15 @@ common_exp
NODE *n2 = $2->nexti->memory;
size_t nlen;
+ // 1.5 "" # can't fold this if program mucks with CONVFMT.
+ // See test #12 in test/posix.awk.
+ if ((n1->flags & (NUMBER|NUMINT)) != 0 || (n2->flags & (NUMBER|NUMINT)) != 0)
+ goto plain_concat;
+
n1 = force_string(n1);
n2 = force_string(n2);
nlen = n1->stlen + n2->stlen;
- erealloc(n1->stptr, char *, nlen + 2, "constant fold");
+ erealloc(n1->stptr, char *, nlen + 1, "constant fold");
memcpy(n1->stptr + n1->stlen, n2->stptr, n2->stlen);
n1->stlen = nlen;
n1->stptr[nlen] = '\0';
@@ -1378,6 +1567,7 @@ common_exp
bcfree($2);
$$ = $1;
} else {
+ plain_concat:
$$ = list_append(list_merge($1, $2), instruction(Op_concat));
$$->lasti->concat_flag = (is_simple_var ? CSVAR : 0);
$$->lasti->expr_count = count;
@@ -1481,7 +1671,7 @@ non_post_simp_exp
if ($2->opcode == Op_match_rec) {
$2->opcode = Op_nomatch;
$1->opcode = Op_push_i;
- $1->memory = make_number(0.0);
+ $1->memory = make_profile_number(0.0, "0", 1);
$$ = list_append(list_append(list_create($1),
instruction(Op_field_spec)), $2);
} else {
@@ -1490,7 +1680,7 @@ non_post_simp_exp
&& ($2->nexti->memory->flags & (MPFN|MPZN)) == 0
) {
NODE *n = $2->nexti->memory;
- if ((n->flags & (STRCUR|STRING)) != 0) {
+ if ((n->flags & STRING) != 0) {
n->numbr = (AWKNUM) (n->stlen == 0);
n->flags &= ~(STRCUR|STRING);
n->flags |= (NUMCUR|NUMBER);
@@ -1510,13 +1700,13 @@ non_post_simp_exp
}
| '(' exp r_paren
{ $$ = $2; }
- | LEX_BUILTIN '(' opt_expression_list r_paren
+ | LEX_BUILTIN '(' opt_fcall_expression_list r_paren
{
$$ = snode($3, $1);
if ($$ == NULL)
YYABORT;
}
- | LEX_LENGTH '(' opt_expression_list r_paren
+ | LEX_LENGTH '(' opt_fcall_expression_list r_paren
{
$$ = snode($3, $1);
if ($$ == NULL)
@@ -1558,7 +1748,7 @@ non_post_simp_exp
| '-' simp_exp %prec UNARY
{
if ($2->lasti->opcode == Op_push_i
- && ($2->lasti->memory->flags & (STRCUR|STRING)) == 0
+ && ($2->lasti->memory->flags & STRING) == 0
) {
NODE *n = $2->lasti->memory;
(void) force_number(n);
@@ -1572,13 +1762,21 @@ non_post_simp_exp
}
| '+' simp_exp %prec UNARY
{
- /*
- * was: $$ = $2
- * POSIX semantics: force a conversion to numeric type
- */
- $1->opcode = Op_plus_i;
- $1->memory = make_number(0.0);
- $$ = list_append($2, $1);
+ if ($2->lasti->opcode == Op_push_i
+ && ($2->lasti->memory->flags & STRING) == 0
+ && ($2->lasti->memory->flags & NUMCONSTSTR) != 0) {
+ NODE *n = $2->lasti->memory;
+ add_sign_to_num(n, '+');
+ $$ = $2;
+ bcfree($1);
+ } else {
+ /*
+ * was: $$ = $2
+ * POSIX semantics: force a conversion to numeric type
+ */
+ $1->opcode = Op_unary_plus;
+ $$ = list_append($2, $1);
+ }
}
;
@@ -1625,14 +1823,14 @@ func_call
;
direct_func_call
- : FUNC_CALL '(' opt_expression_list r_paren
+ : FUNC_CALL '(' opt_fcall_expression_list r_paren
{
NODE *n;
if (! at_seen) {
n = lookup($1->func_name);
if (n != NULL && n->type != Node_func
- && n->type != Node_ext_func && n->type != Node_old_ext_func) {
+ && n->type != Node_ext_func) {
error_ln($1->source_line,
_("attempt to use non-function `%s' in function call"),
$1->func_name);
@@ -1878,9 +2076,6 @@ static const struct token tokentab[] = {
{"eval", Op_symbol, LEX_EVAL, 0, 0, 0},
{"exit", Op_K_exit, LEX_EXIT, 0, 0, 0},
{"exp", Op_builtin, LEX_BUILTIN, A(1), do_exp, MPF(exp)},
-#ifdef DYNAMIC
-{"extension", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3), do_ext, 0},
-#endif
{"fflush", Op_builtin, LEX_BUILTIN, A(0)|A(1), do_fflush, 0},
{"for", Op_K_for, LEX_FOR, BREAK|CONTINUE, 0, 0},
{"func", Op_func, LEX_FUNCTION, NOT_POSIX|NOT_OLD, 0, 0},
@@ -1893,6 +2088,7 @@ static const struct token tokentab[] = {
{"include", Op_symbol, LEX_INCLUDE, GAWKX, 0, 0},
{"index", Op_builtin, LEX_BUILTIN, A(2), do_index, 0},
{"int", Op_builtin, LEX_BUILTIN, A(1), do_int, MPF(int)},
+{"intdiv", Op_builtin, LEX_BUILTIN, GAWKX|A(3), do_intdiv, MPF(intdiv)},
{"isarray", Op_builtin, LEX_BUILTIN, GAWKX|A(1), do_isarray, 0},
{"length", Op_builtin, LEX_LENGTH, A(0)|A(1), do_length, 0},
{"load", Op_symbol, LEX_LOAD, GAWKX, 0, 0},
@@ -1926,6 +2122,7 @@ static const struct token tokentab[] = {
{"systime", Op_builtin, LEX_BUILTIN, GAWKX|A(0), do_systime, 0},
{"tolower", Op_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_tolower, 0},
{"toupper", Op_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_toupper, 0},
+{"typeof", Op_builtin, LEX_BUILTIN, GAWKX|A(1), do_typeof, 0},
{"while", Op_K_while, LEX_WHILE, BREAK|CONTINUE, 0, 0},
{"xor", Op_builtin, LEX_BUILTIN, GAWKX, do_xor, MPF(xor)},
};
@@ -1967,6 +2164,8 @@ negate_num(NODE *n)
int tval = 0;
#endif
+ add_sign_to_num(n, '-');
+
if (! is_mpg_number(n)) {
n->numbr = -n->numbr;
return;
@@ -1999,6 +2198,21 @@ negate_num(NODE *n)
#endif
}
+/* add_sign_to_num --- make a constant unary plus or minus for profiling */
+
+static void
+add_sign_to_num(NODE *n, char sign)
+{
+ if ((n->flags & NUMCONSTSTR) != 0) {
+ char *s;
+
+ s = n->stptr;
+ memmove(& s[1], & s[0], n->stlen + 1);
+ s[0] = sign;
+ n->stlen++;
+ }
+}
+
/* print_included_from --- print `Included from ..' file names and locations */
static void
@@ -2101,7 +2315,6 @@ yyerror(const char *m, ...)
char *buf;
int count;
static char end_of_file_line[] = "(END OF FILE)";
- char save;
print_included_from();
@@ -2132,25 +2345,17 @@ yyerror(const char *m, ...)
bp = thisline + strlen(thisline);
}
- /*
- * Saving and restoring *bp keeps valgrind happy,
- * since the guts of glibc uses strlen, even though
- * we're passing an explict precision. Sigh.
- *
- * 8/2003: We may not need this anymore.
- */
- save = *bp;
- *bp = '\0';
-
msg("%.*s", (int) (bp - thisline), thisline);
- *bp = save;
va_start(args, m);
if (mesg == NULL)
mesg = m;
- count = (bp - thisline) + strlen(mesg) + 2 + 1;
- emalloc(buf, char *, count, "yyerror");
+ count = strlen(mesg) + 1;
+ if (lexptr != NULL)
+ count += (lexeme - thisline) + 2;
+ emalloc(buf, char *, count+1, "yyerror");
+ memset(buf, 0, count+1);
bp = buf;
@@ -2232,6 +2437,11 @@ mk_program()
cp = end_block;
else
cp = list_merge(begin_block, end_block);
+ if (program_comment != NULL) {
+ (void) list_prepend(cp, program_comment);
+ }
+ if (comment != NULL)
+ (void) list_append(cp, comment);
(void) list_append(cp, ip_atexit);
(void) list_append(cp, instruction(Op_stop));
@@ -2264,6 +2474,12 @@ mk_program()
if (begin_block != NULL)
cp = list_merge(begin_block, cp);
+ if (program_comment != NULL) {
+ (void) list_prepend(cp, program_comment);
+ }
+ if (comment != NULL) {
+ (void) list_append(cp, comment);
+ }
(void) list_append(cp, ip_atexit);
(void) list_append(cp, instruction(Op_stop));
@@ -2271,6 +2487,10 @@ out:
/* delete the Op_list, not needed */
tmp = cp->nexti;
bcfree(cp);
+ /* these variables are not used again but zap them anyway. */
+ comment = NULL;
+ function_comment = NULL;
+ program_comment = NULL;
return tmp;
#undef begin_block
@@ -2297,7 +2517,7 @@ parse_program(INSTRUCTION **pcode)
ip_newfile = ip_rec = ip_atexit = ip_beginfile = ip_endfile = NULL;
else {
ip_endfile = instruction(Op_no_op);
- ip_beginfile = instruction(Op_no_op);
+ main_beginfile = ip_beginfile = instruction(Op_no_op);
ip_rec = instruction(Op_get_record); /* target for `next', also ip_newfile */
ip_newfile = bcalloc(Op_newfile, 2, 0); /* target for `nextfile' */
ip_newfile->target_jmp = ip_end;
@@ -2768,7 +2988,7 @@ get_src_buf()
lexend = lexptr + n;
if (n == 0) {
static bool warned = false;
- if (do_lint && newfile && ! warned){
+ if (do_lint && newfile && ! warned) {
warned = true;
sourceline = 0;
lintwarn(_("source file `%s' is empty"), source);
@@ -2859,7 +3079,7 @@ again:
mbstate_t tmp_state;
size_t mbclen;
- for (idx = 0 ; lexptr + idx < lexend ; idx++) {
+ for (idx = 0; lexptr + idx < lexend; idx++) {
tmp_state = cur_mbstate;
mbclen = mbrlen(lexptr, idx + 1, &tmp_state);
@@ -2916,6 +3136,122 @@ pushback(void)
(! lexeof && lexptr && lexptr > lexptr_begin ? lexptr-- : lexptr);
}
+/* check_comment --- check for block comment */
+
+void
+check_comment(void)
+{
+ if (comment != NULL) {
+ if (first_rule) {
+ program_comment = comment;
+ } else
+ block_comment = comment;
+ comment = NULL;
+ }
+ first_rule = false;
+}
+
+/*
+ * get_comment --- collect comment text.
+ * Flag = EOL_COMMENT for end-of-line comments.
+ * Flag = FULL_COMMENT for self-contained comments.
+ */
+
+int
+get_comment(int flag)
+{
+ int c;
+ int sl;
+ tok = tokstart;
+ tokadd('#');
+ sl = sourceline;
+ char *p1;
+ char *p2;
+
+ while (true) {
+ while ((c = nextc(false)) != '\n' && c != END_FILE) {
+ /* ignore \r characters */
+ if (c != '\r')
+ tokadd(c);
+ }
+ if (flag == EOL_COMMENT) {
+ /* comment at end of line. */
+ if (c == '\n')
+ tokadd(c);
+ break;
+ }
+ if (c == '\n') {
+ tokadd(c);
+ sourceline++;
+ do {
+ c = nextc(false);
+ if (c == '\n') {
+ sourceline++;
+ tokadd(c);
+ }
+ } while (isspace(c) && c != END_FILE);
+ if (c == END_FILE)
+ break;
+ else if (c != '#') {
+ pushback();
+ sourceline--;
+ break;
+ } else
+ tokadd(c);
+ } else
+ break;
+ }
+
+ if (comment != NULL)
+ prior_comment = comment;
+
+ /* remove any trailing blank lines (consecutive \n) from comment */
+ p1 = tok - 1;
+ p2 = tok - 2;
+ while (*p1 == '\n' && *p2 == '\n') {
+ p1--;
+ p2--;
+ tok--;
+ }
+
+ comment = bcalloc(Op_comment, 1, sl);
+ comment->source_file = source;
+ comment->memory = make_str_node(tokstart, tok - tokstart, 0);
+ comment->memory->comment_type = flag;
+
+ return c;
+}
+
+/* split_comment --- split initial comment text into program and function parts */
+
+static void
+split_comment(void)
+{
+ char *p;
+ int l;
+ NODE *n;
+
+ p = comment_to_save->memory->stptr;
+ l = comment_to_save->memory->stlen - 3;
+ /* have at least two comments so split at last blank line (\n\n) */
+ while (l >= 0) {
+ if (p[l] == '\n' && p[l+1] == '\n') {
+ function_comment = comment_to_save;
+ n = function_comment->memory;
+ function_comment->memory = make_str_node(p + l + 2, n->stlen - l - 2, 0);
+ /* create program comment */
+ program_comment = bcalloc(Op_comment, 1, sourceline);
+ program_comment->source_file = comment_to_save->source_file;
+ p[l + 2] = 0;
+ program_comment->memory = make_str_node(p, l + 2, 0);
+ comment_to_save = NULL;
+ freenode(n);
+ break;
+ }
+ else
+ l--;
+ }
+}
/* allow_newline --- allow newline after &&, ||, ? and : */
@@ -2931,8 +3267,13 @@ allow_newline(void)
break;
}
if (c == '#') {
- while ((c = nextc(false)) != '\n' && c != END_FILE)
- continue;
+ if (do_pretty_print && ! do_profile) {
+ /* collect comment byte code iff doing pretty print but not profiling. */
+ c = get_comment(EOL_COMMENT);
+ } else {
+ while ((c = nextc(false)) != '\n' && c != END_FILE)
+ continue;
+ }
if (c == END_FILE) {
pushback();
break;
@@ -2955,7 +3296,8 @@ allow_newline(void)
* removes the warnings.
*/
-static int newline_eof()
+static int
+newline_eof()
{
/* NB: a newline at end does not start a source line. */
if (lasttok != NEWLINE) {
@@ -3027,6 +3369,7 @@ yylex(void)
lexeme = lexptr;
thisline = NULL;
+
if (want_regexp) {
int in_brack = 0; /* count brackets, [[:alnum:]] allowed */
int b_index = -1;
@@ -3113,7 +3456,9 @@ end_regexp:
peek);
}
}
- return lasttok = REGEXP;
+ lasttok = REGEXP;
+
+ return lasttok;
case '\n':
pushback();
yyerror(_("unterminated regexp"));
@@ -3149,9 +3494,23 @@ retry:
return lasttok = NEWLINE;
case '#': /* it's a comment */
- while ((c = nextc(false)) != '\n') {
+ if (do_pretty_print && ! do_profile) {
+ /*
+ * Collect comment byte code iff doing pretty print
+ * but not profiling.
+ */
+ if (lasttok == NEWLINE || lasttok == 0)
+ c = get_comment(FULL_COMMENT);
+ else
+ c = get_comment(EOL_COMMENT);
+
if (c == END_FILE)
return lasttok = NEWLINE_EOF;
+ } else {
+ while ((c = nextc(false)) != '\n') {
+ if (c == END_FILE)
+ return lasttok = NEWLINE_EOF;
+ }
}
sourceline++;
return lasttok = NEWLINE;
@@ -3163,7 +3522,7 @@ retry:
case '\\':
#ifdef RELAXED_CONTINUATION
/*
- * This code puports to allow comments and/or whitespace
+ * This code purports to allow comments and/or whitespace
* after the `\' at the end of a line used for continuation.
* Use it at your own risk. We think it's a bad idea, which
* is why it's not on by default.
@@ -3180,9 +3539,13 @@ retry:
lintwarn(
_("use of `\\ #...' line continuation is not portable"));
}
- while ((c = nextc(false)) != '\n')
- if (c == END_FILE)
- break;
+ if (do_pretty_print && ! do_profile)
+ c = get_comment(EOL_COMMENT);
+ else {
+ while ((c = nextc(false)) != '\n')
+ if (c == END_FILE)
+ break;
+ }
}
pushback();
}
@@ -3398,7 +3761,7 @@ retry:
lastline = sourceline;
return lasttok = c;
}
- did_newline++;
+ did_newline = true;
--lexptr; /* pick up } next time */
return lasttok = NEWLINE;
@@ -3606,14 +3969,19 @@ retry:
IEEE_FMT(r->mpg_numbr, tval);
}
yylval->memory = r;
+ if (do_pretty_print) {
+ yylval->memory->stptr = estrdup(tokstart, strlen(tokstart)-1);
+ yylval->memory->stlen = strlen(tokstart)-1;
+ yylval->memory->flags |= NUMCONSTSTR;
+ }
return lasttok = YNUMBER;
}
#endif
if (base != 10)
- d = nondec2awknum(tokstart, strlen(tokstart));
+ d = nondec2awknum(tokstart, strlen(tokstart), NULL);
else
d = atof(tokstart);
- yylval->memory = make_number(d);
+ yylval->memory = make_profile_number(d, tokstart, strlen(tokstart) - 1);
if (d <= INT32_MAX && d >= INT32_MIN && d == (int32_t) d)
yylval->memory->flags |= NUMINT;
return lasttok = YNUMBER;
@@ -3696,6 +4064,33 @@ retry:
&& lasttok != '@')
goto out;
+ /* allow parameter names to shadow the names of gawk extension built-ins */
+ if ((tokentab[mid].flags & GAWKX) != 0) {
+ NODE *f;
+
+ switch (want_param_names) {
+ case FUNC_HEADER:
+ /* in header, defining parameter names */
+ goto out;
+ case FUNC_BODY:
+ /* in body, name must be in symbol table for it to be a parameter */
+ if ((f = lookup(tokstart)) != NULL) {
+ if (f->type == Node_builtin_func)
+ break;
+ else
+ goto out;
+ }
+ /* else
+ fall through */
+ case DONT_CHECK:
+ /* regular code */
+ break;
+ default:
+ cant_happen();
+ break;
+ }
+ }
+
if (do_lint) {
if ((tokentab[mid].flags & GAWKX) != 0 && (warntab[mid] & GAWKX) == 0) {
lintwarn(_("`%s' is a gawk extension"),
@@ -3896,7 +4291,7 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
INSTRUCTION *expr;
expr = list_create(instruction(Op_push_i));
- expr->nexti->memory = make_number(0.0);
+ expr->nexti->memory = make_profile_number(0.0, "0", 1);
(void) mk_expression_list(subn,
list_append(expr, instruction(Op_field_spec)));
}
@@ -3944,7 +4339,7 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
r->sub_flags |= GENSUB;
if (nexp == 3) {
ip = instruction(Op_push_i);
- ip->memory = make_number(0.0);
+ ip->memory = make_profile_number(0.0, "0", 1);
(void) mk_expression_list(subn,
list_append(list_create(ip), instruction(Op_field_spec)));
}
@@ -3955,7 +4350,7 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
}
#ifdef HAVE_MPFR
- /* N.B.: There isn't any special processing for an alternate function below */
+ /* N.B.: If necessary, add special processing for alternate builtin, below */
if (do_mpfr && tokentab[idx].ptr2)
r->builtin = tokentab[idx].ptr2;
else
@@ -3973,17 +4368,26 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
list = list_create(r);
(void) list_prepend(list, instruction(Op_field_spec));
(void) list_prepend(list, instruction(Op_push_i));
- list->nexti->memory = make_number(0.0);
+ list->nexti->memory = make_profile_number(0.0, "0", 1);
return list;
} else {
arg = subn->nexti;
if (arg->nexti == arg->lasti && arg->nexti->opcode == Op_push)
arg->nexti->opcode = Op_push_arg; /* argument may be array */
}
- } else if (r->builtin == do_isarray) {
+ } else if (r->builtin == do_isarray || r->builtin == do_typeof) {
arg = subn->nexti;
if (arg->nexti == arg->lasti && arg->nexti->opcode == Op_push)
- arg->nexti->opcode = Op_push_arg; /* argument may be array */
+ arg->nexti->opcode = Op_push_arg_untyped; /* argument may be untyped */
+ } else if (r->builtin == do_intdiv
+#ifdef HAVE_MPFR
+ || r->builtin == MPF(intdiv)
+#endif
+ ) {
+ arg = subn->nexti->lasti->nexti->lasti->nexti; /* 3rd arg list */
+ ip = arg->lasti;
+ if (ip->opcode == Op_push)
+ ip->opcode = Op_push_array;
} else if (r->builtin == do_match) {
static bool warned = false;
@@ -4060,7 +4464,7 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
} else if (do_intl /* --gen-po */
&& r->builtin == do_dcgettext /* dcgettext(...) */
&& subn->nexti->lasti->opcode == Op_push_i /* 1st arg is constant */
- && (subn->nexti->lasti->memory->flags & STRCUR) != 0) { /* it's a string constant */
+ && (subn->nexti->lasti->memory->flags & STRING) != 0) { /* it's a string constant */
/* ala xgettext, dcgettext("some string" ...) dumps the string */
NODE *str = subn->nexti->lasti->memory;
@@ -4072,9 +4476,9 @@ snode(INSTRUCTION *subn, INSTRUCTION *r)
} else if (do_intl /* --gen-po */
&& r->builtin == do_dcngettext /* dcngettext(...) */
&& subn->nexti->lasti->opcode == Op_push_i /* 1st arg is constant */
- && (subn->nexti->lasti->memory->flags & STRCUR) != 0 /* it's a string constant */
+ && (subn->nexti->lasti->memory->flags & STRING) != 0 /* it's a string constant */
&& subn->nexti->lasti->nexti->lasti->opcode == Op_push_i /* 2nd arg is constant too */
- && (subn->nexti->lasti->nexti->lasti->memory->flags & STRCUR) != 0) { /* it's a string constant */
+ && (subn->nexti->lasti->nexti->lasti->memory->flags & STRING) != 0) { /* it's a string constant */
/* ala xgettext, dcngettext("some string", "some plural" ...) dumps the string */
NODE *str1 = subn->nexti->lasti->memory;
NODE *str2 = subn->nexti->lasti->nexti->lasti->memory;
@@ -4181,18 +4585,6 @@ valinfo(NODE *n, Func_print print_func, FILE *fp)
else
#endif
print_func(fp, "%.17g\n", n->numbr);
- } else if ((n->flags & STRCUR) != 0) {
- pp_string_fp(print_func, fp, n->stptr, n->stlen, '"', false);
- print_func(fp, "\n");
- } else if ((n->flags & NUMCUR) != 0) {
-#ifdef HAVE_MPFR
- if (is_mpg_float(n))
- print_func(fp, "%s\n", mpg_fmt("%.17R*g", ROUND_MODE, n->mpg_numbr));
- else if (is_mpg_integer(n))
- print_func(fp, "%s\n", mpg_fmt("%Zd", n->mpg_i));
- else
-#endif
- print_func(fp, "%.17g\n", n->numbr);
} else
print_func(fp, "?? flags %s\n", flags2str(n->flags));
}
@@ -4281,6 +4673,14 @@ mk_function(INSTRUCTION *fi, INSTRUCTION *def)
(t + 1)->tail_call = true;
}
+ /* add any pre-function comment to start of action for profile.c */
+
+ if (function_comment != NULL) {
+ function_comment->source_line = 0;
+ (void) list_prepend(def, function_comment);
+ function_comment = NULL;
+ }
+
/* add an implicit return at end;
* also used by 'return' command in debugger
*/
@@ -4555,6 +4955,7 @@ make_regnode(int type, NODE *exp)
}
n->re_exp = exp;
n->re_flags = CONSTANT;
+ n->valref = 1;
}
return n;
}
@@ -4570,6 +4971,8 @@ mk_rexp(INSTRUCTION *list)
ip = list->nexti;
if (ip == list->lasti && ip->opcode == Op_match_rec)
ip->opcode = Op_push_re;
+ else if (ip == list->lasti && ip->opcode == Op_push_re)
+ ; /* do nothing --- @/.../ */
else {
ip = instruction(Op_push_re);
ip->memory = make_regnode(Node_dynregex, NULL);
@@ -4764,6 +5167,8 @@ mk_binary(INSTRUCTION *s1, INSTRUCTION *s2, INSTRUCTION *op)
}
op->opcode = Op_push_i;
+ // We don't need to call make_profile_number() here since
+ // optimizing is disabled when doing pretty printing.
op->memory = make_number(res);
unref(n1);
unref(n2);
@@ -4985,7 +5390,11 @@ append_rule(INSTRUCTION *pattern, INSTRUCTION *action)
(rp + 1)->lasti = action->lasti;
(rp + 2)->first_line = pattern->source_line;
(rp + 2)->last_line = lastline;
- ip = list_prepend(action, rp);
+ if (block_comment != NULL) {
+ ip = list_prepend(list_prepend(action, block_comment), rp);
+ block_comment = NULL;
+ } else
+ ip = list_prepend(action, rp);
} else {
rp = bcalloc(Op_rule, 3, 0);
@@ -5027,7 +5436,6 @@ append_rule(INSTRUCTION *pattern, INSTRUCTION *action)
action),
tp);
}
-
}
list_append(rule_list, rp + 1);
@@ -5597,6 +6005,26 @@ list_merge(INSTRUCTION *l1, INSTRUCTION *l2)
return l1;
}
+/* add_pending_comment --- add a pending comment to a statement */
+
+static inline INSTRUCTION *
+add_pending_comment(INSTRUCTION *stmt)
+{
+ INSTRUCTION *ret = stmt;
+
+ if (prior_comment != NULL) {
+ if (function_comment != prior_comment)
+ ret = list_append(stmt, prior_comment);
+ prior_comment = NULL;
+ } else if (comment != NULL && comment->memory->comment_type == EOL_COMMENT) {
+ if (function_comment != comment)
+ ret = list_append(stmt, comment);
+ comment = NULL;
+ }
+
+ return ret;
+}
+
/* See if name is a special token. */
int
@@ -5802,3 +6230,20 @@ is_identchar(int c)
{
return (is_alnum(c) || c == '_');
}
+
+/* make_profile_number --- make a number that can be printed when profiling */
+
+static NODE *
+make_profile_number(double d, const char *str, size_t len)
+{
+ NODE *n = make_number(d);
+ if (do_pretty_print) {
+ // extra byte in case need to add minus sign in negate_num
+ n->stptr = estrdup(str, len + 1);
+ n->stptr[len] = '\0';
+ n->stlen = len;
+ n->flags |= NUMCONSTSTR;
+ }
+
+ return n;
+}