LCOV - code coverage report
Current view: top level - src/3rdPartyLibraries/fortran-parser - token.c (source / functions) Hit Total Coverage
Test: ROSE Lines: 31 62 50.0 %
Date: 2022-12-08 13:48:47 Functions: 3 6 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /******************************************************************************
       2             :  * Copyright (c) 2005, 2006 Los Alamos National Security, LLC.  This
       3             :  * material was produced under U.S. Government contract
       4             :  * DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL), which
       5             :  * is operated by the Los Alamos National Security, LLC (LANS) for the
       6             :  * U.S. Department of Energy. The U.S. Government has rights to use,
       7             :  * reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
       8             :  * LANS MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY
       9             :  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
      10             :  * produce derivative works, such modified software should be clearly
      11             :  * marked, so as not to confuse it with the version available from
      12             :  * LANL.
      13             :  *
      14             :  * Additionally, this program and the accompanying materials
      15             :  * are made available under the terms of the Eclipse Public License v1.0
      16             :  * which accompanies this distribution, and is available at
      17             :  * http://www.eclipse.org/legal/epl-v10.html
      18             :  *****************************************************************************/
      19             : 
      20             : #ifdef __cplusplus
      21             : extern "C" {
      22             : #endif
      23             : 
      24             : #include <stdlib.h>
      25             : #include <stdio.h>
      26             : #include <limits.h>
      27             : #include <string.h>
      28             : #include "token.h"
      29             : //#include "fortran_error_handler.h"
      30             : 
      31             : #define INIT_TOKEN_LIST_SIZE 1024
      32             : 
      33             :   Token_t **token_list   = NULL;
      34             :   Token_t *token_current = NULL;
      35             :   int token_list_size = 0;
      36             :   int num_tokens = 0;
      37             :   
      38           0 :   Token_t *create_token(int line, int col, int type, const char *text)
      39             :   {
      40           0 :          Token_t *tmp_token = NULL;
      41             : 
      42           0 :          tmp_token = (Token_t*)malloc(sizeof(Token_t));
      43           0 :          tmp_token->line = line;
      44           0 :          tmp_token->col = col;
      45           0 :          tmp_token->type = type;
      46             :          /* Make a copy of our own to make sure it isn't freed on us.  */
      47           0 :          if(text != NULL)
      48           0 :                 tmp_token->text = strdup(text);
      49             :          else
      50           0 :                 tmp_token->text = NULL;
      51             : 
      52           0 :          return tmp_token;
      53             :   }
      54             : 
      55        5234 :   void free_token(Token_t *tmp_token)
      56             :   {
      57             :          /* Nothing to do.  */
      58        5234 :          if(tmp_token == NULL)
      59             :                 return;
      60             :   
      61             :          /* Free up the memory allocated for the string.  */
      62        5234 :          if(tmp_token->text != NULL)
      63        5234 :                 free(tmp_token->text);
      64             :          /* Free up the token memory itself.  */
      65        5234 :          free(tmp_token);
      66             :   }
      67             : 
      68          37 :   void free_token_list()
      69             :   {
      70          37 :      int i;
      71             : 
      72             :      /* Laksono 2009.12.15: free the error handler */
      73             :      //  fortran_error_handler_end();
      74             : 
      75        5271 :      for (i = 0; i < num_tokens; i++) {
      76        5234 :         if (token_list[i] != NULL) {
      77        5234 :            free_token(token_list[i]);
      78             :         }
      79             :      }
      80             : 
      81          37 :      if (token_list != NULL) {
      82          34 :         num_tokens = 0;
      83          34 :         token_list_size = 0;
      84          34 :         free(token_list);
      85          34 :         token_list = NULL;
      86             :      }
      87             : 
      88          37 :      return;
      89             :   }
      90             : 
      91        5234 :   void register_token(Token_t *tmp_token)
      92             :   {
      93        5234 :          if(token_list == NULL)
      94             :          {
      95          34 :                 int i;
      96             :                 
      97             :                 /* We haven't allocated a token list yet.  This is done here because
      98             :                         we currently don't have a single entry point into the actions, so we
      99             :                         don't have a place to do this.  We could expect the user to call an
     100             :                         init routine, as one option....  */
     101          34 :                 token_list = (Token_t**)malloc(INIT_TOKEN_LIST_SIZE * sizeof(Token_t *));
     102          34 :                 if(token_list == NULL)
     103             :                 {
     104           0 :                   fprintf(stderr, "Error: Unable to allocate token_list!\n");
     105           0 :                   exit(1);
     106             :                 }
     107          34 :                 token_list_size = INIT_TOKEN_LIST_SIZE;
     108             : 
     109             :                 /* Initialize the array pointers to NULL.  */
     110       34850 :                 for(i = 0; i < token_list_size; i++)
     111       34816 :                   token_list[i] = NULL;
     112             : 
     113             :                 /* Laksono 2009.12.05:  intercept SIGABRT */
     114             :                 //fortran_error_handler_begin();
     115             : 
     116             :          }
     117        5200 :          else if(num_tokens == token_list_size)
     118             :          {
     119           0 :                 int i;
     120           0 :                 int orig_size = token_list_size;
     121             :                 
     122             :                 /* The list is full so reallocate to twice it's current size.  */
     123           0 :                 token_list = (Token_t**)realloc(token_list,(token_list_size << 1) * sizeof(Token_t *));
     124           0 :                 if(token_list == NULL)
     125             :                 {
     126           0 :                   fprintf(stderr, "Error: Out of memory for token_list\n");
     127           0 :                   exit(1);
     128             :                 }
     129           0 :                 token_list_size = token_list_size << 1;
     130             : 
     131             :                 /* Initialize all new elements to NULL.  */
     132           0 :                 for(i = orig_size; i < token_list_size; i++)
     133           0 :                   token_list[i] = NULL;
     134             :          }
     135             : 
     136             :          /* The list is allocated and there is enough room for the new element.  */
     137        5234 :          token_list[num_tokens] = tmp_token;
     138        5234 :          num_tokens++;
     139        5234 :          if (tmp_token->line >0)
     140        5234 :             token_current = tmp_token;
     141             :          //printf("token %d: l=%d t=%d s=%s\n", num_tokens, tmp_token->line, tmp_token->type, tmp_token->text);
     142             : 
     143        5234 :          if(num_tokens >= INT_MAX)
     144             :          {
     145           0 :                 fprintf(stderr, "Error: Maximum number of tokens reached!\n");
     146           0 :                 exit(1);
     147             :          }
     148             : 
     149        5234 :          return;
     150             :   }
     151             : 
     152           0 :   void print_token(Token_t *tmp_token)
     153             :   {
     154             :          /* The fields of these are printed in a form similar to what ANTLR would
     155             :                  print for a Token printed as a String.  However, we don't copy all of
     156             :                  ANLTR's internal info for the Token, so the first field (@0) and the
     157             :                  '0:0=' part of the second field have no meaning for our C tokens but
     158             :                  are there for similarity.  */
     159           0 :          printf("[@0, '0:0=%s', <%d>, %d:%d]\n", 
     160           0 :                           (tmp_token->text == NULL ? "null" : tmp_token->text),
     161             :                           tmp_token->type, tmp_token->line, tmp_token->col);
     162           0 :   }
     163             : 
     164           0 :   Token_t *get_latest_token()
     165             :   {
     166           0 :      int ltok = num_tokens-1;
     167           0 :      Token_t *current_token = token_current;
     168             : /*
     169             :      if (token_list == NULL) return NULL;
     170             :      do {
     171             :         current_token = token_list[ltok];
     172             :         ltok--; 
     173             :      } while (ltok>=0 && current_token != NULL && current_token->line == 0);
     174             : */
     175             :      //printf(">> get_latest_token %d %d:'%s'\n", ltok, current_token->line, current_token->text);
     176           0 :      return current_token;
     177             :   }
     178             : 
     179             : #ifdef __cplusplus
     180             : } /* End extern C. */
     181             : #endif

Generated by: LCOV version 1.14