home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcu16.zip / wlmCompiler / wlc.y < prev   
Text File  |  1991-10-03  |  10KB  |  428 lines

  1. /*
  2.  * Copyright 1991 Cornell University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Cornell U. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Cornell U. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * CORNELL UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL CORNELL UNIVERSITY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  18.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Gene W. Dykes, Program of Computer Graphics
  23.  *          580 Theory Center, Cornell University, Ithaca, NY 14853
  24.  *          (607) 255-6713   gwd@graphics.cornell.edu
  25.  */
  26.  
  27.  
  28. %{
  29. /* global declaractions */
  30.  
  31. Stack *fetus_stack ;
  32. Stack *incl_stack ;
  33.  
  34. static Directive *directive ;
  35.  
  36. static char *resource_unify () ;
  37. static char *perm_string = "String" ;
  38.  
  39. %}
  40.  
  41. %start    init
  42.  
  43. %union    {
  44.     float    f_val ;
  45.     int    i_val ;
  46.     char    *s_val ;
  47.     char    c_val ;
  48.     WlmDirectiveOperator op_val ;
  49.     }
  50.  
  51. %type <op_val>    test_operator
  52. %type <s_val>    resource_word
  53. %token <s_val>    WORD
  54. %token <s_val>    STRING
  55. %token        IF_KEY
  56. %token        SET_VALUES_KEY
  57. %token        MANAGE_KEY
  58. %token        INCL_KEY
  59. %token         ERROR
  60.  
  61. %%
  62.  
  63. init        :
  64.     {
  65.     Cu_stack_create ("Fetus", &fetus_stack, 100) ;
  66.     Cu_stack_create ("Child", &ww->child_stack, 100) ;
  67.     Cu_stack_create ("Include", &incl_stack, 10) ;
  68.     ww->cur_depth = 0 ;
  69.     ww->max_depth = 0 ;
  70.     }
  71.         | init widget_equivalent
  72.         ;
  73.  
  74. widget_equivalent : widget
  75.           | inclusion
  76.           ;
  77.  
  78. inclusion    :    INCL_KEY STRING
  79.     {
  80.     Cu_stack_push (incl_stack, (int) layout_input) ;
  81.     layout_input = (WlmInputRecord *)XtCalloc(1, sizeof (WlmInputRecord)) ;
  82.     read_layout ($2, layout_input) ;
  83.     }
  84.         ;
  85.  
  86. widget        : widget_header '{' widget_body '}'
  87.     {
  88.     Fetus *fetus ;
  89.     Fetus *parent ;
  90.     /*
  91.      * pop off fetus stack,
  92.      * pop children (if any) from child_stack
  93.      * push the widget to child stack
  94.      */
  95.     fetus = (Fetus *) Cu_stack_pop (fetus_stack) ;
  96.     parent = (Fetus *) Cu_stack_peek (fetus_stack, STACK_NEXT_POP) ;
  97.     if (parent)
  98.     {
  99.     parent->n_children++ ;
  100.     }
  101.     if (fetus->n_children)
  102.     {
  103.     Cardinal i ;
  104.  
  105.     fetus->children = (Fetus **)
  106.               XtMalloc (fetus->n_children * sizeof (Fetus *)) ;
  107.     for (i=0;  i < fetus->n_children;  i++)
  108.         {
  109.         fetus->children[i] = (Fetus *) Cu_stack_pop (ww->child_stack) ;
  110.         }
  111.     }
  112.     Cu_stack_push (ww->child_stack, (int) fetus) ;
  113.     ww->cur_depth-- ;
  114.     }
  115.         | widget_header '{' '}'
  116.         ;
  117.  
  118. widget_header    : WORD
  119.     {
  120.     /* allocate and push onto fetus stack */
  121.     Fetus *fetus = (Fetus *) XtMalloc (sizeof (Fetus)) ;
  122.     fetus->class_name = $1 ;
  123.     fetus->n_directives = 0 ;
  124.     fetus->directives = NULL ;
  125.     fetus->n_children = 0 ;
  126.     fetus->n_resources = 0 ;
  127.     fetus->resource = NULL ;
  128.     fetus->n_manage_list = 0 ;
  129.     fetus->manage_list = NULL ;
  130.     Cu_stack_push (fetus_stack, (int) fetus) ;
  131.     ww->cur_depth++ ;
  132.     if (ww->cur_depth > ww->max_depth)
  133.     ww->max_depth = ww->cur_depth ;
  134.     }
  135.         ;
  136.  
  137. widget_body    : widget_element
  138.         | widget_body widget_element
  139.         ;
  140.  
  141. widget_element    : directive
  142.     {
  143.     Fetus *fetus = (Fetus *) Cu_stack_peek (fetus_stack, STACK_NEXT_POP) ;
  144.     Directive *save = fetus->directives ;
  145.     fetus->directives = directive ;
  146.     directive->next = save ;
  147.     fetus->n_directives++ ;
  148.     }
  149.         | widget_resource
  150.         | manage_resource
  151.         | global_resource
  152.         | widget_equivalent
  153.         ;
  154.  
  155. global_resource    : '*' resource_word STRING
  156.     {
  157.     Fetus *fetus = (Fetus *) Cu_stack_peek (fetus_stack, STACK_NEXT_POP) ;
  158.     Resource *save = fetus->resource ;
  159.     fetus->resource = (Resource *) XtMalloc (sizeof(Resource)) ;
  160.     fetus->resource->name = resource_unify ($2) ;
  161.     fetus->resource->value = $3 ;
  162.     fetus->resource->context = WlmGlobalContext ;
  163.     fetus->resource->next = save ;
  164.     fetus->n_resources++ ;
  165.     }
  166.         ;
  167.  
  168. resource_word : WORD
  169.     {
  170.     }
  171.           | WORD '*' resource_word
  172.     {
  173.     $$ = XtMalloc (strlen($1) + strlen($3) + 2) ;
  174.     sprintf ($$, "%s*%s", $1, $3) ;
  175.     }
  176.           ;
  177.  
  178. widget_resource    : WORD STRING
  179.     {
  180.     Fetus *fetus = (Fetus *) Cu_stack_peek (fetus_stack, STACK_NEXT_POP) ;
  181.     Resource *save = fetus->resource ;
  182.     fetus->resource = (Resource *) XtMalloc (sizeof(Resource)) ;
  183.     fetus->resource->name = resource_unify ($1) ;
  184.     fetus->resource->value = $2 ;
  185.     fetus->resource->context = WlmLocalContext ;
  186.     fetus->resource->next = save ;
  187.     fetus->n_resources++ ;
  188.     }
  189.         ;
  190.  
  191. manage_resource : MANAGE_KEY manage_args
  192.         ;
  193.  
  194. manage_args    : manage_arg
  195.         | manage_args maybe_comma manage_arg
  196.         ;
  197.  
  198. manage_arg    : STRING ':' WORD STRING
  199.     {
  200.     /* For now, presume that "Button" is the only valid class */
  201.     Fetus *fetus = (Fetus *) Cu_stack_peek (fetus_stack, STACK_NEXT_POP) ;
  202.     WlmManageItem *save = fetus->manage_list ;
  203.     fetus->manage_list = (WlmManageItem *) XtMalloc (sizeof(WlmManageItem)) ;
  204.     fetus->manage_list->widget = $1 ;
  205.     fetus->manage_list->type = $3 ;
  206.     fetus->manage_list->value = $4 ;
  207.     fetus->manage_list->next = save ;
  208.     fetus->n_manage_list++ ;
  209.     }
  210.         | STRING
  211.     {
  212.     Fetus *fetus = (Fetus *) Cu_stack_peek (fetus_stack, STACK_NEXT_POP) ;
  213.     WlmManageItem *save = fetus->manage_list ;
  214.     fetus->manage_list = (WlmManageItem *) XtMalloc (sizeof(WlmManageItem)) ;
  215.     fetus->manage_list->widget = $1 ;
  216.     fetus->manage_list->type = perm_string ;
  217.     fetus->manage_list->value = $1 ;
  218.     fetus->manage_list->next = save ;
  219.     fetus->n_manage_list++ ;
  220.     }
  221.         ;
  222.  
  223. directive : directive_head callback_name '(' test_group ')' r_target_info
  224.       | directive_head callback_name '(' test_group ')' f_target_info
  225.       ;
  226.  
  227. directive_head    : IF_KEY
  228.     {
  229.     /* Make sure that everything is zeroed */
  230.     directive = (Directive *) XtCalloc (1, sizeof(Directive)) ;
  231.     directive->n_call_comparisons = 0 ;
  232.     }
  233.         ;
  234.  
  235. callback_name    : WORD
  236.     {
  237.     directive->callback_name = $1 ;
  238.     }
  239.         ;
  240.  
  241. test_group    :
  242.         | test_series
  243.         ;
  244.  
  245. test_series    : test_pair
  246.         | test_series test_conjunction test_pair
  247.         ;
  248.  
  249. test_pair    : WORD STRING test_operator WORD test_indices
  250.     {
  251.     directive->call_data_converter[directive->n_call_comparisons] = $1 ;
  252.     directive->call_data[directive->n_call_comparisons] = $2 ;
  253.     directive->call_data_operator[directive->n_call_comparisons] = $3 ;
  254.     directive->n_call_comparisons++ ;
  255.     }
  256.         | WORD test_indices test_operator WORD STRING
  257.     {
  258.     directive->call_data_converter[directive->n_call_comparisons] = $4 ;
  259.     directive->call_data[directive->n_call_comparisons] = $5 ;
  260.     directive->call_data_operator[directive->n_call_comparisons] = $3 ;
  261.     directive->n_call_comparisons++ ;
  262.     }
  263.         ;
  264.  
  265. r_target_info    : SET_VALUES_KEY '(' r_target_args ')'
  266.         ;
  267.  
  268. r_target_args    : target_class target_name ',' resource_name maybe_comma  resource_value
  269.         ;
  270.  
  271. f_target_info    : procedure_name '(' f_target_args ')'
  272.         ;
  273.  
  274. f_target_args    : target_class target_name target_args
  275.         ;
  276.  
  277. maybe_comma    :
  278.         | ','
  279.         ;
  280.  
  281. target_class    : WORD
  282.     {
  283.     directive->target_class = $1 ;
  284.     }
  285.  
  286. target_name    : STRING
  287.     {
  288.     directive->target_name = $1 ;
  289.     }
  290.  
  291. resource_name    : WORD
  292.     {
  293.     directive->resource.name = resource_unify ($1) ;
  294.     }
  295.  
  296. procedure_name    : WORD
  297.     {
  298.     directive->procedure = $1 ;
  299.     directive->n_arguments = 0 ;
  300.     }
  301.  
  302. resource_value    : STRING
  303.     {
  304.     directive->resource.value = $1 ;
  305.     }
  306.  
  307. target_args    : 
  308.          | ',' argument_list
  309.         ;
  310.  
  311. argument_list    : argument
  312.         | argument_list ',' argument
  313.         ;
  314.  
  315. argument    : WORD STRING
  316.     {
  317.     directive->argument_converters[directive->n_arguments] = $1 ;
  318.     directive->argument_strings[directive->n_arguments] = $2 ;
  319.     directive->n_arguments++ ;
  320.     }
  321.         ;
  322.  
  323. test_conjunction : '&' '&'
  324.          ;
  325.  
  326. test_operator    : '=' '='
  327.     {
  328.     $$ = WlmDirectiveEquivalence ;
  329.     directive->n_call_indices[directive->n_call_comparisons] = 0 ;
  330.     }
  331.         | '!' '='
  332.     {
  333.     $$ = WlmDirectiveNonEquivalence ;
  334.     directive->n_call_indices[directive->n_call_comparisons] = 0 ;
  335.     }
  336.         | '>' 
  337.     {
  338.     $$ = WlmDirectiveGreaterThan ;
  339.     directive->n_call_indices[directive->n_call_comparisons] = 0 ;
  340.     }
  341.         | '<'
  342.     {
  343.     $$ = WlmDirectiveLessThan ;
  344.     directive->n_call_indices[directive->n_call_comparisons] = 0 ;
  345.     }
  346.         | '>' '='
  347.     {
  348.     $$ = WlmDirectiveGreaterThanOrEqualTo ;
  349.     directive->n_call_indices[directive->n_call_comparisons] = 0 ;
  350.     }
  351.         | '<' '='
  352.     {
  353.     $$ = WlmDirectiveLessThanOrEqualTo ;
  354.     directive->n_call_indices[directive->n_call_comparisons] = 0 ;
  355.     }
  356.         ;
  357.  
  358. test_indices    :
  359.         | index_series
  360.         ;
  361.  
  362. index_series    : test_index
  363.         | index_series test_index
  364.         ;
  365.  
  366. test_index    : '[' WORD ']'
  367.     {
  368.     directive->call_data_index[directive->n_call_comparisons]
  369.                   [directive->n_call_indices[directive->n_call_comparisons]] = atoi($2) ;
  370.     directive->n_call_indices[directive->n_call_comparisons]++ ;
  371.     }
  372.         ;
  373.  
  374. %%
  375.  
  376. /*
  377.  * -v => y.output
  378.  * -d => y.tab.h
  379.  * -l => no #lines (for -g (cc))
  380.  *
  381.  * left recursion is better (as in init above)
  382.  */
  383.  
  384. #include <ctype.h>
  385.  
  386. static char *
  387. resource_unify (string)
  388.     char *string ;
  389. {
  390. /* replace occurrences of "_x" with "X" */
  391. int length = strlen (string) ;
  392. Cardinal i, j ;
  393. Cardinal start =0 ;
  394.  
  395. if (strncmp (string, "XtN", 3) == 0)
  396.     start = 3 ;
  397.  
  398. for (i=start;  i < length-1;  i++)
  399.     {
  400.     if (string[i] == '_' && islower(string[i+1]))
  401.     {
  402.     string[i] = toupper(string[i+1]) ;
  403.     for (j=i+1;  j < length-1;  j++)
  404.         string[j] = string[j+1] ;
  405.     string[j] = '\0' ;
  406.     }
  407.     }
  408.  
  409. return &string[start] ;
  410. }
  411.  
  412. static int
  413. yywrap ()
  414. {
  415. XtFree (layout_input->input_buffer) ;
  416. XtFree (layout_input) ;
  417.  
  418. if (Cu_stack_size (incl_stack) == 0)
  419.     {
  420.     return 1 ;
  421.     }
  422.  
  423. layout_input = (WlmInputRecord *) Cu_stack_pop (incl_stack) ;
  424. return 0 ;
  425. }
  426.  
  427. #include "wlc.lex.c"
  428.