/***********************************************************************
Author: Zihan Chen (vczh)
Licensed under https://github.com/vczh-libraries/License
***********************************************************************/
#ifndef VCZH_REGEX_REGEX
#define VCZH_REGEX_REGEX
#include <Vlpp.h>
namespace
vl
{
namespace
regex_internal
{
class
PureResult
;
class
PureInterpretor
;
class
RichResult
;
class
RichInterpretor
;
}
namespace
regex
{
/***********************************************************************
Data Structure
***********************************************************************/
/// <summary>A sub string of the string that a <see cref="Regex"/> is matched against.</summary>
class
RegexString
:
public
Object
{
protected
:
WString
value
;
vint
start
;
vint
length
;
public
:
RegexString
(
vint
_start
=
0
);
RegexString
(
const
WString
&
_string
,
vint
_start
,
vint
_length
);
/// <summary>The position of the input string in characters.</summary>
/// <returns>The position.</returns>
vint
Start
()
const
;
/// <summary>The size of the sub string in characters.</summary>
/// <returns>The size.</returns>
vint
Length
()
const
;
/// <summary>Get the sub string as a <see cref="WString"/>.</summary>
/// <returns>The sub string.</returns>
const
WString
&
Value
()
const
;
bool
operator
=
=
(
const
RegexString
&
string
)
const
;
};
/// <summary>A match produces by a <see cref="Regex"/>.</summary>
class
RegexMatch
:
public
Object
,
private
NotCopyable
{
friend
class
Regex
;
public
:
typedef
Ptr
<
RegexMatch
>
Ref
;
typedef
collections
::
List
<
Ref
>
List
;
typedef
collections
::
List
<
RegexString
>
CaptureList
;
typedef
collections
::
Group
<
WString
,
RegexString
>
CaptureGroup
;
protected
:
collections
::
List
<
RegexString
>
captures
;
collections
::
Group
<
WString
,
RegexString
>
groups
;
bool
success
;
RegexString
result
;
RegexMatch
(
const
WString
&
_string
,
regex_internal
::
PureResult
*
_result
);
RegexMatch
(
const
WString
&
_string
,
regex_internal
::
RichResult
*
_result
,
regex_internal
::
RichInterpretor
*
_rich
);
RegexMatch
(
const
RegexString
&
_result
);
public
:
/// <summary>
/// Test if this match is a succeeded match or a failed match.
/// A failed match will only appear when calling [M:vl.regex.Regex.Split] or [M:vl.regex.Regex.Cut].
/// In other cases, failed matches are either not included in the result.
/// </summary>
/// <returns>Returns true if this match is a succeeded match.</returns>
bool
Success
()
const
;
/// <summary>Get the matched sub string.</summary>
/// <returns>The matched sub string.</returns>
const
RegexString
&
Result
()
const
;
/// <summary>Get all sub strings that are captured anonymously.</summary>
/// <returns>All sub strings that are captured anonymously.</returns>
/// <example><![CDATA[
/// int main()
/// {
/// Regex regex(L"^/.*?((?C/S+)(/.*?))+$");
/// auto match = regex.MatchHead(L"C++ and C# are my favorite programing languages");
/// <li><b>(<name>regex)</b>: Capture matched fragment in a named group called "name"</li>
/// <li><b>(<$i>)</b>: Match the i-th captured fragment, begins from 0</li>
/// <li><b>(<$name;i>)</b>: Match the i-th captured fragment in the named group called "name", begins from 0</li>
/// <li><b>(<$name>)</b>: Match any captured fragment in the named group called "name"</li>
/// </ul>
/// </li>
/// <li>
/// <b>MISC</b>
/// <ul>
/// <li><b>(=regex)</b>: The prefix of the following text should match the regex, but it is not counted in the whole match <b>(DFA incompatible)</b></li>
/// <li><b>(!regex)</b>: Any prefix of the following text should not match the regex, and it is not counted in the whole match <b>(DFA incompatible)</b></li>
/// <li><b>(<#name>regex)</b>: Name the regex "name", and it applies here</li>
/// <li><b>(<&name>)</b>: Copy the named regex "name" here and apply</li>
/// </ul>
/// </li>
/// </ul>
/// </p>
/// <p>
/// The regular expression has pupre mode and rich mode.
/// Pure mode means the regular expression is driven by a DFA, while the rich mode is not.
/// </p>
/// <p>
/// The regular expression can test a string instead of matching.
/// Testing only returns a bool very indicating success or failure.
/// </p>
/// </summary>
class
Regex
:
public
Object
,
private
NotCopyable
{
protected
:
regex_internal
::
PureInterpretor
*
pure
=
nullptr
;
regex_internal
::
RichInterpretor
*
rich
=
nullptr
;
void
Process
(
const
WString
&
text
,
bool
keepEmpty
,
bool
keepSuccess
,
bool
keepFail
,
RegexMatch
::
List
&
matches
)
const
;
public
:
/// <summary>Create a regular expression. It will crash if the regular expression produces syntax error.</summary>
/// <param name="code">The regular expression in a string.</param>
/// <param name="preferPure">Set to true to use DFA if possible.</param>
Regex
(
const
WString
&
code
,
bool
preferPure
=
true
);
~
Regex
();
/// <summary>Test is a DFA used to match a string.</summary>
/// <returns>Returns true if a DFA is used.</returns>
bool
IsPureMatch
()
const
;
/// <summary>Test is a DFA used to test a string. It ignores all capturing.</summary>
/// <returns>Returns true if a DFA is used.</returns>
bool
IsPureTest
()
const
;
/// <summary>Match a prefix of the text.</summary>
/// <returns>Returns the match. Returns null if failed.</returns>
/// <param name="text">The text to match.</param>
/// <example><![CDATA[
/// int main()
/// {
/// Regex regex(L"C/S+");
/// auto match = regex.MatchHead(L"C++ and C# are my favorite programing languages");
/// Console::WriteLine(match->Result().Value());
/// }
/// ]]></example>
RegexMatch
::
Ref
MatchHead
(
const
WString
&
text
)
const
;
/// <summary>Match a sub string of the text.</summary>
/// <returns>Returns the first match. Returns null if failed.</returns>
/// <param name="text">The text to match.</param>
/// <example><![CDATA[
/// int main()
/// {
/// Regex regex(L"C/S+");
/// auto match = regex.Match(L"C++ and C# are my favorite programing languages");
/// Console::WriteLine(match->Result().Value());
/// }
/// ]]></example>
RegexMatch
::
Ref
Match
(
const
WString
&
text
)
const
;
/// <summary>Match a prefix of the text, ignoring all capturing.</summary>
/// <returns>Returns true if it succeeded.</returns>
/// <param name="text">The text to match.</param>
bool
TestHead
(
const
WString
&
text
)
const
;
/// <summary>Match a sub string of the text, ignoring all capturing.</summary>
/// <returns>Returns true if succeeded.</returns>
/// <param name="text">The text to match.</param>
bool
Test
(
const
WString
&
text
)
const
;
/// <summary>Find all matched fragments in the given text, returning all matched sub strings.</summary>
/// <param name="text">The text to match.</param>
/// <param name="matches">Returns all succeeded matches.</param>
/// <example><![CDATA[
/// int main()
/// {
/// Regex regex(L"C/S+");
/// RegexMatch::List matches;
/// regex.Search(L"C++ and C# are my favorite programing languages", matches);
/// FOREACH(Ptr<RegexMatch>, match, matches)
/// {
/// Console::WriteLine(match->Result().Value());
/// }
/// }
/// ]]></example>
void
Search
(
const
WString
&
text
,
RegexMatch
::
List
&
matches
)
const
;
/// <summary>Split the text by matched sub strings, returning all unmatched sub strings.</summary>
/// <param name="text">The text to match.</param>
/// <param name="keepEmptyMatch">Set to true to keep all empty unmatched sub strings. This could happen when there is nothing between two matched sub strings.</param>
/// <param name="matches">Returns all failed matches.</param>
/// <example><![CDATA[
/// int main()
/// {
/// Regex regex(L"C/S+");
/// RegexMatch::List matches;
/// regex.Split(L"C++ and C# are my favorite programing languages", false, matches);
/// FOREACH(Ptr<RegexMatch>, match, matches)
/// {
/// Console::WriteLine(match->Result().Value());
/// }
/// }
/// ]]></example>
void
Split
(
const
WString
&
text
,
bool
keepEmptyMatch
,
RegexMatch
::
List
&
matches
)
const
;
/// <summary>Cut the text by matched sub strings, returning all matched and unmatched sub strings.</summary>
/// <param name="text">The text to match.</param>
/// <param name="keepEmptyMatch">Set to true to keep all empty matches. This could happen when there is nothing between two matched sub strings.</param>
/// <param name="matches">Returns all succeeded and failed matches.</param>
/// <example><![CDATA[
/// int main()
/// {
/// Regex regex(L"C/S+");
/// RegexMatch::List matches;
/// regex.Cut(L"C++ and C# are my favorite programing languages", false, matches);
/// <summary>Position in the input string in characters.</summary>
vint
start
;
/// <summary>Size of this token in characters.</summary>
vint
length
;
/// <summary>The token id, begins at 0, represents the regular expression in the list (the first argument in the contructor of <see cref="RegexLexer"/>) that matches this token. -1 means this token is produced by an error.</summary>
vint
token
;
/// <summary>The pointer to where this token starts in the input string .</summary>
/// <remarks>This pointer comes from a <see cref="WString"/> that used to be analyzed. You should keep a variable to that string alive, so that to keep this pointer alive.</remarks>
const
wchar_t
*
reading
;
/// <summary>The "codeIndex" argument from [M:vl.regex.RegexLexer.Parse].</summary>
vint
codeIndex
;
/// <summary>True if this token is complete. False if this token does not end here. This could happend when colorizing a text line by line.</summary>
bool
completeToken
;
/// <summary>Row number of the first character, begins at 0.</summary>
vint
rowStart
;
/// <summary>Column number of the first character, begins at 0.</summary>
vint
columnStart
;
/// <summary>Row number of the last character, begins at 0.</summary>
vint
rowEnd
;
/// <summary>Column number of the last character, begins at 0.</summary>
vint
columnEnd
;
bool
operator
=
=
(
const
RegexToken
&
_token
)
const
;
bool
operator
=
=
(
const
wchar_t
*
_token
)
const
;
};
/// <summary>Token information for <see cref="RegexProc::extendProc"/>.</summary>
struct
RegexProcessingToken
{
/// <summary>
/// The read only start position of the token.
/// This value will be -1 if <see cref="interTokenState"/> is not null.
/// </summary>
const
vint
start
;
/// <summary>
/// The length of the token, allowing to be updated by the callback.
/// When the callback returns, the length is not allowed to be decreased.
/// This value will be -1 if <see cref="interTokenState"/> is not null.
/// </summary>
vint
length
;
/// <summary>
/// The id of the token, allowing to be updated by the callback.
/// </summary>
vint
token
;
/// <summary>
/// The flag indicating if this token is completed, allowing to be updated by the callback.
/// </summary>
bool
completeToken
;
/// <summary>
/// The inter token state object, allowing to be updated by the callback.
/// When the callback returns:
/// <ul>
/// <li>if the completeText parameter is true in <see cref="RegexProc::extendProc"/>, it should be nullptr.</li>
/// <li>if the token does not end at the end of the input, it should not be nullptr.</li>
/// <li>if a token is completed in one attemp of extending, it should be nullptr.</li>
/// </ul>
/// </summary>
void
*
interTokenState
;
RegexProcessingToken
(
vint
_start
,
vint
_length
,
vint
_token
,
bool
_completeToken
,
void
*
_interTokenState
)
:start(
_start
)
, length(
_length
)
, token(
_token
)
, completeToken(
_completeToken
)
, interTokenState(
_interTokenState
)
{
}
};
using
RegexInterTokenStateDeleter
=
void
(*)(
void
* interTokenState);
using
RegexTokenExtendProc
=
void
(*)(
void
* argument,
const
wchar_t
* reading,
vint
length,
bool
completeText,
RegexProcessingToken
& processingToken);
using
RegexTokenColorizeProc
=
void
(*)(
void
* argument,
vint
start,
vint
length,
vint
token);
/// <summary>Callback procedures</summary>
struct
RegexProc
{
/// <summary>
/// The deleter which deletes <see cref="RegexProcessingToken::interTokenState"/> created by <see cref="extendProc"/>.
/// This callback is not called automatically.
/// It is here to make the maintainance convenient for the caller.
/// </summary>
RegexInterTokenStateDeleter
deleter
=
nullptr
;
/// <summary>
/// <p>The token extend callback. It is called after recognizing any token, and run a customized procedure to modify the token based on the given context.</p>
/// <p>If the length parameter is -1, it means the caller does not measure the incoming text buffer, which automatically indicates that the buffer is null-terminated.</p>
/// <p>If the length parameter is not -1, it means the number of available characters in the buffer.</p>
/// <p>The completeText parameter could be true or false. When it is false, it means that the buffer does not contain all the text.</p>
/// </summary>
/// <remarks>
/// <p>
/// This is very useful to recognize any token that cannot be expressed using a regular expression.
/// For example, a C++ literal string R"tag(the conteng)tag".
/// It is recommended to add a token for <b>R"tag(</b>,
/// and then use this extend proc to search for a <b>)tag"</b> to complete the token.
/// </p>
/// <p>
/// <b>Important</b>:
/// when colorizing a text line by line,
/// a cross-line token could be incomplete at the end of the line.
/// Because a given buffer ends at the end of that line,
/// the extend proc is not able to know right now about what is going on in the future.
/// Here is what <see cref="RegexProcessingToken::interTokenState"/> is designed for,
/// the extend proc can store anything it wants using that pointer.
/// </p>
/// <p>
/// The caller can get this pointer from the return value of <see cref="RegexLexerColorizer::Colorize"/>.
/// This pointer only available for cross-line tokens, it is obvious that one line produces at most one such pointer.
/// Then the caller keeps calling that function to walk throught the whole string.
/// When the return value is changed, the pointer is no longer used, and it can be deleted by calling <see cref="deleter"/> manually.
/// </p>
/// <p>
/// The first argument is <see cref="argument"/>.
/// </p>
/// <p>
/// The second argument is a pointer to the buffer of the first character in this token.
/// If the previous token is incomplete, then the buffer begins at the first character of the new buffer.
/// </p>
/// <p>
/// The third argument is the length of the recognized token in characters.
/// </p>
/// <p>
/// The fourth character indicates if the token is completed.
/// Even if a token is completed, but the extend proc found that, the extend exceeds the end of the buffer,
/// then it can update the value to make it incomplete.
/// </p>
/// <p>
/// The fifth contains the context for this token. Fields except "start" are allowed to be updated by the extend proc.
/// if (lastInterTokenState && lastInterTokenState != interTokenState)
/// {
/// // call the deleter manually
/// proc.deleter(lastInterTokenState);
/// }
/// lastInterTokenState = interTokenState;
///
/// argument.processingText = nullptr;
/// colorizer.Pass(L'\r');
/// colorizer.Pass(L'\n');
/// Console::WriteLine(L"");
/// }
/// }
/// ]]></example>
RegexTokenExtendProc
extendProc
=
nullptr
;
/// <summary>
/// <p>
/// The colorizer callback. It is called when a token is recognized.
/// </p>
/// <p>
/// The first argument is <see cref="argument"/>.
/// </p>
/// <p>
/// The second argument is the position of the first character of the token in characters.
/// </p>
/// <p>
/// The third argument is the length of the recognized token in characters.
/// </p>
/// <p>
/// The fourth character is the regular expression in the list (the first argument in the contructor of <see cref="RegexLexer"/>) that matches this token.
/// </p>
/// </summary>
RegexTokenColorizeProc
colorizeProc
=
nullptr
;
/// <summary>
/// The argument object that is the first argument for <see cref="extendProc"/> and <see cref="colorizeProc"/>.
/// </summary>
void
*
argument
=
nullptr
;
};
/// <summary>Token collection representing the result from the lexical analyzer. Call <see cref="RegexLexer::Parse"/> to create this object.</summary>
/// <example><![CDATA[
/// int main()
/// {
/// List<WString> tokenDefs;
/// tokenDefs.Add(L"/d+");
/// tokenDefs.Add(L"/w+");
/// tokenDefs.Add(L"/s+");
///
/// RegexLexer lexer(tokenDefs, {});
/// WString input = L"I have 2 books.";
/// auto tokenResult = lexer.Parse(input);
///
/// FOREACH(RegexToken, token, tokenResult)
/// {
/// // input must be in a variable
/// // because token.reading points to a position from input.Buffer();
/// <param name="tokens">Returns all tokens.</param>
/// <param name="discard">A callback to decide which kind of tokens to discard. The input is [F:vl.regex.RegexToken.token]. Returns true to discard this kind of tokens.</param>
/// <summary>A type for walking through a text against a <see cref="RegexLexer"/>. Call <see cref="RegexLexer::Walk"/> to create this object.</summary>
/// <example><![CDATA[
/// int main()
/// {
/// List<WString> tokenDefs;
/// tokenDefs.Add(L"/d+./d+");
/// tokenDefs.Add(L"/d+");
/// tokenDefs.Add(L"/w+");
/// tokenDefs.Add(L"/s+");
///
/// RegexLexer lexer(tokenDefs, {});
/// RegexLexerWalker walker = lexer.Walk();
///
/// WString input = L"This book costs 2.5. That book costs 2.";
/// <remarks>Callbacks in <see cref="RegexProc"/> will be called <b>except colorizeProc</b>, which is from the second argument of the constructor of <see cref="RegexLexer"/>.</remarks>
void
Pass
(
wchar_t
input
);
/// <summary>Get the start DFA state number, which represents the correct state before colorizing any characters.</summary>
/// <returns>The DFA state number.</returns>
vint
GetStartState
()
const
;
/// <summary>Colorize a text.</summary>
/// <returns>An inter token state at the end of this line. It could be the same object to which is returned from the previous call.</returns>
/// <param name="input">The text to colorize.</param>
/// <param name="length">Size of the text in characters.</param>
/// <remarks>
/// <p>See <see cref="RegexProcessingToken::interTokenState"/> and <see cref="RegexProc::extendProc"/> for more information about the return value.</p>
/// <p>Callbacks in <see cref="RegexProc"/> will be called, which is from the second argument of the constructor of <see cref="RegexLexer"/>.</p>
/// </remarks>
void
*
Colorize
(
const
wchar_t
*
input
,
vint
length
);
};
/// <summary>Lexical analyzer.</summary>
class
RegexLexer
:
public
Object
,
private
NotCopyable
{
protected
:
regex_internal
::
PureInterpretor
*
pure
=
nullptr
;
collections
::
Array
<
vint
>
ids
;
collections
::
Array
<
vint
>
stateTokens
;
RegexProc
proc
;
public
:
/// <summary>Create a lexical analyzer by a set of regular expressions. [F:vl.regex.RegexToken.token] will be the index of the matched regular expression in the first argument.</summary>
/// <param name="tokens">ALl regular expression, each one represent a kind of tokens.</param>
/// <param name="_proc">Configuration of all callbacks.</param>
RegexLexer
(
const
collections
::
IEnumerable
<
WString
>&
tokens
,
RegexProc
_proc
);
~
RegexLexer
();
/// <summary>Tokenize an input text.</summary>
/// <returns>All tokens, including recognized tokens or unrecognized tokens. For unrecognized tokens, [F:vl.regex.RegexToken.token] will be -1.</returns>
/// <param name="code">The text to tokenize.</param>
/// <param name="codeIndex">Extra information that will be copied to [F:vl.regex.RegexToken.codeIndex].</param>
/// <remarks>Callbacks in <see cref="RegexProc"/> will be called when iterating through tokens, which is from the second argument of the constructor of <see cref="RegexLexer"/>.</remarks>
RegexTokens
Parse
(
const
WString
&
code
,
vint
codeIndex
=-
1
)
const
;
/// <summary>Create a equivalence walker from this lexical analyzer. A walker enable you to walk throught characters one by one,</summary>
/// <returns>The walker.</returns>
RegexLexerWalker
Walk
()
const
;
/// <summary>Create a equivalence colorizer from this lexical analyzer.</summary>