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 |
4072 |
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 |
4072 |
factory = (pANTLR3_TOKEN_FACTORY) ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_FACTORY)); |
43 |
|
|
44 |
4072 |
if (factory == NULL) |
45 |
|
{ |
46 |
|
return NULL; |
47 |
|
} |
48 |
|
|
49 |
|
/* Install factory API |
50 |
|
*/ |
51 |
4072 |
factory->newToken = newPoolToken; |
52 |
4072 |
factory->close = factoryClose; |
53 |
4072 |
factory->setInputStream = setInputStream; |
54 |
|
|
55 |
|
/* Allocate the initial pool |
56 |
|
*/ |
57 |
4072 |
factory->thisPool = size; |
58 |
4072 |
factory->nextToken = 0; |
59 |
4072 |
factory->pools = (pANTLR3_COMMON_TOKEN*) ANTLR3_MALLOC(sizeof(pANTLR3_COMMON_TOKEN)); |
60 |
8144 |
factory->pools[0] = |
61 |
|
(pANTLR3_COMMON_TOKEN) |
62 |
8144 |
ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TOKEN) * size)); |
63 |
|
|
64 |
|
/* Set up the tokens once and for all */ |
65 |
20360 |
for( i=0; i < size; i++ ) { |
66 |
16288 |
tok = factory->pools[0] + i; |
67 |
16288 |
antlr3SetTokenAPI(tok); |
68 |
|
|
69 |
|
/* It is factory made, and we need to copy the string factory pointer |
70 |
|
*/ |
71 |
16288 |
tok->factoryMade = ANTLR3_TRUE; |
72 |
16288 |
tok->strFactory = input == NULL ? NULL : input->strFactory; |
73 |
16288 |
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 |
4072 |
antlr3SetTokenAPI(&factory->unTruc); |
80 |
|
|
81 |
|
/* Set some initial variables for future copying |
82 |
|
*/ |
83 |
4072 |
factory->unTruc.factoryMade = ANTLR3_TRUE; |
84 |
|
|
85 |
|
// Input stream |
86 |
|
// |
87 |
4072 |
setInputStream(factory, input); |
88 |
|
|
89 |
4072 |
return factory; |
90 |
|
|
91 |
|
} |
92 |
|
|
93 |
|
static pANTLR3_COMMON_TOKEN |
94 |
11624762 |
newPoolToken(pANTLR3_TOKEN_FACTORY factory) |
95 |
|
{ |
96 |
11624762 |
ANTLR3_UINT32 size = factory->thisPool; |
97 |
11624762 |
pANTLR3_COMMON_TOKEN tok = factory->pools[0] + (factory->nextToken % size); |
98 |
11624762 |
if(factory->nextToken >= size && tok->custom != NULL && tok->freeCustom != NULL) { |
99 |
|
tok->freeCustom(tok->custom); |
100 |
|
tok->custom = NULL; |
101 |
|
} |
102 |
11624762 |
factory->nextToken++; |
103 |
|
|
104 |
11624762 |
return tok; |
105 |
|
} |
106 |
|
|
107 |
|
static void |
108 |
4072 |
factoryClose (pANTLR3_TOKEN_FACTORY factory) |
109 |
|
{ |
110 |
|
ANTLR3_UINT32 i; |
111 |
4072 |
ANTLR3_UINT32 size = factory->thisPool; |
112 |
|
pANTLR3_COMMON_TOKEN tok; |
113 |
|
|
114 |
20187 |
for(i = 0; i < size && i < factory->nextToken; i++) { |
115 |
16115 |
tok = factory->pools[0] + i; |
116 |
16115 |
if(tok->custom != NULL && tok->freeCustom != NULL) { |
117 |
|
tok->freeCustom(tok->custom); |
118 |
|
tok->custom = NULL; |
119 |
|
} |
120 |
|
} |
121 |
4072 |
ANTLR3_FREE(factory->pools[0]); |
122 |
4072 |
ANTLR3_FREE(factory->pools); |
123 |
4072 |
ANTLR3_FREE(factory); |
124 |
|
|
125 |
4072 |
} |
126 |
|
|
127 |
|
static void |
128 |
4086 |
setInputStream (pANTLR3_TOKEN_FACTORY factory, pANTLR3_INPUT_STREAM input) |
129 |
|
{ |
130 |
4086 |
factory->input = input; |
131 |
4086 |
factory->unTruc.input = input; |
132 |
4086 |
if (input != NULL) |
133 |
|
{ |
134 |
4086 |
factory->unTruc.strFactory = input->strFactory; |
135 |
|
} |
136 |
|
else |
137 |
|
{ |
138 |
|
factory->unTruc.strFactory = NULL; |
139 |
|
} |
140 |
4086 |
} |
141 |
|
|
142 |
|
} // namespace parser |
143 |
|
} // namespace cvc5 |