Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for other regex flavours #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/OnigRegExp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import OnigScanner, { IOnigCaptureIndex } from './OnigScanner'
import OnigScanner, { IOnigCaptureIndex, IOnigOptions } from './OnigScanner'
import OnigString from './OnigString';

export interface IOnigSearchResult extends IOnigCaptureIndex {
Expand All @@ -11,12 +11,13 @@ class OnigRegExp {
/**
* Create a new regex with the given pattern
* @param source A string pattern
* @param options Optional settings to specify syntax/regex flavour
*/
constructor(source: string) {
constructor(source: string, options?: IOnigOptions) {
this.source = source

try {
this.scanner = new OnigScanner([this.source])
this.scanner = new OnigScanner([this.source], options)
} catch (error) {
// doesn't make much sense, but this to pass atom/node-oniguruam tests
}
Expand Down
38 changes: 36 additions & 2 deletions src/OnigScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ interface INativeOnigHInfo {
regexTPtrs: Uint8Array | null
}

enum OnigSyntax {
ONIG_SYNTAX_ONIGURUMA,
ONIG_SYNTAX_ASIS,
ONIG_SYNTAX_POSIX_BASIC,
ONIG_SYNTAX_POSIX_EXTENDED,
ONIG_SYNTAX_EMACS,
ONIG_SYNTAX_GREP,
ONIG_SYNTAX_GNU_REGEX,
ONIG_SYNTAX_JAVA,
ONIG_SYNTAX_PERL,
ONIG_SYNTAX_PERL_NG,
ONIG_SYNTAX_RUBY,
}

export interface IOnigOptions {
syntax: 'perl' | 'posix' | 'java'
}

const onigSyntaxMap = new Map<string, OnigSyntax>()
.set('perl', OnigSyntax.ONIG_SYNTAX_PERL_NG)
.set('posix', OnigSyntax.ONIG_SYNTAX_POSIX_EXTENDED)
.set('java', OnigSyntax.ONIG_SYNTAX_JAVA)

export interface IOnigCaptureIndex {
index: number
start: number
Expand Down Expand Up @@ -51,11 +74,13 @@ const cache: Cache<OnigScanner, INativeOnigHInfo> = new LRUCache({

export class OnigScanner {
private sources: string[]
private syntax: OnigSyntax = OnigSyntax.ONIG_SYNTAX_ONIGURUMA
/**
* Create a new scanner with the given patterns
* @param patterns An array of string patterns
* @param options Optional settings to specify syntax/regex flavour
*/
constructor(patterns: string[]) {
constructor(patterns: string[], options?: IOnigOptions) {
if (onigasmH === null) {
throw new Error(`Onigasm has not been initialized, call loadWASM from 'onigasm' exports before using any other API`)
}
Expand All @@ -65,6 +90,15 @@ export class OnigScanner {
throw new TypeError(`First parameter to OnigScanner constructor must be array of (pattern) strings`)
}
}

if (options && typeof options.syntax === 'string') {
if (onigSyntaxMap.has(options.syntax)) {
this.syntax = onigSyntaxMap.get(options.syntax)
} else {
throw new Error(`Unsupported syntax ${options.syntax}`)
}
}

this.sources = patterns.slice()
}

Expand Down Expand Up @@ -109,7 +143,7 @@ export class OnigScanner {
const regexTPtrs = []
for (let i = 0; i < this.sources.length; i++) {
const pattern = this.sources[i];
status = onigasmH.ccall('compilePattern', 'number', ['string', 'number'], [pattern, regexTAddrRecieverPtr])
status = onigasmH.ccall('compilePattern', 'number', ['string', 'number'], [pattern, regexTAddrRecieverPtr], this.syntax)
if (status !== 0) {
const errString = onigasmH.ccall('getLastError', 'string')
throw new Error(errString)
Expand Down
21 changes: 19 additions & 2 deletions src/onigasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ char *getLastError()
}

EMSCRIPTEN_KEEPALIVE
int compilePattern(UChar *pattern, int *regexTPtr)
int compilePattern(UChar *pattern, int *regexTPtr, int syntaxCode)
{
int r;
regex_t *reg;
OnigErrorInfo einfo;
OnigSyntaxType *syntax;
// see enum OnigSyntax in `./OnigScanner.ts`
switch (syntaxCode)
{
case 3:
syntax = ONIG_SYNTAX_POSIX_EXTENDED;
break;
case 7:
syntax = ONIG_SYNTAX_JAVA;
break;
case 8:
syntax = ONIG_SYNTAX_PERL_NG;
break;
default:
syntax = ONIG_SYNTAX_DEFAULT;
}

r = onig_new(&reg, pattern, pattern + strlen((char *)pattern),
ONIG_OPTION_CAPTURE_GROUP, ONIG_ENCODING_UTF8,
ONIG_SYNTAX_DEFAULT, &einfo);
syntax, &einfo);
if (r != ONIG_NORMAL)
{
lastErrCode = r;
Expand Down