forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexPattern.cpp
More file actions
182 lines (160 loc) · 5.86 KB
/
RegexPattern.cpp
File metadata and controls
182 lines (160 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
namespace UnifiedRegex
{
RegexPattern::RegexPattern(Js::JavascriptLibrary *const library, Program* program, bool isLiteral)
: library(library), isLiteral(isLiteral), isShallowClone(false)
{
rep.unified.program = program;
rep.unified.matcher = 0;
rep.unified.trigramInfo = 0;
}
RegexPattern *RegexPattern::New(Js::ScriptContext *scriptContext, Program* program, bool isLiteral)
{
return
RecyclerNewFinalized(
scriptContext->GetRecycler(),
RegexPattern,
scriptContext->GetLibrary(),
program,
isLiteral);
}
void RegexPattern::Finalize(bool isShutdown)
{
if(isShutdown)
return;
const auto scriptContext = GetScriptContext();
if(!scriptContext)
return;
#if DBG
// In JSRT, we might not have a chance to close at finalize time.
if(!isLiteral && !scriptContext->IsClosed() && !scriptContext->GetThreadContext()->IsJSRT())
{
const auto source = GetSource();
RegexPattern *p;
Assert(
!GetScriptContext()->GetDynamicRegexMap()->TryGetValue(
RegexKey(source.GetBuffer(), source.GetLength(), GetFlags()),
&p) || ( source.GetLength() == 0 ) ||
p != this);
}
#endif
if(isShallowClone)
return;
rep.unified.program->FreeBody(scriptContext->RegexAllocator());
}
void RegexPattern::Dispose(bool isShutdown)
{
}
Js::ScriptContext *RegexPattern::GetScriptContext() const
{
return library->GetScriptContext();
}
Js::InternalString RegexPattern::GetSource() const
{
return Js::InternalString(rep.unified.program->source, rep.unified.program->sourceLen);
}
RegexFlags RegexPattern::GetFlags() const
{
return rep.unified.program->flags;
}
int RegexPattern::NumGroups() const
{
return rep.unified.program->numGroups;
}
bool RegexPattern::IsIgnoreCase() const
{
return (rep.unified.program->flags & IgnoreCaseRegexFlag) != 0;
}
bool RegexPattern::IsGlobal() const
{
return (rep.unified.program->flags & GlobalRegexFlag) != 0;
}
bool RegexPattern::IsMultiline() const
{
return (rep.unified.program->flags & MultilineRegexFlag) != 0;
}
bool RegexPattern::IsUnicode() const
{
return GetScriptContext()->GetConfig()->IsES6UnicodeExtensionsEnabled() && (rep.unified.program->flags & UnicodeRegexFlag) != 0;
}
bool RegexPattern::IsSticky() const
{
return GetScriptContext()->GetConfig()->IsES6RegExStickyEnabled() && (rep.unified.program->flags & StickyRegexFlag) != 0;
}
bool RegexPattern::WasLastMatchSuccessful() const
{
return rep.unified.matcher != 0 && rep.unified.matcher->WasLastMatchSuccessful();
}
GroupInfo RegexPattern::GetGroup(int groupId) const
{
Assert(groupId == 0 || WasLastMatchSuccessful());
Assert(groupId >= 0 && groupId < NumGroups());
return rep.unified.matcher->GetGroup(groupId);
}
RegexPattern *RegexPattern::CopyToScriptContext(Js::ScriptContext *scriptContext)
{
// This routine assumes that this instance will outlive the copy, which is the case for copy-on-write,
// and therefore doesn't copy the immutable parts of the pattern. This should not be confused with a
// would be CloneToScriptContext which will would clone the immutable parts as well because the lifetime
// of a clone might be longer than the original.
RegexPattern *result = UnifiedRegex::RegexPattern::New(scriptContext, rep.unified.program, isLiteral);
Matcher *matcherClone = rep.unified.matcher ? rep.unified.matcher->CloneToScriptContext(scriptContext, result) : nullptr;
result->rep.unified.matcher = matcherClone;
result->isShallowClone = true;
return result;
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void RegexPattern::Print(DebugWriter* w)
{
w->Print(_u("/"));
Js::InternalString str = GetSource();
if (str.GetLength() == 0)
w->Print(_u("(?:)"));
else
{
for (charcount_t i = 0; i < str.GetLength(); ++i)
{
const char16 c = str.GetBuffer()[i];
switch(c)
{
case _u('/'):
w->Print(_u("\\%lc"), c);
break;
case _u('\n'):
case _u('\r'):
case _u('\x2028'):
case _u('\x2029'):
w->PrintEscapedChar(c);
break;
case _u('\\'):
Assert(i + 1 < str.GetLength()); // cannot end in a '\'
w->Print(_u("\\%lc"), str.GetBuffer()[++i]);
break;
default:
w->PrintEscapedChar(c);
break;
}
}
}
w->Print(_u("/"));
if (IsIgnoreCase())
w->Print(_u("i"));
if (IsGlobal())
w->Print(_u("g"));
if (IsMultiline())
w->Print(_u("m"));
if (IsUnicode())
w->Print(_u("u"));
if (IsSticky())
w->Print(_u("y"));
w->Print(_u(" /* "));
w->Print(_u(", "));
w->Print(isLiteral ? _u("literal") : _u("dynamic"));
w->Print(_u(" */"));
}
#endif
}