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

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