diff options
-rwxr-xr-x | ChangeLog | 172 | ||||
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | awk.h | 14 | ||||
-rw-r--r-- | awkgram.c | 1551 | ||||
-rw-r--r-- | awkgram.y | 743 | ||||
-rw-r--r-- | debug.c | 56 | ||||
-rw-r--r-- | doc/ChangeLog | 10 | ||||
-rw-r--r-- | doc/gawk.info | 478 | ||||
-rw-r--r-- | doc/gawk.texi | 13 | ||||
-rw-r--r-- | doc/gawktexi.in | 13 | ||||
-rw-r--r-- | profile.c | 307 | ||||
-rw-r--r-- | test/ChangeLog | 8 | ||||
-rw-r--r-- | test/profile0.ok | 2 | ||||
-rw-r--r-- | test/profile10.ok | 12 |
14 files changed, 2099 insertions, 1284 deletions
@@ -18,6 +18,47 @@ 2018-11-11 Arnold D. Robbins <arnold@skeeve.com> * main.c (usage): Improve output for -Z in the help. +2018-11-11 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (outer_comment): New variable. + (Grammar): More changes. We now get the simple case of leading + and trailing comments, but not all the cases. + +2018-11-11 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (trailing_comment): New variable. + (Grammar): For `action', append both trailing comments. This may + change. For `statements', append the value of `trailing_comment' + if set. At `statement := l_brace statements rbrace' save + trailing_comment from r_brace. + (make_braced_statements): Don't append the comment from r_brace + to the statement list. + +2018-10-30 Arnold D. Robbins <arnold@skeeve.com> + + * awk.h (NODE): New field: sub.nodep.x.cmnt, holds comment for + expressions being pretty-printed. + * awkgram.y (Grammar): For expression lists, save any comment + that came after a comma in the list. + * profile.c (pp_push): Accept a fourth argument which is any + comment associated with the expression. Either it's there or + it's NULL. Save it in the pp_comment field of the node being pushed. + (tabs, tabs_len, check_indent_level): Made into static globals. + (pprint): Adjust all calls to pp_push(). Fix parenthesization + for casts in string lengthes when indenting. + (pp_list): If a popped item has a comment, include it and the + following indentation in the formatted result. + +2018-10-28 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (include_source): Add second parameter to return + SRCFILE pointer. + (Grammar): For @include, save the comment for later dumping + along with the list of include files. + (make_include_comment): Removed. No longer used. + * profile.c: Update copyright year. + (print_include_list): New function. + (dump_prog): Call it. 2018-10-24 Arnold D. Robbins <arnold@skeeve.com> @@ -27,11 +68,121 @@ * config.sub: Updated from GNULIB. +2018-10-20 Arnold D. Robbins <arnold@skeeve.com> + + * awk.h (SRCFILE): Add comment field for comments on @load statements. + * awkgram.y (include_source): Type change to boolean. + (load_library): Type change to boolean, additiona parameter to + bring the SRCFILE struct up to where we can add the comment into it. + (make_include_comment): New function. Not used yet. + (Grammar): Add comment for @load statements. Start on preserving + @include statements and their comments for eventual inclusion + into the pretty-printed code. + * profile.c (print_lib_list): Made a little smarter about printing + the header and indentation. Print the comment if there is one. + +2018-10-17 Arnold D. Robbins <arnold@skeeve.com> + + * awk.h (commenttype): Add FOR_COMMENT. + * awkgram.y (Grammar): Handle all the opt_nls cases in + regular for statements. + * debug.c (print_instruction): Print the comments in Op_K_for. + * profile.c (pprint): Handle printing comments. + +2018-10-17 Arnold D. Robbins <arnold@skeeve.com> + + * NEWS: Updated. + * awkgram.y (Grammar): Distinguish `print' and `print $0' in + what gets profiled / pretty-printed. + * profile.c (pprint): For case and default, add final newline + if there is no comment to print. + +2018-10-16 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (Grammar): Improve comment handling for many plain + statements. Improve handling for case and default. + Handle comments in `for (iggy in foo)' loops. + (yylex): After a colon, only allow newline if was part of ?:. + (merge_comments): Improve coding so we don't get two newlines + at the end of a merged comment. + * debug.c (print_instruction): Handle comments for case and default. + Simplify printing of comments. + * profile.c (pprint): Handle comments for case and default. + Remove compiler warning in Op_and/Op_or handling. + +2018-10-14 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (Grammar): Add comment handling for do...while. + Regularize comments about `else ...'. + * debug.c (print_instruction): Improve handling of comments for + do-while and switch, and in general. + * profile.c (pprint): Revise for do...while. + +2018-10-10 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (make_braced_statements): New function. + (Grammar): Use it in the right places instead of inline code. + * debug.c (print_instruction): For Op_comment, fix type string. + * profile.c (pprint): Move tabs and tabs_len to top of function. + For Op_and and Op_or, handle comments. Use new check_indent_level + for Op_and, Op_or and Op_cond_exp. + 2018-10-10 Arnold D. Robbins <arnold@skeeve.com> * debug.c (print_instruction): For Op_comment, use print_func instead of fprintf to print the comment type. +2018-10-10 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (Grammar): For statement -> { statements }, fix comment + handling. For `if' statement add comment support. + * profile.c (pp_print): Print comments associated with `if' and `else'. + +2018-10-09 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (Grammar): Fix handling of empty statement (just a semi- + colon). + (merge_comments): If no chained comment and no second comment, + just return early. + +2018-10-09 Arnold D. Robbins <arnold@skeeve.com> + + * awk.h (enum commenttype): New enum. + (NODE): Add it to sub.val. + (EOL_COMMENT, FULL_COMMENT): Replaced with above enum values. + * awkgram.y (Grammar): Finish up handling comments in function headers + and bodies. Get trailing comments at end of program + (get_comment): When doing comments, if we got EOF, push it back so that + multiple comments get merged together. + (merge_comments): Allow second parameter to be NULL. + * profile.c (pp_print): Change to use above enum everywhere. For + Op_K_print_rec produce plain `print' instead of `print $0'. Handle + comments in ?:. Handle printing function comments. + (print_comment): Simplify `after_newline' assignment. Add assertion + that chaining is only two deep. + +2018-10-06 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (action): Improve handling of comments attached + to braces. Helps with function bodies. + +2018-10-04 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y (merge_comments): Change return type to void. Adjust calls. + (Grammar): For action, pull comments out of braces and stick + into the list. For function_prologue, get comments from parameters + and ending newline, merge, and save. Wherever nls and opt_nls + are used, be sure to pass their values up via $$. For various + cases that can be empty, explicitly set $$ = NULL. + * profile.c (pprint): Get switch working. Get ?: working. + (print_comment): Print any chained comment. + (pp_func): Start revising. + +2018-10-03 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y: Range expressions, enable comment stuff. + Switch statement: start on comment handling. + 2018-10-01 Nelson H.F. Beebe <beebe@math.utah.edu> * custom.h (__builtin_expect): Define for non-GNU compilers. @@ -61,6 +212,23 @@ 2018-09-21 Arnold D. Robbins <arnold@skeeve.com> + * awk.h (INSTRUCTION): Add comment field to carry + comment around during parsing. + * awkgram.y (merge_comments): New function. + (split_comment, check_comment, comment, prior_comment, + comment_to_save, program_comment, function_comment, + block_comment): Removed. + (grammar): Remove old code and start passing the comment + up via yylval and the newlines in the grammar. + +2018-09-21 Arnold D. Robbins <arnold@skeeve.com> + + * awkgram.y: Undo change of 2016-11-28 to make switch + head a separate production, in preparation for revamping + comment handling. + +2018-09-21 Arnold D. Robbins <arnold@skeeve.com> + * re.c (make_regexp): Handle backslash at end of input string. Thanks to Anatoly Trosinenko <anatoly.trosinenko@gmail.com> for the report. @@ -1740,6 +1908,10 @@ * dfa.c: Sync with GNULIB. Twice in one day. + Unrelated: Start improving profiling comments for switch/case. + + * awkgram.y (switch_head): New production. + 2016-11-21 Arnold D. Robbins <arnold@skeeve.com> * dfa.c: Sync with GNULIB. @@ -15,6 +15,10 @@ Changes from 4.2.x to 5.0.0 3. The code now makes some stronger assumptions about a C99 environment. +4. Comment handling in the pretty-printer has been reworked almost completely +from scratch. As a result, comments in many corner cases that were previously +lost are now included in the formatted output. + Changes from 4.2.1 to 4.2.2 --------------------------- @@ -296,6 +296,12 @@ typedef union bucket_item { } hi; } BUCKET; +enum commenttype { + EOL_COMMENT = 1, + BLOCK_COMMENT, + FOR_COMMENT // special case +}; + /* string hash table */ #define ahnext hs.next #define ahname hs.name /* a string index node */ @@ -340,6 +346,7 @@ typedef struct exp_node { struct exp_node *extra; void (*aptr)(void); long xl; + void *cmnt; // used by pretty printer } x; char *name; size_t reserved; @@ -368,6 +375,7 @@ typedef struct exp_node { wchar_t *wsp; size_t wslen; struct exp_node *typre; + enum commenttype comtype; } val; } sub; NODETYPE type; @@ -565,9 +573,7 @@ typedef struct exp_node { #define alevel sub.nodep.x.xl /* Op_comment */ -#define comment_type sub.val.idx -#define EOL_COMMENT 1 -#define FULL_COMMENT 2 +#define comment_type sub.val.comtype /* --------------------------------lint warning types----------------------------*/ typedef enum lintvals { @@ -764,6 +770,7 @@ typedef struct exp_instruction { awk_ext_func_t *exf; } x; + struct exp_instruction *comment; short source_line; short pool_size; // memory management in symbol.c OPCODE opcode; @@ -1009,6 +1016,7 @@ typedef struct srcfile { char *lexeme; char *lexptr_begin; int lasttok; + INSTRUCTION *comment; /* comment on @load line */ } SRCFILE; // structure for INSTRUCTION pool, needed mainly for debugger @@ -65,7 +65,7 @@ /* First part of user prologue. */ -#line 26 "awkgram.y" /* yacc.c:338 */ +#line 47 "awkgram.y" /* yacc.c:338 */ #ifdef GAWKDEBUG #define YYDEBUG 12 @@ -96,11 +96,13 @@ static int isnoeffect(OPCODE type); static INSTRUCTION *make_assignable(INSTRUCTION *ip); static void dumpintlstr(const char *str, size_t len); static void dumpintlstr2(const char *str1, size_t len1, const char *str2, size_t len2); -static int include_source(INSTRUCTION *file); -static int load_library(INSTRUCTION *file); +static bool include_source(INSTRUCTION *file, void **srcfile_p); +static bool load_library(INSTRUCTION *file, void **srcfile_p); static void next_sourcefile(void); static char *tokexpand(void); static NODE *set_profile_text(NODE *n, const char *str, size_t len); +static INSTRUCTION *trailing_comment; +static INSTRUCTION *outer_comment; #define instruction(t) bcalloc(t, 1, 0) @@ -127,8 +129,8 @@ 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 merge_comments(INSTRUCTION *c1, INSTRUCTION *c2); +static INSTRUCTION *make_braced_statements(INSTRUCTION *lbrace, INSTRUCTION *stmts, INSTRUCTION *rbrace); static void add_sign_to_num(NODE *n, char sign); static bool at_seen = false; @@ -195,27 +197,19 @@ 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 func_first = true; // can nuke 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); #define YYSTYPE INSTRUCTION * -#line 219 "awkgram.c" /* yacc.c:338 */ +#line 213 "awkgram.c" /* yacc.c:338 */ # ifndef YY_NULLPTR # if defined __cplusplus # if 201103L <= __cplusplus @@ -665,27 +659,27 @@ static const yytype_uint8 yytranslate[] = /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ static const yytype_uint16 yyrline[] = { - 0, 215, 215, 217, 222, 223, 227, 239, 244, 255, - 262, 268, 277, 285, 287, 292, 300, 302, 308, 316, - 326, 356, 370, 384, 392, 403, 415, 417, 419, 425, - 433, 434, 438, 438, 484, 483, 517, 532, 534, 539, - 549, 596, 601, 602, 606, 608, 610, 617, 707, 749, - 791, 904, 911, 918, 929, 939, 949, 959, 971, 988, - 987, 1001, 1013, 1013, 1112, 1112, 1146, 1177, 1186, 1187, - 1193, 1194, 1201, 1206, 1218, 1232, 1234, 1242, 1249, 1251, - 1259, 1268, 1270, 1279, 1280, 1288, 1293, 1293, 1306, 1310, - 1318, 1319, 1322, 1324, 1329, 1330, 1339, 1340, 1345, 1350, - 1359, 1361, 1363, 1370, 1371, 1377, 1378, 1383, 1385, 1390, - 1392, 1400, 1405, 1414, 1415, 1420, 1422, 1427, 1429, 1437, - 1442, 1450, 1451, 1456, 1463, 1467, 1469, 1471, 1484, 1501, - 1511, 1518, 1520, 1525, 1527, 1529, 1537, 1539, 1544, 1546, - 1551, 1553, 1555, 1612, 1614, 1616, 1618, 1620, 1622, 1624, - 1626, 1640, 1645, 1650, 1675, 1681, 1683, 1685, 1687, 1689, - 1691, 1696, 1700, 1732, 1740, 1746, 1752, 1765, 1766, 1767, - 1772, 1777, 1781, 1785, 1800, 1821, 1826, 1863, 1892, 1893, - 1899, 1900, 1905, 1907, 1914, 1931, 1948, 1950, 1957, 1962, - 1970, 1980, 1992, 2001, 2005, 2009, 2013, 2017, 2021, 2024, - 2026, 2030, 2034, 2038 + 0, 232, 232, 233, 238, 249, 253, 265, 270, 284, + 291, 301, 314, 324, 326, 331, 341, 343, 349, 353, + 358, 388, 401, 414, 421, 431, 449, 451, 453, 459, + 467, 468, 472, 472, 506, 505, 539, 554, 556, 561, + 562, 580, 585, 586, 590, 601, 606, 613, 721, 772, + 822, 948, 969, 990, 1000, 1010, 1020, 1031, 1044, 1062, + 1061, 1075, 1093, 1093, 1191, 1191, 1224, 1254, 1262, 1263, + 1269, 1270, 1277, 1282, 1295, 1310, 1312, 1320, 1327, 1329, + 1337, 1346, 1348, 1357, 1358, 1366, 1371, 1371, 1384, 1390, + 1402, 1406, 1428, 1429, 1435, 1436, 1445, 1446, 1451, 1456, + 1473, 1475, 1477, 1484, 1485, 1491, 1492, 1497, 1499, 1506, + 1508, 1516, 1521, 1532, 1533, 1538, 1540, 1547, 1549, 1557, + 1562, 1572, 1573, 1578, 1585, 1589, 1591, 1593, 1606, 1623, + 1633, 1640, 1642, 1647, 1649, 1651, 1659, 1661, 1666, 1668, + 1673, 1675, 1677, 1734, 1736, 1738, 1740, 1742, 1744, 1746, + 1748, 1762, 1767, 1772, 1797, 1803, 1805, 1807, 1809, 1811, + 1813, 1818, 1822, 1854, 1862, 1868, 1874, 1887, 1888, 1889, + 1894, 1899, 1903, 1907, 1922, 1943, 1948, 1985, 2014, 2015, + 2021, 2022, 2027, 2029, 2036, 2053, 2070, 2072, 2079, 2084, + 2092, 2102, 2114, 2123, 2127, 2132, 2136, 2140, 2144, 2149, + 2150, 2154, 2158, 2162 }; #endif @@ -1882,8 +1876,14 @@ yyreduce: YY_REDUCE_PRINT (yyn); switch (yyn) { - case 3: -#line 218 "awkgram.y" /* yacc.c:1645 */ + case 2: +#line 232 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = NULL; } +#line 1883 "awkgram.c" /* yacc.c:1645 */ + break; + + case 3: +#line 234 "awkgram.y" /* yacc.c:1645 */ { rule = 0; yyerrok; @@ -1891,16 +1891,31 @@ yyreduce: #line 1892 "awkgram.c" /* yacc.c:1645 */ break; + case 4: +#line 239 "awkgram.y" /* yacc.c:1645 */ + { + if ((yyvsp[0]) != NULL) { + if ((yyvsp[-1]) == NULL) { + outer_comment = (yyvsp[0]); + } else { + trailing_comment = (yyvsp[0]); + } + } + (yyval) = (yyvsp[-1]); + } +#line 1907 "awkgram.c" /* yacc.c:1645 */ + break; + case 5: -#line 224 "awkgram.y" /* yacc.c:1645 */ +#line 250 "awkgram.y" /* yacc.c:1645 */ { next_sourcefile(); } -#line 1900 "awkgram.c" /* yacc.c:1645 */ +#line 1915 "awkgram.c" /* yacc.c:1645 */ break; case 6: -#line 228 "awkgram.y" /* yacc.c:1645 */ +#line 254 "awkgram.y" /* yacc.c:1645 */ { rule = 0; /* @@ -1909,20 +1924,20 @@ yyreduce: */ /* yyerrok; */ } -#line 1913 "awkgram.c" /* yacc.c:1645 */ +#line 1928 "awkgram.c" /* yacc.c:1645 */ break; case 7: -#line 240 "awkgram.y" /* yacc.c:1645 */ +#line 266 "awkgram.y" /* yacc.c:1645 */ { (void) append_rule((yyvsp[-1]), (yyvsp[0])); first_rule = false; } -#line 1922 "awkgram.c" /* yacc.c:1645 */ +#line 1937 "awkgram.c" /* yacc.c:1645 */ break; case 8: -#line 245 "awkgram.y" /* yacc.c:1645 */ +#line 271 "awkgram.y" /* yacc.c:1645 */ { if (rule != Rule) { msg(_("%s blocks must have an action part"), ruletab[rule]); @@ -1930,119 +1945,125 @@ yyreduce: } else if ((yyvsp[-1]) == NULL) { msg(_("each rule must have a pattern or an action part")); errcount++; - } else /* pattern rule with non-empty pattern */ + } else { /* pattern rule with non-empty pattern */ + if ((yyvsp[0]) != NULL) + list_append((yyvsp[-1]), (yyvsp[0])); (void) append_rule((yyvsp[-1]), NULL); + } } -#line 1937 "awkgram.c" /* yacc.c:1645 */ +#line 1955 "awkgram.c" /* yacc.c:1645 */ break; case 9: -#line 256 "awkgram.y" /* yacc.c:1645 */ +#line 285 "awkgram.y" /* yacc.c:1645 */ { in_function = NULL; (void) mk_function((yyvsp[-1]), (yyvsp[0])); want_param_names = DONT_CHECK; yyerrok; } -#line 1948 "awkgram.c" /* yacc.c:1645 */ +#line 1966 "awkgram.c" /* yacc.c:1645 */ break; case 10: -#line 263 "awkgram.y" /* yacc.c:1645 */ +#line 292 "awkgram.y" /* yacc.c:1645 */ { want_source = false; at_seen = false; + if ((yyvsp[-1]) != NULL && (yyvsp[0]) != NULL) { + SRCFILE *s = (SRCFILE *) (yyvsp[-1]); + s->comment = (yyvsp[0]); + } yyerrok; } -#line 1958 "awkgram.c" /* yacc.c:1645 */ +#line 1980 "awkgram.c" /* yacc.c:1645 */ break; case 11: -#line 269 "awkgram.y" /* yacc.c:1645 */ +#line 302 "awkgram.y" /* yacc.c:1645 */ { want_source = false; at_seen = false; + if ((yyvsp[-1]) != NULL && (yyvsp[0]) != NULL) { + SRCFILE *s = (SRCFILE *) (yyvsp[-1]); + s->comment = (yyvsp[0]); + } yyerrok; } -#line 1968 "awkgram.c" /* yacc.c:1645 */ +#line 1994 "awkgram.c" /* yacc.c:1645 */ break; case 12: -#line 278 "awkgram.y" /* yacc.c:1645 */ +#line 315 "awkgram.y" /* yacc.c:1645 */ { - if (include_source((yyvsp[0])) < 0) + void *srcfile = NULL; + + if (! include_source((yyvsp[0]), & srcfile)) YYABORT; efree((yyvsp[0])->lextok); bcfree((yyvsp[0])); - (yyval) = NULL; + (yyval) = (INSTRUCTION *) srcfile; } -#line 1980 "awkgram.c" /* yacc.c:1645 */ +#line 2008 "awkgram.c" /* yacc.c:1645 */ break; case 13: -#line 286 "awkgram.y" /* yacc.c:1645 */ +#line 325 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 1986 "awkgram.c" /* yacc.c:1645 */ +#line 2014 "awkgram.c" /* yacc.c:1645 */ break; case 14: -#line 288 "awkgram.y" /* yacc.c:1645 */ +#line 327 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 1992 "awkgram.c" /* yacc.c:1645 */ +#line 2020 "awkgram.c" /* yacc.c:1645 */ break; case 15: -#line 293 "awkgram.y" /* yacc.c:1645 */ +#line 332 "awkgram.y" /* yacc.c:1645 */ { - if (load_library((yyvsp[0])) < 0) + void *srcfile; + + if (! load_library((yyvsp[0]), & srcfile)) YYABORT; efree((yyvsp[0])->lextok); bcfree((yyvsp[0])); - (yyval) = NULL; + (yyval) = (INSTRUCTION *) srcfile; } -#line 2004 "awkgram.c" /* yacc.c:1645 */ +#line 2034 "awkgram.c" /* yacc.c:1645 */ break; case 16: -#line 301 "awkgram.y" /* yacc.c:1645 */ +#line 342 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 2010 "awkgram.c" /* yacc.c:1645 */ +#line 2040 "awkgram.c" /* yacc.c:1645 */ break; case 17: -#line 303 "awkgram.y" /* yacc.c:1645 */ +#line 344 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 2016 "awkgram.c" /* yacc.c:1645 */ +#line 2046 "awkgram.c" /* yacc.c:1645 */ break; case 18: -#line 308 "awkgram.y" /* yacc.c:1645 */ +#line 349 "awkgram.y" /* yacc.c:1645 */ { rule = Rule; - if (comment != NULL) { - (yyval) = list_create(comment); - comment = NULL; - } else - (yyval) = NULL; + (yyval) = NULL; } -#line 2029 "awkgram.c" /* yacc.c:1645 */ +#line 2055 "awkgram.c" /* yacc.c:1645 */ break; case 19: -#line 317 "awkgram.y" /* yacc.c:1645 */ +#line 354 "awkgram.y" /* yacc.c:1645 */ { rule = Rule; - if (comment != NULL) { - (yyval) = list_prepend((yyvsp[0]), comment); - comment = NULL; - } else - (yyval) = (yyvsp[0]); } -#line 2042 "awkgram.c" /* yacc.c:1645 */ +#line 2063 "awkgram.c" /* yacc.c:1645 */ break; case 20: -#line 327 "awkgram.y" /* yacc.c:1645 */ +#line 359 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *tp; @@ -2065,18 +2086,18 @@ yyreduce: ((yyvsp[-3])->nexti + 1)->condpair_left = (yyvsp[-3])->lasti; ((yyvsp[-3])->nexti + 1)->condpair_right = (yyvsp[0])->lasti; } - if (comment != NULL) { - (yyval) = list_append(list_merge(list_prepend((yyvsp[-3]), comment), (yyvsp[0])), tp); - comment = NULL; - } else + /* Put any comments in front of the range expression */ + if ((yyvsp[-1]) != NULL) + (yyval) = list_append(list_merge(list_prepend((yyvsp[-3]), (yyvsp[-1])), (yyvsp[0])), tp); + else (yyval) = list_append(list_merge((yyvsp[-3]), (yyvsp[0])), tp); rule = Rule; } -#line 2076 "awkgram.c" /* yacc.c:1645 */ +#line 2097 "awkgram.c" /* yacc.c:1645 */ break; case 21: -#line 357 "awkgram.y" /* yacc.c:1645 */ +#line 389 "awkgram.y" /* yacc.c:1645 */ { static int begin_seen = 0; @@ -2087,14 +2108,13 @@ yyreduce: (yyvsp[0])->in_rule = rule = BEGIN; (yyvsp[0])->source_file = source; - check_comment(); (yyval) = (yyvsp[0]); } -#line 2094 "awkgram.c" /* yacc.c:1645 */ +#line 2114 "awkgram.c" /* yacc.c:1645 */ break; case 22: -#line 371 "awkgram.y" /* yacc.c:1645 */ +#line 402 "awkgram.y" /* yacc.c:1645 */ { static int end_seen = 0; @@ -2105,116 +2125,107 @@ yyreduce: (yyvsp[0])->in_rule = rule = END; (yyvsp[0])->source_file = source; - check_comment(); (yyval) = (yyvsp[0]); } -#line 2112 "awkgram.c" /* yacc.c:1645 */ +#line 2131 "awkgram.c" /* yacc.c:1645 */ break; case 23: -#line 385 "awkgram.y" /* yacc.c:1645 */ +#line 415 "awkgram.y" /* yacc.c:1645 */ { func_first = false; (yyvsp[0])->in_rule = rule = BEGINFILE; (yyvsp[0])->source_file = source; - check_comment(); (yyval) = (yyvsp[0]); } -#line 2124 "awkgram.c" /* yacc.c:1645 */ +#line 2142 "awkgram.c" /* yacc.c:1645 */ break; case 24: -#line 393 "awkgram.y" /* yacc.c:1645 */ +#line 422 "awkgram.y" /* yacc.c:1645 */ { func_first = false; (yyvsp[0])->in_rule = rule = ENDFILE; (yyvsp[0])->source_file = source; - check_comment(); (yyval) = (yyvsp[0]); } -#line 2136 "awkgram.c" /* yacc.c:1645 */ +#line 2153 "awkgram.c" /* yacc.c:1645 */ break; case 25: -#line 404 "awkgram.y" /* yacc.c:1645 */ +#line 432 "awkgram.y" /* yacc.c:1645 */ { - INSTRUCTION *ip; - if ((yyvsp[-3]) == NULL) - ip = list_create(instruction(Op_no_op)); - else - ip = (yyvsp[-3]); + INSTRUCTION *ip = make_braced_statements((yyvsp[-4]), (yyvsp[-3]), (yyvsp[-2])); + + if ((yyvsp[-2]) != NULL && (yyvsp[0]) != NULL) { + merge_comments((yyvsp[-2]), (yyvsp[0])); + trailing_comment = (yyvsp[-2]); + } else if ((yyvsp[-2]) != NULL) { + trailing_comment = (yyvsp[-2]); + } else if ((yyvsp[0]) != NULL) { + trailing_comment = (yyvsp[0]); + } + (yyval) = ip; } -#line 2149 "awkgram.c" /* yacc.c:1645 */ +#line 2172 "awkgram.c" /* yacc.c:1645 */ break; case 26: -#line 416 "awkgram.y" /* yacc.c:1645 */ +#line 450 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 2155 "awkgram.c" /* yacc.c:1645 */ +#line 2178 "awkgram.c" /* yacc.c:1645 */ break; case 27: -#line 418 "awkgram.y" /* yacc.c:1645 */ +#line 452 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 2161 "awkgram.c" /* yacc.c:1645 */ +#line 2184 "awkgram.c" /* yacc.c:1645 */ break; case 28: -#line 420 "awkgram.y" /* yacc.c:1645 */ +#line 454 "awkgram.y" /* yacc.c:1645 */ { yyerror(_("`%s' is a built-in function, it cannot be redefined"), tokstart); YYABORT; } -#line 2171 "awkgram.c" /* yacc.c:1645 */ +#line 2194 "awkgram.c" /* yacc.c:1645 */ break; case 29: -#line 426 "awkgram.y" /* yacc.c:1645 */ +#line 460 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); at_seen = false; } -#line 2180 "awkgram.c" /* yacc.c:1645 */ +#line 2203 "awkgram.c" /* yacc.c:1645 */ break; case 32: -#line 438 "awkgram.y" /* yacc.c:1645 */ +#line 472 "awkgram.y" /* yacc.c:1645 */ { want_param_names = FUNC_HEADER; } -#line 2186 "awkgram.c" /* yacc.c:1645 */ +#line 2209 "awkgram.c" /* yacc.c:1645 */ break; case 33: -#line 439 "awkgram.y" /* yacc.c:1645 */ +#line 473 "awkgram.y" /* yacc.c:1645 */ { - /* - * 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; + INSTRUCTION *func_comment = NULL; + // Merge any comments found in the parameter list with those + // following the function header, associate the whole shebang + // with the function as one block comment. + if ((yyvsp[-2]) != NULL && (yyvsp[-2])->comment != NULL) { + if ((yyvsp[0]) != NULL) { + merge_comments((yyvsp[-2])->comment, (yyvsp[0])); + } + func_comment = (yyvsp[-2])->comment; + } else if ((yyvsp[0]) != NULL) { + func_comment = (yyvsp[0]); } - func_first = false; + (yyvsp[-6])->source_file = source; + (yyvsp[-6])->comment = func_comment; if (install_function((yyvsp[-5])->lextok, (yyvsp[-6]), (yyvsp[-2])) < 0) YYABORT; in_function = (yyvsp[-5])->lextok; @@ -2224,17 +2235,17 @@ yyreduce: (yyval) = (yyvsp[-6]); want_param_names = FUNC_BODY; } -#line 2228 "awkgram.c" /* yacc.c:1645 */ +#line 2239 "awkgram.c" /* yacc.c:1645 */ break; case 34: -#line 484 "awkgram.y" /* yacc.c:1645 */ +#line 506 "awkgram.y" /* yacc.c:1645 */ { want_regexp = true; } -#line 2234 "awkgram.c" /* yacc.c:1645 */ +#line 2245 "awkgram.c" /* yacc.c:1645 */ break; case 35: -#line 486 "awkgram.y" /* yacc.c:1645 */ +#line 508 "awkgram.y" /* yacc.c:1645 */ { NODE *n, *exp; char *re; @@ -2263,11 +2274,11 @@ yyreduce: (yyval)->opcode = Op_match_rec; (yyval)->memory = n; } -#line 2267 "awkgram.c" /* yacc.c:1645 */ +#line 2278 "awkgram.c" /* yacc.c:1645 */ break; case 36: -#line 518 "awkgram.y" /* yacc.c:1645 */ +#line 540 "awkgram.y" /* yacc.c:1645 */ { char *re; size_t len; @@ -2280,112 +2291,98 @@ yyreduce: (yyval)->opcode = Op_push_re; (yyval)->memory = make_typed_regex(re, len); } -#line 2284 "awkgram.c" /* yacc.c:1645 */ +#line 2295 "awkgram.c" /* yacc.c:1645 */ break; case 37: -#line 533 "awkgram.y" /* yacc.c:1645 */ +#line 555 "awkgram.y" /* yacc.c:1645 */ { bcfree((yyvsp[0])); } -#line 2290 "awkgram.c" /* yacc.c:1645 */ +#line 2301 "awkgram.c" /* yacc.c:1645 */ break; case 39: -#line 539 "awkgram.y" /* yacc.c:1645 */ - { - if (prior_comment != NULL) { - (yyval) = list_create(prior_comment); - prior_comment = NULL; - } else if (comment != NULL) { - (yyval) = list_create(comment); - comment = NULL; - } else - (yyval) = NULL; - } -#line 2305 "awkgram.c" /* yacc.c:1645 */ +#line 561 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = NULL; } +#line 2307 "awkgram.c" /* yacc.c:1645 */ break; case 40: -#line 550 "awkgram.y" /* yacc.c:1645 */ +#line 563 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[0]) == NULL) { - if (prior_comment != NULL) { - (yyval) = list_append((yyvsp[-1]), prior_comment); - prior_comment = NULL; - if (comment != NULL) { - (yyval) = list_append((yyval), comment); - comment = NULL; - } - } else if (comment != NULL) { - (yyval) = list_append((yyvsp[-1]), comment); - comment = NULL; - } else - (yyval) = (yyvsp[-1]); + (yyval) = (yyvsp[-1]); } else { add_lint((yyvsp[0]), LINT_no_effect); if ((yyvsp[-1]) == NULL) { - if (prior_comment != NULL) { - (yyval) = list_append((yyvsp[0]), prior_comment); - prior_comment = NULL; - if (comment != NULL) { - (yyval) = list_append((yyval), comment); - comment = NULL; - } - } else if (comment != NULL) { - (yyval) = list_append((yyvsp[0]), comment); - comment = NULL; - } else - (yyval) = (yyvsp[0]); + (yyval) = (yyvsp[0]); } else { - if (prior_comment != NULL) { - list_append((yyvsp[0]), prior_comment); - prior_comment = NULL; - if (comment != NULL) { - list_append((yyvsp[0]), comment); - comment = NULL; - } - } else if (comment != NULL) { - list_append((yyvsp[0]), comment); - comment = NULL; - } (yyval) = list_merge((yyvsp[-1]), (yyvsp[0])); } } + if (trailing_comment != NULL) { + (yyval) = list_append((yyval), trailing_comment); + trailing_comment = NULL; + } yyerrok; } -#line 2356 "awkgram.c" /* yacc.c:1645 */ +#line 2329 "awkgram.c" /* yacc.c:1645 */ break; case 41: -#line 597 "awkgram.y" /* yacc.c:1645 */ +#line 581 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 2362 "awkgram.c" /* yacc.c:1645 */ +#line 2335 "awkgram.c" /* yacc.c:1645 */ + break; + + case 42: +#line 585 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = (yyvsp[0]); } +#line 2341 "awkgram.c" /* yacc.c:1645 */ + break; + + case 43: +#line 586 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = (yyvsp[0]); } +#line 2347 "awkgram.c" /* yacc.c:1645 */ break; case 44: -#line 607 "awkgram.y" /* yacc.c:1645 */ - { (yyval) = NULL; } -#line 2368 "awkgram.c" /* yacc.c:1645 */ +#line 591 "awkgram.y" /* yacc.c:1645 */ + { + if ((yyvsp[0]) != NULL) { + INSTRUCTION *ip; + + merge_comments((yyvsp[0]), NULL); + ip = list_create(instruction(Op_no_op)); + (yyval) = list_append(ip, (yyvsp[0])); + } else + (yyval) = NULL; + } +#line 2362 "awkgram.c" /* yacc.c:1645 */ break; case 45: -#line 609 "awkgram.y" /* yacc.c:1645 */ - { (yyval) = (yyvsp[-1]); } -#line 2374 "awkgram.c" /* yacc.c:1645 */ +#line 602 "awkgram.y" /* yacc.c:1645 */ + { + trailing_comment = (yyvsp[0]); // NULL or comment + (yyval) = make_braced_statements((yyvsp[-2]), (yyvsp[-1]), (yyvsp[0])); + } +#line 2371 "awkgram.c" /* yacc.c:1645 */ break; case 46: -#line 611 "awkgram.y" /* yacc.c:1645 */ +#line 607 "awkgram.y" /* yacc.c:1645 */ { if (do_pretty_print) (yyval) = list_prepend((yyvsp[0]), instruction(Op_exec_count)); else (yyval) = (yyvsp[0]); } -#line 2385 "awkgram.c" /* yacc.c:1645 */ +#line 2382 "awkgram.c" /* yacc.c:1645 */ break; case 47: -#line 618 "awkgram.y" /* yacc.c:1645 */ +#line 614 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *dflt, *curr = NULL, *cexp, *cstmt; INSTRUCTION *ip, *nextc, *tbreak; @@ -2403,8 +2400,9 @@ yyreduce: if ((yyvsp[-2]) != NULL) { curr = (yyvsp[-2])->nexti; bcfree((yyvsp[-2])); /* Op_list */ - } /* else - curr = NULL; */ + } + /* else + curr = NULL; */ for (; curr != NULL; curr = nextc) { INSTRUCTION *caseexp = curr->case_exp; @@ -2460,26 +2458,43 @@ yyreduce: ip = (yyvsp[-6]); if (do_pretty_print) { + // first merge comments + INSTRUCTION *head_comment = NULL; + + if ((yyvsp[-4]) != NULL && (yyvsp[-3]) != NULL) { + merge_comments((yyvsp[-4]), (yyvsp[-3])); + head_comment = (yyvsp[-4]); + } else if ((yyvsp[-4]) != NULL) + head_comment = (yyvsp[-4]); + else + head_comment = (yyvsp[-3]); + + (yyvsp[-8])->comment = head_comment; + (void) list_prepend(ip, (yyvsp[-8])); (void) list_prepend(ip, instruction(Op_exec_count)); (yyvsp[-8])->target_break = tbreak; ((yyvsp[-8]) + 1)->switch_start = cexp->nexti; ((yyvsp[-8]) + 1)->switch_end = cexp->lasti; - }/* else - $1 is NULL */ + ((yyvsp[-8]) + 1)->switch_end->comment = (yyvsp[0]); + } + /* else + $1 is NULL */ (void) list_append(cexp, dflt); (void) list_merge(ip, cexp); + if ((yyvsp[-1]) != NULL) + (void) list_append(cstmt, (yyvsp[-1])); (yyval) = list_merge(ip, cstmt); break_allowed--; fix_break_continue(ip, tbreak, NULL); } -#line 2479 "awkgram.c" /* yacc.c:1645 */ +#line 2494 "awkgram.c" /* yacc.c:1645 */ break; case 48: -#line 708 "awkgram.y" /* yacc.c:1645 */ +#line 722 "awkgram.y" /* yacc.c:1645 */ { /* * ----------------- @@ -2508,8 +2523,17 @@ yyreduce: (yyvsp[-5])->target_continue = tcont; ((yyvsp[-5]) + 1)->while_body = ip->lasti; (void) list_prepend(ip, (yyvsp[-5])); - }/* else - $1 is NULL */ + } + /* else + $1 is NULL */ + + if ((yyvsp[-1]) != NULL) { + if ((yyvsp[0]) == NULL) + (yyvsp[0]) = list_create(instruction(Op_no_op)); + + (yyvsp[-1])->memory->comment_type = BLOCK_COMMENT; + (yyvsp[0]) = list_prepend((yyvsp[0]), (yyvsp[-1])); + } if ((yyvsp[0]) != NULL) (void) list_merge(ip, (yyvsp[0])); @@ -2521,11 +2545,11 @@ yyreduce: continue_allowed--; fix_break_continue(ip, tbreak, tcont); } -#line 2525 "awkgram.c" /* yacc.c:1645 */ +#line 2549 "awkgram.c" /* yacc.c:1645 */ break; case 49: -#line 750 "awkgram.y" /* yacc.c:1645 */ +#line 773 "awkgram.y" /* yacc.c:1645 */ { /* * ----------------- @@ -2548,8 +2572,13 @@ yyreduce: ip = list_merge((yyvsp[-5]), (yyvsp[-2])); else ip = list_prepend((yyvsp[-2]), instruction(Op_no_op)); + + if ((yyvsp[-6]) != NULL) + (void) list_prepend(ip, (yyvsp[-6])); + if (do_pretty_print) (void) list_prepend(ip, instruction(Op_exec_count)); + (void) list_append(ip, instruction(Op_jmp_true)); ip->lasti->target_jmp = ip->nexti; (yyval) = list_append(ip, tbreak); @@ -2564,14 +2593,17 @@ yyreduce: ((yyvsp[-7]) + 1)->doloop_cond = tcont; (yyval) = list_prepend(ip, (yyvsp[-7])); bcfree((yyvsp[-4])); - } /* else + if ((yyvsp[0]) != NULL) + (yyvsp[-7])->comment = (yyvsp[0]); + } + /* else $1 and $4 are NULLs */ } -#line 2571 "awkgram.c" /* yacc.c:1645 */ +#line 2603 "awkgram.c" /* yacc.c:1645 */ break; case 50: -#line 792 "awkgram.y" /* yacc.c:1645 */ +#line 823 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *ip; char *var_name = (yyvsp[-5])->lextok; @@ -2584,7 +2616,8 @@ yyreduce: && strcmp((yyvsp[0])->nexti->memory->vname, var_name) == 0 ) { - /* Efficiency hack. Recognize the special case of + /* + * Efficiency hack. Recognize the special case of * * for (iggy in foo) * delete foo[iggy] @@ -2616,6 +2649,10 @@ yyreduce: bcfree((yyvsp[-5])); bcfree((yyvsp[-4])); bcfree((yyvsp[-3])); + if ((yyvsp[-1]) != NULL) { + merge_comments((yyvsp[-1]), NULL); + (yyvsp[0]) = list_prepend((yyvsp[0]), (yyvsp[-1])); + } (yyval) = (yyvsp[0]); } else goto regular_loop; @@ -2650,8 +2687,9 @@ regular_loop: (yyvsp[-7])->target_continue = tcont; (yyvsp[-7])->target_break = tbreak; (void) list_append(ip, (yyvsp[-7])); - } /* else - $1 is NULL */ + } + /* else + $1 is NULL */ /* add update_FOO instruction if necessary */ if ((yyvsp[-4])->array_var->type == Node_var && (yyvsp[-4])->array_var->var_update) { @@ -2672,8 +2710,15 @@ regular_loop: ((yyvsp[-7]) + 1)->forloop_body = ip->lasti; } - if ((yyvsp[0]) != NULL) + if ((yyvsp[-1]) != NULL) + merge_comments((yyvsp[-1]), NULL); + + if ((yyvsp[0]) != NULL) { + if ((yyvsp[-1]) != NULL) + (yyvsp[0]) = list_prepend((yyvsp[0]), (yyvsp[-1])); (void) list_merge(ip, (yyvsp[0])); + } else if ((yyvsp[-1]) != NULL) + (void) list_append(ip, (yyvsp[-1])); (void) list_append(ip, instruction(Op_jmp)); ip->lasti->target_jmp = (yyvsp[-4]); @@ -2684,73 +2729,100 @@ regular_loop: break_allowed--; continue_allowed--; } -#line 2688 "awkgram.c" /* yacc.c:1645 */ +#line 2733 "awkgram.c" /* yacc.c:1645 */ break; case 51: -#line 905 "awkgram.y" /* yacc.c:1645 */ +#line 949 "awkgram.y" /* yacc.c:1645 */ { + if ((yyvsp[-7]) != NULL) { + merge_comments((yyvsp[-7]), NULL); + (yyvsp[-11])->comment = (yyvsp[-7]); + } + if ((yyvsp[-4]) != NULL) { + merge_comments((yyvsp[-4]), NULL); + if ((yyvsp[-11])->comment == NULL) { + (yyvsp[-4])->memory->comment_type = FOR_COMMENT; + (yyvsp[-11])->comment = (yyvsp[-4]); + } else + (yyvsp[-11])->comment->comment = (yyvsp[-4]); + } + if ((yyvsp[-1]) != NULL) + (yyvsp[0]) = list_prepend((yyvsp[0]), (yyvsp[-1])); (yyval) = mk_for_loop((yyvsp[-11]), (yyvsp[-9]), (yyvsp[-6]), (yyvsp[-3]), (yyvsp[0])); break_allowed--; continue_allowed--; } -#line 2699 "awkgram.c" /* yacc.c:1645 */ +#line 2758 "awkgram.c" /* yacc.c:1645 */ break; case 52: -#line 912 "awkgram.y" /* yacc.c:1645 */ +#line 970 "awkgram.y" /* yacc.c:1645 */ { + if ((yyvsp[-6]) != NULL) { + merge_comments((yyvsp[-6]), NULL); + (yyvsp[-10])->comment = (yyvsp[-6]); + } + if ((yyvsp[-4]) != NULL) { + merge_comments((yyvsp[-4]), NULL); + if ((yyvsp[-10])->comment == NULL) { + (yyvsp[-4])->memory->comment_type = FOR_COMMENT; + (yyvsp[-10])->comment = (yyvsp[-4]); + } else + (yyvsp[-10])->comment->comment = (yyvsp[-4]); + } + if ((yyvsp[-1]) != NULL) + (yyvsp[0]) = list_prepend((yyvsp[0]), (yyvsp[-1])); (yyval) = mk_for_loop((yyvsp[-10]), (yyvsp[-8]), (INSTRUCTION *) NULL, (yyvsp[-3]), (yyvsp[0])); break_allowed--; continue_allowed--; } -#line 2710 "awkgram.c" /* yacc.c:1645 */ +#line 2783 "awkgram.c" /* yacc.c:1645 */ break; case 53: -#line 919 "awkgram.y" /* yacc.c:1645 */ +#line 991 "awkgram.y" /* yacc.c:1645 */ { if (do_pretty_print) (yyval) = list_prepend((yyvsp[0]), instruction(Op_exec_count)); else (yyval) = (yyvsp[0]); - (yyval) = add_pending_comment((yyval)); } -#line 2722 "awkgram.c" /* yacc.c:1645 */ +#line 2794 "awkgram.c" /* yacc.c:1645 */ break; case 54: -#line 930 "awkgram.y" /* yacc.c:1645 */ +#line 1001 "awkgram.y" /* yacc.c:1645 */ { if (! break_allowed) error_ln((yyvsp[-1])->source_line, _("`break' is not allowed outside a loop or switch")); (yyvsp[-1])->target_jmp = NULL; (yyval) = list_create((yyvsp[-1])); - (yyval) = add_pending_comment((yyval)); - + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyval), (yyvsp[0])); } -#line 2736 "awkgram.c" /* yacc.c:1645 */ +#line 2808 "awkgram.c" /* yacc.c:1645 */ break; case 55: -#line 940 "awkgram.y" /* yacc.c:1645 */ +#line 1011 "awkgram.y" /* yacc.c:1645 */ { if (! continue_allowed) error_ln((yyvsp[-1])->source_line, _("`continue' is not allowed outside a loop")); (yyvsp[-1])->target_jmp = NULL; (yyval) = list_create((yyvsp[-1])); - (yyval) = add_pending_comment((yyval)); - + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyval), (yyvsp[0])); } -#line 2750 "awkgram.c" /* yacc.c:1645 */ +#line 2822 "awkgram.c" /* yacc.c:1645 */ break; case 56: -#line 950 "awkgram.y" /* yacc.c:1645 */ +#line 1021 "awkgram.y" /* yacc.c:1645 */ { /* if inside function (rule = 0), resolve context at run-time */ if (rule && rule != Rule) @@ -2758,13 +2830,14 @@ regular_loop: _("`next' used in %s action"), ruletab[rule]); (yyvsp[-1])->target_jmp = ip_rec; (yyval) = list_create((yyvsp[-1])); - (yyval) = add_pending_comment((yyval)); + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyval), (yyvsp[0])); } -#line 2764 "awkgram.c" /* yacc.c:1645 */ +#line 2837 "awkgram.c" /* yacc.c:1645 */ break; case 57: -#line 960 "awkgram.y" /* yacc.c:1645 */ +#line 1032 "awkgram.y" /* yacc.c:1645 */ { /* if inside function (rule = 0), resolve context at run-time */ if (rule == BEGIN || rule == END || rule == ENDFILE) @@ -2774,13 +2847,14 @@ regular_loop: (yyvsp[-1])->target_newfile = ip_newfile; (yyvsp[-1])->target_endfile = ip_endfile; (yyval) = list_create((yyvsp[-1])); - (yyval) = add_pending_comment((yyval)); + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyval), (yyvsp[0])); } -#line 2780 "awkgram.c" /* yacc.c:1645 */ +#line 2854 "awkgram.c" /* yacc.c:1645 */ break; case 58: -#line 972 "awkgram.y" /* yacc.c:1645 */ +#line 1045 "awkgram.y" /* yacc.c:1645 */ { /* Initialize the two possible jump targets, the actual target * is resolved at run-time. @@ -2794,22 +2868,23 @@ regular_loop: (yyval)->nexti->memory = dupnode(Nnull_string); } else (yyval) = list_append((yyvsp[-1]), (yyvsp[-2])); - (yyval) = add_pending_comment((yyval)); + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyval), (yyvsp[0])); } -#line 2800 "awkgram.c" /* yacc.c:1645 */ +#line 2875 "awkgram.c" /* yacc.c:1645 */ break; case 59: -#line 988 "awkgram.y" /* yacc.c:1645 */ +#line 1062 "awkgram.y" /* yacc.c:1645 */ { if (! in_function) yyerror(_("`return' used outside function context")); } -#line 2809 "awkgram.c" /* yacc.c:1645 */ +#line 2884 "awkgram.c" /* yacc.c:1645 */ break; case 60: -#line 991 "awkgram.y" /* yacc.c:1645 */ +#line 1065 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[-1]) == NULL) { (yyval) = list_create((yyvsp[-3])); @@ -2817,20 +2892,31 @@ regular_loop: (yyval)->nexti->memory = dupnode(Nnull_string); } else (yyval) = list_append((yyvsp[-1]), (yyvsp[-3])); + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyval), (yyvsp[0])); + } +#line 2899 "awkgram.c" /* yacc.c:1645 */ + break; - (yyval) = add_pending_comment((yyval)); + case 61: +#line 1076 "awkgram.y" /* yacc.c:1645 */ + { + if ((yyvsp[0]) != NULL) + (yyval) = list_append((yyvsp[-1]), (yyvsp[0])); + else + (yyval) = (yyvsp[-1]); } -#line 2824 "awkgram.c" /* yacc.c:1645 */ +#line 2910 "awkgram.c" /* yacc.c:1645 */ break; case 62: -#line 1013 "awkgram.y" /* yacc.c:1645 */ +#line 1093 "awkgram.y" /* yacc.c:1645 */ { in_print = true; in_parens = 0; } -#line 2830 "awkgram.c" /* yacc.c:1645 */ +#line 2916 "awkgram.c" /* yacc.c:1645 */ break; case 63: -#line 1014 "awkgram.y" /* yacc.c:1645 */ +#line 1094 "awkgram.y" /* yacc.c:1645 */ { /* * Optimization: plain `print' has no expression list, so $3 is null. @@ -2838,7 +2924,7 @@ regular_loop: * which is faster for these two cases. */ - if ((yyvsp[-3])->opcode == Op_K_print && + if (do_optimize && (yyvsp[-3])->opcode == Op_K_print && ((yyvsp[-1]) == NULL || ((yyvsp[-1])->lasti->opcode == Op_field_spec && (yyvsp[-1])->nexti->nexti->nexti == (yyvsp[-1])->lasti @@ -2926,19 +3012,18 @@ regular_print: } } } - (yyval) = add_pending_comment((yyval)); } -#line 2932 "awkgram.c" /* yacc.c:1645 */ +#line 3017 "awkgram.c" /* yacc.c:1645 */ break; case 64: -#line 1112 "awkgram.y" /* yacc.c:1645 */ +#line 1191 "awkgram.y" /* yacc.c:1645 */ { sub_counter = 0; } -#line 2938 "awkgram.c" /* yacc.c:1645 */ +#line 3023 "awkgram.c" /* yacc.c:1645 */ break; case 65: -#line 1113 "awkgram.y" /* yacc.c:1645 */ +#line 1192 "awkgram.y" /* yacc.c:1645 */ { char *arr = (yyvsp[-2])->lextok; @@ -2970,13 +3055,12 @@ regular_print: (yyvsp[-3])->expr_count = sub_counter; (yyval) = list_append(list_append((yyvsp[0]), (yyvsp[-2])), (yyvsp[-3])); } - (yyval) = add_pending_comment((yyval)); } -#line 2976 "awkgram.c" /* yacc.c:1645 */ +#line 3060 "awkgram.c" /* yacc.c:1645 */ break; case 66: -#line 1151 "awkgram.y" /* yacc.c:1645 */ +#line 1229 "awkgram.y" /* yacc.c:1645 */ { static bool warned = false; char *arr = (yyvsp[-1])->lextok; @@ -3001,57 +3085,55 @@ regular_print: else if ((yyvsp[-1])->memory == func_table) fatal(_("`delete' is not allowed with FUNCTAB")); } - (yyval) = add_pending_comment((yyval)); } -#line 3007 "awkgram.c" /* yacc.c:1645 */ +#line 3090 "awkgram.c" /* yacc.c:1645 */ break; case 67: -#line 1178 "awkgram.y" /* yacc.c:1645 */ +#line 1255 "awkgram.y" /* yacc.c:1645 */ { (yyval) = optimize_assignment((yyvsp[0])); - (yyval) = add_pending_comment((yyval)); } -#line 3016 "awkgram.c" /* yacc.c:1645 */ +#line 3098 "awkgram.c" /* yacc.c:1645 */ break; case 68: -#line 1186 "awkgram.y" /* yacc.c:1645 */ +#line 1262 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3022 "awkgram.c" /* yacc.c:1645 */ +#line 3104 "awkgram.c" /* yacc.c:1645 */ break; case 69: -#line 1188 "awkgram.y" /* yacc.c:1645 */ +#line 1264 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3028 "awkgram.c" /* yacc.c:1645 */ +#line 3110 "awkgram.c" /* yacc.c:1645 */ break; case 70: -#line 1193 "awkgram.y" /* yacc.c:1645 */ +#line 1269 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3034 "awkgram.c" /* yacc.c:1645 */ +#line 3116 "awkgram.c" /* yacc.c:1645 */ break; case 71: -#line 1195 "awkgram.y" /* yacc.c:1645 */ +#line 1271 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[-1]) == NULL) (yyval) = list_create((yyvsp[0])); else (yyval) = list_prepend((yyvsp[-1]), (yyvsp[0])); } -#line 3045 "awkgram.c" /* yacc.c:1645 */ +#line 3127 "awkgram.c" /* yacc.c:1645 */ break; case 72: -#line 1202 "awkgram.y" /* yacc.c:1645 */ +#line 1278 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3051 "awkgram.c" /* yacc.c:1645 */ +#line 3133 "awkgram.c" /* yacc.c:1645 */ break; case 73: -#line 1207 "awkgram.y" /* yacc.c:1645 */ +#line 1283 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *casestmt = (yyvsp[0]); if ((yyvsp[0]) == NULL) @@ -3060,14 +3142,15 @@ regular_print: (void) list_prepend(casestmt, instruction(Op_exec_count)); (yyvsp[-4])->case_exp = (yyvsp[-3]); (yyvsp[-4])->case_stmt = casestmt; + (yyvsp[-4])->comment = (yyvsp[-1]); bcfree((yyvsp[-2])); (yyval) = (yyvsp[-4]); } -#line 3067 "awkgram.c" /* yacc.c:1645 */ +#line 3150 "awkgram.c" /* yacc.c:1645 */ break; case 74: -#line 1219 "awkgram.y" /* yacc.c:1645 */ +#line 1296 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *casestmt = (yyvsp[0]); if ((yyvsp[0]) == NULL) @@ -3076,19 +3159,20 @@ regular_print: (void) list_prepend(casestmt, instruction(Op_exec_count)); bcfree((yyvsp[-2])); (yyvsp[-3])->case_stmt = casestmt; + (yyvsp[-3])->comment = (yyvsp[-1]); (yyval) = (yyvsp[-3]); } -#line 3082 "awkgram.c" /* yacc.c:1645 */ +#line 3166 "awkgram.c" /* yacc.c:1645 */ break; case 75: -#line 1233 "awkgram.y" /* yacc.c:1645 */ +#line 1311 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3088 "awkgram.c" /* yacc.c:1645 */ +#line 3172 "awkgram.c" /* yacc.c:1645 */ break; case 76: -#line 1235 "awkgram.y" /* yacc.c:1645 */ +#line 1313 "awkgram.y" /* yacc.c:1645 */ { NODE *n = (yyvsp[0])->memory; (void) force_number(n); @@ -3096,28 +3180,28 @@ regular_print: bcfree((yyvsp[-1])); (yyval) = (yyvsp[0]); } -#line 3100 "awkgram.c" /* yacc.c:1645 */ +#line 3184 "awkgram.c" /* yacc.c:1645 */ break; case 77: -#line 1243 "awkgram.y" /* yacc.c:1645 */ +#line 1321 "awkgram.y" /* yacc.c:1645 */ { NODE *n = (yyvsp[0])->lasti->memory; bcfree((yyvsp[-1])); add_sign_to_num(n, '+'); (yyval) = (yyvsp[0]); } -#line 3111 "awkgram.c" /* yacc.c:1645 */ +#line 3195 "awkgram.c" /* yacc.c:1645 */ break; case 78: -#line 1250 "awkgram.y" /* yacc.c:1645 */ +#line 1328 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3117 "awkgram.c" /* yacc.c:1645 */ +#line 3201 "awkgram.c" /* yacc.c:1645 */ break; case 79: -#line 1252 "awkgram.y" /* yacc.c:1645 */ +#line 1330 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[0])->memory->type == Node_regex) (yyvsp[0])->opcode = Op_push_re; @@ -3125,57 +3209,57 @@ regular_print: (yyvsp[0])->opcode = Op_push; (yyval) = (yyvsp[0]); } -#line 3129 "awkgram.c" /* yacc.c:1645 */ +#line 3213 "awkgram.c" /* yacc.c:1645 */ break; case 80: -#line 1260 "awkgram.y" /* yacc.c:1645 */ +#line 1338 "awkgram.y" /* yacc.c:1645 */ { assert(((yyvsp[0])->memory->flags & REGEX) == REGEX); (yyvsp[0])->opcode = Op_push_re; (yyval) = (yyvsp[0]); } -#line 3139 "awkgram.c" /* yacc.c:1645 */ +#line 3223 "awkgram.c" /* yacc.c:1645 */ break; case 81: -#line 1269 "awkgram.y" /* yacc.c:1645 */ +#line 1347 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3145 "awkgram.c" /* yacc.c:1645 */ +#line 3229 "awkgram.c" /* yacc.c:1645 */ break; case 82: -#line 1271 "awkgram.y" /* yacc.c:1645 */ +#line 1349 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3151 "awkgram.c" /* yacc.c:1645 */ +#line 3235 "awkgram.c" /* yacc.c:1645 */ break; case 84: -#line 1281 "awkgram.y" /* yacc.c:1645 */ +#line 1359 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[-1]); } -#line 3159 "awkgram.c" /* yacc.c:1645 */ +#line 3243 "awkgram.c" /* yacc.c:1645 */ break; case 85: -#line 1288 "awkgram.y" /* yacc.c:1645 */ +#line 1366 "awkgram.y" /* yacc.c:1645 */ { in_print = false; in_parens = 0; (yyval) = NULL; } -#line 3169 "awkgram.c" /* yacc.c:1645 */ +#line 3253 "awkgram.c" /* yacc.c:1645 */ break; case 86: -#line 1293 "awkgram.y" /* yacc.c:1645 */ +#line 1371 "awkgram.y" /* yacc.c:1645 */ { in_print = false; in_parens = 0; } -#line 3175 "awkgram.c" /* yacc.c:1645 */ +#line 3259 "awkgram.c" /* yacc.c:1645 */ break; case 87: -#line 1294 "awkgram.y" /* yacc.c:1645 */ +#line 1372 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[-2])->redir_type == redirect_twoway && (yyvsp[0])->lasti->opcode == Op_K_getline_redir @@ -3185,139 +3269,197 @@ regular_print: lintwarn(_("concatenation as I/O `>' redirection target is ambiguous")); (yyval) = list_prepend((yyvsp[0]), (yyvsp[-2])); } -#line 3189 "awkgram.c" /* yacc.c:1645 */ +#line 3273 "awkgram.c" /* yacc.c:1645 */ break; case 88: -#line 1307 "awkgram.y" /* yacc.c:1645 */ +#line 1385 "awkgram.y" /* yacc.c:1645 */ { + if ((yyvsp[-1]) != NULL) + (yyvsp[-5])->comment = (yyvsp[-1]); (yyval) = mk_condition((yyvsp[-3]), (yyvsp[-5]), (yyvsp[0]), NULL, NULL); } -#line 3197 "awkgram.c" /* yacc.c:1645 */ +#line 3283 "awkgram.c" /* yacc.c:1645 */ break; case 89: -#line 1312 "awkgram.y" /* yacc.c:1645 */ +#line 1392 "awkgram.y" /* yacc.c:1645 */ { + if ((yyvsp[-4]) != NULL) + (yyvsp[-8])->comment = (yyvsp[-4]); + if ((yyvsp[-1]) != NULL) + (yyvsp[-2])->comment = (yyvsp[-1]); (yyval) = mk_condition((yyvsp[-6]), (yyvsp[-8]), (yyvsp[-3]), (yyvsp[-2]), (yyvsp[0])); } -#line 3205 "awkgram.c" /* yacc.c:1645 */ +#line 3295 "awkgram.c" /* yacc.c:1645 */ + break; + + case 90: +#line 1403 "awkgram.y" /* yacc.c:1645 */ + { + (yyval) = (yyvsp[0]); + } +#line 3303 "awkgram.c" /* yacc.c:1645 */ + break; + + case 91: +#line 1407 "awkgram.y" /* yacc.c:1645 */ + { + if ((yyvsp[-1]) != NULL && (yyvsp[0]) != NULL) { + if ((yyvsp[-1])->memory->comment_type == EOL_COMMENT) { + assert((yyvsp[0])->memory->comment_type == BLOCK_COMMENT); + (yyvsp[-1])->comment = (yyvsp[0]); // chain them + } else { + merge_comments((yyvsp[-1]), (yyvsp[0])); + } + + (yyval) = (yyvsp[-1]); + } else if ((yyvsp[-1]) != NULL) { + (yyval) = (yyvsp[-1]); + } else if ((yyvsp[0]) != NULL) { + (yyval) = (yyvsp[0]); + } else + (yyval) = NULL; + } +#line 3325 "awkgram.c" /* yacc.c:1645 */ + break; + + case 92: +#line 1428 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = NULL; } +#line 3331 "awkgram.c" /* yacc.c:1645 */ + break; + + case 93: +#line 1430 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = (yyvsp[0]); } +#line 3337 "awkgram.c" /* yacc.c:1645 */ break; case 94: -#line 1329 "awkgram.y" /* yacc.c:1645 */ +#line 1435 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3211 "awkgram.c" /* yacc.c:1645 */ +#line 3343 "awkgram.c" /* yacc.c:1645 */ break; case 95: -#line 1331 "awkgram.y" /* yacc.c:1645 */ +#line 1437 "awkgram.y" /* yacc.c:1645 */ { bcfree((yyvsp[-1])); (yyval) = (yyvsp[0]); } -#line 3220 "awkgram.c" /* yacc.c:1645 */ +#line 3352 "awkgram.c" /* yacc.c:1645 */ break; case 96: -#line 1339 "awkgram.y" /* yacc.c:1645 */ +#line 1445 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3226 "awkgram.c" /* yacc.c:1645 */ +#line 3358 "awkgram.c" /* yacc.c:1645 */ break; case 97: -#line 1341 "awkgram.y" /* yacc.c:1645 */ +#line 1447 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3232 "awkgram.c" /* yacc.c:1645 */ +#line 3364 "awkgram.c" /* yacc.c:1645 */ break; case 98: -#line 1346 "awkgram.y" /* yacc.c:1645 */ +#line 1452 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[0])->param_count = 0; (yyval) = list_create((yyvsp[0])); } -#line 3241 "awkgram.c" /* yacc.c:1645 */ +#line 3373 "awkgram.c" /* yacc.c:1645 */ break; case 99: -#line 1351 "awkgram.y" /* yacc.c:1645 */ +#line 1457 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[-2]) != NULL && (yyvsp[0]) != NULL) { - (yyvsp[0])->param_count = (yyvsp[-2])->lasti->param_count + 1; + (yyvsp[0])->param_count = (yyvsp[-2])->lasti->param_count + 1; (yyval) = list_append((yyvsp[-2]), (yyvsp[0])); yyerrok; + + // newlines are allowed after commas, catch any comments + if ((yyvsp[-1]) != NULL) { + if ((yyvsp[-2])->comment != NULL) + merge_comments((yyvsp[-2])->comment, (yyvsp[-1])); + else + (yyvsp[-2])->comment = (yyvsp[-1]); + } } else (yyval) = NULL; } -#line 3254 "awkgram.c" /* yacc.c:1645 */ +#line 3394 "awkgram.c" /* yacc.c:1645 */ break; case 100: -#line 1360 "awkgram.y" /* yacc.c:1645 */ +#line 1474 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3260 "awkgram.c" /* yacc.c:1645 */ +#line 3400 "awkgram.c" /* yacc.c:1645 */ break; case 101: -#line 1362 "awkgram.y" /* yacc.c:1645 */ +#line 1476 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[-1]); } -#line 3266 "awkgram.c" /* yacc.c:1645 */ +#line 3406 "awkgram.c" /* yacc.c:1645 */ break; case 102: -#line 1364 "awkgram.y" /* yacc.c:1645 */ +#line 1478 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[-2]); } -#line 3272 "awkgram.c" /* yacc.c:1645 */ +#line 3412 "awkgram.c" /* yacc.c:1645 */ break; case 103: -#line 1370 "awkgram.y" /* yacc.c:1645 */ +#line 1484 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3278 "awkgram.c" /* yacc.c:1645 */ +#line 3418 "awkgram.c" /* yacc.c:1645 */ break; case 104: -#line 1372 "awkgram.y" /* yacc.c:1645 */ +#line 1486 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3284 "awkgram.c" /* yacc.c:1645 */ +#line 3424 "awkgram.c" /* yacc.c:1645 */ break; case 105: -#line 1377 "awkgram.y" /* yacc.c:1645 */ +#line 1491 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3290 "awkgram.c" /* yacc.c:1645 */ +#line 3430 "awkgram.c" /* yacc.c:1645 */ break; case 106: -#line 1379 "awkgram.y" /* yacc.c:1645 */ +#line 1493 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3296 "awkgram.c" /* yacc.c:1645 */ +#line 3436 "awkgram.c" /* yacc.c:1645 */ break; case 107: -#line 1384 "awkgram.y" /* yacc.c:1645 */ +#line 1498 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_expression_list(NULL, (yyvsp[0])); } -#line 3302 "awkgram.c" /* yacc.c:1645 */ +#line 3442 "awkgram.c" /* yacc.c:1645 */ break; case 108: -#line 1386 "awkgram.y" /* yacc.c:1645 */ +#line 1500 "awkgram.y" /* yacc.c:1645 */ { + if ((yyvsp[-1]) != NULL) + (yyvsp[-2])->lasti->comment = (yyvsp[-1]); (yyval) = mk_expression_list((yyvsp[-2]), (yyvsp[0])); yyerrok; } -#line 3311 "awkgram.c" /* yacc.c:1645 */ +#line 3453 "awkgram.c" /* yacc.c:1645 */ break; case 109: -#line 1391 "awkgram.y" /* yacc.c:1645 */ +#line 1507 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3317 "awkgram.c" /* yacc.c:1645 */ +#line 3459 "awkgram.c" /* yacc.c:1645 */ break; case 110: -#line 1393 "awkgram.y" /* yacc.c:1645 */ +#line 1509 "awkgram.y" /* yacc.c:1645 */ { /* * Returning the expression list instead of NULL lets @@ -3325,62 +3467,66 @@ regular_print: */ (yyval) = (yyvsp[-1]); } -#line 3329 "awkgram.c" /* yacc.c:1645 */ +#line 3471 "awkgram.c" /* yacc.c:1645 */ break; case 111: -#line 1401 "awkgram.y" /* yacc.c:1645 */ +#line 1517 "awkgram.y" /* yacc.c:1645 */ { /* Ditto */ (yyval) = mk_expression_list((yyvsp[-2]), (yyvsp[0])); } -#line 3338 "awkgram.c" /* yacc.c:1645 */ +#line 3480 "awkgram.c" /* yacc.c:1645 */ break; case 112: -#line 1406 "awkgram.y" /* yacc.c:1645 */ +#line 1522 "awkgram.y" /* yacc.c:1645 */ { /* Ditto */ + if ((yyvsp[-1]) != NULL) + (yyvsp[-2])->lasti->comment = (yyvsp[-1]); (yyval) = (yyvsp[-2]); } -#line 3347 "awkgram.c" /* yacc.c:1645 */ +#line 3491 "awkgram.c" /* yacc.c:1645 */ break; case 113: -#line 1414 "awkgram.y" /* yacc.c:1645 */ +#line 1532 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3353 "awkgram.c" /* yacc.c:1645 */ +#line 3497 "awkgram.c" /* yacc.c:1645 */ break; case 114: -#line 1416 "awkgram.y" /* yacc.c:1645 */ +#line 1534 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3359 "awkgram.c" /* yacc.c:1645 */ +#line 3503 "awkgram.c" /* yacc.c:1645 */ break; case 115: -#line 1421 "awkgram.y" /* yacc.c:1645 */ +#line 1539 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_expression_list(NULL, (yyvsp[0])); } -#line 3365 "awkgram.c" /* yacc.c:1645 */ +#line 3509 "awkgram.c" /* yacc.c:1645 */ break; case 116: -#line 1423 "awkgram.y" /* yacc.c:1645 */ +#line 1541 "awkgram.y" /* yacc.c:1645 */ { + if ((yyvsp[-1]) != NULL) + (yyvsp[-2])->lasti->comment = (yyvsp[-1]); (yyval) = mk_expression_list((yyvsp[-2]), (yyvsp[0])); yyerrok; } -#line 3374 "awkgram.c" /* yacc.c:1645 */ +#line 3520 "awkgram.c" /* yacc.c:1645 */ break; case 117: -#line 1428 "awkgram.y" /* yacc.c:1645 */ +#line 1548 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 3380 "awkgram.c" /* yacc.c:1645 */ +#line 3526 "awkgram.c" /* yacc.c:1645 */ break; case 118: -#line 1430 "awkgram.y" /* yacc.c:1645 */ +#line 1550 "awkgram.y" /* yacc.c:1645 */ { /* * Returning the expression list instead of NULL lets @@ -3388,72 +3534,74 @@ regular_print: */ (yyval) = (yyvsp[-1]); } -#line 3392 "awkgram.c" /* yacc.c:1645 */ +#line 3538 "awkgram.c" /* yacc.c:1645 */ break; case 119: -#line 1438 "awkgram.y" /* yacc.c:1645 */ +#line 1558 "awkgram.y" /* yacc.c:1645 */ { /* Ditto */ (yyval) = mk_expression_list((yyvsp[-2]), (yyvsp[0])); } -#line 3401 "awkgram.c" /* yacc.c:1645 */ +#line 3547 "awkgram.c" /* yacc.c:1645 */ break; case 120: -#line 1443 "awkgram.y" /* yacc.c:1645 */ +#line 1563 "awkgram.y" /* yacc.c:1645 */ { /* Ditto */ + if ((yyvsp[-1]) != NULL) + (yyvsp[-2])->comment = (yyvsp[-1]); (yyval) = (yyvsp[-2]); } -#line 3410 "awkgram.c" /* yacc.c:1645 */ +#line 3558 "awkgram.c" /* yacc.c:1645 */ break; case 121: -#line 1450 "awkgram.y" /* yacc.c:1645 */ +#line 1572 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3416 "awkgram.c" /* yacc.c:1645 */ +#line 3564 "awkgram.c" /* yacc.c:1645 */ break; case 122: -#line 1451 "awkgram.y" /* yacc.c:1645 */ +#line 1573 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_create((yyvsp[0])); } -#line 3422 "awkgram.c" /* yacc.c:1645 */ +#line 3570 "awkgram.c" /* yacc.c:1645 */ break; case 123: -#line 1457 "awkgram.y" /* yacc.c:1645 */ +#line 1579 "awkgram.y" /* yacc.c:1645 */ { if (do_lint && (yyvsp[0])->lasti->opcode == Op_match_rec) lintwarn_ln((yyvsp[-1])->source_line, _("regular expression on right of assignment")); (yyval) = mk_assignment((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3433 "awkgram.c" /* yacc.c:1645 */ +#line 3581 "awkgram.c" /* yacc.c:1645 */ break; case 124: -#line 1464 "awkgram.y" /* yacc.c:1645 */ +#line 1586 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_assignment((yyvsp[-2]), list_create((yyvsp[0])), (yyvsp[-1])); } -#line 3441 "awkgram.c" /* yacc.c:1645 */ +#line 3589 "awkgram.c" /* yacc.c:1645 */ break; case 125: -#line 1468 "awkgram.y" /* yacc.c:1645 */ +#line 1590 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_boolean((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3447 "awkgram.c" /* yacc.c:1645 */ +#line 3595 "awkgram.c" /* yacc.c:1645 */ break; case 126: -#line 1470 "awkgram.y" /* yacc.c:1645 */ +#line 1592 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_boolean((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3453 "awkgram.c" /* yacc.c:1645 */ +#line 3601 "awkgram.c" /* yacc.c:1645 */ break; case 127: -#line 1472 "awkgram.y" /* yacc.c:1645 */ +#line 1594 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[-2])->lasti->opcode == Op_match_rec) warning_ln((yyvsp[-1])->source_line, @@ -3466,11 +3614,11 @@ regular_print: bcfree((yyvsp[0])); (yyval) = list_append((yyvsp[-2]), (yyvsp[-1])); } -#line 3470 "awkgram.c" /* yacc.c:1645 */ +#line 3618 "awkgram.c" /* yacc.c:1645 */ break; case 128: -#line 1485 "awkgram.y" /* yacc.c:1645 */ +#line 1607 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[-2])->lasti->opcode == Op_match_rec) warning_ln((yyvsp[-1])->source_line, @@ -3487,11 +3635,11 @@ regular_print: (yyval) = list_append(list_merge((yyvsp[-2]), (yyvsp[0])), (yyvsp[-1])); } } -#line 3491 "awkgram.c" /* yacc.c:1645 */ +#line 3639 "awkgram.c" /* yacc.c:1645 */ break; case 129: -#line 1502 "awkgram.y" /* yacc.c:1645 */ +#line 1624 "awkgram.y" /* yacc.c:1645 */ { if (do_lint_old) warning_ln((yyvsp[-1])->source_line, @@ -3501,91 +3649,91 @@ regular_print: (yyvsp[-1])->expr_count = 1; (yyval) = list_append(list_merge((yyvsp[-2]), (yyvsp[0])), (yyvsp[-1])); } -#line 3505 "awkgram.c" /* yacc.c:1645 */ +#line 3653 "awkgram.c" /* yacc.c:1645 */ break; case 130: -#line 1512 "awkgram.y" /* yacc.c:1645 */ +#line 1634 "awkgram.y" /* yacc.c:1645 */ { if (do_lint && (yyvsp[0])->lasti->opcode == Op_match_rec) lintwarn_ln((yyvsp[-1])->source_line, _("regular expression on right of comparison")); (yyval) = list_append(list_merge((yyvsp[-2]), (yyvsp[0])), (yyvsp[-1])); } -#line 3516 "awkgram.c" /* yacc.c:1645 */ +#line 3664 "awkgram.c" /* yacc.c:1645 */ break; case 131: -#line 1519 "awkgram.y" /* yacc.c:1645 */ +#line 1641 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_condition((yyvsp[-4]), (yyvsp[-3]), (yyvsp[-2]), (yyvsp[-1]), (yyvsp[0])); } -#line 3522 "awkgram.c" /* yacc.c:1645 */ +#line 3670 "awkgram.c" /* yacc.c:1645 */ break; case 132: -#line 1521 "awkgram.y" /* yacc.c:1645 */ +#line 1643 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3528 "awkgram.c" /* yacc.c:1645 */ +#line 3676 "awkgram.c" /* yacc.c:1645 */ break; case 133: -#line 1526 "awkgram.y" /* yacc.c:1645 */ +#line 1648 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3534 "awkgram.c" /* yacc.c:1645 */ +#line 3682 "awkgram.c" /* yacc.c:1645 */ break; case 134: -#line 1528 "awkgram.y" /* yacc.c:1645 */ +#line 1650 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3540 "awkgram.c" /* yacc.c:1645 */ +#line 3688 "awkgram.c" /* yacc.c:1645 */ break; case 135: -#line 1530 "awkgram.y" /* yacc.c:1645 */ +#line 1652 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[0])->opcode = Op_assign_quotient; (yyval) = (yyvsp[0]); } -#line 3549 "awkgram.c" /* yacc.c:1645 */ +#line 3697 "awkgram.c" /* yacc.c:1645 */ break; case 136: -#line 1538 "awkgram.y" /* yacc.c:1645 */ +#line 1660 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3555 "awkgram.c" /* yacc.c:1645 */ +#line 3703 "awkgram.c" /* yacc.c:1645 */ break; case 137: -#line 1540 "awkgram.y" /* yacc.c:1645 */ +#line 1662 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3561 "awkgram.c" /* yacc.c:1645 */ +#line 3709 "awkgram.c" /* yacc.c:1645 */ break; case 138: -#line 1545 "awkgram.y" /* yacc.c:1645 */ +#line 1667 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3567 "awkgram.c" /* yacc.c:1645 */ +#line 3715 "awkgram.c" /* yacc.c:1645 */ break; case 139: -#line 1547 "awkgram.y" /* yacc.c:1645 */ +#line 1669 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3573 "awkgram.c" /* yacc.c:1645 */ +#line 3721 "awkgram.c" /* yacc.c:1645 */ break; case 140: -#line 1552 "awkgram.y" /* yacc.c:1645 */ +#line 1674 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3579 "awkgram.c" /* yacc.c:1645 */ +#line 3727 "awkgram.c" /* yacc.c:1645 */ break; case 141: -#line 1554 "awkgram.y" /* yacc.c:1645 */ +#line 1676 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 3585 "awkgram.c" /* yacc.c:1645 */ +#line 3733 "awkgram.c" /* yacc.c:1645 */ break; case 142: -#line 1556 "awkgram.y" /* yacc.c:1645 */ +#line 1678 "awkgram.y" /* yacc.c:1645 */ { int count = 2; bool is_simple_var = false; @@ -3639,47 +3787,47 @@ regular_print: max_args = count; } } -#line 3643 "awkgram.c" /* yacc.c:1645 */ +#line 3791 "awkgram.c" /* yacc.c:1645 */ break; case 144: -#line 1615 "awkgram.y" /* yacc.c:1645 */ +#line 1737 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3649 "awkgram.c" /* yacc.c:1645 */ +#line 3797 "awkgram.c" /* yacc.c:1645 */ break; case 145: -#line 1617 "awkgram.y" /* yacc.c:1645 */ +#line 1739 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3655 "awkgram.c" /* yacc.c:1645 */ +#line 3803 "awkgram.c" /* yacc.c:1645 */ break; case 146: -#line 1619 "awkgram.y" /* yacc.c:1645 */ +#line 1741 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3661 "awkgram.c" /* yacc.c:1645 */ +#line 3809 "awkgram.c" /* yacc.c:1645 */ break; case 147: -#line 1621 "awkgram.y" /* yacc.c:1645 */ +#line 1743 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3667 "awkgram.c" /* yacc.c:1645 */ +#line 3815 "awkgram.c" /* yacc.c:1645 */ break; case 148: -#line 1623 "awkgram.y" /* yacc.c:1645 */ +#line 1745 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3673 "awkgram.c" /* yacc.c:1645 */ +#line 3821 "awkgram.c" /* yacc.c:1645 */ break; case 149: -#line 1625 "awkgram.y" /* yacc.c:1645 */ +#line 1747 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3679 "awkgram.c" /* yacc.c:1645 */ +#line 3827 "awkgram.c" /* yacc.c:1645 */ break; case 150: -#line 1627 "awkgram.y" /* yacc.c:1645 */ +#line 1749 "awkgram.y" /* yacc.c:1645 */ { /* * In BEGINFILE/ENDFILE, allow `getline [var] < file' @@ -3693,29 +3841,29 @@ regular_print: _("non-redirected `getline' undefined inside END action")); (yyval) = mk_getline((yyvsp[-2]), (yyvsp[-1]), (yyvsp[0]), redirect_input); } -#line 3697 "awkgram.c" /* yacc.c:1645 */ +#line 3845 "awkgram.c" /* yacc.c:1645 */ break; case 151: -#line 1641 "awkgram.y" /* yacc.c:1645 */ +#line 1763 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[0])->opcode = Op_postincrement; (yyval) = mk_assignment((yyvsp[-1]), NULL, (yyvsp[0])); } -#line 3706 "awkgram.c" /* yacc.c:1645 */ +#line 3854 "awkgram.c" /* yacc.c:1645 */ break; case 152: -#line 1646 "awkgram.y" /* yacc.c:1645 */ +#line 1768 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[0])->opcode = Op_postdecrement; (yyval) = mk_assignment((yyvsp[-1]), NULL, (yyvsp[0])); } -#line 3715 "awkgram.c" /* yacc.c:1645 */ +#line 3863 "awkgram.c" /* yacc.c:1645 */ break; case 153: -#line 1651 "awkgram.y" /* yacc.c:1645 */ +#line 1773 "awkgram.y" /* yacc.c:1645 */ { if (do_lint_old) { warning_ln((yyvsp[-1])->source_line, @@ -3735,64 +3883,64 @@ regular_print: (yyval) = list_append(list_merge(t, (yyvsp[0])), (yyvsp[-1])); } } -#line 3739 "awkgram.c" /* yacc.c:1645 */ +#line 3887 "awkgram.c" /* yacc.c:1645 */ break; case 154: -#line 1676 "awkgram.y" /* yacc.c:1645 */ +#line 1798 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_getline((yyvsp[-1]), (yyvsp[0]), (yyvsp[-3]), (yyvsp[-2])->redir_type); bcfree((yyvsp[-2])); } -#line 3748 "awkgram.c" /* yacc.c:1645 */ +#line 3896 "awkgram.c" /* yacc.c:1645 */ break; case 155: -#line 1682 "awkgram.y" /* yacc.c:1645 */ +#line 1804 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3754 "awkgram.c" /* yacc.c:1645 */ +#line 3902 "awkgram.c" /* yacc.c:1645 */ break; case 156: -#line 1684 "awkgram.y" /* yacc.c:1645 */ +#line 1806 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3760 "awkgram.c" /* yacc.c:1645 */ +#line 3908 "awkgram.c" /* yacc.c:1645 */ break; case 157: -#line 1686 "awkgram.y" /* yacc.c:1645 */ +#line 1808 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3766 "awkgram.c" /* yacc.c:1645 */ +#line 3914 "awkgram.c" /* yacc.c:1645 */ break; case 158: -#line 1688 "awkgram.y" /* yacc.c:1645 */ +#line 1810 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3772 "awkgram.c" /* yacc.c:1645 */ +#line 3920 "awkgram.c" /* yacc.c:1645 */ break; case 159: -#line 1690 "awkgram.y" /* yacc.c:1645 */ +#line 1812 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3778 "awkgram.c" /* yacc.c:1645 */ +#line 3926 "awkgram.c" /* yacc.c:1645 */ break; case 160: -#line 1692 "awkgram.y" /* yacc.c:1645 */ +#line 1814 "awkgram.y" /* yacc.c:1645 */ { (yyval) = mk_binary((yyvsp[-2]), (yyvsp[0]), (yyvsp[-1])); } -#line 3784 "awkgram.c" /* yacc.c:1645 */ +#line 3932 "awkgram.c" /* yacc.c:1645 */ break; case 161: -#line 1697 "awkgram.y" /* yacc.c:1645 */ +#line 1819 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_create((yyvsp[0])); } -#line 3792 "awkgram.c" /* yacc.c:1645 */ +#line 3940 "awkgram.c" /* yacc.c:1645 */ break; case 162: -#line 1701 "awkgram.y" /* yacc.c:1645 */ +#line 1823 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[0])->opcode == Op_match_rec) { (yyvsp[0])->opcode = Op_nomatch; @@ -3824,11 +3972,11 @@ regular_print: } } } -#line 3828 "awkgram.c" /* yacc.c:1645 */ +#line 3976 "awkgram.c" /* yacc.c:1645 */ break; case 163: -#line 1733 "awkgram.y" /* yacc.c:1645 */ +#line 1855 "awkgram.y" /* yacc.c:1645 */ { // Always include. Allows us to lint warn on // print "foo" > "bar" 1 @@ -3836,31 +3984,31 @@ regular_print: // print "foo" > ("bar" 1) (yyval) = list_append((yyvsp[-1]), bcalloc(Op_parens, 1, sourceline)); } -#line 3840 "awkgram.c" /* yacc.c:1645 */ +#line 3988 "awkgram.c" /* yacc.c:1645 */ break; case 164: -#line 1741 "awkgram.y" /* yacc.c:1645 */ +#line 1863 "awkgram.y" /* yacc.c:1645 */ { (yyval) = snode((yyvsp[-1]), (yyvsp[-3])); if ((yyval) == NULL) YYABORT; } -#line 3850 "awkgram.c" /* yacc.c:1645 */ +#line 3998 "awkgram.c" /* yacc.c:1645 */ break; case 165: -#line 1747 "awkgram.y" /* yacc.c:1645 */ +#line 1869 "awkgram.y" /* yacc.c:1645 */ { (yyval) = snode((yyvsp[-1]), (yyvsp[-3])); if ((yyval) == NULL) YYABORT; } -#line 3860 "awkgram.c" /* yacc.c:1645 */ +#line 4008 "awkgram.c" /* yacc.c:1645 */ break; case 166: -#line 1753 "awkgram.y" /* yacc.c:1645 */ +#line 1875 "awkgram.y" /* yacc.c:1645 */ { static bool warned = false; @@ -3873,45 +4021,45 @@ regular_print: if ((yyval) == NULL) YYABORT; } -#line 3877 "awkgram.c" /* yacc.c:1645 */ +#line 4025 "awkgram.c" /* yacc.c:1645 */ break; case 169: -#line 1768 "awkgram.y" /* yacc.c:1645 */ +#line 1890 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[-1])->opcode = Op_preincrement; (yyval) = mk_assignment((yyvsp[0]), NULL, (yyvsp[-1])); } -#line 3886 "awkgram.c" /* yacc.c:1645 */ +#line 4034 "awkgram.c" /* yacc.c:1645 */ break; case 170: -#line 1773 "awkgram.y" /* yacc.c:1645 */ +#line 1895 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[-1])->opcode = Op_predecrement; (yyval) = mk_assignment((yyvsp[0]), NULL, (yyvsp[-1])); } -#line 3895 "awkgram.c" /* yacc.c:1645 */ +#line 4043 "awkgram.c" /* yacc.c:1645 */ break; case 171: -#line 1778 "awkgram.y" /* yacc.c:1645 */ +#line 1900 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_create((yyvsp[0])); } -#line 3903 "awkgram.c" /* yacc.c:1645 */ +#line 4051 "awkgram.c" /* yacc.c:1645 */ break; case 172: -#line 1782 "awkgram.y" /* yacc.c:1645 */ +#line 1904 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_create((yyvsp[0])); } -#line 3911 "awkgram.c" /* yacc.c:1645 */ +#line 4059 "awkgram.c" /* yacc.c:1645 */ break; case 173: -#line 1786 "awkgram.y" /* yacc.c:1645 */ +#line 1908 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[0])->lasti->opcode == Op_push_i && ((yyvsp[0])->lasti->memory->flags & STRING) == 0 @@ -3926,11 +4074,11 @@ regular_print: (yyval) = list_append((yyvsp[0]), (yyvsp[-1])); } } -#line 3930 "awkgram.c" /* yacc.c:1645 */ +#line 4078 "awkgram.c" /* yacc.c:1645 */ break; case 174: -#line 1801 "awkgram.y" /* yacc.c:1645 */ +#line 1923 "awkgram.y" /* yacc.c:1645 */ { if ((yyvsp[0])->lasti->opcode == Op_push_i && ((yyvsp[0])->lasti->memory->flags & STRING) == 0 @@ -3948,20 +4096,20 @@ regular_print: (yyval) = list_append((yyvsp[0]), (yyvsp[-1])); } } -#line 3952 "awkgram.c" /* yacc.c:1645 */ +#line 4100 "awkgram.c" /* yacc.c:1645 */ break; case 175: -#line 1822 "awkgram.y" /* yacc.c:1645 */ +#line 1944 "awkgram.y" /* yacc.c:1645 */ { func_use((yyvsp[0])->lasti->func_name, FUNC_USE); (yyval) = (yyvsp[0]); } -#line 3961 "awkgram.c" /* yacc.c:1645 */ +#line 4109 "awkgram.c" /* yacc.c:1645 */ break; case 176: -#line 1827 "awkgram.y" /* yacc.c:1645 */ +#line 1949 "awkgram.y" /* yacc.c:1645 */ { /* indirect function call */ INSTRUCTION *f, *t; @@ -3995,11 +4143,11 @@ regular_print: (yyval) = list_prepend((yyvsp[0]), t); at_seen = false; } -#line 3999 "awkgram.c" /* yacc.c:1645 */ +#line 4147 "awkgram.c" /* yacc.c:1645 */ break; case 177: -#line 1864 "awkgram.y" /* yacc.c:1645 */ +#line 1986 "awkgram.y" /* yacc.c:1645 */ { NODE *n; @@ -4024,49 +4172,49 @@ regular_print: (yyval) = list_append(t, (yyvsp[-3])); } } -#line 4028 "awkgram.c" /* yacc.c:1645 */ +#line 4176 "awkgram.c" /* yacc.c:1645 */ break; case 178: -#line 1892 "awkgram.y" /* yacc.c:1645 */ +#line 2014 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 4034 "awkgram.c" /* yacc.c:1645 */ +#line 4182 "awkgram.c" /* yacc.c:1645 */ break; case 179: -#line 1894 "awkgram.y" /* yacc.c:1645 */ +#line 2016 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 4040 "awkgram.c" /* yacc.c:1645 */ +#line 4188 "awkgram.c" /* yacc.c:1645 */ break; case 180: -#line 1899 "awkgram.y" /* yacc.c:1645 */ +#line 2021 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 4046 "awkgram.c" /* yacc.c:1645 */ +#line 4194 "awkgram.c" /* yacc.c:1645 */ break; case 181: -#line 1901 "awkgram.y" /* yacc.c:1645 */ +#line 2023 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[-1]); } -#line 4052 "awkgram.c" /* yacc.c:1645 */ +#line 4200 "awkgram.c" /* yacc.c:1645 */ break; case 182: -#line 1906 "awkgram.y" /* yacc.c:1645 */ +#line 2028 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 4058 "awkgram.c" /* yacc.c:1645 */ +#line 4206 "awkgram.c" /* yacc.c:1645 */ break; case 183: -#line 1908 "awkgram.y" /* yacc.c:1645 */ +#line 2030 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_merge((yyvsp[-1]), (yyvsp[0])); } -#line 4066 "awkgram.c" /* yacc.c:1645 */ +#line 4214 "awkgram.c" /* yacc.c:1645 */ break; case 184: -#line 1915 "awkgram.y" /* yacc.c:1645 */ +#line 2037 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *ip = (yyvsp[0])->lasti; int count = ip->sub_count; /* # of SUBSEP-seperated expressions */ @@ -4080,11 +4228,11 @@ regular_print: sub_counter++; /* count # of dimensions */ (yyval) = (yyvsp[0]); } -#line 4084 "awkgram.c" /* yacc.c:1645 */ +#line 4232 "awkgram.c" /* yacc.c:1645 */ break; case 185: -#line 1932 "awkgram.y" /* yacc.c:1645 */ +#line 2054 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *t = (yyvsp[-1]); if ((yyvsp[-1]) == NULL) { @@ -4098,31 +4246,31 @@ regular_print: (yyvsp[0])->sub_count = count_expressions(&t, false); (yyval) = list_append(t, (yyvsp[0])); } -#line 4102 "awkgram.c" /* yacc.c:1645 */ +#line 4250 "awkgram.c" /* yacc.c:1645 */ break; case 186: -#line 1949 "awkgram.y" /* yacc.c:1645 */ +#line 2071 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); } -#line 4108 "awkgram.c" /* yacc.c:1645 */ +#line 4256 "awkgram.c" /* yacc.c:1645 */ break; case 187: -#line 1951 "awkgram.y" /* yacc.c:1645 */ +#line 2073 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_merge((yyvsp[-1]), (yyvsp[0])); } -#line 4116 "awkgram.c" /* yacc.c:1645 */ +#line 4264 "awkgram.c" /* yacc.c:1645 */ break; case 188: -#line 1958 "awkgram.y" /* yacc.c:1645 */ +#line 2080 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[-1]); } -#line 4122 "awkgram.c" /* yacc.c:1645 */ +#line 4270 "awkgram.c" /* yacc.c:1645 */ break; case 189: -#line 1963 "awkgram.y" /* yacc.c:1645 */ +#line 2085 "awkgram.y" /* yacc.c:1645 */ { char *var_name = (yyvsp[0])->lextok; @@ -4130,22 +4278,22 @@ regular_print: (yyvsp[0])->memory = variable((yyvsp[0])->source_line, var_name, Node_var_new); (yyval) = list_create((yyvsp[0])); } -#line 4134 "awkgram.c" /* yacc.c:1645 */ +#line 4282 "awkgram.c" /* yacc.c:1645 */ break; case 190: -#line 1971 "awkgram.y" /* yacc.c:1645 */ +#line 2093 "awkgram.y" /* yacc.c:1645 */ { char *arr = (yyvsp[-1])->lextok; (yyvsp[-1])->memory = variable((yyvsp[-1])->source_line, arr, Node_var_new); (yyvsp[-1])->opcode = Op_push_array; (yyval) = list_prepend((yyvsp[0]), (yyvsp[-1])); } -#line 4145 "awkgram.c" /* yacc.c:1645 */ +#line 4293 "awkgram.c" /* yacc.c:1645 */ break; case 191: -#line 1981 "awkgram.y" /* yacc.c:1645 */ +#line 2103 "awkgram.y" /* yacc.c:1645 */ { INSTRUCTION *ip = (yyvsp[0])->nexti; if (ip->opcode == Op_push @@ -4157,73 +4305,85 @@ regular_print: } else (yyval) = (yyvsp[0]); } -#line 4161 "awkgram.c" /* yacc.c:1645 */ +#line 4309 "awkgram.c" /* yacc.c:1645 */ break; case 192: -#line 1993 "awkgram.y" /* yacc.c:1645 */ +#line 2115 "awkgram.y" /* yacc.c:1645 */ { (yyval) = list_append((yyvsp[-1]), (yyvsp[-2])); if ((yyvsp[0]) != NULL) mk_assignment((yyvsp[-1]), NULL, (yyvsp[0])); } -#line 4171 "awkgram.c" /* yacc.c:1645 */ +#line 4319 "awkgram.c" /* yacc.c:1645 */ break; case 193: -#line 2002 "awkgram.y" /* yacc.c:1645 */ +#line 2124 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[0])->opcode = Op_postincrement; } -#line 4179 "awkgram.c" /* yacc.c:1645 */ +#line 4327 "awkgram.c" /* yacc.c:1645 */ break; case 194: -#line 2006 "awkgram.y" /* yacc.c:1645 */ +#line 2128 "awkgram.y" /* yacc.c:1645 */ { (yyvsp[0])->opcode = Op_postdecrement; } -#line 4187 "awkgram.c" /* yacc.c:1645 */ +#line 4335 "awkgram.c" /* yacc.c:1645 */ break; case 195: -#line 2009 "awkgram.y" /* yacc.c:1645 */ +#line 2132 "awkgram.y" /* yacc.c:1645 */ { (yyval) = NULL; } -#line 4193 "awkgram.c" /* yacc.c:1645 */ +#line 4341 "awkgram.c" /* yacc.c:1645 */ + break; + + case 196: +#line 2136 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = (yyvsp[0]); } +#line 4347 "awkgram.c" /* yacc.c:1645 */ break; case 197: -#line 2017 "awkgram.y" /* yacc.c:1645 */ - { yyerrok; } -#line 4199 "awkgram.c" /* yacc.c:1645 */ +#line 2140 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = (yyvsp[0]); yyerrok; } +#line 4353 "awkgram.c" /* yacc.c:1645 */ break; case 198: -#line 2021 "awkgram.y" /* yacc.c:1645 */ +#line 2144 "awkgram.y" /* yacc.c:1645 */ { yyerrok; } -#line 4205 "awkgram.c" /* yacc.c:1645 */ +#line 4359 "awkgram.c" /* yacc.c:1645 */ + break; + + case 199: +#line 2149 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = NULL; } +#line 4365 "awkgram.c" /* yacc.c:1645 */ break; case 201: -#line 2030 "awkgram.y" /* yacc.c:1645 */ +#line 2154 "awkgram.y" /* yacc.c:1645 */ { yyerrok; } -#line 4211 "awkgram.c" /* yacc.c:1645 */ +#line 4371 "awkgram.c" /* yacc.c:1645 */ break; case 202: -#line 2034 "awkgram.y" /* yacc.c:1645 */ +#line 2158 "awkgram.y" /* yacc.c:1645 */ { (yyval) = (yyvsp[0]); yyerrok; } -#line 4217 "awkgram.c" /* yacc.c:1645 */ +#line 4377 "awkgram.c" /* yacc.c:1645 */ break; case 203: -#line 2038 "awkgram.y" /* yacc.c:1645 */ - { yyerrok; } -#line 4223 "awkgram.c" /* yacc.c:1645 */ +#line 2162 "awkgram.y" /* yacc.c:1645 */ + { (yyval) = (yyvsp[0]); yyerrok; } +#line 4383 "awkgram.c" /* yacc.c:1645 */ break; -#line 4227 "awkgram.c" /* yacc.c:1645 */ +#line 4387 "awkgram.c" /* yacc.c:1645 */ default: break; } /* User semantic actions sometimes alter yychar, and that requires @@ -4450,7 +4610,7 @@ yyreturn: #endif return yyresult; } -#line 2040 "awkgram.y" /* yacc.c:1903 */ +#line 2164 "awkgram.y" /* yacc.c:1903 */ struct token { @@ -4529,8 +4689,8 @@ static const struct token tokentab[] = { {"exp", Op_builtin, LEX_BUILTIN, A(1), do_exp, MPF(exp)}, {"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}, -{"function",Op_func, LEX_FUNCTION, NOT_OLD, 0, 0}, +{"func", Op_func, LEX_FUNCTION, NOT_POSIX|NOT_OLD, 0, 0}, +{"function", Op_func, LEX_FUNCTION, NOT_OLD, 0, 0}, {"gensub", Op_sub_builtin, LEX_BUILTIN, GAWKX|A(3)|A(4), 0, 0}, {"getline", Op_K_getline_redir, LEX_GETLINE, NOT_OLD, 0, 0}, {"gsub", Op_sub_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), 0, 0}, @@ -4889,11 +5049,18 @@ mk_program() cp = end_block; else cp = list_merge(begin_block, end_block); - if (program_comment != NULL) { - (void) list_prepend(cp, program_comment); + + + if (outer_comment != NULL) { + (void) list_prepend(cp, outer_comment); + outer_comment = NULL; } - if (comment != NULL) - (void) list_append(cp, comment); + + if (trailing_comment != NULL) { + (void) list_append(cp, trailing_comment); + trailing_comment = NULL; + } + (void) list_append(cp, ip_atexit); (void) list_append(cp, instruction(Op_stop)); @@ -4926,12 +5093,6 @@ 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)); @@ -4939,10 +5100,6 @@ 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 @@ -5117,33 +5274,35 @@ add_srcfile(enum srctype stype, char *src, SRCFILE *thisfile, bool *already_incl /* include_source --- read program from source included using `@include' */ -static int -include_source(INSTRUCTION *file) +static bool +include_source(INSTRUCTION *file, void **srcfile_p) { SRCFILE *s; char *src = file->lextok; int errcode; bool already_included; + *srcfile_p = NULL; + if (do_traditional || do_posix) { error_ln(file->source_line, _("@include is a gawk extension")); - return -1; + return false; } if (strlen(src) == 0) { if (do_lint) lintwarn_ln(file->source_line, _("empty filename after @include")); - return 0; + return true; } s = add_srcfile(SRC_INC, src, sourcefile, &already_included, &errcode); if (s == NULL) { if (already_included) - return 0; + return true; error_ln(file->source_line, _("can't open source file `%s' for reading (%s)"), src, errcode ? strerror(errcode) : _("reason unknown")); - return -1; + return false; } /* save scanner state for the current sourcefile */ @@ -5162,42 +5321,46 @@ include_source(INSTRUCTION *file) lasttok = 0; lexeof = false; eof_warned = false; - return 0; + *srcfile_p = (void *) s; + return true; } /* load_library --- load a shared library */ -static int -load_library(INSTRUCTION *file) +static bool +load_library(INSTRUCTION *file, void **srcfile_p) { SRCFILE *s; char *src = file->lextok; int errcode; bool already_included; + *srcfile_p = NULL; + if (do_traditional || do_posix) { error_ln(file->source_line, _("@load is a gawk extension")); - return -1; + return false; } if (strlen(src) == 0) { if (do_lint) lintwarn_ln(file->source_line, _("empty filename after @load")); - return 0; + return true; } s = add_srcfile(SRC_EXTLIB, src, sourcefile, &already_included, &errcode); if (s == NULL) { if (already_included) - return 0; + return true; error_ln(file->source_line, _("can't open shared library `%s' for reading (%s)"), src, errcode ? strerror(errcode) : _("reason unknown")); - return -1; + return false; } load_ext(s->fullpath); - return 0; + *srcfile_p = (void *) s; + return true; } /* next_sourcefile --- read program from the next source in srcfiles */ @@ -5605,37 +5768,23 @@ 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. + * Flag = BLOCK_COMMENT for self-contained comments. */ -int -get_comment(int flag) +static int +get_comment(enum commenttype flag, INSTRUCTION **comment_instruction) { int c; int sl; + char *p1; + char *p2; + tok = tokstart; tokadd('#'); sl = sourceline; - char *p1; - char *p2; while (true) { while ((c = nextc(false)) != '\n' && c != END_FILE) { @@ -5671,9 +5820,6 @@ get_comment(int flag) break; } - if (comment != NULL) - prior_comment = comment; - /* remove any trailing blank lines (consecutive \n) from comment */ p1 = tok - 1; p2 = tok - 2; @@ -5683,49 +5829,18 @@ get_comment(int flag) 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; + (*comment_instruction) = bcalloc(Op_comment, 1, sl); + (*comment_instruction)->source_file = source; + (*comment_instruction)->memory = make_str_node(tokstart, tok - tokstart, 0); + (*comment_instruction)->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_string(p + l + 2, n->stlen - l - 2); - /* 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 : */ static void -allow_newline(void) +allow_newline(INSTRUCTION **new_comment) { int c; @@ -5737,8 +5852,8 @@ allow_newline(void) } if (c == '#') { if (do_pretty_print && ! do_profile) { - /* collect comment byte code iff doing pretty print but not profiling. */ - c = get_comment(EOL_COMMENT); + /* collect comment byte code iff doing pretty print but not profiling. */ + c = get_comment(EOL_COMMENT, new_comment); } else { while ((c = nextc(false)) != '\n' && c != END_FILE) continue; @@ -5805,6 +5920,7 @@ yylex(void) bool intlstr = false; AWKNUM d; bool collecting_typed_regexp = false; + static int qm_col_count = 0; #define GET_INSTRUCTION(op) bcalloc(op, 1, sourceline) @@ -5969,18 +6085,25 @@ retry: return lasttok = NEWLINE; case '#': /* it's a comment */ + yylval = NULL; if (do_pretty_print && ! do_profile) { /* * Collect comment byte code iff doing pretty print * but not profiling. */ + INSTRUCTION *new_comment; + if (lasttok == NEWLINE || lasttok == 0) - c = get_comment(FULL_COMMENT); + c = get_comment(BLOCK_COMMENT, & new_comment); else - c = get_comment(EOL_COMMENT); + c = get_comment(EOL_COMMENT, & new_comment); - if (c == END_FILE) - return lasttok = NEWLINE_EOF; + yylval = new_comment; + + if (c == END_FILE) { + pushback(); + return lasttok = NEWLINE; + } } else { while ((c = nextc(false)) != '\n') { if (c == END_FILE) @@ -6009,7 +6132,10 @@ retry: * Use it at your own risk. We think it's a bad idea, which * is why it's not on by default. */ + yylval = NULL; if (! do_traditional) { + INSTRUCTION *new_comment; + /* strip trailing white-space and/or comment */ while ((c = nextc(true)) == ' ' || c == '\t' || c == '\r') continue; @@ -6021,9 +6147,11 @@ retry: lintwarn( _("use of `\\ #...' line continuation is not portable")); } - if (do_pretty_print && ! do_profile) - c = get_comment(EOL_COMMENT); - else { + if (do_pretty_print && ! do_profile) { + c = get_comment(EOL_COMMENT, & new_comment); + yylval = new_comment; + return lasttok = c; + } else { while ((c = nextc(false)) != '\n') if (c == END_FILE) break; @@ -6044,11 +6172,19 @@ retry: } break; - case ':': case '?': + qm_col_count++; + // fall through + case ':': yylval = GET_INSTRUCTION(Op_cond_exp); - if (! do_posix) - allow_newline(); + if (c == ':' && qm_col_count > 0) { + if (do_posix) { + INSTRUCTION *new_comment = NULL; + allow_newline(& new_comment); + yylval->comment = new_comment; + } + qm_col_count--; + } return lasttok = c; /* @@ -6470,7 +6606,10 @@ retry: case '&': if ((c = nextc(true)) == '&') { yylval = GET_INSTRUCTION(Op_and); - allow_newline(); + INSTRUCTION *new_comment = NULL; + allow_newline(& new_comment); + yylval->comment = new_comment; + return lasttok = LEX_AND; } pushback(); @@ -6480,11 +6619,15 @@ retry: case '|': if ((c = nextc(true)) == '|') { yylval = GET_INSTRUCTION(Op_or); - allow_newline(); + INSTRUCTION *new_comment = NULL; + allow_newline(& new_comment); + yylval->comment = new_comment; + return lasttok = LEX_OR; } else if (! do_traditional && c == '&') { yylval = GET_INSTRUCTION(Op_symbol); yylval->redir_type = redirect_twoway; + return lasttok = (in_print && in_parens == 0 ? IO_OUT : IO_IN); } pushback(); @@ -6674,8 +6817,11 @@ out: yylval->lextok = tokkey; #define SMART_ALECK 1 - if (SMART_ALECK && do_lint - && ! goto_warned && strcasecmp(tokkey, "goto") == 0) { + if (SMART_ALECK + && do_lint + && ! goto_warned + && tolower(tokkey[0]) == 'g' + && strcasecmp(tokkey, "goto") == 0) { goto_warned = true; lintwarn(_("`goto' considered harmful!")); } @@ -7146,15 +7292,8 @@ mk_function(INSTRUCTION *fi, INSTRUCTION *def) thisfunc = fi->func_body; assert(thisfunc != NULL); - /* 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; + /* + * Add an implicit return at end; * also used by 'return' command in debugger */ @@ -7865,12 +8004,7 @@ append_rule(INSTRUCTION *pattern, INSTRUCTION *action) (rp + 1)->lasti = action->lasti; (rp + 2)->first_line = pattern->source_line; (rp + 2)->last_line = lastline; - if (block_comment != NULL) { - ip = list_prepend(list_prepend(action, block_comment), rp); - block_comment = NULL; - } else - ip = list_prepend(action, rp); - + ip = list_prepend(action, rp); } else { rp = bcalloc(Op_rule, 3, 0); rp->in_rule = Rule; @@ -8275,8 +8409,9 @@ mk_for_loop(INSTRUCTION *forp, INSTRUCTION *init, INSTRUCTION *cond, forp->target_break = tbreak; forp->target_continue = tcont; ret = list_prepend(ret, forp); - } /* else - forp is NULL */ + } + /* else + forp is NULL */ return ret; } @@ -8490,26 +8625,6 @@ 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 @@ -8749,3 +8864,91 @@ set_profile_text(NODE *n, const char *str, size_t len) return n; } + +/* + * merge_comments --- merge c2 into c1 and free c2 if successful. + * Allow c2 to be NULL, in which case just merged chained + * comments in c1. + */ + +static void +merge_comments(INSTRUCTION *c1, INSTRUCTION *c2) +{ + assert(c1->opcode == Op_comment); + + if (c1->comment == NULL && c2 == NULL) // nothing to do + return; + + size_t total = c1->memory->stlen; + if (c1->comment != NULL) + total += 1 /* \n */ + c1->comment->memory->stlen; + + if (c2 != NULL) { + assert(c2->opcode == Op_comment); + total += 1 /* \n */ + c2->memory->stlen; + if (c2->comment != NULL) + total += c2->comment->memory->stlen + 1; + } + + char *buffer; + emalloc(buffer, char *, total + 1, "merge_comments"); + + strcpy(buffer, c1->memory->stptr); + if (c1->comment != NULL) { + strcat(buffer, "\n"); + strcat(buffer, c1->comment->memory->stptr); + } + + if (c2 != NULL) { + strcat(buffer, "\n"); + strcat(buffer, c2->memory->stptr); + if (c2->comment != NULL) { + strcat(buffer, "\n"); + strcat(buffer, c2->comment->memory->stptr); + } + + unref(c2->memory); + if (c2->comment != NULL) { + unref(c2->comment->memory); + bcfree(c2->comment); + c2->comment = NULL; + } + bcfree(c2); + } + + c1->memory->comment_type = BLOCK_COMMENT; + free(c1->memory->stptr); + c1->memory->stptr = buffer; + c1->memory->stlen = strlen(buffer); + + // now free everything else + if (c1->comment != NULL) { + unref(c1->comment->memory); + bcfree(c1->comment); + c1->comment = NULL; + } +} + +/* make_braced_statements --- handle `l_brace statements r_brace' with comments */ + +static INSTRUCTION * +make_braced_statements(INSTRUCTION *lbrace, INSTRUCTION *stmts, INSTRUCTION *rbrace) +{ + INSTRUCTION *ip; + + if (stmts == NULL) + ip = list_create(instruction(Op_no_op)); + else + ip = stmts; + + if (lbrace != NULL) { + INSTRUCTION *comment2 = lbrace->comment; + if (comment2 != NULL) { + ip = list_prepend(ip, comment2); + lbrace->comment = NULL; + } + ip = list_prepend(ip, lbrace); + } + + return ip; +} @@ -1,4 +1,25 @@ /* +TODO: +-- Handle comments outside blocks +-- Put together a big test file +DONE: +-- Get comments from all instances of nls +-- After , in parameter list +-- After ? and : +-- switch statement +-- After , in a range expression in a pattern +-- After && and || +-- case part of switch statement +-- for(;;) statement +-- for(iggy in foo) statement +-- Comments after commas in expressions lists in print/f and function calls +-- Handle EOL comments on a closing right brace +-- Get comments from all instances of opt_nls +-- Get comments from all instances of l_brace +-- Get comments from all instances of r_brace +-- Review statement lists and handling of statement_term +*/ +/* * awkgram.y --- yacc/bison parser */ @@ -53,11 +74,13 @@ static int isnoeffect(OPCODE type); static INSTRUCTION *make_assignable(INSTRUCTION *ip); static void dumpintlstr(const char *str, size_t len); static void dumpintlstr2(const char *str1, size_t len1, const char *str2, size_t len2); -static int include_source(INSTRUCTION *file); -static int load_library(INSTRUCTION *file); +static bool include_source(INSTRUCTION *file, void **srcfile_p); +static bool load_library(INSTRUCTION *file, void **srcfile_p); static void next_sourcefile(void); static char *tokexpand(void); static NODE *set_profile_text(NODE *n, const char *str, size_t len); +static INSTRUCTION *trailing_comment; +static INSTRUCTION *outer_comment; #define instruction(t) bcalloc(t, 1, 0) @@ -84,8 +107,8 @@ 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 merge_comments(INSTRUCTION *c1, INSTRUCTION *c2); +static INSTRUCTION *make_braced_statements(INSTRUCTION *lbrace, INSTRUCTION *stmts, INSTRUCTION *rbrace); static void add_sign_to_num(NODE *n, char sign); static bool at_seen = false; @@ -152,21 +175,13 @@ 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 func_first = true; // can nuke 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); @@ -214,12 +229,23 @@ extern double fmod(double x, double y); program : /* empty */ + { $$ = NULL; } | program rule { rule = 0; yyerrok; } | program nls + { + if ($2 != NULL) { + if ($1 == NULL) { + outer_comment = $2; + } else { + trailing_comment = $2; + } + } + $$ = $1; + } | program LEX_EOF { next_sourcefile(); @@ -249,8 +275,11 @@ rule } else if ($1 == NULL) { msg(_("each rule must have a pattern or an action part")); errcount++; - } else /* pattern rule with non-empty pattern */ + } else { /* pattern rule with non-empty pattern */ + if ($2 != NULL) + list_append($1, $2); (void) append_rule($1, NULL); + } } | function_prologue action { @@ -263,12 +292,20 @@ rule { want_source = false; at_seen = false; + if ($3 != NULL && $4 != NULL) { + SRCFILE *s = (SRCFILE *) $3; + s->comment = $4; + } yyerrok; } | '@' LEX_LOAD library statement_term { want_source = false; at_seen = false; + if ($3 != NULL && $4 != NULL) { + SRCFILE *s = (SRCFILE *) $3; + s->comment = $4; + } yyerrok; } ; @@ -276,11 +313,13 @@ rule source : FILENAME { - if (include_source($1) < 0) + void *srcfile = NULL; + + if (! include_source($1, & srcfile)) YYABORT; efree($1->lextok); bcfree($1); - $$ = NULL; + $$ = (INSTRUCTION *) srcfile; } | FILENAME error { $$ = NULL; } @@ -291,11 +330,13 @@ source library : FILENAME { - if (load_library($1) < 0) + void *srcfile; + + if (! load_library($1, & srcfile)) YYABORT; efree($1->lextok); bcfree($1); - $$ = NULL; + $$ = (INSTRUCTION *) srcfile; } | FILENAME error { $$ = NULL; } @@ -307,20 +348,11 @@ pattern : /* empty */ { rule = Rule; - if (comment != NULL) { - $$ = list_create(comment); - comment = NULL; - } else - $$ = NULL; + $$ = NULL; } | exp { rule = Rule; - if (comment != NULL) { - $$ = list_prepend($1, comment); - comment = NULL; - } else - $$ = $1; } | exp ',' opt_nls exp @@ -346,10 +378,10 @@ pattern ($1->nexti + 1)->condpair_left = $1->lasti; ($1->nexti + 1)->condpair_right = $4->lasti; } - if (comment != NULL) { - $$ = list_append(list_merge(list_prepend($1, comment), $4), tp); - comment = NULL; - } else + /* Put any comments in front of the range expression */ + if ($3 != NULL) + $$ = list_append(list_merge(list_prepend($1, $3), $4), tp); + else $$ = list_append(list_merge($1, $4), tp); rule = Rule; } @@ -364,7 +396,6 @@ pattern $1->in_rule = rule = BEGIN; $1->source_file = source; - check_comment(); $$ = $1; } | LEX_END @@ -378,7 +409,6 @@ pattern $1->in_rule = rule = END; $1->source_file = source; - check_comment(); $$ = $1; } | LEX_BEGINFILE @@ -386,7 +416,6 @@ pattern func_first = false; $1->in_rule = rule = BEGINFILE; $1->source_file = source; - check_comment(); $$ = $1; } | LEX_ENDFILE @@ -394,7 +423,6 @@ pattern func_first = false; $1->in_rule = rule = ENDFILE; $1->source_file = source; - check_comment(); $$ = $1; } ; @@ -402,11 +430,17 @@ pattern action : l_brace statements r_brace opt_semi opt_nls { - INSTRUCTION *ip; - if ($2 == NULL) - ip = list_create(instruction(Op_no_op)); - else - ip = $2; + INSTRUCTION *ip = make_braced_statements($1, $2, $3); + + if ($3 != NULL && $5 != NULL) { + merge_comments($3, $5); + trailing_comment = $3; + } else if ($3 != NULL) { + trailing_comment = $3; + } else if ($5 != NULL) { + trailing_comment = $5; + } + $$ = ip; } ; @@ -437,33 +471,21 @@ lex_builtin function_prologue : 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; + INSTRUCTION *func_comment = NULL; + // Merge any comments found in the parameter list with those + // following the function header, associate the whole shebang + // with the function as one block comment. + if ($5 != NULL && $5->comment != NULL) { + if ($7 != NULL) { + merge_comments($5->comment, $7); + } + func_comment = $5->comment; + } else if ($7 != NULL) { + func_comment = $7; } - func_first = false; + $1->source_file = source; + $1->comment = func_comment; if (install_function($2->lextok, $1, $5) < 0) YYABORT; in_function = $2->lextok; @@ -536,61 +558,23 @@ a_slash statements : /* empty */ - { - if (prior_comment != NULL) { - $$ = list_create(prior_comment); - prior_comment = NULL; - } else if (comment != NULL) { - $$ = list_create(comment); - comment = NULL; - } else - $$ = NULL; - } + { $$ = NULL; } | statements statement { 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; + $$ = $1; } else { add_lint($2, LINT_no_effect); 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; + $$ = $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); } } + if (trailing_comment != NULL) { + $$ = list_append($$, trailing_comment); + trailing_comment = NULL; + } yyerrok; } | statements error @@ -598,15 +582,27 @@ statements ; statement_term - : nls - | semi opt_nls + : nls { $$ = $1; } + | semi opt_nls { $$ = $2; } ; statement : semi opt_nls - { $$ = NULL; } + { + if ($2 != NULL) { + INSTRUCTION *ip; + + merge_comments($2, NULL); + ip = list_create(instruction(Op_no_op)); + $$ = list_append(ip, $2); + } else + $$ = NULL; + } | l_brace statements r_brace - { $$ = $2; } + { + trailing_comment = $3; // NULL or comment + $$ = make_braced_statements($1, $2, $3); + } | if_statement { if (do_pretty_print) @@ -632,8 +628,9 @@ statement if ($7 != NULL) { curr = $7->nexti; bcfree($7); /* Op_list */ - } /* else - curr = NULL; */ + } + /* else + curr = NULL; */ for (; curr != NULL; curr = nextc) { INSTRUCTION *caseexp = curr->case_exp; @@ -689,16 +686,33 @@ statement ip = $3; if (do_pretty_print) { + // first merge comments + INSTRUCTION *head_comment = NULL; + + if ($5 != NULL && $6 != NULL) { + merge_comments($5, $6); + head_comment = $5; + } else if ($5 != NULL) + head_comment = $5; + else + head_comment = $6; + + $1->comment = head_comment; + (void) list_prepend(ip, $1); (void) list_prepend(ip, instruction(Op_exec_count)); $1->target_break = tbreak; ($1 + 1)->switch_start = cexp->nexti; ($1 + 1)->switch_end = cexp->lasti; - }/* else - $1 is NULL */ + ($1 + 1)->switch_end->comment = $9; + } + /* else + $1 is NULL */ (void) list_append(cexp, dflt); (void) list_merge(ip, cexp); + if ($8 != NULL) + (void) list_append(cstmt, $8); $$ = list_merge(ip, cstmt); break_allowed--; @@ -733,8 +747,17 @@ statement $1->target_continue = tcont; ($1 + 1)->while_body = ip->lasti; (void) list_prepend(ip, $1); - }/* else - $1 is NULL */ + } + /* else + $1 is NULL */ + + if ($5 != NULL) { + if ($6 == NULL) + $6 = list_create(instruction(Op_no_op)); + + $5->memory->comment_type = BLOCK_COMMENT; + $6 = list_prepend($6, $5); + } if ($6 != NULL) (void) list_merge(ip, $6); @@ -769,8 +792,13 @@ statement ip = list_merge($3, $6); else ip = list_prepend($6, instruction(Op_no_op)); + + if ($2 != NULL) + (void) list_prepend(ip, $2); + if (do_pretty_print) (void) list_prepend(ip, instruction(Op_exec_count)); + (void) list_append(ip, instruction(Op_jmp_true)); ip->lasti->target_jmp = ip->nexti; $$ = list_append(ip, tbreak); @@ -785,7 +813,10 @@ statement ($1 + 1)->doloop_cond = tcont; $$ = list_prepend(ip, $1); bcfree($4); - } /* else + if ($8 != NULL) + $1->comment = $8; + } + /* else $1 and $4 are NULLs */ } | LEX_FOR '(' NAME LEX_IN simple_variable r_paren opt_nls statement @@ -801,7 +832,8 @@ statement && strcmp($8->nexti->memory->vname, var_name) == 0 ) { - /* Efficiency hack. Recognize the special case of + /* + * Efficiency hack. Recognize the special case of * * for (iggy in foo) * delete foo[iggy] @@ -833,6 +865,10 @@ statement bcfree($3); bcfree($4); bcfree($5); + if ($7 != NULL) { + merge_comments($7, NULL); + $8 = list_prepend($8, $7); + } $$ = $8; } else goto regular_loop; @@ -867,8 +903,9 @@ regular_loop: $1->target_continue = tcont; $1->target_break = tbreak; (void) list_append(ip, $1); - } /* else - $1 is NULL */ + } + /* else + $1 is NULL */ /* add update_FOO instruction if necessary */ if ($4->array_var->type == Node_var && $4->array_var->var_update) { @@ -889,8 +926,15 @@ regular_loop: ($1 + 1)->forloop_body = ip->lasti; } - if ($8 != NULL) + if ($7 != NULL) + merge_comments($7, NULL); + + if ($8 != NULL) { + if ($7 != NULL) + $8 = list_prepend($8, $7); (void) list_merge(ip, $8); + } else if ($7 != NULL) + (void) list_append(ip, $7); (void) list_append(ip, instruction(Op_jmp)); ip->lasti->target_jmp = $4; @@ -903,6 +947,20 @@ regular_loop: } | LEX_FOR '(' opt_simple_stmt semi opt_nls exp semi opt_nls opt_simple_stmt r_paren opt_nls statement { + if ($5 != NULL) { + merge_comments($5, NULL); + $1->comment = $5; + } + if ($8 != NULL) { + merge_comments($8, NULL); + if ($1->comment == NULL) { + $8->memory->comment_type = FOR_COMMENT; + $1->comment = $8; + } else + $1->comment->comment = $8; + } + if ($11 != NULL) + $12 = list_prepend($12, $11); $$ = mk_for_loop($1, $3, $6, $9, $12); break_allowed--; @@ -910,6 +968,20 @@ regular_loop: } | LEX_FOR '(' opt_simple_stmt semi opt_nls semi opt_nls opt_simple_stmt r_paren opt_nls statement { + if ($5 != NULL) { + merge_comments($5, NULL); + $1->comment = $5; + } + if ($7 != NULL) { + merge_comments($7, NULL); + if ($1->comment == NULL) { + $7->memory->comment_type = FOR_COMMENT; + $1->comment = $7; + } else + $1->comment->comment = $7; + } + if ($10 != NULL) + $11 = list_prepend($11, $10); $$ = mk_for_loop($1, $3, (INSTRUCTION *) NULL, $8, $11); break_allowed--; @@ -921,7 +993,6 @@ regular_loop: $$ = list_prepend($1, instruction(Op_exec_count)); else $$ = $1; - $$ = add_pending_comment($$); } ; @@ -933,8 +1004,8 @@ non_compound_stmt _("`break' is not allowed outside a loop or switch")); $1->target_jmp = NULL; $$ = list_create($1); - $$ = add_pending_comment($$); - + if ($2 != NULL) + $$ = list_append($$, $2); } | LEX_CONTINUE statement_term { @@ -943,8 +1014,8 @@ non_compound_stmt _("`continue' is not allowed outside a loop")); $1->target_jmp = NULL; $$ = list_create($1); - $$ = add_pending_comment($$); - + if ($2 != NULL) + $$ = list_append($$, $2); } | LEX_NEXT statement_term { @@ -954,7 +1025,8 @@ non_compound_stmt _("`next' used in %s action"), ruletab[rule]); $1->target_jmp = ip_rec; $$ = list_create($1); - $$ = add_pending_comment($$); + if ($2 != NULL) + $$ = list_append($$, $2); } | LEX_NEXTFILE statement_term { @@ -966,7 +1038,8 @@ non_compound_stmt $1->target_newfile = ip_newfile; $1->target_endfile = ip_endfile; $$ = list_create($1); - $$ = add_pending_comment($$); + if ($2 != NULL) + $$ = list_append($$, $2); } | LEX_EXIT opt_exp statement_term { @@ -982,7 +1055,8 @@ non_compound_stmt $$->nexti->memory = dupnode(Nnull_string); } else $$ = list_append($2, $1); - $$ = add_pending_comment($$); + if ($3 != NULL) + $$ = list_append($$, $3); } | LEX_RETURN { @@ -995,10 +1069,16 @@ non_compound_stmt $$->nexti->memory = dupnode(Nnull_string); } else $$ = list_append($3, $1); - - $$ = add_pending_comment($$); + if ($4 != NULL) + $$ = list_append($$, $4); } | simple_stmt statement_term + { + if ($2 != NULL) + $$ = list_append($1, $2); + else + $$ = $1; + } ; /* @@ -1018,7 +1098,7 @@ simple_stmt * which is faster for these two cases. */ - if ($1->opcode == Op_K_print && + if (do_optimize && $1->opcode == Op_K_print && ($3 == NULL || ($3->lasti->opcode == Op_field_spec && $3->nexti->nexti->nexti == $3->lasti @@ -1106,7 +1186,6 @@ regular_print: } } } - $$ = add_pending_comment($$); } | LEX_DELETE NAME { sub_counter = 0; } delete_subscript_list @@ -1141,7 +1220,6 @@ regular_print: $1->expr_count = sub_counter; $$ = list_append(list_append($4, $2), $1); } - $$ = add_pending_comment($$); } | LEX_DELETE '(' NAME ')' /* @@ -1172,12 +1250,10 @@ regular_print: else if ($3->memory == func_table) fatal(_("`delete' is not allowed with FUNCTAB")); } - $$ = add_pending_comment($$); } | exp { $$ = optimize_assignment($1); - $$ = add_pending_comment($$); } ; @@ -1212,6 +1288,7 @@ case_statement (void) list_prepend(casestmt, instruction(Op_exec_count)); $1->case_exp = $2; $1->case_stmt = casestmt; + $1->comment = $4; bcfree($3); $$ = $1; } @@ -1224,6 +1301,7 @@ case_statement (void) list_prepend(casestmt, instruction(Op_exec_count)); bcfree($2); $1->case_stmt = casestmt; + $1->comment = $3; $$ = $1; } ; @@ -1305,23 +1383,51 @@ output_redir if_statement : LEX_IF '(' exp r_paren opt_nls statement { + if ($5 != NULL) + $1->comment = $5; $$ = mk_condition($3, $1, $6, NULL, NULL); } | LEX_IF '(' exp r_paren opt_nls statement LEX_ELSE opt_nls statement { + if ($5 != NULL) + $1->comment = $5; + if ($8 != NULL) + $7->comment = $8; $$ = mk_condition($3, $1, $6, $7, $9); } ; nls : NEWLINE + { + $$ = $1; + } | nls NEWLINE + { + if ($1 != NULL && $2 != NULL) { + if ($1->memory->comment_type == EOL_COMMENT) { + assert($2->memory->comment_type == BLOCK_COMMENT); + $1->comment = $2; // chain them + } else { + merge_comments($1, $2); + } + + $$ = $1; + } else if ($1 != NULL) { + $$ = $1; + } else if ($2 != NULL) { + $$ = $2; + } else + $$ = NULL; + } ; opt_nls : /* empty */ + { $$ = NULL; } | nls + { $$ = $1; } ; input_redir @@ -1350,9 +1456,17 @@ param_list | param_list comma NAME { if ($1 != NULL && $3 != NULL) { - $3->param_count = $1->lasti->param_count + 1; + $3->param_count = $1->lasti->param_count + 1; $$ = list_append($1, $3); yyerrok; + + // newlines are allowed after commas, catch any comments + if ($2 != NULL) { + if ($1->comment != NULL) + merge_comments($1->comment, $2); + else + $1->comment = $2; + } } else $$ = NULL; } @@ -1384,6 +1498,8 @@ expression_list { $$ = mk_expression_list(NULL, $1); } | expression_list comma exp { + if ($2 != NULL) + $1->lasti->comment = $2; $$ = mk_expression_list($1, $3); yyerrok; } @@ -1405,6 +1521,8 @@ expression_list | expression_list comma error { /* Ditto */ + if ($2 != NULL) + $1->lasti->comment = $2; $$ = $1; } ; @@ -1421,6 +1539,8 @@ fcall_expression_list { $$ = mk_expression_list(NULL, $1); } | fcall_expression_list comma fcall_exp { + if ($2 != NULL) + $1->lasti->comment = $2; $$ = mk_expression_list($1, $3); yyerrok; } @@ -1442,6 +1562,8 @@ fcall_expression_list | fcall_expression_list comma error { /* Ditto */ + if ($2 != NULL) + $1->comment = $2; $$ = $1; } ; @@ -2006,15 +2128,16 @@ opt_incdec { $1->opcode = Op_postdecrement; } - | /* empty */ { $$ = NULL; } + | /* empty */ + { $$ = NULL; } ; l_brace - : '{' opt_nls + : '{' opt_nls { $$ = $2; } ; r_brace - : '}' opt_nls { yyerrok; } + : '}' opt_nls { $$ = $2; yyerrok; } ; r_paren @@ -2023,6 +2146,7 @@ r_paren opt_semi : /* empty */ + { $$ = NULL; } | semi ; @@ -2035,7 +2159,7 @@ colon ; comma - : ',' opt_nls { yyerrok; } + : ',' opt_nls { $$ = $2; yyerrok; } ; %% @@ -2115,8 +2239,8 @@ static const struct token tokentab[] = { {"exp", Op_builtin, LEX_BUILTIN, A(1), do_exp, MPF(exp)}, {"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}, -{"function",Op_func, LEX_FUNCTION, NOT_OLD, 0, 0}, +{"func", Op_func, LEX_FUNCTION, NOT_POSIX|NOT_OLD, 0, 0}, +{"function", Op_func, LEX_FUNCTION, NOT_OLD, 0, 0}, {"gensub", Op_sub_builtin, LEX_BUILTIN, GAWKX|A(3)|A(4), 0, 0}, {"getline", Op_K_getline_redir, LEX_GETLINE, NOT_OLD, 0, 0}, {"gsub", Op_sub_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), 0, 0}, @@ -2475,11 +2599,18 @@ mk_program() cp = end_block; else cp = list_merge(begin_block, end_block); - if (program_comment != NULL) { - (void) list_prepend(cp, program_comment); + + + if (outer_comment != NULL) { + (void) list_prepend(cp, outer_comment); + outer_comment = NULL; + } + + if (trailing_comment != NULL) { + (void) list_append(cp, trailing_comment); + trailing_comment = NULL; } - if (comment != NULL) - (void) list_append(cp, comment); + (void) list_append(cp, ip_atexit); (void) list_append(cp, instruction(Op_stop)); @@ -2512,12 +2643,6 @@ 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)); @@ -2525,10 +2650,6 @@ 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 @@ -2703,33 +2824,35 @@ add_srcfile(enum srctype stype, char *src, SRCFILE *thisfile, bool *already_incl /* include_source --- read program from source included using `@include' */ -static int -include_source(INSTRUCTION *file) +static bool +include_source(INSTRUCTION *file, void **srcfile_p) { SRCFILE *s; char *src = file->lextok; int errcode; bool already_included; + *srcfile_p = NULL; + if (do_traditional || do_posix) { error_ln(file->source_line, _("@include is a gawk extension")); - return -1; + return false; } if (strlen(src) == 0) { if (do_lint) lintwarn_ln(file->source_line, _("empty filename after @include")); - return 0; + return true; } s = add_srcfile(SRC_INC, src, sourcefile, &already_included, &errcode); if (s == NULL) { if (already_included) - return 0; + return true; error_ln(file->source_line, _("can't open source file `%s' for reading (%s)"), src, errcode ? strerror(errcode) : _("reason unknown")); - return -1; + return false; } /* save scanner state for the current sourcefile */ @@ -2748,42 +2871,46 @@ include_source(INSTRUCTION *file) lasttok = 0; lexeof = false; eof_warned = false; - return 0; + *srcfile_p = (void *) s; + return true; } /* load_library --- load a shared library */ -static int -load_library(INSTRUCTION *file) +static bool +load_library(INSTRUCTION *file, void **srcfile_p) { SRCFILE *s; char *src = file->lextok; int errcode; bool already_included; + *srcfile_p = NULL; + if (do_traditional || do_posix) { error_ln(file->source_line, _("@load is a gawk extension")); - return -1; + return false; } if (strlen(src) == 0) { if (do_lint) lintwarn_ln(file->source_line, _("empty filename after @load")); - return 0; + return true; } s = add_srcfile(SRC_EXTLIB, src, sourcefile, &already_included, &errcode); if (s == NULL) { if (already_included) - return 0; + return true; error_ln(file->source_line, _("can't open shared library `%s' for reading (%s)"), src, errcode ? strerror(errcode) : _("reason unknown")); - return -1; + return false; } load_ext(s->fullpath); - return 0; + *srcfile_p = (void *) s; + return true; } /* next_sourcefile --- read program from the next source in srcfiles */ @@ -3191,37 +3318,23 @@ 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. + * Flag = BLOCK_COMMENT for self-contained comments. */ -int -get_comment(int flag) +static int +get_comment(enum commenttype flag, INSTRUCTION **comment_instruction) { int c; int sl; + char *p1; + char *p2; + tok = tokstart; tokadd('#'); sl = sourceline; - char *p1; - char *p2; while (true) { while ((c = nextc(false)) != '\n' && c != END_FILE) { @@ -3257,9 +3370,6 @@ get_comment(int flag) break; } - if (comment != NULL) - prior_comment = comment; - /* remove any trailing blank lines (consecutive \n) from comment */ p1 = tok - 1; p2 = tok - 2; @@ -3269,49 +3379,18 @@ get_comment(int flag) 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; + (*comment_instruction) = bcalloc(Op_comment, 1, sl); + (*comment_instruction)->source_file = source; + (*comment_instruction)->memory = make_str_node(tokstart, tok - tokstart, 0); + (*comment_instruction)->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_string(p + l + 2, n->stlen - l - 2); - /* 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 : */ static void -allow_newline(void) +allow_newline(INSTRUCTION **new_comment) { int c; @@ -3323,8 +3402,8 @@ allow_newline(void) } if (c == '#') { if (do_pretty_print && ! do_profile) { - /* collect comment byte code iff doing pretty print but not profiling. */ - c = get_comment(EOL_COMMENT); + /* collect comment byte code iff doing pretty print but not profiling. */ + c = get_comment(EOL_COMMENT, new_comment); } else { while ((c = nextc(false)) != '\n' && c != END_FILE) continue; @@ -3391,6 +3470,7 @@ yylex(void) bool intlstr = false; AWKNUM d; bool collecting_typed_regexp = false; + static int qm_col_count = 0; #define GET_INSTRUCTION(op) bcalloc(op, 1, sourceline) @@ -3555,18 +3635,25 @@ retry: return lasttok = NEWLINE; case '#': /* it's a comment */ + yylval = NULL; if (do_pretty_print && ! do_profile) { /* * Collect comment byte code iff doing pretty print * but not profiling. */ + INSTRUCTION *new_comment; + if (lasttok == NEWLINE || lasttok == 0) - c = get_comment(FULL_COMMENT); + c = get_comment(BLOCK_COMMENT, & new_comment); else - c = get_comment(EOL_COMMENT); + c = get_comment(EOL_COMMENT, & new_comment); - if (c == END_FILE) - return lasttok = NEWLINE_EOF; + yylval = new_comment; + + if (c == END_FILE) { + pushback(); + return lasttok = NEWLINE; + } } else { while ((c = nextc(false)) != '\n') { if (c == END_FILE) @@ -3595,7 +3682,10 @@ retry: * Use it at your own risk. We think it's a bad idea, which * is why it's not on by default. */ + yylval = NULL; if (! do_traditional) { + INSTRUCTION *new_comment; + /* strip trailing white-space and/or comment */ while ((c = nextc(true)) == ' ' || c == '\t' || c == '\r') continue; @@ -3607,9 +3697,11 @@ retry: lintwarn( _("use of `\\ #...' line continuation is not portable")); } - if (do_pretty_print && ! do_profile) - c = get_comment(EOL_COMMENT); - else { + if (do_pretty_print && ! do_profile) { + c = get_comment(EOL_COMMENT, & new_comment); + yylval = new_comment; + return lasttok = c; + } else { while ((c = nextc(false)) != '\n') if (c == END_FILE) break; @@ -3630,11 +3722,19 @@ retry: } break; - case ':': case '?': + qm_col_count++; + // fall through + case ':': yylval = GET_INSTRUCTION(Op_cond_exp); - if (! do_posix) - allow_newline(); + if (c == ':' && qm_col_count > 0) { + if (do_posix) { + INSTRUCTION *new_comment = NULL; + allow_newline(& new_comment); + yylval->comment = new_comment; + } + qm_col_count--; + } return lasttok = c; /* @@ -4056,7 +4156,10 @@ retry: case '&': if ((c = nextc(true)) == '&') { yylval = GET_INSTRUCTION(Op_and); - allow_newline(); + INSTRUCTION *new_comment = NULL; + allow_newline(& new_comment); + yylval->comment = new_comment; + return lasttok = LEX_AND; } pushback(); @@ -4066,11 +4169,15 @@ retry: case '|': if ((c = nextc(true)) == '|') { yylval = GET_INSTRUCTION(Op_or); - allow_newline(); + INSTRUCTION *new_comment = NULL; + allow_newline(& new_comment); + yylval->comment = new_comment; + return lasttok = LEX_OR; } else if (! do_traditional && c == '&') { yylval = GET_INSTRUCTION(Op_symbol); yylval->redir_type = redirect_twoway; + return lasttok = (in_print && in_parens == 0 ? IO_OUT : IO_IN); } pushback(); @@ -4260,8 +4367,11 @@ out: yylval->lextok = tokkey; #define SMART_ALECK 1 - if (SMART_ALECK && do_lint - && ! goto_warned && strcasecmp(tokkey, "goto") == 0) { + if (SMART_ALECK + && do_lint + && ! goto_warned + && tolower(tokkey[0]) == 'g' + && strcasecmp(tokkey, "goto") == 0) { goto_warned = true; lintwarn(_("`goto' considered harmful!")); } @@ -4732,15 +4842,8 @@ mk_function(INSTRUCTION *fi, INSTRUCTION *def) thisfunc = fi->func_body; assert(thisfunc != NULL); - /* 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; + /* + * Add an implicit return at end; * also used by 'return' command in debugger */ @@ -5451,12 +5554,7 @@ append_rule(INSTRUCTION *pattern, INSTRUCTION *action) (rp + 1)->lasti = action->lasti; (rp + 2)->first_line = pattern->source_line; (rp + 2)->last_line = lastline; - if (block_comment != NULL) { - ip = list_prepend(list_prepend(action, block_comment), rp); - block_comment = NULL; - } else - ip = list_prepend(action, rp); - + ip = list_prepend(action, rp); } else { rp = bcalloc(Op_rule, 3, 0); rp->in_rule = Rule; @@ -5861,8 +5959,9 @@ mk_for_loop(INSTRUCTION *forp, INSTRUCTION *init, INSTRUCTION *cond, forp->target_break = tbreak; forp->target_continue = tcont; ret = list_prepend(ret, forp); - } /* else - forp is NULL */ + } + /* else + forp is NULL */ return ret; } @@ -6076,26 +6175,6 @@ 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 @@ -6335,3 +6414,91 @@ set_profile_text(NODE *n, const char *str, size_t len) return n; } + +/* + * merge_comments --- merge c2 into c1 and free c2 if successful. + * Allow c2 to be NULL, in which case just merged chained + * comments in c1. + */ + +static void +merge_comments(INSTRUCTION *c1, INSTRUCTION *c2) +{ + assert(c1->opcode == Op_comment); + + if (c1->comment == NULL && c2 == NULL) // nothing to do + return; + + size_t total = c1->memory->stlen; + if (c1->comment != NULL) + total += 1 /* \n */ + c1->comment->memory->stlen; + + if (c2 != NULL) { + assert(c2->opcode == Op_comment); + total += 1 /* \n */ + c2->memory->stlen; + if (c2->comment != NULL) + total += c2->comment->memory->stlen + 1; + } + + char *buffer; + emalloc(buffer, char *, total + 1, "merge_comments"); + + strcpy(buffer, c1->memory->stptr); + if (c1->comment != NULL) { + strcat(buffer, "\n"); + strcat(buffer, c1->comment->memory->stptr); + } + + if (c2 != NULL) { + strcat(buffer, "\n"); + strcat(buffer, c2->memory->stptr); + if (c2->comment != NULL) { + strcat(buffer, "\n"); + strcat(buffer, c2->comment->memory->stptr); + } + + unref(c2->memory); + if (c2->comment != NULL) { + unref(c2->comment->memory); + bcfree(c2->comment); + c2->comment = NULL; + } + bcfree(c2); + } + + c1->memory->comment_type = BLOCK_COMMENT; + free(c1->memory->stptr); + c1->memory->stptr = buffer; + c1->memory->stlen = strlen(buffer); + + // now free everything else + if (c1->comment != NULL) { + unref(c1->comment->memory); + bcfree(c1->comment); + c1->comment = NULL; + } +} + +/* make_braced_statements --- handle `l_brace statements r_brace' with comments */ + +static INSTRUCTION * +make_braced_statements(INSTRUCTION *lbrace, INSTRUCTION *stmts, INSTRUCTION *rbrace) +{ + INSTRUCTION *ip; + + if (stmts == NULL) + ip = list_create(instruction(Op_no_op)); + else + ip = stmts; + + if (lbrace != NULL) { + INSTRUCTION *comment2 = lbrace->comment; + if (comment2 != NULL) { + ip = list_prepend(ip, comment2); + lbrace->comment = NULL; + } + ip = list_prepend(ip, lbrace); + } + + return ip; +} @@ -3807,7 +3807,12 @@ print_instruction(INSTRUCTION *pc, Func_print print_func, FILE *fp, int in_dump) break; case Op_K_do: - print_func(fp, "[doloop_cond = %p] [target_break = %p]\n", (pc+1)->doloop_cond, pc->target_break); + print_func(fp, "[doloop_cond = %p] [target_break = %p]", (pc+1)->doloop_cond, pc->target_break); + if (pc->comment) + print_func(fp, " [comment = %p]", pc->comment); + print_func(fp, "\n"); + if (pc->comment) + print_instruction(pc->comment, print_func, fp, in_dump); break; case Op_K_for: @@ -3815,15 +3820,44 @@ print_instruction(INSTRUCTION *pc, Func_print print_func, FILE *fp, int in_dump) /* fall through */ case Op_K_arrayfor: print_func(fp, "[forloop_body = %p] ", (pc+1)->forloop_body); - print_func(fp, "[target_break = %p] [target_continue = %p]\n", pc->target_break, pc->target_continue); + print_func(fp, "[target_break = %p] [target_continue = %p]", pc->target_break, pc->target_continue); + if (pc->comment != NULL) { + print_func(fp, " [comment = %p]\n", (pc)->comment); + print_instruction(pc->comment, print_func, fp, in_dump); + } else + print_func(fp, "\n"); break; case Op_K_switch: + { + bool need_newline = false; print_func(fp, "[switch_start = %p] [switch_end = %p]\n", (pc+1)->switch_start, (pc+1)->switch_end); + if (pc->comment || (pc+1)->switch_end->comment) + print_func(fp, "%*s", noffset, ""); + if (pc->comment) { + print_func(fp, "[start_comment = %p]", pc->comment); + need_newline = true; + } + if ((pc+1)->switch_end->comment) { + print_func(fp, "[end_comment = %p]", (pc + 1)->switch_end->comment); + need_newline = true; + } + if (need_newline) + print_func(fp, "\n"); + if (pc->comment) + print_instruction(pc->comment, print_func, fp, in_dump); + if ((pc+1)->switch_end->comment) + print_instruction((pc+1)->switch_end->comment, print_func, fp, in_dump); + } break; case Op_K_default: - print_func(fp, "[stmt_start = %p] [stmt_end = %p]\n", pc->stmt_start, pc->stmt_end); + print_func(fp, "[stmt_start = %p] [stmt_end = %p]", pc->stmt_start, pc->stmt_end); + if (pc->comment) { + print_func(fp, " [comment = %p]\n", pc->comment); + print_instruction(pc->comment, print_func, fp, in_dump); + } else + print_func(fp, "\n"); break; case Op_var_update: @@ -3915,8 +3949,13 @@ print_instruction(INSTRUCTION *pc, Func_print print_func, FILE *fp, int in_dump) break; case Op_K_case: - print_func(fp, "[target_jmp = %p] [match_exp = %s]\n", + print_func(fp, "[target_jmp = %p] [match_exp = %s]", pc->target_jmp, (pc + 1)->match_exp ? "true" : "false"); + if (pc->comment) { + print_func(fp, " [comment = %p]\n", pc->comment); + print_instruction(pc->comment, print_func, fp, in_dump); + } else + print_func(fp, "\n"); break; case Op_arrayfor_incr: @@ -4032,9 +4071,14 @@ print_instruction(INSTRUCTION *pc, Func_print print_func, FILE *fp, int in_dump) case Op_comment: print_memory(pc->memory, func, print_func, fp); - print_func(fp, " [comment_type = %s]\n", + print_func(fp, " [comment_type = %s]", pc->memory->comment_type == EOL_COMMENT ? - "EOL" : "FULL"); + "EOL" : "BLOCK"); + if (pc->comment) { + print_func(fp, " [comment = %p]\n", pc->comment); + print_instruction(pc->comment, print_func, fp, in_dump); + } else + print_func(fp, "\n"); break; case Op_push_i: diff --git a/doc/ChangeLog b/doc/ChangeLog index 98448839..2875d9a8 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -8,6 +8,10 @@ * gawktexi.in): Small typo fixes. Thanks to Antonio Giovanni Colombo for pointing them out. +2018-11-01 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in (Profiling): Review and update. + 2018-10-30 Arnold D. Robbins <arnold@skeeve.com> * gawktexi.in (Arrays of Arrays): Typo fix in code. Thanks to Alto Tom @@ -25,6 +29,12 @@ Communications Guidelines, with URL. * texinfo.tex: Updated from GNULIB. +2018-10-17 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in (Profiling): Revise example for pattern without + action and note that the profiler distinguishes `print' and + `print $0'. + 2018-09-23 Arnold D. Robbins <arnold@skeeve.com> * gawktexi.in (Extracting): Note that patch levels above diff --git a/doc/gawk.info b/doc/gawk.info index 6f5c2a31..e059a12e 100644 --- a/doc/gawk.info +++ b/doc/gawk.info @@ -20772,7 +20772,8 @@ output. They are as follows: structure of the program and the precedence rules. For example, '(3 + 5) * 4' means add three and five, then multiply the total by four. However, '3 + 5 * 4' has no parentheses, and means '3 + (5 * - 4)'. + 4)'. However, explicit parentheses in the source program are + retained. * Parentheses are used around the arguments to 'print' and 'printf' only when the 'print' or 'printf' statement is followed by a @@ -20794,10 +20795,11 @@ representation. Also, things such as: come out as: /foo/ { - print $0 + print } -which is correct, but possibly unexpected. +which is correct, but possibly unexpected. (If a program uses both +'print $0' and plain 'print', that distinction is retained.) Besides creating profiles when a program has completed, 'gawk' can produce a profile while it is running. This is useful if your 'awk' @@ -20853,7 +20855,10 @@ without any execution counts. profiling, and that created when pretty-printing. Pretty-printed output preserves the original comments that were in the program, although their placement may not correspond exactly to their original locations in the -source code.(1) +source code. However, no comments should be lost. Also, 'gawk' does +the best it can to preserve the distinction between comments at the end +of a statement and comments on lines by themselves. This isn't always +perfect, though. However, as a deliberate design decision, profiling output _omits_ the original program's comments. This allows you to focus on the @@ -20873,14 +20878,6 @@ disable 'gawk''s default optimizations. constants; if you used an octal or hexadecimal value in your source code, it will appear that way in the output. - ---------- Footnotes ---------- - - (1) 'gawk' does the best it can to preserve the distinction between -comments at the end of a statement and comments on lines by themselves. -Due to implementation constraints, it does not always do so correctly, -particularly for 'switch' statements. The 'gawk' maintainers hope to -improve this in a subsequent release. - File: gawk.info, Node: Advanced Features Summary, Prev: Profiling, Up: Advanced Features @@ -34156,7 +34153,7 @@ Index * dump debugger command: Miscellaneous Debugger Commands. (line 9) * dupword.awk program: Dupword Program. (line 31) -* dynamic profiling: Profiling. (line 177) +* dynamic profiling: Profiling. (line 179) * dynamically loaded extensions: Dynamic Extensions. (line 6) * e debugger command (alias for enable): Breakpoint Control. (line 73) * EBCDIC: Ordinal Functions. (line 45) @@ -34545,7 +34542,7 @@ Index * G., Daniel Richard: Acknowledgments. (line 60) * G., Daniel Richard <1>: Maintainers. (line 14) * Garfinkle, Scott: Contributors. (line 35) -* gawk program, dynamic profiling: Profiling. (line 177) +* gawk program, dynamic profiling: Profiling. (line 179) * gawk version: Auto-set. (line 241) * gawk, ARGIND variable in: Other Arguments. (line 15) * gawk, awk and: Preface. (line 21) @@ -34750,7 +34747,7 @@ Index * history expansion, in debugger: Readline Support. (line 6) * histsort.awk program: History Sorting. (line 25) * Hughes, Phil: Acknowledgments. (line 43) -* HUP signal, for dynamic profiling: Profiling. (line 209) +* HUP signal, for dynamic profiling: Profiling. (line 211) * hyphen (-), - operator: Precedence. (line 51) * hyphen (-), - operator <1>: Precedence. (line 57) * hyphen (-), -- operator: Increment Ops. (line 48) @@ -34832,7 +34829,7 @@ Index * installing gawk: Installation. (line 6) * instruction tracing, in debugger: Debugger Info. (line 90) * int: Numeric Functions. (line 24) -* INT signal (MS-Windows): Profiling. (line 212) +* INT signal (MS-Windows): Profiling. (line 214) * integer array indices: Numeric Array Subscripts. (line 31) * integers, arbitrary precision: Arbitrary Precision Integers. @@ -34888,7 +34885,7 @@ Index * Kernighan, Brian <8>: Other Versions. (line 13) * Kernighan, Brian <9>: Basic Data Typing. (line 54) * Kernighan, Brian <10>: Glossary. (line 204) -* kill command, dynamic profiling: Profiling. (line 186) +* kill command, dynamic profiling: Profiling. (line 188) * Knights, jedi: Undocumented. (line 6) * Kwok, Conrad: Contributors. (line 35) * l debugger command (alias for list): Miscellaneous Debugger Commands. @@ -35402,7 +35399,7 @@ Index * PROCINFO, values of sorted_in: Controlling Scanning. (line 26) * profiling awk programs: Profiling. (line 6) -* profiling awk programs, dynamically: Profiling. (line 177) +* profiling awk programs, dynamically: Profiling. (line 179) * program identifiers: Auto-set. (line 193) * program, definition of: Getting Started. (line 21) * programming conventions, --non-decimal-data option: Nondecimal Data. @@ -35439,7 +35436,7 @@ Index * QuikTrim Awk: Other Versions. (line 135) * quit debugger command: Miscellaneous Debugger Commands. (line 100) -* QUIT signal (MS-Windows): Profiling. (line 212) +* QUIT signal (MS-Windows): Profiling. (line 214) * quoting in gawk command lines: Long. (line 26) * quoting in gawk command lines, tricks for: Quoting. (line 91) * quoting, for small awk programs: Comments. (line 27) @@ -35729,15 +35726,15 @@ Index (line 130) * sidebar, Using \n in Bracket Expressions of Dynamic Regexps: Computed Regexps. (line 58) -* SIGHUP signal, for dynamic profiling: Profiling. (line 209) -* SIGINT signal (MS-Windows): Profiling. (line 212) -* signals, HUP/SIGHUP, for profiling: Profiling. (line 209) -* signals, INT/SIGINT (MS-Windows): Profiling. (line 212) -* signals, QUIT/SIGQUIT (MS-Windows): Profiling. (line 212) -* signals, USR1/SIGUSR1, for profiling: Profiling. (line 186) +* SIGHUP signal, for dynamic profiling: Profiling. (line 211) +* SIGINT signal (MS-Windows): Profiling. (line 214) +* signals, HUP/SIGHUP, for profiling: Profiling. (line 211) +* signals, INT/SIGINT (MS-Windows): Profiling. (line 214) +* signals, QUIT/SIGQUIT (MS-Windows): Profiling. (line 214) +* signals, USR1/SIGUSR1, for profiling: Profiling. (line 188) * signature program: Signature Program. (line 6) -* SIGQUIT signal (MS-Windows): Profiling. (line 212) -* SIGUSR1 signal, for dynamic profiling: Profiling. (line 186) +* SIGQUIT signal (MS-Windows): Profiling. (line 214) +* SIGUSR1 signal, for dynamic profiling: Profiling. (line 188) * silent debugger command: Debugger Execution Control. (line 10) * sin: Numeric Functions. (line 75) @@ -36017,7 +36014,7 @@ Index * user-modifiable variables: User-modified. (line 6) * users, information about, printing: Id Program. (line 6) * users, information about, retrieving: Passwd Functions. (line 16) -* USR1 signal, for dynamic profiling: Profiling. (line 186) +* USR1 signal, for dynamic profiling: Profiling. (line 188) * values, numeric: Basic Data Typing. (line 13) * values, string: Basic Data Typing. (line 13) * variable assignments and input files: Other Arguments. (line 26) @@ -36485,218 +36482,217 @@ Ref: Two-way I/O-Footnote-1835647 Ref: Two-way I/O-Footnote-2835834 Node: TCP/IP Networking835916 Node: Profiling839034 -Ref: Profiling-Footnote-1847706 -Node: Advanced Features Summary848029 -Node: Internationalization849873 -Node: I18N and L10N851353 -Node: Explaining gettext852040 -Ref: Explaining gettext-Footnote-1857932 -Ref: Explaining gettext-Footnote-2858117 -Node: Programmer i18n858282 -Ref: Programmer i18n-Footnote-1863231 -Node: Translator i18n863280 -Node: String Extraction864074 -Ref: String Extraction-Footnote-1865206 -Node: Printf Ordering865292 -Ref: Printf Ordering-Footnote-1868078 -Node: I18N Portability868142 -Ref: I18N Portability-Footnote-1870598 -Node: I18N Example870661 -Ref: I18N Example-Footnote-1873467 -Node: Gawk I18N873540 -Node: I18N Summary874185 -Node: Debugger875526 -Node: Debugging876549 -Node: Debugging Concepts876990 -Node: Debugging Terms878799 -Node: Awk Debugging881374 -Node: Sample Debugging Session882280 -Node: Debugger Invocation882814 -Node: Finding The Bug884200 -Node: List of Debugger Commands890678 -Node: Breakpoint Control892011 -Node: Debugger Execution Control895705 -Node: Viewing And Changing Data899067 -Node: Execution Stack902441 -Node: Debugger Info904078 -Node: Miscellaneous Debugger Commands908149 -Node: Readline Support913211 -Node: Limitations914107 -Node: Debugging Summary916216 -Node: Arbitrary Precision Arithmetic917495 -Node: Computer Arithmetic918980 -Ref: table-numeric-ranges922746 -Ref: table-floating-point-ranges923239 -Ref: Computer Arithmetic-Footnote-1923897 -Node: Math Definitions923954 -Ref: table-ieee-formats927270 -Ref: Math Definitions-Footnote-1927873 -Node: MPFR features927978 -Node: FP Math Caution929696 -Ref: FP Math Caution-Footnote-1930768 -Node: Inexactness of computations931137 -Node: Inexact representation932097 -Node: Comparing FP Values933457 -Node: Errors accumulate934698 -Node: Getting Accuracy936131 -Node: Try To Round938841 -Node: Setting precision939740 -Ref: table-predefined-precision-strings940437 -Node: Setting the rounding mode942267 -Ref: table-gawk-rounding-modes942641 -Ref: Setting the rounding mode-Footnote-1946572 -Node: Arbitrary Precision Integers946751 -Ref: Arbitrary Precision Integers-Footnote-1949926 -Node: Checking for MPFR950075 -Node: POSIX Floating Point Problems951549 -Ref: POSIX Floating Point Problems-Footnote-1955834 -Node: Floating point summary955872 -Node: Dynamic Extensions958062 -Node: Extension Intro959615 -Node: Plugin License960881 -Node: Extension Mechanism Outline961678 -Ref: figure-load-extension962117 -Ref: figure-register-new-function963682 -Ref: figure-call-new-function964774 -Node: Extension API Description966836 -Node: Extension API Functions Introduction968478 -Node: General Data Types974018 -Ref: General Data Types-Footnote-1982379 -Node: Memory Allocation Functions982678 -Ref: Memory Allocation Functions-Footnote-1986888 -Node: Constructor Functions986987 -Node: Registration Functions990573 -Node: Extension Functions991258 -Node: Exit Callback Functions996473 -Node: Extension Version String997723 -Node: Input Parsers998386 -Node: Output Wrappers1011107 -Node: Two-way processors1015619 -Node: Printing Messages1017884 -Ref: Printing Messages-Footnote-11019055 -Node: Updating ERRNO1019208 -Node: Requesting Values1019947 -Ref: table-value-types-returned1020684 -Node: Accessing Parameters1021620 -Node: Symbol Table Access1022855 -Node: Symbol table by name1023367 -Node: Symbol table by cookie1025156 -Ref: Symbol table by cookie-Footnote-11029341 -Node: Cached values1029405 -Ref: Cached values-Footnote-11032941 -Node: Array Manipulation1033094 -Ref: Array Manipulation-Footnote-11034185 -Node: Array Data Types1034222 -Ref: Array Data Types-Footnote-11036880 -Node: Array Functions1036972 -Node: Flattening Arrays1041470 -Node: Creating Arrays1048446 -Node: Redirection API1053213 -Node: Extension API Variables1056046 -Node: Extension Versioning1056757 -Ref: gawk-api-version1057186 -Node: Extension GMP/MPFR Versioning1058917 -Node: Extension API Informational Variables1060545 -Node: Extension API Boilerplate1061618 -Node: Changes from API V11065592 -Node: Finding Extensions1067164 -Node: Extension Example1067723 -Node: Internal File Description1068521 -Node: Internal File Ops1072601 -Ref: Internal File Ops-Footnote-11083951 -Node: Using Internal File Ops1084091 -Ref: Using Internal File Ops-Footnote-11086474 -Node: Extension Samples1086748 -Node: Extension Sample File Functions1088277 -Node: Extension Sample Fnmatch1095926 -Node: Extension Sample Fork1097413 -Node: Extension Sample Inplace1098631 -Node: Extension Sample Ord1101848 -Node: Extension Sample Readdir1102684 -Ref: table-readdir-file-types1103573 -Node: Extension Sample Revout1104378 -Node: Extension Sample Rev2way1104967 -Node: Extension Sample Read write array1105707 -Node: Extension Sample Readfile1107649 -Node: Extension Sample Time1108744 -Node: Extension Sample API Tests1110092 -Node: gawkextlib1110584 -Node: Extension summary1113502 -Node: Extension Exercises1117204 -Node: Language History1118702 -Node: V7/SVR3.11120358 -Node: SVR41122510 -Node: POSIX1123944 -Node: BTL1125324 -Node: POSIX/GNU1126053 -Node: Feature History1131831 -Node: Common Extensions1147690 -Node: Ranges and Locales1148973 -Ref: Ranges and Locales-Footnote-11153589 -Ref: Ranges and Locales-Footnote-21153616 -Ref: Ranges and Locales-Footnote-31153851 -Node: Contributors1154072 -Node: History summary1160017 -Node: Installation1161397 -Node: Gawk Distribution1162341 -Node: Getting1162825 -Node: Extracting1163788 -Node: Distribution contents1165426 -Node: Unix Installation1171906 -Node: Quick Installation1172588 -Node: Shell Startup Files1175002 -Node: Additional Configuration Options1176091 -Node: Configuration Philosophy1178256 -Node: Non-Unix Installation1180625 -Node: PC Installation1181085 -Node: PC Binary Installation1181923 -Node: PC Compiling1182358 -Node: PC Using1183475 -Node: Cygwin1186690 -Node: MSYS1187789 -Node: VMS Installation1188290 -Node: VMS Compilation1189081 -Ref: VMS Compilation-Footnote-11190310 -Node: VMS Dynamic Extensions1190368 -Node: VMS Installation Details1192053 -Node: VMS Running1194306 -Node: VMS GNV1198585 -Node: VMS Old Gawk1199320 -Node: Bugs1199791 -Node: Bug address1200454 -Node: Usenet1203436 -Node: Maintainers1204440 -Node: Other Versions1205701 -Node: Installation summary1212259 -Node: Notes1213461 -Node: Compatibility Mode1214255 -Node: Additions1215037 -Node: Accessing The Source1215962 -Node: Adding Code1217399 -Node: New Ports1223618 -Node: Derived Files1228106 -Ref: Derived Files-Footnote-11233752 -Ref: Derived Files-Footnote-21233787 -Ref: Derived Files-Footnote-31234385 -Node: Future Extensions1234499 -Node: Implementation Limitations1235157 -Node: Extension Design1236340 -Node: Old Extension Problems1237484 -Ref: Old Extension Problems-Footnote-11239002 -Node: Extension New Mechanism Goals1239059 -Ref: Extension New Mechanism Goals-Footnote-11242423 -Node: Extension Other Design Decisions1242612 -Node: Extension Future Growth1244725 -Node: Notes summary1245561 -Node: Basic Concepts1246736 -Node: Basic High Level1247417 -Ref: figure-general-flow1247699 -Ref: figure-process-flow1248384 -Ref: Basic High Level-Footnote-11251685 -Node: Basic Data Typing1251870 -Node: Glossary1255198 -Node: Copying1287036 -Node: GNU Free Documentation License1324579 -Node: Index1349699 +Node: Advanced Features Summary848039 +Node: Internationalization849883 +Node: I18N and L10N851363 +Node: Explaining gettext852050 +Ref: Explaining gettext-Footnote-1857942 +Ref: Explaining gettext-Footnote-2858127 +Node: Programmer i18n858292 +Ref: Programmer i18n-Footnote-1863241 +Node: Translator i18n863290 +Node: String Extraction864084 +Ref: String Extraction-Footnote-1865216 +Node: Printf Ordering865302 +Ref: Printf Ordering-Footnote-1868088 +Node: I18N Portability868152 +Ref: I18N Portability-Footnote-1870608 +Node: I18N Example870671 +Ref: I18N Example-Footnote-1873477 +Node: Gawk I18N873550 +Node: I18N Summary874195 +Node: Debugger875536 +Node: Debugging876559 +Node: Debugging Concepts877000 +Node: Debugging Terms878809 +Node: Awk Debugging881384 +Node: Sample Debugging Session882290 +Node: Debugger Invocation882824 +Node: Finding The Bug884210 +Node: List of Debugger Commands890688 +Node: Breakpoint Control892021 +Node: Debugger Execution Control895715 +Node: Viewing And Changing Data899077 +Node: Execution Stack902451 +Node: Debugger Info904088 +Node: Miscellaneous Debugger Commands908159 +Node: Readline Support913221 +Node: Limitations914117 +Node: Debugging Summary916226 +Node: Arbitrary Precision Arithmetic917505 +Node: Computer Arithmetic918990 +Ref: table-numeric-ranges922756 +Ref: table-floating-point-ranges923249 +Ref: Computer Arithmetic-Footnote-1923907 +Node: Math Definitions923964 +Ref: table-ieee-formats927280 +Ref: Math Definitions-Footnote-1927883 +Node: MPFR features927988 +Node: FP Math Caution929706 +Ref: FP Math Caution-Footnote-1930778 +Node: Inexactness of computations931147 +Node: Inexact representation932107 +Node: Comparing FP Values933467 +Node: Errors accumulate934708 +Node: Getting Accuracy936141 +Node: Try To Round938851 +Node: Setting precision939750 +Ref: table-predefined-precision-strings940447 +Node: Setting the rounding mode942277 +Ref: table-gawk-rounding-modes942651 +Ref: Setting the rounding mode-Footnote-1946582 +Node: Arbitrary Precision Integers946761 +Ref: Arbitrary Precision Integers-Footnote-1949936 +Node: Checking for MPFR950085 +Node: POSIX Floating Point Problems951559 +Ref: POSIX Floating Point Problems-Footnote-1955844 +Node: Floating point summary955882 +Node: Dynamic Extensions958072 +Node: Extension Intro959625 +Node: Plugin License960891 +Node: Extension Mechanism Outline961688 +Ref: figure-load-extension962127 +Ref: figure-register-new-function963692 +Ref: figure-call-new-function964784 +Node: Extension API Description966846 +Node: Extension API Functions Introduction968488 +Node: General Data Types974028 +Ref: General Data Types-Footnote-1982389 +Node: Memory Allocation Functions982688 +Ref: Memory Allocation Functions-Footnote-1986898 +Node: Constructor Functions986997 +Node: Registration Functions990583 +Node: Extension Functions991268 +Node: Exit Callback Functions996483 +Node: Extension Version String997733 +Node: Input Parsers998396 +Node: Output Wrappers1011117 +Node: Two-way processors1015629 +Node: Printing Messages1017894 +Ref: Printing Messages-Footnote-11019065 +Node: Updating ERRNO1019218 +Node: Requesting Values1019957 +Ref: table-value-types-returned1020694 +Node: Accessing Parameters1021630 +Node: Symbol Table Access1022865 +Node: Symbol table by name1023377 +Node: Symbol table by cookie1025166 +Ref: Symbol table by cookie-Footnote-11029351 +Node: Cached values1029415 +Ref: Cached values-Footnote-11032951 +Node: Array Manipulation1033104 +Ref: Array Manipulation-Footnote-11034195 +Node: Array Data Types1034232 +Ref: Array Data Types-Footnote-11036890 +Node: Array Functions1036982 +Node: Flattening Arrays1041480 +Node: Creating Arrays1048456 +Node: Redirection API1053223 +Node: Extension API Variables1056056 +Node: Extension Versioning1056767 +Ref: gawk-api-version1057196 +Node: Extension GMP/MPFR Versioning1058927 +Node: Extension API Informational Variables1060555 +Node: Extension API Boilerplate1061628 +Node: Changes from API V11065602 +Node: Finding Extensions1067174 +Node: Extension Example1067733 +Node: Internal File Description1068531 +Node: Internal File Ops1072611 +Ref: Internal File Ops-Footnote-11083961 +Node: Using Internal File Ops1084101 +Ref: Using Internal File Ops-Footnote-11086484 +Node: Extension Samples1086758 +Node: Extension Sample File Functions1088287 +Node: Extension Sample Fnmatch1095936 +Node: Extension Sample Fork1097423 +Node: Extension Sample Inplace1098641 +Node: Extension Sample Ord1101858 +Node: Extension Sample Readdir1102694 +Ref: table-readdir-file-types1103583 +Node: Extension Sample Revout1104388 +Node: Extension Sample Rev2way1104977 +Node: Extension Sample Read write array1105717 +Node: Extension Sample Readfile1107659 +Node: Extension Sample Time1108754 +Node: Extension Sample API Tests1110102 +Node: gawkextlib1110594 +Node: Extension summary1113512 +Node: Extension Exercises1117214 +Node: Language History1118712 +Node: V7/SVR3.11120368 +Node: SVR41122520 +Node: POSIX1123954 +Node: BTL1125334 +Node: POSIX/GNU1126063 +Node: Feature History1131841 +Node: Common Extensions1147700 +Node: Ranges and Locales1148983 +Ref: Ranges and Locales-Footnote-11153599 +Ref: Ranges and Locales-Footnote-21153626 +Ref: Ranges and Locales-Footnote-31153861 +Node: Contributors1154082 +Node: History summary1160027 +Node: Installation1161407 +Node: Gawk Distribution1162351 +Node: Getting1162835 +Node: Extracting1163798 +Node: Distribution contents1165436 +Node: Unix Installation1171916 +Node: Quick Installation1172598 +Node: Shell Startup Files1175012 +Node: Additional Configuration Options1176101 +Node: Configuration Philosophy1178266 +Node: Non-Unix Installation1180635 +Node: PC Installation1181095 +Node: PC Binary Installation1181933 +Node: PC Compiling1182368 +Node: PC Using1183485 +Node: Cygwin1186700 +Node: MSYS1187799 +Node: VMS Installation1188300 +Node: VMS Compilation1189091 +Ref: VMS Compilation-Footnote-11190320 +Node: VMS Dynamic Extensions1190378 +Node: VMS Installation Details1192063 +Node: VMS Running1194316 +Node: VMS GNV1198595 +Node: VMS Old Gawk1199330 +Node: Bugs1199801 +Node: Bug address1200464 +Node: Usenet1203446 +Node: Maintainers1204450 +Node: Other Versions1205711 +Node: Installation summary1212269 +Node: Notes1213471 +Node: Compatibility Mode1214265 +Node: Additions1215047 +Node: Accessing The Source1215972 +Node: Adding Code1217409 +Node: New Ports1223628 +Node: Derived Files1228116 +Ref: Derived Files-Footnote-11233762 +Ref: Derived Files-Footnote-21233797 +Ref: Derived Files-Footnote-31234395 +Node: Future Extensions1234509 +Node: Implementation Limitations1235167 +Node: Extension Design1236350 +Node: Old Extension Problems1237494 +Ref: Old Extension Problems-Footnote-11239012 +Node: Extension New Mechanism Goals1239069 +Ref: Extension New Mechanism Goals-Footnote-11242433 +Node: Extension Other Design Decisions1242622 +Node: Extension Future Growth1244735 +Node: Notes summary1245571 +Node: Basic Concepts1246746 +Node: Basic High Level1247427 +Ref: figure-general-flow1247709 +Ref: figure-process-flow1248394 +Ref: Basic High Level-Footnote-11251695 +Node: Basic Data Typing1251880 +Node: Glossary1255208 +Node: Copying1287046 +Node: GNU Free Documentation License1324589 +Node: Index1349709 End Tag Table diff --git a/doc/gawk.texi b/doc/gawk.texi index 9b890fe6..22358d1e 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -29097,6 +29097,7 @@ of the program and the precedence rules. For example, @samp{(3 + 5) * 4} means add three and five, then multiply the total by four. However, @samp{3 + 5 * 4} has no parentheses, and means @samp{3 + (5 * 4)}. +However, explicit parentheses in the source program are retained. @ignore @item @@ -29135,12 +29136,14 @@ come out as: @example /foo/ @{ - print $0 + print @} @end example @noindent which is correct, but possibly unexpected. +(If a program uses both @samp{print $0} and plain +@samp{print}, that distinction is retained.) @cindex profiling @command{awk} programs, dynamically @cindex @command{gawk} program, dynamic profiling @@ -29225,12 +29228,10 @@ There is a significant difference between the output created when profiling, and that created when pretty-printing. Pretty-printed output preserves the original comments that were in the program, although their placement may not correspond exactly to their original locations in the -source code.@footnote{@command{gawk} does the best it can to preserve +source code. However, no comments should be lost. +Also, @command{gawk} does the best it can to preserve the distinction between comments at the end of a statement and comments -on lines by themselves. Due to implementation constraints, it does not -always do so correctly, particularly for @code{switch} statements. The -@command{gawk} maintainers hope to improve this in a subsequent -release.} +on lines by themselves. This isn't always perfect, though. However, as a deliberate design decision, profiling output @emph{omits} the original program's comments. This allows you to focus on the diff --git a/doc/gawktexi.in b/doc/gawktexi.in index f4bf9860..3484cde2 100644 --- a/doc/gawktexi.in +++ b/doc/gawktexi.in @@ -28110,6 +28110,7 @@ of the program and the precedence rules. For example, @samp{(3 + 5) * 4} means add three and five, then multiply the total by four. However, @samp{3 + 5 * 4} has no parentheses, and means @samp{3 + (5 * 4)}. +However, explicit parentheses in the source program are retained. @ignore @item @@ -28148,12 +28149,14 @@ come out as: @example /foo/ @{ - print $0 + print @} @end example @noindent which is correct, but possibly unexpected. +(If a program uses both @samp{print $0} and plain +@samp{print}, that distinction is retained.) @cindex profiling @command{awk} programs, dynamically @cindex @command{gawk} program, dynamic profiling @@ -28238,12 +28241,10 @@ There is a significant difference between the output created when profiling, and that created when pretty-printing. Pretty-printed output preserves the original comments that were in the program, although their placement may not correspond exactly to their original locations in the -source code.@footnote{@command{gawk} does the best it can to preserve +source code. However, no comments should be lost. +Also, @command{gawk} does the best it can to preserve the distinction between comments at the end of a statement and comments -on lines by themselves. Due to implementation constraints, it does not -always do so correctly, particularly for @code{switch} statements. The -@command{gawk} maintainers hope to improve this in a subsequent -release.} +on lines by themselves. This isn't always perfect, though. However, as a deliberate design decision, profiling output @emph{omits} the original program's comments. This allows you to focus on the @@ -3,7 +3,7 @@ */ /* - * Copyright (C) 1999-2017 the Free Software Foundation, Inc. + * Copyright (C) 1999-2018 the Free Software Foundation, Inc. * * This file is part of GAWK, the GNU implementation of the * AWK Programming Language. @@ -37,7 +37,7 @@ static char *pp_typed_regex(const char *in_str, size_t len, int delim); static bool is_binary(int type); static bool is_scalar(int type); static int prec_level(int type); -static void pp_push(int type, char *s, int flag); +static void pp_push(int type, char *s, int flag, INSTRUCTION *comment); static NODE *pp_pop(void); static void print_comment(INSTRUCTION *pc, long in); const char *redir2str(int redirtype); @@ -45,6 +45,7 @@ const char *redir2str(int redirtype); #define pp_str vname #define pp_len sub.nodep.reserved #define pp_next rnode +#define pp_comment sub.nodep.x.cmnt #define DONT_FREE 1 #define CAN_FREE 2 @@ -60,6 +61,15 @@ static FILE *prof_fp; /* where to send the profile */ static long indent_level = 0; +static const char tabs[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; +static const size_t tabs_len = sizeof(tabs) - 1; + +#define check_indent_level() \ + if (indent_level + 1 > tabs_len) \ + /* We're allowed to be snarky, occasionally. */ \ + fatal(_("Program indentation level too deep. Consider refactoring your code")); + + #define SPACEOVER 0 #define NO_PPRINT_FLAGS 0 @@ -159,7 +169,7 @@ indent_out(void) /* pp_push --- push a pretty printed string onto the stack */ static void -pp_push(int type, char *s, int flag) +pp_push(int type, char *s, int flag, INSTRUCTION *comment) { NODE *n; getnode(n); @@ -168,6 +178,7 @@ pp_push(int type, char *s, int flag) n->flags = flag; n->type = type; n->pp_next = pp_stack; + n->pp_comment = comment; pp_stack = n; } @@ -238,9 +249,9 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) if (rule != Rule) { /* Allow for pre-non-rule-block comment */ - if (pc->nexti != (pc +1)->firsti + if (pc->nexti != (pc+1)->firsti && pc->nexti->opcode == Op_comment - && pc->nexti->memory->comment_type == FULL_COMMENT) + && pc->nexti->memory->comment_type == BLOCK_COMMENT) print_comment(pc->nexti, -1); ip1 = (pc + 1)->firsti; ip2 = (pc + 1)->lasti; @@ -302,9 +313,9 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) case Op_push_i: m = pc->memory; if (m == Nnull_string) /* optional return or exit value; don't print 0 or "" */ - pp_push(pc->opcode, m->stptr, DONT_FREE); + pp_push(pc->opcode, m->stptr, DONT_FREE, pc->comment); else if ((m->flags & NUMBER) != 0) - pp_push(pc->opcode, pp_number(m), CAN_FREE); + pp_push(pc->opcode, pp_number(m), CAN_FREE, pc->comment); else { str = pp_string(m->stptr, m->stlen, '"'); if ((m->flags & INTLSTR) != 0) { @@ -312,13 +323,13 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) str = pp_group3("_", tmp, ""); efree(tmp); } - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); } break; case Op_store_var: if (pc->initval != NULL) - pp_push(Op_push_i, pp_node(pc->initval), CAN_FREE); + pp_push(Op_push_i, pp_node(pc->initval), CAN_FREE, pc->comment); /* fall through */ case Op_store_sub: case Op_assign_concat: @@ -331,14 +342,14 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) m = pc->memory; switch (m->type) { case Node_param_list: - pp_push(pc->opcode, func_params[m->param_cnt].param, DONT_FREE); + pp_push(pc->opcode, func_params[m->param_cnt].param, DONT_FREE, pc->comment); break; case Node_var: case Node_var_new: case Node_var_array: if (m->vname != NULL) - pp_push(pc->opcode, m->vname, DONT_FREE); + pp_push(pc->opcode, m->vname, DONT_FREE, pc->comment); else fatal(_("internal error: %s with null vname"), nodetype2str(m->type)); @@ -390,7 +401,7 @@ cleanup: str = pp_group3(t1->pp_str, tmp, ""); efree(tmp); pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_and: @@ -399,10 +410,24 @@ cleanup: t2 = pp_pop(); t1 = pp_pop(); parenthesize(pc->opcode, t1, t2); - str = pp_group3(t1->pp_str, op2str(pc->opcode), t2->pp_str); + if (pc->comment == NULL) + str = pp_group3(t1->pp_str, op2str(pc->opcode), t2->pp_str); + else { + check_indent_level(); + + size_t len = strlen(t1->pp_str) + + strlen(op2str(pc->opcode)) + strlen(t2->pp_str) // foo && bar + + indent_level + 1 // indent + + pc->comment->memory->stlen + 3; // tab comment + + emalloc(str, char *, len, "pprint"); + sprintf(str, "%s%s%s%.*s %s", t1->pp_str, op2str(pc->opcode), + pc->comment->memory->stptr, + (int) (indent_level + 1), tabs, t2->pp_str); + } pp_free(t1); pp_free(t2); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); pc = pc->target_jmp; break; @@ -424,14 +449,14 @@ cleanup: str = pp_group3(t1->pp_str, op2str(pc->opcode), tmp); efree(tmp); pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_parens: t1 = pp_pop(); str = pp_group3("(", t1->pp_str, ")"); pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_plus: @@ -452,7 +477,7 @@ cleanup: str = pp_group3(t1->pp_str, op2str(pc->opcode), t2->pp_str); pp_free(t1); pp_free(t2); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_preincrement: @@ -465,7 +490,7 @@ cleanup: else str = pp_group3(t1->pp_str, op2str(pc->opcode), ""); pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_field_spec: @@ -483,7 +508,7 @@ cleanup: /* optypes table (eval.c) includes space after ! */ str = pp_group3(op2str(pc->opcode), t1->pp_str, ""); pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_assign: @@ -498,7 +523,7 @@ cleanup: str = pp_group3(t2->pp_str, op2str(pc->opcode), t1->pp_str); pp_free(t2); pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_store_field: @@ -515,7 +540,7 @@ cleanup: case Op_concat: str = pp_concat(pc->expr_count); - pp_push(Op_concat, str, CAN_FREE); + pp_push(Op_concat, str, CAN_FREE, pc->comment); break; case Op_K_delete: @@ -560,7 +585,7 @@ cleanup: pp_free(t2); } pp_free(t1); - pp_push(Op_in_array, str, CAN_FREE); + pp_push(Op_in_array, str, CAN_FREE, pc->comment); } break; @@ -595,7 +620,7 @@ cleanup: tmp = pp_list(pc->expr_count, "()", ", "); str = pp_group3(fname, tmp, ""); efree(tmp); - pp_push(Op_sub_builtin, str, CAN_FREE); + pp_push(Op_sub_builtin, str, CAN_FREE, pc->comment); } break; @@ -614,7 +639,7 @@ cleanup: efree(tmp); } else str = pp_group3(fname, "()", ""); - pp_push(Op_builtin, str, CAN_FREE); + pp_push(Op_builtin, str, CAN_FREE, pc->comment); } else fatal(_("internal error: builtin with null fname")); } @@ -624,7 +649,8 @@ cleanup: case Op_K_printf: case Op_K_print_rec: if (pc->opcode == Op_K_print_rec) - tmp = pp_group3(" ", op2str(Op_field_spec), "0"); + // instead of `print $0', just `print' + tmp = strdup(""); else if (pc->redir_type != 0) tmp = pp_list(pc->expr_count, "()", ", "); else { @@ -660,7 +686,7 @@ cleanup: assert((pc->memory->flags & REGEX) != 0); str = pp_typed_regex(pc->memory->stptr, pc->memory->stlen, '/'); } - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); } break; @@ -692,7 +718,7 @@ cleanup: efree(restr); } pp_free(t1); - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); } break; @@ -720,7 +746,7 @@ cleanup: pp_free(t2); } else str = tmp; - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); break; case Op_indirect_func_call: @@ -745,7 +771,7 @@ cleanup: t1 = pp_pop(); /* indirect var */ pp_free(t1); } - pp_push(pc->opcode, str, CAN_FREE); + pp_push(pc->opcode, str, CAN_FREE, pc->comment); } break; @@ -786,7 +812,7 @@ cleanup: str = pp_group3(t1->pp_str, ", ", t2->pp_str); pp_free(t1); pp_free(t2); - pp_push(Op_line_range, str, CAN_FREE); + pp_push(Op_line_range, str, CAN_FREE, pc->comment); pc = ip1->condpair_right; break; @@ -820,13 +846,34 @@ cleanup: indent(SPACEOVER); t1 = pp_pop(); fprintf(prof_fp, "} %s (%s)", op2str(Op_K_while), t1->pp_str); + if (pc->comment) + fprintf(prof_fp, "\t%s", pc->comment->memory->stptr); + else { + end_line(pc->target_break); + skip_comment = true; + } pp_free(t1); - end_line(pc->target_break); - skip_comment = true; pc = pc->target_break; break; case Op_K_for: + { + INSTRUCTION *comment1 = NULL, *comment2 = NULL; + + if (pc->comment != NULL) { + comment1 = pc->comment; + pc->comment = NULL; + if (comment1 != NULL && comment1->comment != NULL) { + comment2 = comment1->comment; + comment1->comment = NULL; + } + if (comment2 == NULL && comment1->memory->comment_type == FOR_COMMENT) { + comment2 = comment1; + comment2->memory->comment_type = EOL_COMMENT; + comment1 = NULL; + } + } + ip1 = pc + 1; indent(ip1->forloop_body->exec_count); fprintf(prof_fp, "%s (", op2str(pc->opcode)); @@ -834,12 +881,19 @@ cleanup: /* If empty for looop header, print it a little more nicely. */ if ( pc->nexti->opcode == Op_no_op && ip1->forloop_cond == pc->nexti - && pc->target_continue->opcode == Op_jmp) { + && pc->target_continue->opcode == Op_jmp + && comment1 == NULL && comment2 == NULL) { fprintf(prof_fp, ";;"); } else { pprint(pc->nexti, ip1->forloop_cond, IN_FOR_HEADER); fprintf(prof_fp, "; "); + if (comment1 != NULL) { + print_comment(comment1, 0); + indent(ip1->forloop_body->exec_count); + indent(1); + } + if (ip1->forloop_cond->opcode == Op_no_op && ip1->forloop_cond->nexti == ip1->forloop_body) fprintf(prof_fp, "; "); @@ -850,6 +904,12 @@ cleanup: pp_free(t1); } + if (comment2 != NULL) { + print_comment(comment2, 0); + indent(ip1->forloop_body->exec_count); + indent(1); + } + pprint(pc->target_continue, pc->target_break, IN_FOR_HEADER); } fprintf(prof_fp, ") {"); @@ -863,6 +923,7 @@ cleanup: end_line(pc->target_break); skip_comment = true; pc = pc->target_break; + } break; case Op_K_arrayfor: @@ -901,10 +962,14 @@ cleanup: pprint(pc->nexti, ip1->switch_start, NO_PPRINT_FLAGS); t1 = pp_pop(); fprintf(prof_fp, "%s) {\n", t1->pp_str); + if (pc->comment) + print_comment(pc->comment, 0); pp_free(t1); pprint(ip1->switch_start, ip1->switch_end, NO_PPRINT_FLAGS); indent(SPACEOVER); fprintf(prof_fp, "}\n"); + if (ip1->switch_end->comment) + print_comment(ip1->switch_end->comment, 0); pc = pc->target_break; break; @@ -914,13 +979,20 @@ cleanup: if (pc->opcode == Op_K_case) { t1 = pp_pop(); fprintf(prof_fp, "%s %s:", op2str(pc->opcode), t1->pp_str); - pc = end_line(pc); pp_free(t1); - } else { + } else fprintf(prof_fp, "%s:", op2str(pc->opcode)); - pc = end_line(pc); - } + indent_in(); + if (pc->comment != NULL) { + if (pc->comment->memory->comment_type == EOL_COMMENT) + fprintf(prof_fp, "\t%s", pc->comment->memory->stptr); + else { + fprintf(prof_fp, "\n"); + print_comment(pc->comment, indent_level); + } + } else + fprintf(prof_fp, "\n"); pprint(pc->stmt_start->nexti, pc->stmt_end->nexti, NO_PPRINT_FLAGS); indent_out(); break; @@ -937,6 +1009,8 @@ cleanup: fprintf(prof_fp, " # %ld", ip1->exec_count); ip1 = end_line(ip1); indent_in(); + if (pc->comment != NULL) + print_comment(pc->comment, indent_level); pprint(ip1->nexti, pc->branch_else, NO_PPRINT_FLAGS); indent_out(); pc = pc->branch_else; @@ -944,7 +1018,7 @@ cleanup: indent(SPACEOVER); fprintf(prof_fp, "}"); if (pc->nexti->nexti->opcode != Op_comment - || pc->nexti->nexti->memory->comment_type == FULL_COMMENT) + || pc->nexti->nexti->memory->comment_type == BLOCK_COMMENT) fprintf(prof_fp, "\n"); /* else It will be printed at the top. */ @@ -980,6 +1054,8 @@ cleanup: end_line(pc); skip_comment = true; indent_in(); + if (pc->comment != NULL) + print_comment(pc->comment, indent_level); pprint(pc->nexti, pc->branch_end, NO_PPRINT_FLAGS); indent_out(); indent(SPACEOVER); @@ -999,6 +1075,9 @@ cleanup: { NODE *f, *t, *cond; size_t len; + INSTRUCTION *qm_comment = NULL, *colon_comment = NULL; + + qm_comment = pc->comment; pprint(pc->nexti, pc->branch_if, NO_PPRINT_FLAGS); ip1 = pc->branch_if; @@ -1006,6 +1085,7 @@ cleanup: ip1 = pc->branch_else->nexti; pc = ip1->nexti; + colon_comment = pc->comment; assert(pc->opcode == Op_cond_exp); pprint(pc->nexti, pc->branch_end, NO_PPRINT_FLAGS); @@ -1013,14 +1093,77 @@ cleanup: t = pp_pop(); cond = pp_pop(); - len = f->pp_len + t->pp_len + cond->pp_len + 12; - emalloc(str, char *, len, "pprint"); - sprintf(str, "%s ? %s : %s", cond->pp_str, t->pp_str, f->pp_str); + /* + * This stuff handles comments that come after a ?, :, or both. + * Allowing newlines after ? and : is a gawk extension. + * Theoretically this is fragile, since ?: expressions can be nested. + * In practice, it's not, since if there was a comment following ? or : + * in the original code, then it wasn't nested. + */ + + len = f->pp_len + t->pp_len + cond->pp_len + 12; + if (qm_comment == NULL && colon_comment == NULL) { + // easy case + emalloc(str, char *, len, "pprint"); + sprintf(str, "%s ? %s : %s", cond->pp_str, t->pp_str, f->pp_str); + } else if (qm_comment != NULL && colon_comment != NULL) { + check_indent_level(); + len += qm_comment->memory->stlen + // comments + colon_comment->memory->stlen + + 2 * (indent_level + 1) + 3 + // indentation + t->pp_len + 6; + emalloc(str, char *, len, "pprint"); + sprintf(str, + "%s ? %s" // cond ? comment + "%.*s %s" // indent true-part + " : %s" // : comment + "%.*s %s", // indent false-part + cond->pp_str, // condition + qm_comment->memory->stptr, // comment + (int) (indent_level + 1), tabs, // indent + t->pp_str, // true part + colon_comment->memory->stptr, // comment + (int) (indent_level + 1), tabs, // indent + f->pp_str // false part + ); + } else if (qm_comment != NULL) { + check_indent_level(); + len += qm_comment->memory->stlen + // comment + 1 * (indent_level + 1) + 3 + // indentation + t->pp_len + 3; + emalloc(str, char *, len, "pprint"); + sprintf(str, + "%s ? %s" // cond ? comment + "%.*s %s" // indent true-part + " : %s", // : false-part + cond->pp_str, // condition + qm_comment->memory->stptr, // comment + (int) (indent_level + 1), tabs, // indent + t->pp_str, // true part + f->pp_str // false part + ); + } else { + check_indent_level(); + len += colon_comment->memory->stlen + // comment + 1 * (indent_level + 1) + 3 + // indentation + t->pp_len + 3; + emalloc(str, char *, len, "pprint"); + sprintf(str, + "%s ? %s" // cond ? true-part + " : %s" // : comment + "%.*s %s", // indent false-part + cond->pp_str, // condition + t->pp_str, // true part + colon_comment->memory->stptr, // comment + (int) (indent_level + 1), tabs, // indent + f->pp_str // false part + ); + } pp_free(cond); pp_free(t); pp_free(f); - pp_push(Op_cond_exp, str, CAN_FREE); + pp_push(Op_cond_exp, str, CAN_FREE, pc->comment); pc = pc->branch_end; } break; @@ -1065,7 +1208,7 @@ end_line(INSTRUCTION *ip) return ret; } -/* pp_string_fp --- printy print a string to the fp */ +/* pp_string_fp --- pretty print a string to the fp */ /* * This routine concentrates string pretty printing in one place, @@ -1124,17 +1267,59 @@ print_lib_list(FILE *prof_fp) { SRCFILE *s; static bool printed_header = false; + const char *indent = ""; + bool found = false; + + if (do_profile) + indent = "\t"; for (s = srcfiles->next; s != srcfiles; s = s->next) { if (s->stype == SRC_EXTLIB) { + if (do_profile && ! printed_header) { + printed_header = true; + fprintf(prof_fp, _("%s# Loaded extensions (-l and/or @load)\n\n"), indent); + } + found = true; + fprintf(prof_fp, "%s@load \"%s\"", indent, s->src); + if (s->comment != NULL) { + fprintf(prof_fp, "\t"); + print_comment(s->comment, indent_level + 1); + } else + fprintf(prof_fp, "\n"); + } + } + if (found) /* we found some */ + fprintf(prof_fp, "\n"); +} + +/* print_include_list --- print a list of all files included */ + +static void +print_include_list(FILE *prof_fp) +{ + SRCFILE *s; + static bool printed_header = false; + bool found = false; + + if (do_profile) + return; + + for (s = srcfiles->next; s != srcfiles; s = s->next) { + if (s->stype == SRC_INC) { if (! printed_header) { printed_header = true; - fprintf(prof_fp, _("\t# Loaded extensions (-l and/or @load)\n\n")); + fprintf(prof_fp, _("\n# Included files (-i and/or @include)\n\n")); } - fprintf(prof_fp, "\t@load \"%s\"\n", s->src); + found = true; + fprintf(prof_fp, "# @include \"%s\"", s->src); + if (s->comment != NULL) { + fprintf(prof_fp, "\t"); + print_comment(s->comment, indent_level + 1); + } else + fprintf(prof_fp, "\n"); } } - if (printed_header) /* we found some */ + if (found) /* we found some */ fprintf(prof_fp, "\n"); } @@ -1158,8 +1343,13 @@ print_comment(INSTRUCTION* pc, long in) after_newline = false; } putc(*text, prof_fp); - if (*text == '\n') - after_newline = true; + after_newline = (*text == '\n'); + } + + if (pc->comment) { + // chaining should only be two deep + assert(pc->comment->comment == NULL); + print_comment(pc->comment, in); } } @@ -1181,6 +1371,7 @@ dump_prog(INSTRUCTION *code) fprintf(prof_fp, _("\t# gawk profile, created %s\n"), ctime(& now)); print_lib_list(prof_fp); pprint(code, NULL, NO_PPRINT_FLAGS); + print_include_list(prof_fp); } /* prec_level --- return the precedence of an operator, for paren tests */ @@ -1535,6 +1726,7 @@ pp_list(int nargs, const char *paren, const char *delim) size_t len; size_t delimlen; int i; + INSTRUCTION *comment = NULL; if (pp_args == NULL) { npp_args = nargs; @@ -1552,12 +1744,17 @@ pp_list(int nargs, const char *paren, const char *delim) for (i = 1; i <= nargs; i++) { r = pp_args[i] = pp_pop(); len += r->pp_len + delimlen; + if (r->pp_comment != NULL) { + comment = (INSTRUCTION *) r->pp_comment; + len += comment->memory->stlen + indent_level + 1; // comment\n ident + } } if (paren != NULL) { assert(strlen(paren) == 2); len += 2; } } + comment = NULL; emalloc(str, char *, len + 1, "pp_list"); s = str; @@ -1573,6 +1770,14 @@ pp_list(int nargs, const char *paren, const char *delim) memcpy(s, delim, delimlen); s += delimlen; } + if (r->pp_comment != NULL) { + check_indent_level(); + comment = (INSTRUCTION *) r->pp_comment; + memcpy(s, comment->memory->stptr, comment->memory->stlen); + s += comment->memory->stlen; + memcpy(s, tabs, indent_level + 1); + s += indent_level + 1; + } r = pp_args[i]; memcpy(s, r->pp_str, r->pp_len); s += r->pp_len; @@ -1736,10 +1941,8 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) fprintf(prof_fp, "\n"); /* print any function comment */ - if (fp->opcode == Op_comment && fp->source_line == 0) { - print_comment(fp, -1); /* -1 ==> don't indent */ - fp = fp->nexti; - } + if (pc->comment != NULL) + print_comment(pc->comment, -1); /* -1 ==> don't indent */ indent(pc->nexti->exec_count); fprintf(prof_fp, "%s %s(", op2str(Op_K_function), func->vname); diff --git a/test/ChangeLog b/test/ChangeLog index 36599fdd..918d695b 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -9,6 +9,14 @@ * Makefile.am (EXTRA_DIST): New test: typedregex4. * typedregex4.awk, typedregex4.ok: New files. +2018-11-11 Arnold D. Robbins <arnold@skeeve.com> + + * profile10.ok: Updated after code change. + +2018-10-14 Arnold D. Robbins <arnold@skeeve.com> + + * profile0.ok: Updated after code change. + 2018-10-10 Arnold D. Robbins <arnold@skeeve.com> * Makefile.am (profile1): Add minus to ignore errors on final diff --git a/test/profile0.ok b/test/profile0.ok index 2e3c5728..42c464ba 100644 --- a/test/profile0.ok +++ b/test/profile0.ok @@ -1,6 +1,6 @@ # Rule(s) 2 NR == 1 { # 1 - 1 print $0 + 1 print } diff --git a/test/profile10.ok b/test/profile10.ok index 13f0b67b..0f77bd38 100644 --- a/test/profile10.ok +++ b/test/profile10.ok @@ -17,14 +17,12 @@ BEGIN { # Comment 0 print "MNO" # Comment 16 } # Comment 17 switch (q) { - case "a": - # Comment 18 + # Comment 18 + case "a": # Comment 19 case "b": - # Comment 19 - break # Comment 20 - # Comment 21 - default: - # Comment 22 + # Comment 20 + break # Comment 21 + default: # Comment 22 break # Comment 23 } # Comment 24 |