aboutsummaryrefslogtreecommitdiffstats
path: root/awkgram.y
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2016-04-02 22:35:33 +0300
committerArnold D. Robbins <arnold@skeeve.com>2016-04-02 22:35:33 +0300
commit2ca6fd0026a03da5ac6a658fd744f90dde374ce5 (patch)
tree46bd99d83e7a071e345e4a660ba22391c1bbccd6 /awkgram.y
parente03ebf24e322d0aa14bfc0f8b427861a80b71b64 (diff)
parent80841bfbe180589bfc3d66d15792994d28490702 (diff)
downloadegawk-2ca6fd0026a03da5ac6a658fd744f90dde374ce5.tar.gz
egawk-2ca6fd0026a03da5ac6a658fd744f90dde374ce5.tar.bz2
egawk-2ca6fd0026a03da5ac6a658fd744f90dde374ce5.zip
Merge branch 'master' into feature/cmake
Diffstat (limited to 'awkgram.y')
-rw-r--r--awkgram.y132
1 files changed, 109 insertions, 23 deletions
diff --git a/awkgram.y b/awkgram.y
index 593eddd8..946edce7 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -151,6 +151,8 @@ 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;
@@ -162,6 +164,7 @@ 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);
@@ -439,13 +442,23 @@ function_prologue
* than one such comments, treat the last as a function
* comment.
*/
- if (comment != NULL && func_first
- && strstr(comment->memory->stptr, "\n\n") != NULL)
+ 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 != NULL) {
- function_comment = comment;
- comment = NULL;
+ if (comment_to_save != NULL) {
+ function_comment = comment_to_save;
+ comment_to_save = NULL;
}
func_first = false;
$1->source_file = source;
@@ -529,31 +542,54 @@ a_slash
statements
: /* empty */
{
- if (comment != NULL) {
+ if (prior_comment != NULL) {
+ $$ = list_create(prior_comment);
+ prior_comment = NULL;
+ } else if (comment != NULL) {
$$ = list_create(comment);
comment = NULL;
- } else $$ = NULL;
+ } else
+ $$ = NULL;
}
| statements statement
{
if ($2 == NULL) {
- if (comment == NULL)
- $$ = $1;
- else {
+ 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) {
- if (comment == NULL)
- $$ = $2;
- else {
+ 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 (comment != NULL) {
+ 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;
}
@@ -890,6 +926,7 @@ regular_loop:
$$ = list_prepend($1, instruction(Op_exec_count));
else
$$ = $1;
+ $$ = add_pending_comment($$);
}
;
@@ -901,6 +938,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
@@ -910,6 +948,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
@@ -920,6 +959,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
{
@@ -931,6 +971,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
{
@@ -946,6 +987,7 @@ non_compound_stmt
$$->nexti->memory = dupnode(Nnull_string);
} else
$$ = list_append($2, $1);
+ $$ = add_pending_comment($$);
}
| LEX_RETURN
{
@@ -970,6 +1012,7 @@ non_compound_stmt
$$ = list_append($3, $1);
}
+ $$ = add_pending_comment($$);
}
| simple_stmt statement_term
;
@@ -1079,6 +1122,7 @@ regular_print:
}
}
}
+ $$ = add_pending_comment($$);
}
| LEX_DELETE NAME { sub_counter = 0; } delete_subscript_list
@@ -1113,6 +1157,7 @@ regular_print:
$1->expr_count = sub_counter;
$$ = list_append(list_append($4, $2), $1);
}
+ $$ = add_pending_comment($$);
}
| LEX_DELETE '(' NAME ')'
/*
@@ -1143,9 +1188,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
@@ -3133,10 +3182,14 @@ get_comment(int flag)
tok = tokstart;
tokadd('#');
sl = sourceline;
+ char *p1;
+ char *p2;
while (true) {
while ((c = nextc(false)) != '\n' && c != END_FILE) {
- tokadd(c);
+ /* ignore \r characters */
+ if (c != '\r')
+ tokadd(c);
}
if (flag == EOL_COMMENT) {
/* comment at end of line. */
@@ -3165,6 +3218,19 @@ get_comment(int flag)
} 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);
@@ -3182,20 +3248,20 @@ split_comment(void)
int l;
NODE *n;
- p = comment->memory->stptr;
- l = comment->memory->stlen - 3;
+ 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;
+ 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->source_file;
+ program_comment->source_file = comment_to_save->source_file;
p[l + 2] = 0;
program_comment->memory = make_str_node(p, l + 2, 0);
- comment = NULL;
+ comment_to_save = NULL;
freenode(n);
break;
}
@@ -5973,6 +6039,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