summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--changelog.md3
-rw-r--r--lib/impure/nre.nim2
-rw-r--r--lib/impure/re.nim2
-rw-r--r--lib/js/jsre.nim43
-rw-r--r--tools/kochdocs.nim7
5 files changed, 53 insertions, 4 deletions
diff --git a/changelog.md b/changelog.md
index 030644951..8446f9467 100644
--- a/changelog.md
+++ b/changelog.md
@@ -122,6 +122,9 @@
 
 - `strformat.fmt` and `strformat.&` support `= specifier`. `fmt"{expr=}"` now expands to `fmt"expr={expr}"`.
 
+- Add `jsre` module, [Regular Expressions for the JavaScript target.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
+
+
 ## Language changes
 - In the newruntime it is now allowed to assign to the discriminator field
   without restrictions as long as case object doesn't have custom destructor.
diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim
index fe71f89fe..653d4d1c5 100644
--- a/lib/impure/nre.nim
+++ b/lib/impure/nre.nim
@@ -7,7 +7,7 @@
 #
 
 when defined(js):
-  {.error: "This library needs to be compiled with a c-like backend, and depends on PCRE.".}
+  {.error: "This library needs to be compiled with a c-like backend, and depends on PCRE; See jsre for JS backend.".}
 
 ## What is NRE?
 ## ============
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index 1a6622677..fe3ea96ff 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -8,7 +8,7 @@
 #
 
 when defined(js):
-  {.error: "This library needs to be compiled with a c-like backend, and depends on PCRE.".}
+  {.error: "This library needs to be compiled with a c-like backend, and depends on PCRE; See jsre for JS backend.".}
 
 ## Regular expression support for Nim.
 ##
diff --git a/lib/js/jsre.nim b/lib/js/jsre.nim
new file mode 100644
index 000000000..31cfa3d0b
--- /dev/null
+++ b/lib/js/jsre.nim
@@ -0,0 +1,43 @@
+## Regular Expressions for the JavaScript target.
+## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
+
+type RegExp* {.importjs.} = object    ## Regular Expressions for JavaScript target.
+  flags* {.importjs.}: cstring        ## cstring that contains the flags of the RegExp object.
+  dotAll* {.importjs.}: bool          ## Whether `.` matches newlines or not.
+  global* {.importjs.}: bool          ## Whether to test against all possible matches in a string, or only against the first.
+  ignoreCase* {.importjs.}: bool      ## Whether to ignore case while attempting a match in a string.
+  multiline* {.importjs.}: bool       ## Whether to search in strings across multiple lines.
+  source* {.importjs.}: cstring       ## The text of the pattern.
+  sticky* {.importjs.}: bool          ## Whether the search is sticky.
+  unicode* {.importjs.}: bool         ## Whether Unicode features are enabled.
+  lastIndex* {.importjs.}: cint       ## Index at which to start the next match (read/write property).
+  input* {.importjs.}: cstring        ## Read-only and modified on successful match.
+  lastMatch* {.importjs.}: cstring    ## Ditto.
+  lastParen* {.importjs.}: cstring    ## Ditto.
+  leftContext* {.importjs.}: cstring  ## Ditto.
+  rightContext* {.importjs.}: cstring ## Ditto.
+
+func newRegExp*(pattern: cstring; flags: cstring): RegExp {.importjs: "new RegExp(@)".}
+  ## Creates a new RegExp object.
+
+func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.compile(@)".}
+  ## Recompiles a regular expression during execution of a script.
+
+func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
+  ## Executes a search for a match in its string parameter.
+
+func test*(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)".}
+  ## Tests for a match in its string parameter.
+
+func toString*(self: RegExp): cstring {.importjs: "#.toString()".}
+  ## Returns a string representing the RegExp object.
+
+
+runnableExamples:
+  let jsregex: RegExp = newRegExp(r"\s+", r"i")
+  jsregex.compile(r"\w+", r"i")
+  doAssert jsregex.test(r"nim javascript")
+  doAssert jsregex.exec(r"nim javascript") == @["nim".cstring]
+  doAssert jsregex.toString() == r"/\w+/i"
+  jsregex.compile(r"[0-9]", r"i")
+  doAssert jsregex.test(r"0123456789abcd")
diff --git a/tools/kochdocs.nim b/tools/kochdocs.nim
index 12d3c31a3..43b802ee2 100644
--- a/tools/kochdocs.nim
+++ b/tools/kochdocs.nim
@@ -15,6 +15,8 @@ const
 
 var nimExe*: string
 
+template isJsOnly(file: string): bool = file.isRelativeTo("lib/js")
+
 proc exe*(f: string): string =
   result = addFileExt(f, ExeExt)
   when defined(windows):
@@ -256,10 +258,11 @@ proc buildDoc(nimArgs, destPath: string) =
       destPath / changeFileExt(splitFile(d).name, "html"), d]
     i.inc
   for d in items(doc):
+    let extra = if isJsOnly(d): " --backend:js " else: ""
     var nimArgs2 = nimArgs
     if d.isRelativeTo("compiler"): doAssert false
-    commands[i] = nim & " doc $# --git.url:$# --outdir:$# --index:on $#" %
-      [nimArgs2, gitUrl, destPath, d]
+    commands[i] = nim & " doc $# $# --git.url:$# --outdir:$# --index:on $#" %
+      [extra, nimArgs2, gitUrl, destPath, d]
     i.inc
   for d in items(withoutIndex):
     commands[i] = nim & " doc2 $# --git.url:$# -o:$# $#" %