Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): rename CondLexer to CondParser



details:   https://anonhg.NetBSD.org/src/rev/02b179047043
branches:  trunk
changeset: 938553:02b179047043
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Sep 11 04:07:44 2020 +0000

description:
make(1): rename CondLexer to CondParser

The name CondLexer was wrong since this type is about parsing conditions
that can be arbitrarily nested.

diffstat:

 usr.bin/make/cond.c |  219 ++++++++++++++++++++++++++-------------------------
 1 files changed, 111 insertions(+), 108 deletions(-)

diffs (truncated from 555 to 300 lines):

diff -r 28daba1097ae -r 02b179047043 usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Fri Sep 11 03:54:14 2020 +0000
+++ b/usr.bin/make/cond.c       Fri Sep 11 04:07:44 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.120 2020/09/10 23:37:54 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.121 2020/09/11 04:07:44 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: cond.c,v 1.120 2020/09/10 23:37:54 rillig Exp $";
+static char rcsid[] = "$NetBSD: cond.c,v 1.121 2020/09/11 04:07:44 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)cond.c     8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: cond.c,v 1.120 2020/09/10 23:37:54 rillig Exp $");
+__RCSID("$NetBSD: cond.c,v 1.121 2020/09/11 04:07:44 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -128,17 +128,20 @@
  *
  * 'symbol' is some other symbol to which the default function is applied.
  *
- * Tokens are scanned from the lexer. The scanner (CondToken)
- * will return TOK_AND for '&' and '&&', TOK_OR for '|' and '||',
- * TOK_NOT for '!', TOK_LPAREN for '(', TOK_RPAREN for ')' and will evaluate
- * the other terminal symbols, using either the default function or the
- * function given in the terminal, and return the result as either TOK_TRUE
- * or TOK_FALSE.
+ * The tokens are scanned by CondToken, which returns:
+ *     TOK_AND         for '&' or '&&'
+ *     TOK_OR          for '|' or '||'
+ *     TOK_NOT         for '!'
+ *     TOK_LPAREN      for '('
+ *     TOK_RPAREN      for ')'
+ * Other terminal symbols are evaluated using either the default function or
+ * the function given in the terminal, they return either TOK_TRUE or
+ * TOK_FALSE.
  *
  * TOK_FALSE is 0 and TOK_TRUE 1 so we can directly assign C comparisons.
  *
- * All Non-Terminal functions (CondE, CondF and CondT) return TOK_ERROR on
- * error.
+ * All non-terminal functions (CondE, CondF and CondT) return either
+ * TOK_FALSE, TOK_TRUE, or TOK_ERROR on error.
  */
 typedef enum {
     TOK_FALSE = 0, TOK_TRUE = 1, TOK_AND, TOK_OR, TOK_NOT,
@@ -149,10 +152,10 @@
     const struct If *if_info;  /* Info for current statement */
     const char *condExpr;      /* The expression to parse */
     Token curr;                        /* Single push-back token used in parsing */
-} CondLexer;
+} CondParser;
 
-static Token CondE(CondLexer *lex, Boolean);
-static CondEvalResult do_Cond_EvalExpression(CondLexer *lex, Boolean *);
+static Token CondE(CondParser *par, Boolean);
+static CondEvalResult do_Cond_EvalExpression(CondParser *par, Boolean *);
 
 static unsigned int cond_depth = 0;    /* current .if nesting level */
 static unsigned int cond_min_depth = 0;        /* depth at makefile open */
@@ -177,19 +180,19 @@
 
 /* Push back the most recent token read. We only need one level of this. */
 static void
-CondLexer_PushBack(CondLexer *lex, Token t)
+CondParser_PushBack(CondParser *par, Token t)
 {
-    assert(lex->curr == TOK_NONE);
+    assert(par->curr == TOK_NONE);
     assert(t != TOK_NONE);
 
-    lex->curr = t;
+    par->curr = t;
 }
 
 static void
-CondLexer_SkipWhitespace(CondLexer *lex)
+CondParser_SkipWhitespace(CondParser *par)
 {
-    while (isspace((unsigned char)lex->condExpr[0]))
-       lex->condExpr++;
+    while (isspace((unsigned char)par->condExpr[0]))
+       par->condExpr++;
 }
 
 /* Parse the argument of a built-in function.
@@ -399,7 +402,7 @@
  */
 /* coverity:[+alloc : arg-*3] */
 static const char *
