"Fossies" - the Fresh Open Source Software Archive

Member "cook-2.34/make2cook/vargram.y" (25 Sep 2010, 8682 Bytes) of package /linux/misc/old/cook-2.34.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bison source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  *      cook - file construction tool
    3  *      Copyright (C) 1994, 1997, 2002, 2006-2008 Peter Miller
    4  *
    5  *      This program is free software; you can redistribute it and/or modify
    6  *      it under the terms of the GNU General Public License as published by
    7  *      the Free Software Foundation; either version 3 of the License, or
    8  *      (at your option) any later version.
    9  *
   10  *      This program is distributed in the hope that it will be useful,
   11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *      GNU General Public License for more details.
   14  *
   15  *      You should have received a copy of the GNU General Public License
   16  *      along with this program. If not, see
   17  *      <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 %{
   21 
   22 #include <common/ac/stdio.h>
   23 #include <common/ac/stdlib.h>
   24 #include <common/ac/string.h>
   25 
   26 #include <common/trace.h>
   27 #include <make2cook/vargram.h>
   28 #include <make2cook/variable.h>
   29 
   30 #ifdef  DEBUG
   31 #define YYDEBUG 1
   32 #ifdef YYBISON
   33 #define fprintf yytrace2
   34 #else
   35 #define printf trace_where(__FILE__, __LINE__), yytrace
   36 #endif
   37 extern int      yydebug;
   38 #endif
   39 
   40 
   41 static string_ty *
   42 patvar(string_ty *name, string_ty *from, string_ty *to)
   43 {
   44     string_ty       *tmp;
   45     string_ty       *result;
   46     string_ty       *s_from;
   47     string_ty       *s_to;
   48 
   49     if (!strchr(from->str_text, '%'))
   50     {
   51         tmp = from;
   52         if (tmp->str_length == 0)
   53             from = str_from_c("%0%");
   54         else if (tmp->str_text[0] == '.')
   55             from = str_format("%%0%%%s", tmp->str_text);
   56         else
   57             from = str_format("%%0%%.%s", tmp->str_text);
   58         str_free(tmp);
   59     }
   60     else
   61     {
   62         tmp = from;
   63         s_from = str_from_c("%");
   64         s_to = str_from_c("%0%");
   65         from = str_substitute(s_from, s_to, tmp);
   66         str_free(tmp);
   67     }
   68 
   69     if (!strchr(to->str_text, '%'))
   70     {
   71         tmp = to;
   72         if (tmp->str_length == 0)
   73             to = str_from_c("%0%");
   74         else if (tmp->str_text[0] == '.')
   75             to = str_format("%%0%%%s", tmp->str_text);
   76         else
   77             to = str_format("%%0%%.%s", tmp->str_text);
   78         str_free(tmp);
   79     }
   80     else
   81     {
   82         tmp = to;
   83         s_from = str_from_c("%");
   84         s_to = str_from_c("%0%");
   85         to = str_substitute(s_from, s_to, tmp);
   86         str_free(tmp);
   87     }
   88 
   89     tmp = variable_mangle_lookup(name);
   90     str_free(name);
   91     result =
   92         str_format
   93         (
   94             "[patsubst %s %s %s]",
   95             from->str_text,
   96             to->str_text,
   97             tmp->str_text
   98         );
   99     str_free(tmp);
  100     str_free(from);
  101     str_free(to);
  102     return result;
  103 }
  104 
  105 
  106 static string_ty *
  107 function(string_ty *name, string_list_ty *args)
  108 {
  109     string_ty       *s;
  110     string_ty       *result;
  111     static string_ty *foreach;
  112 
  113     if (!foreach)
  114         foreach = str_from_c("foreach");
  115     if (str_equal(name, foreach) && args->nstrings == 3)
  116     {
  117         string_ty       *s_from;
  118         string_ty       *s_to;
  119 
  120         /*
  121          * The foreach function is treated specially.  This is
  122          * not an exact semantic mapping, but it is better than
  123          * nothing.
  124          */
  125         variable_mangle_forget(args->string[0]);
  126         s_from = str_format("[%s]", args->string[0]->str_text);
  127         s_to = str_from_c("%");
  128         s = str_substitute(s_from, s_to, args->string[2]);
  129         result =
  130             str_format
  131             (
  132                 "[fromto %% %s %s]",
  133                 s->str_text,
  134                 args->string[1]->str_text
  135             );
  136         str_free(s);
  137         str_free(s_from);
  138         str_free(s_to);
  139     }
  140     else
  141     {
  142         /*
  143          * Construct the function invokation.  There are
  144          * make-equivalents for all the function names built
  145          * into cook, so there is no need to translate the
  146          * function name.
  147          */
  148         s = wl2str(args, 0, args->nstrings - 1, (char *)0);
  149         string_list_destructor(args);
  150         result = str_format("[%s %s]", name->str_text, s->str_text);
  151         str_free(s);
  152     }
  153     str_free(name);
  154     string_list_destructor(args);
  155     trace(("result = \"%s\";\n", result->str_text));
  156     return result;
  157 }
  158 
  159 %}
  160 
  161 %token  COLON
  162 %token  COMMA
  163 %token  DOLLAR
  164 %token  EQU
  165 %token  LB
  166 %token  LP
  167 %token  PLAIN
  168 %token  RB
  169 %token  RP
  170 %token  SPACE
  171 
  172 %union
  173 {
  174     string_ty       *lv_string;
  175     string_list_ty  lv_list;
  176 }
  177 
  178 %type <lv_string> arg
  179 %type <lv_string> argc
  180 %type <lv_list>   csl
  181 %type <lv_string> gizzards
  182 %type <lv_string> name
  183 %type <lv_string> namec
  184 %type <lv_string> oname
  185 %type <lv_string> ossl
  186 %type <lv_string> parens
  187 %type <lv_string> PLAIN
  188 %type <lv_list>   ssl
  189 %type <lv_string> var
  190 
  191 %%
  192 
  193 main
  194     : dbg
  195         { variable_mangle_result(str_from_c("")); }
  196     | dbg strings
  197     | error
  198         { variable_mangle_result(str_from_c("")); }
  199     ;
  200 
  201 dbg
  202     : /* empty */
  203         {
  204 #if YYDEBUG
  205             yydebug = trace_pretest_;
  206 #endif
  207         }
  208     ;
  209 
  210 strings
  211     : string
  212     | strings string
  213     | strings SPACE
  214     ;
  215 
  216 string
  217     : gizzards
  218         { variable_mangle_result($1); }
  219     | gizzards LP ssl RP
  220         {
  221             size_t          j;
  222 
  223             for (j = 0; j < $3.nstrings; ++j)
  224             {
  225                 variable_mangle_result
  226                 (
  227                     str_format("%s(%s)", $1->str_text, $3.string[j]->str_text)
  228                 );
  229             }
  230             str_free($1);
  231             string_list_destructor(&$3);
  232         }
  233     ;
  234 
  235 gizzards
  236     : var
  237         { $$ = $1; }
  238     | gizzards var
  239         {
  240             $$ = str_catenate($1, $2);
  241             str_free($1);
  242             str_free($2);
  243         }
  244     ;
  245 
  246 var
  247     : DOLLAR DOLLAR
  248         { $$ = str_from_c("$"); }
  249     | DOLLAR PLAIN
  250         {
  251             $$ = variable_mangle_lookup($2);
  252             str_free($2);
  253         }
  254     | DOLLAR LP name RP
  255         {
  256             $$ = variable_mangle_lookup($3);
  257             str_free($3);
  258         }
  259     | DOLLAR LB name RB
  260         {
  261             $$ = variable_mangle_lookup($3);
  262             str_free($3);
  263         }
  264     | DOLLAR LP name COLON oname EQU oname RP
  265         { $$ = patvar($3, $5, $7); }
  266     | DOLLAR LB name COLON oname EQU oname RB
  267         { $$ = patvar($3, $5, $7); }
  268     | DOLLAR LP name SPACE csl RP
  269         { $$ = function($3, &$5); }
  270     | DOLLAR LB name SPACE csl RB
  271         { $$ = function($3, &$5); }
  272     | PLAIN
  273         { $$ = $1; }
  274     ;
  275 
  276 name
  277     : namec
  278         { $$ = $1; }
  279     | name namec
  280         {
  281             $$ = str_catenate($1, $2);
  282             str_free($1);
  283             str_free($2);
  284         }
  285     ;
  286 
  287 oname
  288     : /* empty */
  289         { $$ = str_from_c(""); }
  290     | oname namec
  291         {
  292             $$ = str_catenate($1, $2);
  293             str_free($1);
  294             str_free($2);
  295         }
  296     ;
  297 
  298 namec
  299     : var
  300         { $$ = $1; }
  301     | COMMA
  302         { $$ = str_from_c(","); }
  303     ;
  304 
  305 csl
  306     : ossl
  307         {
  308             string_list_constructor(&$$);
  309             string_list_append(&$$, $1);
  310             str_free($1);
  311         }
  312     | csl comma ossl
  313         {
  314             $$ = $1;
  315             string_list_append(&$$, $3);
  316             str_free($3);
  317         }
  318     ;
  319 
  320 ossl
  321     : /* empty */
  322         {
  323             /*
  324              * Guess that empty space separated lists were
  325              * really a single space for substitution.
  326              * E.g. $(subst $(\n), ,$(list))
  327              */
  328             $$ = str_from_c("\" \"");
  329         }
  330     | ssl
  331         {
  332             $$ = wl2str(&$1, 0, $1.nstrings - 1, (char *)0);
  333             string_list_destructor(&$1);
  334         }
  335     | ssl SPACE
  336         {
  337             $$ = wl2str(&$1, 0, $1.nstrings - 1, (char *)0);
  338             string_list_destructor(&$1);
  339         }
  340     ;
  341 
  342 ssl
  343     : arg
  344         {
  345             string_list_constructor(&$$);
  346             string_list_append(&$$, $1);
  347             str_free($1);
  348         }
  349     | ssl SPACE arg
  350         {
  351             $$ = $1;
  352             string_list_append(&$$, $3);
  353             str_free($3);
  354         }
  355     ;
  356 
  357 arg
  358     : argc
  359         { $$ = $1; }
  360     | arg argc
  361         {
  362             $$ = str_catenate($1, $2);
  363             str_free($1);
  364             str_free($2);
  365         }
  366     ;
  367 
  368 argc
  369     : PLAIN
  370         { $$ = $1; }
  371     | EQU
  372         { $$ = str_from_c("\\="); }
  373     | COLON
  374         { $$ = str_from_c("\\:"); }
  375     | var
  376         { $$ = $1; }
  377     | parens
  378         { $$ = $1; }
  379     | error
  380         { $$ = str_from_c(""); }
  381     ;
  382 
  383 parens
  384     : LP csl RP
  385         {
  386             string_ty       *s;
  387 
  388             s = wl2str(&$2, 0, $2.nstrings, ",");
  389             string_list_destructor(&$2);
  390             $$ = str_format("(%s)", s->str_text);
  391             str_free(s);
  392         }
  393     ;
  394 
  395 comma
  396     : COMMA
  397     | comma SPACE
  398     ;