GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/bounded_token_factory.cpp Lines: 42 48 87.5 %
Date: 2021-03-22 Branches: 13 22 59.1 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file bounded_token_factory.cpp
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Christopher L. Conway
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief An ANTLR3 bounded token factory implementation.
13
 **
14
 ** An ANTLR3 bounded token factory implementation.
15
 **/
16
17
#include <antlr3input.h>
18
#include <antlr3commontoken.h>
19
#include <antlr3interfaces.h>
20
#include "parser/bounded_token_factory.h"
21
22
namespace CVC4 {
23
namespace parser {
24
25
static pANTLR3_COMMON_TOKEN
26
newPoolToken(pANTLR3_TOKEN_FACTORY factory);
27
28
static void
29
setInputStream  (pANTLR3_TOKEN_FACTORY factory, pANTLR3_INPUT_STREAM input);
30
31
static  void
32
factoryClose        (pANTLR3_TOKEN_FACTORY factory);
33
34
pANTLR3_TOKEN_FACTORY
35
5764
BoundedTokenFactoryNew(pANTLR3_INPUT_STREAM input,ANTLR3_UINT32 size)
36
{
37
    pANTLR3_TOKEN_FACTORY   factory;
38
    pANTLR3_COMMON_TOKEN tok;
39
    ANTLR3_UINT32 i;
40
41
    /* allocate memory
42
     */
43
5764
    factory     = (pANTLR3_TOKEN_FACTORY) ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_FACTORY));
44
45
5764
    if  (factory == NULL)
46
    {
47
        return  NULL;
48
    }
49
50
    /* Install factory API
51
     */
52
5764
    factory->newToken       =  newPoolToken;
53
5764
    factory->close          =  factoryClose;
54
5764
    factory->setInputStream = setInputStream;
55
56
    /* Allocate the initial pool
57
     */
58
5764
    factory->thisPool   = size;
59
5764
    factory->nextToken  = 0;
60
5764
    factory->pools      = (pANTLR3_COMMON_TOKEN*) ANTLR3_MALLOC(sizeof(pANTLR3_COMMON_TOKEN));
61
11528
    factory->pools[0]  =
62
        (pANTLR3_COMMON_TOKEN)
63
11528
        ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TOKEN) * size));
64
65
    /* Set up the tokens once and for all */
66
34532
    for( i=0; i < size; i++ ) {
67
28768
      tok = factory->pools[0] + i;
68
28768
      antlr3SetTokenAPI(tok);
69
70
      /* It is factory made, and we need to copy the string factory pointer
71
       */
72
28768
      tok->factoryMade  = ANTLR3_TRUE;
73
28768
      tok->strFactory   = input == NULL ? NULL : input->strFactory;
74
28768
      tok->input        = input;
75
    }
76
77
    /* Factory space is good, we now want to initialize our cheating token
78
     * which one it is initialized is the model for all tokens we manufacture
79
     */
80
5764
    antlr3SetTokenAPI(&factory->unTruc);
81
82
    /* Set some initial variables for future copying
83
     */
84
5764
    factory->unTruc.factoryMade = ANTLR3_TRUE;
85
86
    // Input stream
87
    //
88
5764
    setInputStream(factory, input);
89
90
5764
    return  factory;
91
92
}
93
94
static pANTLR3_COMMON_TOKEN
95
17232796
newPoolToken(pANTLR3_TOKEN_FACTORY factory)
96
{
97
17232796
  ANTLR3_UINT32 size = factory->thisPool;
98
17232796
  pANTLR3_COMMON_TOKEN tok = factory->pools[0] + (factory->nextToken % size);
99
17232796
  if(factory->nextToken >= size && tok->custom != NULL && tok->freeCustom != NULL) {
100
    tok->freeCustom(tok->custom);
101
    tok->custom = NULL;
102
  }
103
17232796
  factory->nextToken++;
104
105
17232796
  return tok;
106
}
107
108
static  void
109
5764
factoryClose        (pANTLR3_TOKEN_FACTORY factory)
110
{
111
  ANTLR3_UINT32 i;
112
5764
  ANTLR3_UINT32 size = factory->thisPool;
113
  pANTLR3_COMMON_TOKEN tok;
114
115
33466
  for(i = 0; i < size && i < factory->nextToken; i++) {
116
27702
    tok = factory->pools[0] + i;
117
27702
    if(tok->custom != NULL && tok->freeCustom != NULL) {
118
      tok->freeCustom(tok->custom);
119
      tok->custom = NULL;
120
    }
121
  }
122
5764
  ANTLR3_FREE(factory->pools[0]);
123
5764
  ANTLR3_FREE(factory->pools);
124
5764
  ANTLR3_FREE(factory);
125
126
5764
}
127
128
static void
129
5778
setInputStream  (pANTLR3_TOKEN_FACTORY factory, pANTLR3_INPUT_STREAM input)
130
{
131
5778
    factory->input          =  input;
132
5778
    factory->unTruc.input   =  input;
133
5778
        if      (input != NULL)
134
        {
135
5778
                factory->unTruc.strFactory      = input->strFactory;
136
        }
137
        else
138
        {
139
                factory->unTruc.strFactory = NULL;
140
    }
141
5778
}
142
143
}/* CVC4::parser namespace */
144
}/* CVC4 namespace */