-CondGetString(CondLexer *lex, Boolean doEval, Boolean *quoted, void **freeIt,
+CondGetString(CondParser *par, Boolean doEval, Boolean *quoted, void **freeIt,
              Boolean strictLHS)
 {
     Buffer buf;
@@ -412,25 +415,25 @@
     Buf_Init(&buf, 0);
     str = NULL;
     *freeIt = NULL;
-    *quoted = qt = *lex->condExpr == '"' ? 1 : 0;
+    *quoted = qt = *par->condExpr == '"' ? 1 : 0;
     if (qt)
-       lex->condExpr++;
-    for (start = lex->condExpr; *lex->condExpr && str == NULL;) {
-       switch (*lex->condExpr) {
+       par->condExpr++;
+    for (start = par->condExpr; *par->condExpr && str == NULL;) {
+       switch (*par->condExpr) {
        case '\\':
-           lex->condExpr++;
-           if (lex->condExpr[0] != '\0') {
-               Buf_AddByte(&buf, *lex->condExpr);
-               lex->condExpr++;
+           par->condExpr++;
+           if (par->condExpr[0] != '\0') {
+               Buf_AddByte(&buf, *par->condExpr);
+               par->condExpr++;
            }
            continue;
        case '"':
            if (qt) {
-               lex->condExpr++;        /* we don't want the quotes */
+               par->condExpr++;        /* we don't want the quotes */
                goto got_str;
            }
-           Buf_AddByte(&buf, *lex->condExpr); /* likely? */
-           lex->condExpr++;
+           Buf_AddByte(&buf, *par->condExpr); /* likely? */
+           par->condExpr++;
            continue;
        case ')':
        case '!':
@@ -441,14 +444,14 @@
        case '\t':
            if (!qt)
                goto got_str;
-           Buf_AddByte(&buf, *lex->condExpr);
-           lex->condExpr++;
+           Buf_AddByte(&buf, *par->condExpr);
+           par->condExpr++;
            continue;
        case '$':
            /* if we are in quotes, then an undefined variable is ok */
            eflags = ((!qt && doEval) ? VARE_UNDEFERR : 0) |
                     (doEval ? VARE_WANTRES : 0);
-           str = Var_Parse(lex->condExpr, VAR_CMD, eflags, &len, freeIt);
+           str = Var_Parse(par->condExpr, VAR_CMD, eflags, &len, freeIt);
            if (str == var_Error) {
                if (*freeIt) {
                    free(*freeIt);
@@ -461,16 +464,16 @@
                str = NULL;
                goto cleanup;
            }
-           lex->condExpr += len;
+           par->condExpr += len;
            /*
             * If the '$' was first char (no quotes), and we are
             * followed by space, the operator or end of expression,
             * we are done.
             */
-           if ((lex->condExpr == start + len) &&
-               (*lex->condExpr == '\0' ||
-                isspace((unsigned char)*lex->condExpr) ||
-                strchr("!=><)", *lex->condExpr))) {
+           if ((par->condExpr == start + len) &&
+               (*par->condExpr == '\0' ||
+                isspace((unsigned char)*par->condExpr) ||
+                strchr("!=><)", *par->condExpr))) {
                goto cleanup;
            }
 
@@ -492,8 +495,8 @@
                str = NULL;
                goto cleanup;
            }
-           Buf_AddByte(&buf, *lex->condExpr);
-           lex->condExpr++;
+           Buf_AddByte(&buf, *par->condExpr);
+           par->condExpr++;
            continue;
        }
     }
@@ -521,7 +524,7 @@
 };
 
 static Token
-compare_expression(CondLexer *lex, Boolean doEval)
+compare_expression(CondParser *par, Boolean doEval)
 {
     Token t;
     const char *lhs;
@@ -542,27 +545,27 @@
      * Parse the variable spec and skip over it, saving its
      * value in lhs.
      */
-    lhs = CondGetString(lex, doEval, &lhsQuoted, &lhsFree, lhsStrict);
+    lhs = CondGetString(par, doEval, &lhsQuoted, &lhsFree, lhsStrict);
     if (!lhs)
        goto done;
 
-    CondLexer_SkipWhitespace(lex);
+    CondParser_SkipWhitespace(par);
 
     /*
      * Make sure the operator is a valid one. If it isn't a
      * known relational operator, pretend we got a
      * != 0 comparison.
      */
-    op = lex->condExpr;
-    switch (*lex->condExpr) {
+    op = par->condExpr;
+    switch (*par->condExpr) {
     case '!':
     case '=':
     case '<':
     case '>':
-       if (lex->condExpr[1] == '=') {
-           lex->condExpr += 2;
+       if (par->condExpr[1] == '=') {
+           par->condExpr += 2;
        } else {
-           lex->condExpr += 1;
+           par->condExpr += 1;
        }
        break;
     default:
@@ -581,24 +584,24 @@
            goto done;
        }
        /* For .if ${...} check for non-empty string (defProc is ifdef). */
-       if (lex->if_info->form[0] == 0) {
+       if (par->if_info->form[0] == 0) {
            t = lhs[0] != 0;
            goto done;
        }
        /* Otherwise action default test ... */
-       t = lex->if_info->defProc(strlen(lhs), lhs) != lex->if_info->doNot;
+       t = par->if_info->defProc(strlen(lhs), lhs) != par->if_info->doNot;
        goto done;
     }
 
-    CondLexer_SkipWhitespace(lex);
+    CondParser_SkipWhitespace(par);
 
-    if (*lex->condExpr == '\0') {
+    if (*par->condExpr == '\0') {
        Parse_Error(PARSE_WARNING,
                    "Missing right-hand-side of operator");
        goto done;
     }
 
-    rhs = CondGetString(lex, doEval, &rhsQuoted, &rhsFree, FALSE);
+    rhs = CondGetString(par, doEval, &rhsQuoted, &rhsFree, FALSE);
     if (!rhs)
        goto done;
 
@@ -722,7 +725,7 @@
 }
 
 static Token
-compare_function(CondLexer *lex, Boolean doEval)
+compare_function(CondParser *par, Boolean doEval)
 {
     static const struct fn_def {
        const char *fn_name;
@@ -742,7 +745,7 @@
     Token t;
     char *arg = NULL;
     int arglen;
-    const char *cp = lex->condExpr;
+    const char *cp = par->condExpr;
     const char *cp1;
 
     for (fn_def = fn_defs; fn_def->fn_name != NULL; fn_def++) {
@@ -757,20 +760,20 @@
 
        arglen = fn_def->fn_getarg(doEval, &cp, &arg, fn_def->fn_name);
        if (arglen <= 0) {
-           lex->condExpr = cp;
+           par->condExpr = cp;
            return arglen < 0 ? TOK_ERROR : TOK_FALSE;
        }
        /* Evaluate the argument using the required function. */
        t = !doEval || fn_def->fn_proc(arglen, arg);
        free(arg);
-       lex->condExpr = cp;
+       par->condExpr = cp;
        return t;
     }
 



Home | Main Index | Thread Index | Old Index