• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision3875 (tree)
Time2018-09-19 00:55:19
Authormagicant

Log Message

Simplify parse_compound_command function

A test case for $LINENO is modified as the parser no longer removes the
line continuation before saving the line number of the command.

Change Summary

Incremental Difference

--- yash/branches/token_based_parser/parser.c (revision 3874)
+++ yash/branches/token_based_parser/parser.c (revision 3875)
@@ -609,8 +609,7 @@
609609 __attribute__((nonnull,malloc,warn_unused_result));
610610 static wchar_t *parse_word_as_wcs(parsestate_T *ps)
611611 __attribute__((nonnull,malloc,warn_unused_result));
612-static command_T *parse_compound_command(
613- parsestate_T *ps, const wchar_t *command)
612+static command_T *parse_compound_command(parsestate_T *ps)
614613 __attribute__((nonnull,malloc,warn_unused_result));
615614 static command_T *parse_group(parsestate_T *ps, commandtype_T type)
616615 __attribute__((nonnull,malloc,warn_unused_result));
@@ -1429,7 +1428,7 @@
14291428 serror(ps, get_errmsg_unexpected_token(L"in"), L"in");
14301429 return NULL;
14311430 } else if (ps->tokentype == TT_LPAREN) {
1432- return parse_compound_command(ps, L"(");
1431+ return parse_compound_command(ps);
14331432 } else if (is_command_delimiter_tokentype(ps->tokentype)) {
14341433 if (ps->tokentype == TT_END_OF_INPUT || ps->tokentype == TT_NEWLINE)
14351434 serror(ps, Ngt("a command is missing at the end of input"));
@@ -1439,9 +1438,9 @@
14391438 return NULL;
14401439 }
14411440
1442- t = check_opening_token(ps);
1443- if (t != NULL)
1444- return parse_compound_command(ps, t);
1441+ command_T *result = parse_compound_command(ps);
1442+ if (result != NULL)
1443+ return result;
14451444
14461445 if (psubstitute_alias(ps, AF_NONGLOBAL)) {
14471446 ps->reparse = true;
@@ -1449,7 +1448,7 @@
14491448 }
14501449
14511450 /* parse as a simple command */
1452- command_T *result = xmalloc(sizeof *result);
1451+ result = xmalloc(sizeof *result);
14531452 result->next = NULL;
14541453 result->refcount = 1;
14551454 result->c_lineno = ps->info->lineno;
@@ -2327,43 +2326,38 @@
23272326 }
23282327
23292328 /* Parses a compound command.
2330- * `command' is the name of the command to parse such as "(" and "if". */
2331-command_T *parse_compound_command(parsestate_T *ps, const wchar_t *command)
2329+ * `command' is the name of the command to parse such as "(" and "if".
2330+ * Returns NULL iff the current token does not start a compound command. */
2331+command_T *parse_compound_command(parsestate_T *ps)
23322332 {
23332333 command_T *result;
2334- switch (command[0]) {
2335- case L'(':
2334+ switch (ps->tokentype) {
2335+ case TT_LPAREN:
23362336 result = parse_group(ps, CT_SUBSHELL);
23372337 break;
2338- case L'{':
2338+ case TT_LBRACE:
23392339 result = parse_group(ps, CT_GROUP);
23402340 break;
2341- case L'i':
2341+ case TT_IF:
23422342 result = parse_if(ps);
23432343 break;
2344- case L'f':
2345- switch (command[1]) {
2346- case L'o':
2347- result = parse_for(ps);
2348- break;
2349- case L'u':
2350- result = parse_function(ps);
2351- break;
2352- default:
2353- assert(false);
2354- }
2344+ case TT_FOR:
2345+ result = parse_for(ps);
23552346 break;
2356- case L'w':
2347+ case TT_FUNCTION:
2348+ result = parse_function(ps);
2349+ break;
2350+ case TT_WHILE:
23572351 result = parse_while(ps, true);
23582352 break;
2359- case L'u':
2353+ case TT_UNTIL:
23602354 result = parse_while(ps, false);
23612355 break;
2362- case L'c':
2356+ case TT_CASE:
23632357 result = parse_case(ps);
23642358 break;
23652359 default:
2366- assert(false);
2360+ return NULL;
23672361 }
23682362 parse_redirect_list(ps, &result->c_redirs);
23692363 return result;
@@ -2370,7 +2364,8 @@
23702364 }
23712365
23722366 /* Parses a command group.
2373- * `type' must be either CT_GROUP or CT_SUBSHELL. */
2367+ * `type' must be either CT_GROUP or CT_SUBSHELL.
2368+ * The current token must be the starting "(" or "{". Never returns NULL. */
23742369 command_T *parse_group(parsestate_T *ps, commandtype_T type)
23752370 {
23762371 tokentype_T starttt, endtt;
@@ -2408,7 +2403,8 @@
24082403 return result;
24092404 }
24102405
2411-/* Parses a if command */
2406+/* Parses an if command.
2407+ * The current token must be the starting "if". Never returns NULL. */
24122408 command_T *parse_if(parsestate_T *ps)
24132409 {
24142410 assert(ps->tokentype == TT_IF);
@@ -2462,7 +2458,8 @@
24622458 return result;
24632459 }
24642460
2465-/* Parses a for command. */
2461+/* Parses a for command.
2462+ * The current token must be the starting "for". Never returns NULL. */
24662463 command_T *parse_for(parsestate_T *ps)
24672464 {
24682465 assert(ps->tokentype == TT_FOR);
@@ -2535,7 +2532,8 @@
25352532
25362533 /* Parses a while/until command.
25372534 * `whltype' must be true for the while command and false for the until command.
2538- */
2535+ * The current token must be the starting "while" or "until". Never returns
2536+ * NULL. */
25392537 command_T *parse_while(parsestate_T *ps, bool whltype)
25402538 {
25412539 if (whltype)
@@ -2575,7 +2573,8 @@
25752573 return result;
25762574 }
25772575
2578-/* Parses a case command. */
2576+/* Parses a case command.
2577+ * The current token must be the starting "case". Never returns NULL. */
25792578 command_T *parse_case(parsestate_T *ps)
25802579 {
25812580 assert(ps->tokentype == TT_CASE);
@@ -2693,7 +2692,8 @@
26932692 return pl_toary(&wordlist);
26942693 }
26952694
2696-/* Parses a function definition that starts with the "function" keyword. */
2695+/* Parses a function definition that starts with the "function" keyword.
2696+ * The current token must be "function". Never returns NULL. */
26972697 command_T *parse_function(parsestate_T *ps)
26982698 {
26992699 if (posixly_correct)
@@ -2735,17 +2735,15 @@
27352735 parse_function_body:
27362736 parse_newline_list(ps);
27372737
2738- const wchar_t *t = check_opening_token(ps);
2739- if (t != NULL) {
2740- result->c_funcbody = parse_compound_command(ps, t);
2741- } else if (psubstitute_alias(ps, 0)) {
2742- if (paren)
2743- goto parse_function_body;
2744- else
2745- goto parse_parentheses;
2746- } else {
2738+ result->c_funcbody = parse_compound_command(ps);
2739+ if (result->c_funcbody == NULL) {
2740+ if (psubstitute_alias(ps, 0)) {
2741+ if (paren)
2742+ goto parse_function_body;
2743+ else
2744+ goto parse_parentheses;
2745+ }
27472746 serror(ps, Ngt("a function body must be a compound command"));
2748- result->c_funcbody = NULL;
27492747 }
27502748
27512749 return result;
@@ -2789,21 +2787,19 @@
27892787 }
27902788 next_token(ps);
27912789
2790+ free(c->c_words);
2791+ c->c_type = CT_FUNCDEF;
2792+ c->c_funcname = name;
2793+
27922794 parse_function_body:
27932795 parse_newline_list(ps);
2794-
2795- const wchar_t *t = check_opening_token(ps);
2796- if (t == NULL) {
2796+ c->c_funcbody = parse_compound_command(ps);
2797+ if (c->c_funcbody == NULL) {
27972798 if (psubstitute_alias(ps, 0))
27982799 goto parse_function_body;
27992800 serror(ps, Ngt("a function body must be a compound command"));
2800- return c;
28012801 }
2802- free(c->c_words);
28032802
2804- c->c_type = CT_FUNCDEF;
2805- c->c_funcname = name;
2806- c->c_funcbody = parse_compound_command(ps, t);
28072803 return c;
28082804 }
28092805
Show on old repository browser