public final class RegexLanguage extends com.oracle.truffle.api.TruffleLanguage<RegexLanguage.RegexContext>
This language represents classic regular expressions. It accepts regular expressions in the
following format: options/regex/flags, where options is a comma-separated list of
key-value pairs which affect how the regex is interpreted (see RegexOptions), and
/regex/flags is equivalent to the popular regular expression literal format found in e.g.
JavaScript or Ruby.
When parsing a regular expression, TRegex will return a CallTarget, which, when called,
will yield one of the following results:
TruffleNull object, indicating that TRegex cannot handle the given regexRegexObject, which can be used to match the given regexRegexSyntaxException may be thrown to indicate a syntax error. This exception is an
AbstractTruffleException with exception type ExceptionType.PARSE_ERROR.
Object regex;
try {
regex = getContext().getEnv().parseInternal(Source.newBuilder("regex", "Flavor=ECMAScript/(a|(b))c/i", "myRegex").mimeType("application/tregex").internal(true).build()).call();
} catch (AbstractTruffleException e) {
if (InteropLibrary.getUncached().getExceptionType(e) == ExceptionType.PARSE_ERROR) {
// handle parser error
} else {
// fatal error, this should never happen
}
}
if (InteropLibrary.getUncached().isNull(regex)) {
// regex is not supported by TRegex, fall back to a different regex engine
}
Regex matcher usage example in pseudocode:
regex = <matcher from previous example>
assert(regex.pattern == "(a|(b))c")
assert(regex.flags.ignoreCase == true)
assert(regex.groupCount == 3)
result = regex.exec("xacy", 0)
assert(result.isMatch == true)
assertEquals([result.getStart(0), result.getEnd(0)], [ 1, 3])
assertEquals([result.getStart(1), result.getEnd(1)], [ 1, 2])
assertEquals([result.getStart(2), result.getEnd(2)], [-1, -1])
result2 = regex.exec("xxx", 0)
assert(result2.isMatch == false)
// result2.getStart(...) and result2.getEnd(...) are undefined
Debug loggers: Loggers.RegexOptions,
RegexObject,
Loggers| Modifier and Type | Class and Description |
|---|---|
static class |
RegexLanguage.RegexContext |
com.oracle.truffle.api.TruffleLanguage.ContextLocalFactory<C,T>, com.oracle.truffle.api.TruffleLanguage.ContextLocalProvider<C>, com.oracle.truffle.api.TruffleLanguage.ContextPolicy, com.oracle.truffle.api.TruffleLanguage.ContextReference<C>, com.oracle.truffle.api.TruffleLanguage.ContextThreadLocalFactory<C,T>, com.oracle.truffle.api.TruffleLanguage.Env, com.oracle.truffle.api.TruffleLanguage.ExitMode, com.oracle.truffle.api.TruffleLanguage.InlineParsingRequest, com.oracle.truffle.api.TruffleLanguage.LanguageReference<L extends com.oracle.truffle.api.TruffleLanguage>, com.oracle.truffle.api.TruffleLanguage.ParsingRequest, com.oracle.truffle.api.TruffleLanguage.Registration| Modifier and Type | Field and Description |
|---|---|
static String |
ID |
static String |
MIME_TYPE |
static String |
NAME |
RegexParserGlobals |
parserGlobals |
| Constructor and Description |
|---|
RegexLanguage() |
| Modifier and Type | Method and Description |
|---|---|
protected RegexLanguage.RegexContext |
createContext(com.oracle.truffle.api.TruffleLanguage.Env env) |
GroupBoundaries[] |
getCachedGroupBoundaries() |
protected Object |
getScope(RegexLanguage.RegexContext context) |
protected boolean |
isThreadAccessAllowed(Thread thread,
boolean singleThreaded)
RegexLanguage is thread-safe - it supports parallel parsing requests as well as
parallel access to all AbstractRegexObjects. |
protected com.oracle.truffle.api.CallTarget |
parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest parsingRequest) |
protected boolean |
patchContext(RegexLanguage.RegexContext context,
com.oracle.truffle.api.TruffleLanguage.Env newEnv) |
areOptionsCompatible, disposeContext, disposeThread, exitContext, finalizeContext, finalizeThread, getAsynchronousStackDepth, getLanguageHome, getLanguageView, getOptionDescriptors, initializeContext, initializeMultipleContexts, initializeMultiThreading, initializeThread, isVisible, parsepublic static final String NAME
public static final String ID
public static final String MIME_TYPE
public final RegexParserGlobals parserGlobals
public GroupBoundaries[] getCachedGroupBoundaries()
protected com.oracle.truffle.api.CallTarget parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest parsingRequest)
parse in class com.oracle.truffle.api.TruffleLanguage<RegexLanguage.RegexContext>protected RegexLanguage.RegexContext createContext(com.oracle.truffle.api.TruffleLanguage.Env env)
createContext in class com.oracle.truffle.api.TruffleLanguage<RegexLanguage.RegexContext>protected boolean patchContext(RegexLanguage.RegexContext context, com.oracle.truffle.api.TruffleLanguage.Env newEnv)
patchContext in class com.oracle.truffle.api.TruffleLanguage<RegexLanguage.RegexContext>protected Object getScope(RegexLanguage.RegexContext context)
getScope in class com.oracle.truffle.api.TruffleLanguage<RegexLanguage.RegexContext>protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded)
RegexLanguage is thread-safe - it supports parallel parsing requests as well as
parallel access to all AbstractRegexObjects. Parallel access to
RegexResults objects may lead to duplicate execution
of code, but no wrong results.isThreadAccessAllowed in class com.oracle.truffle.api.TruffleLanguage<RegexLanguage.RegexContext>thread - the thread that accesses the context for the first time.singleThreaded - true if the access is considered single-threaded, false
if more than one thread is active at the same time.true