home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / pspans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  5.4 KB  |  189 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. #include "ada.h"
  11. #include "miscprots.h"
  12. #include "adalexprots.h"
  13. #include "pspansprots.h"
  14.  
  15. extern char *emalloc();
  16.  
  17. int is_terminal_node(short node_kind)                    /*;is_terminal_node*/
  18. {
  19.     return (
  20.         node_kind == AS_STRING ||
  21.         node_kind == AS_STRING_LITERAL ||
  22.         node_kind == AS_CHARACTER_LITERAL ||
  23.         node_kind == AS_INT_LITERAL ||
  24.         node_kind == AS_REAL_LITERAL ||
  25.         node_kind == AS_SIMPLE_NAME ||
  26.         node_kind == AS_OPERATOR ||
  27.         node_kind == AS_MODE ||
  28.         node_kind == AS_PACKAGE_STUB ||
  29.         node_kind == AS_TASK_STUB ||
  30.         node_kind == AS_NULL ||
  31.         node_kind == AS_NULL_S ||
  32.         node_kind == AS_OTHERS ||
  33.         node_kind == AS_GENERIC ||
  34.         node_kind == AS_LINE_NO 
  35.         );
  36. }
  37.  
  38. struct tok_loc *get_left_span(struct ast *node)            /*;get_left_span*/
  39. {
  40.     short nkind,first;
  41.     struct two_pool *firstelem;
  42.     struct ast **astptr;
  43.  
  44.     nkind = node->kind;
  45.     if (is_terminal_node(nkind))
  46.         return (&node->span);
  47.     else if (nkind == AS_OPT)
  48.         return (&node->span);
  49.     else if (nkind == AS_EXIT) {
  50.         return get_left_span(node->links.subast[3]);
  51.     }
  52.     else if (nkind == AS_RETURN) {
  53.         return get_left_span(node->links.subast[3]);
  54.     }
  55.     else if (nkind == AS_RAISE) {
  56.         return get_left_span(node->links.subast[1]);
  57.     }
  58.     else if (nkind == AS_OTHERS_CHOICE) {
  59.         return get_left_span(node->links.subast[2]);
  60.     }
  61.     else if (islist_node[nkind]) {
  62.         if (node->links.list==NULL) /* only for parser */
  63.             return (&node->span);
  64.         else {
  65.             firstelem = node->links.list->link;/* first list element */
  66.             if (firstelem->val.node == opt_node)
  67.                 chaos("get_left_span: list element opt_node");
  68.             return get_left_span(firstelem->val.node);
  69.         }
  70.     }
  71.     else if (isast_node[nkind]) {
  72.         astptr = node->links.subast;
  73.         first = -1;
  74.         if (astptr[0] != opt_node && astptr[0] != (struct ast *)0)
  75.             first = 0;
  76.         else if (N_AST2_DEFINED(nkind)) {
  77.             if (astptr[1] != opt_node && astptr[1] != (struct ast *)0)
  78.                 first = 1;
  79.             else if (N_AST3_DEFINED(nkind)) {
  80.                 if (astptr[2] != opt_node && astptr[2] != (struct ast *)0)
  81.                     first = 2;
  82.                 else if (N_AST4_DEFINED(nkind)) {
  83.                     if (astptr[3] != opt_node && astptr[3] != (struct ast *)0)
  84.                         first = 3;
  85.                 }
  86.             }
  87.         }
  88.         if (first >= 0)
  89.             return get_left_span(astptr[first]);
  90.         else {
  91.             printf("node kind %d\n",nkind);
  92.             chaos("get_left_span: all ast's are opt_node");
  93.         }
  94.     }
  95.     else printf("get_left_span: bad node kind %d\n",nkind);
  96.     chaos("get_left_span");
  97.     return (struct tok_loc *)0;
  98. }
  99.  
  100. struct tok_loc *get_right_span(struct ast *node)        /*;get_right_span*/
  101. {
  102.     short nkind,last,length=1;
  103.     struct ast **astptr;
  104.     struct two_pool *lastelem;
  105.  
  106.     nkind = node->kind;
  107.     if (is_terminal_node(nkind)) {
  108.         if (isval_node[nkind])
  109.             /* as_null, as_null_s, as_others, as_others_choice
  110.              * have no N_VAL field defined
  111.              */
  112.             length = strlen(namelist(node->links.val));
  113.         return (make_span(node->span.line, node->span.col+length-1));
  114.     }
  115.     else if (nkind == AS_OPT)
  116.         return (&node->span);
  117.     else if (nkind == AS_EXIT) {
  118.         if (node->links.subast[1] != opt_node)
  119.             return get_right_span(node->links.subast[1]);
  120.         else if (node->links.subast[0] != opt_node)
  121.             return get_right_span(node->links.subast[0]);
  122.         else return get_right_span(node->links.subast[3]);
  123.     }
  124.     else if (nkind == AS_RETURN) {
  125.         if (node->links.subast[0] != opt_node)
  126.             return get_right_span(node->links.subast[0]);
  127.         else return get_right_span(node->links.subast[3]);
  128.     }
  129.     else if (nkind == AS_RAISE) {
  130.         if (node->links.subast[0] != opt_node)
  131.             return get_right_span(node->links.subast[0]);
  132.         else return get_right_span(node->links.subast[1]);
  133.     }
  134.     else if (nkind == AS_OTHERS_CHOICE) {
  135.         if (node->links.subast[1] != opt_node)
  136.             return get_right_span(node->links.subast[1]);
  137.         else if (node->links.subast[0] != opt_node)
  138.             return get_right_span(node->links.subast[0]);
  139.         else return get_right_span(node->links.subast[2]);
  140.     }
  141.     else if (islist_node[nkind]) {
  142.         if (node->links.list==NULL) /* only for parser */
  143.             return (&node->span);
  144.         else {
  145.             lastelem = node->links.list;
  146. #ifdef TODO
  147.             -- this finds next element in list. we need to search backwards !!!
  148.                 while ((lastelem)->val.node == opt_node) {
  149.                 lastelem = lastelem->link; /* last list element */
  150.                 if (lastelem == node->links.list)
  151.                     chaos("get_right_span: all list elements opt_node");
  152.             }
  153. #endif
  154.             if (lastelem->val.node == opt_node)
  155.                 chaos("get_right_span: list element opt_node");
  156.             return get_right_span((lastelem)->val.node);
  157.         }
  158.     }
  159.     else if (isast_node[nkind]) {
  160.         astptr = node->links.subast;
  161.         if (N_AST4_DEFINED(nkind) && astptr[3] != opt_node 
  162.             && astptr[3] != (struct ast *)0)
  163.             last = 3;
  164.         else if (N_AST3_DEFINED(nkind) && 
  165.             astptr[2] != opt_node && astptr[2] != (struct ast *)0)
  166.             last = 2;
  167.         else if (N_AST2_DEFINED(nkind) && 
  168.             astptr[1] != opt_node && astptr[1] != (struct ast *)0)
  169.             last = 1;
  170.         else if (astptr[0] == opt_node && astptr[0] == (struct ast *)0)
  171.             chaos("get_right_span: all ast's are opt_node");
  172.         else last = 0;
  173.         return get_right_span(astptr[last]);
  174.     }
  175.     else printf("get_right_span: bad node kind %d\n",nkind);
  176.     chaos("get_right_span");
  177.     return (struct tok_loc *)0;
  178. }
  179.  
  180. struct tok_loc *make_span(short line, short col)            /*;make_span*/
  181. {
  182.     struct tok_loc *tok;
  183.  
  184.     tok = (struct tok_loc *) emalloc(sizeof(struct tok_loc));
  185.     tok->line = line;
  186.     tok->col  = col;
  187.     return (tok);
  188. }
  189.