diff --git a/.clang-format-ignore b/.clang-format-ignore index 9723833..711e99e 100644 --- a/.clang-format-ignore +++ b/.clang-format-ignore @@ -1,7 +1,9 @@ -bitwise/documentation -console/documentation -hash/documentation -http/documentation -http/bulk_req.py -json/documentation -random/documentation +src/bitwise/documentation +src/console/documentation +src/hash/documentation +src/http/documentation +src/http/bulk_req.py + +draft/database/documentation +draft/json/documentation +draft/re/documentation diff --git a/.gitignore b/.gitignore index b4a272b..4865160 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,10 @@ build/ +cmake-build-*/ __pycache__/ __arkscript__/ -cformat.ps1 .idea/ -cmake-build-*/ -build/ *.o *.out *.dll @@ -15,4 +13,3 @@ build/ *.so draft/playground - diff --git a/.gitmodules b/.gitmodules index 9563436..f45cb26 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,9 @@ [submodule "submodules/termcolor"] path = submodules/termcolor url = https://github.com/ikalnytskyi/termcolor +[submodule "submodules/abseil"] + path = submodules/abseil + url = https://github.com/abseil/abseil-cpp.git +[submodule "submodules/re2"] + path = submodules/re2 + url = https://github.com/google/re2.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 04dfede..1e2ea49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,15 +2,7 @@ cmake_minimum_required(VERSION 3.16) project(Modules) -set(ARK_MOD_ALL Off CACHE BOOL "Build all the modules") -set(ARK_MOD_DRAFT Off CACHE BOOL "Build all draft modules") +set(ARK_REQUESTED_MODULES "" CACHE STRING "List of module names to build") -if (ARK_MOD_ALL) - message(STATUS "Building modules in modules/src") - add_subdirectory(src) -endif () - -if (ARK_MOD_DRAFT) - message(STATUS "Building modules in modules/draft") - add_subdirectory(draft) -endif () +add_subdirectory(src) +add_subdirectory(draft) diff --git a/README.md b/README.md index e603f2e..f20bbf0 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,13 @@ Main repository: **[ArkScript](https://github.com/ArkScript-lang/Ark)** ### Terminal related -* `console` to change text color when using `print` +* `console` to change text colour when using `print` ### Generic utilities -* `bitwise` for bitwise operations +* `bitwise` for bitwise operations (**deprecated**: now built-in) * `hash` to compute md5 and sha256 hashes +* `re` to use regex ### Data related diff --git a/draft/CMakeLists.txt b/draft/CMakeLists.txt index 21eb1c0..335291e 100644 --- a/draft/CMakeLists.txt +++ b/draft/CMakeLists.txt @@ -1,2 +1,9 @@ -add_subdirectory(database) -add_subdirectory(json) +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|database,)") + add_subdirectory(database) +endif () +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|json,)") + add_subdirectory(json) +endif () +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|re,)") + add_subdirectory(re) +endif () diff --git a/draft/database/CMakeLists.txt b/draft/database/CMakeLists.txt index 3feea7e..eefe489 100644 --- a/draft/database/CMakeLists.txt +++ b/draft/database/CMakeLists.txt @@ -23,4 +23,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${ark_SOURCE_DIR}/lib/${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/draft/json/CMakeLists.txt b/draft/json/CMakeLists.txt index 19ddb77..1314c9d 100644 --- a/draft/json/CMakeLists.txt +++ b/draft/json/CMakeLists.txt @@ -21,4 +21,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${PROJECT_SOURCE_DIR}/../../../${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/draft/re/CMakeLists.txt b/draft/re/CMakeLists.txt new file mode 100644 index 0000000..9b35ada --- /dev/null +++ b/draft/re/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.16) + +project(re) + +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +set(ABSL_ENABLE_INSTALL ON) +set(ABSL_PROPAGATE_CXX_STD ON) +add_subdirectory(${Modules_SOURCE_DIR}/submodules/abseil abseil EXCLUDE_FROM_ALL SYSTEM) +add_subdirectory(${Modules_SOURCE_DIR}/submodules/re2 re2 EXCLUDE_FROM_ALL SYSTEM) + +include_directories(${PROJECT_SOURCE_DIR}/include) +file(GLOB_RECURSE SOURCE_FILES + ${PROJECT_SOURCE_DIR}/src/*.cpp + ${ark_SOURCE_DIR}/thirdparties/fmt/src/format.cc) + +add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES}) + +target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${ark_SOURCE_DIR}/include) +target_link_libraries(${PROJECT_NAME} PRIVATE ArkReactor re2::re2) + +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) + +add_custom_command(TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/draft/re/README.md b/draft/re/README.md new file mode 100644 index 0000000..e69de29 diff --git a/draft/re/documentation/README.md b/draft/re/documentation/README.md new file mode 100644 index 0000000..2d49523 --- /dev/null +++ b/draft/re/documentation/README.md @@ -0,0 +1,696 @@ +--- +title: "re module" +description: "" +summary: "" +date: 2026-08-29T17:18:16+02:00 +lastmod: 2026-08-29T17:18:16+02:00 +draft: false +weight: 410 +toc: true +seo: + title: "" # custom title (optional) + description: "" # custom description (recommended) + canonical: "" # custom canonical URL (optional) + noindex: false # false (default) or true +--- + +{{< highlight_scripts >}} + +Matches are represented by the following `dict` structure: +- `match`: String, what the regex matched +- `start`: Number, start position in the source of the match +- `end`: Number, end position in the source of the match +- `groups`: List of String, the groups the regex matched. Can be empty if the regex didn't have any group + +
+Regular expression syntax (google/re2 wiki/Syntax) + +Regular expressions are a notation for describing sets of character strings. When a particular string is in the set described by a regular expression, we often say that the regular expression *matches* the string. + +The simplest regular expression is a single literal character. Except for the metacharacters like `*+?()|`, characters match themselves. To match a metacharacter, escape it with a backslash: `\+` matches a literal plus character. + +Two regular expressions can be alternated or concatenated to form a new regular expression: if e1 matches s and e2 matches t, then e1|e2 matches s or t, and e1e2 matches st. + +The metacharacters `*`, `+`, and `?` are repetition operators: e1* matches a sequence of zero or more (possibly different) strings, each of which match e1; e1+ matches one or more; e1? matches zero or one. + +The operator precedence, from weakest to strongest binding, is first alternation, then concatenation, and finally the repetition operators. Explicit parentheses can be used to force different meanings, just as in arithmetic expressions. Some examples: `ab|cd` is equivalent to `(ab)|(cd)`; `ab*` is equivalent to `a(b*)`. + +The syntax described so far is most of the traditional Unix `egrep` regular expression syntax. This subset suffices to describe all regular languages: loosely speaking, a regular language is a set of strings that can be matched in a single pass through the text using only a fixed amount of memory. Newer regular expression facilities (notably Perl and those that have copied it) have added many new operators and escape sequences, which make the regular expressions more concise, and sometimes more cryptic, but usually not more powerful. + +This page lists the regular expression syntax accepted by RE2. Note that this syntax is a subset of that accepted by PCRE, roughly speaking, and with various caveats. + +

It also lists some syntax accepted by PCRE, PERL, and VIM.

+ + + + + + + + + + + + + + +
kinds of single-character expressionsexamples
any character, possibly including newline (s=true).
character class[xyz]
negated character class[^xyz]
Perl character class (link)\d
negated Perl character class\D
ASCII character class (link)[[:alpha:]]
negated ASCII character class[[:^alpha:]]
Unicode character class (one-letter name)\pN
Unicode character class\p{Greek}
negated Unicode character class (one-letter name)\PN
negated Unicode character class\P{Greek}
+ + + + + +
Composites
xyx followed by y
x|yx or y (prefer x)
+ + + + + + + + + + + + + + + + + + + +
Repetitions
x*zero or more x, prefer more
x+one or more x, prefer more
x?zero or one x, prefer one
x{n,m}n or n+1 or ... or m x, prefer more
x{n,}n or more x, prefer more
x{n}exactly n x
x*?zero or more x, prefer fewer
x+?one or more x, prefer fewer
x??zero or one x, prefer zero
x{n,m}?n or n+1 or ... or m x, prefer fewer
x{n,}?n or more x, prefer fewer
x{n}?exactly n x
x{}(≡ x*) (NOT SUPPORTED) VIM
x{-}(≡ x*?) (NOT SUPPORTED) VIM
x{-n}(≡ x{n}?) (NOT SUPPORTED) VIM
x=(≡ x?) (NOT SUPPORTED) VIM
+ +

Implementation restriction: The counting forms x{n,m}, x{n,}, and x{n} reject forms that create a minimum or maximum repetition count above 1000. Unlimited repetitions are not subject to this restriction. + + + + + + + + + +
Possessive repetitions
x*+zero or more x, possessive (NOT SUPPORTED)
x++one or more x, possessive (NOT SUPPORTED)
x?+zero or one x, possessive (NOT SUPPORTED)
x{n,m}+n or ... or m x, possessive (NOT SUPPORTED)
x{n,}+n or more x, possessive (NOT SUPPORTED)
x{n}+exactly n x, possessive (NOT SUPPORTED)
+ + + + + + + + + + + + + + + + +
Grouping
(re)numbered capturing group (submatch)
(?P<name>re)named & numbered capturing group (submatch)
(?<name>re)named & numbered capturing group (submatch)
(?'name're)named & numbered capturing group (submatch) (NOT SUPPORTED)
(?:re)non-capturing group
(?flags)set flags within current group; non-capturing
(?flags:re)set flags during re; non-capturing
(?#text)comment (NOT SUPPORTED)
(?|x|y|z)branch numbering reset (NOT SUPPORTED)
(?>re)possessive match of re (NOT SUPPORTED)
re@>possessive match of re (NOT SUPPORTED) VIM
%(re)non-capturing group (NOT SUPPORTED) VIM
+ + + + + + + + +
Flags
icase-insensitive (default false)
mmulti-line mode: ^ and $ match begin/end line in addition to begin/end text (default false)
slet . match \n (default false)
Uungreedy: swap meaning of x* and x*?, x+ and x+?, etc (default false)
+ +

Flag syntax is xyz (set) or -xyz (clear) or xy-z (set xy, clear z). + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Empty strings
^at beginning of text or line (m=true)
$at end of text (like \z not \Z) or line (m=true)
\Aat beginning of text
\bat ASCII word boundary (\w on one side and \W, \A, or \z on the other)
\Bnot at ASCII word boundary
\gat beginning of subtext being searched (NOT SUPPORTED) PCRE
\Gat end of last match (NOT SUPPORTED) PERL
\Zat end of text, or before newline at end of text (NOT SUPPORTED)
\zat end of text
(?=re)before text matching re (NOT SUPPORTED)
(?!re)before text not matching re (NOT SUPPORTED)
(?<=re)after text matching re (NOT SUPPORTED)
(?<!re)after text not matching re (NOT SUPPORTED)
re&before text matching re (NOT SUPPORTED) VIM
re@=before text matching re (NOT SUPPORTED) VIM
re@!before text not matching re (NOT SUPPORTED) VIM
re@<=after text matching re (NOT SUPPORTED) VIM
re@<!after text not matching re (NOT SUPPORTED) VIM
\zssets start of match (= \K) (NOT SUPPORTED) VIM
\zesets end of match (NOT SUPPORTED) VIM
\%^beginning of file (NOT SUPPORTED) VIM
\%$end of file (NOT SUPPORTED) VIM
\%Von screen (NOT SUPPORTED) VIM
\%#cursor position (NOT SUPPORTED) VIM
\%'mmark m position (NOT SUPPORTED) VIM
\%23lin line 23 (NOT SUPPORTED) VIM
\%23cin column 23 (NOT SUPPORTED) VIM
\%23vin virtual column 23 (NOT SUPPORTED) VIM
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Escape sequences
\abell (≡ \007)
\fform feed (≡ \014)
\thorizontal tab (≡ \011)
\nnewline (≡ \012)
\rcarriage return (≡ \015)
\vvertical tab character (≡ \013)
\*literal *, for any punctuation character *
\123octal character code (up to three digits)
\x7Fhex character code (exactly two digits)
\x{10FFFF}hex character code
\Cmatch a single byte even in UTF-8 mode
\Q...\Eliteral text ... even if ... has punctuation
\1backreference (NOT SUPPORTED)
\bbackspace (NOT SUPPORTED) (use \010)
\cKcontrol char ^K (NOT SUPPORTED) (use \001 etc)
\eescape (NOT SUPPORTED) (use \033)
\g1backreference (NOT SUPPORTED)
\g{1}backreference (NOT SUPPORTED)
\g{+1}backreference (NOT SUPPORTED)
\g{-1}backreference (NOT SUPPORTED)
\g{name}named backreference (NOT SUPPORTED)
\g<name>subroutine call (NOT SUPPORTED)
\g'name'subroutine call (NOT SUPPORTED)
\k<name>named backreference (NOT SUPPORTED)
\k'name'named backreference (NOT SUPPORTED)
\lXlowercase X (NOT SUPPORTED)
\uxuppercase x (NOT SUPPORTED)
\L...\Elowercase text ... (NOT SUPPORTED)
\Kreset beginning of $0 (NOT SUPPORTED)
\N{name}named Unicode character (NOT SUPPORTED)
\Rline break (NOT SUPPORTED)
\U...\Eupper case text ... (NOT SUPPORTED)
\Xextended Unicode sequence (NOT SUPPORTED)
\%d123decimal character 123 (NOT SUPPORTED) VIM
\%xFFhex character FF (NOT SUPPORTED) VIM
\%o123octal character 123 (NOT SUPPORTED) VIM
\%u1234Unicode character 0x1234 (NOT SUPPORTED) VIM
\%U12345678Unicode character 0x12345678 (NOT SUPPORTED) VIM
+ + + + + + + + + +
Character class elements
xsingle character
A-Zcharacter range (inclusive)
\dPerl character class
[:foo:]ASCII character class foo
\p{Foo}Unicode character class Foo
\pFUnicode character class F (one-letter name)
+ + + + + + + + + + + +
Named character classes as character class elements
[\d]digits (≡ \d)
[^\d]not digits (≡ \D)
[\D]not digits (≡ \D)
[^\D]not not digits (≡ \d)
[[:name:]]named ASCII class inside character class (≡ [:name:])
[^[:name:]]named ASCII class inside negated character class (≡ [:^name:])
[\p{Name}]named Unicode property inside character class (≡ \p{Name})
[^\p{Name}]named Unicode property inside negated character class (≡ \P{Name})
+ + + + + + + + + + + + + + +
Perl character classes (all ASCII-only)
\ddigits (≡ [0-9])
\Dnot digits (≡ [^0-9])
\swhitespace (≡ [\t\n\f\r ])
\Snot whitespace (≡ [^\t\n\f\r ])
\wword characters (≡ [0-9A-Za-z_])
\Wnot word characters (≡ [^0-9A-Za-z_])
\hhorizontal space (NOT SUPPORTED)
\Hnot horizontal space (NOT SUPPORTED)
\vvertical space (NOT SUPPORTED)
\Vnot vertical space (NOT SUPPORTED)
+ + + + + + + + + + + + + + + + + + +
ASCII character classes
[[:alnum:]]alphanumeric (≡ [0-9A-Za-z])
[[:alpha:]]alphabetic (≡ [A-Za-z])
[[:ascii:]]ASCII (≡ [\x00-\x7F])
[[:blank:]]blank (≡ [\t ])
[[:cntrl:]]control (≡ [\x00-\x1F\x7F])
[[:digit:]]digits (≡ [0-9])
[[:graph:]]graphical (≡ [!-~][A-Za-z0-9!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])
[[:lower:]]lower case (≡ [a-z])
[[:print:]]printable (≡ [ -~][ [:graph:]])
[[:punct:]]punctuation (≡ [!-/:-@[-`{-~])
[[:space:]]whitespace (≡ [\t\n\v\f\r ])
[[:upper:]]upper case (≡ [A-Z])
[[:word:]]word characters (≡ [0-9A-Za-z_])
[[:xdigit:]]hex digit (≡ [0-9A-Fa-f])
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Unicode character class names--general category
Cother
Cccontrol
Cfformat
Cnunassigned code points (NOT SUPPORTED)
Coprivate use
Cssurrogate
Lletter
LCcased letter (NOT SUPPORTED)
L&cased letter (NOT SUPPORTED)
Lllowercase letter
Lmmodifier letter
Loother letter
Lttitlecase letter
Luuppercase letter
Mmark
Mcspacing mark
Meenclosing mark
Mnnon-spacing mark
Nnumber
Nddecimal number
Nlletter number
Noother number
Ppunctuation
Pcconnector punctuation
Pddash punctuation
Peclose punctuation
Pffinal punctuation
Piinitial punctuation
Poother punctuation
Psopen punctuation
Ssymbol
Sccurrency symbol
Skmodifier symbol
Smmath symbol
Soother symbol
Zseparator
Zlline separator
Zpparagraph separator
Zsspace separator
+ +

Unicode character class names--scripts + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Unicode character class names--scripts
Adlam
Ahom
Anatolian_Hieroglyphs
Arabic
Armenian
Avestan
Balinese
Bamum
Bassa_Vah
Batak
Bengali
Bhaiksuki
Bopomofo
Brahmi
Braille
Buginese
Buhid
Canadian_Aboriginal
Carian
Caucasian_Albanian
Chakma
Cham
Cherokee
Chorasmian
Common
Coptic
Cuneiform
Cypriot
Cypro_Minoan
Cyrillic
Deseret
Devanagari
Dives_Akuru
Dogra
Duployan
Egyptian_Hieroglyphs
Elbasan
Elymaic
Ethiopic
Georgian
Glagolitic
Gothic
Grantha
Greek
Gujarati
Gunjala_Gondi
Gurmukhi
Han
Hangul
Hanifi_Rohingya
Hanunoo
Hatran
Hebrew
Hiragana
Imperial_Aramaic
Inherited
Inscriptional_Pahlavi
Inscriptional_Parthian
Javanese
Kaithi
Kannada
Katakana
Kawi
Kayah_Li
Kharoshthi
Khitan_Small_Script
Khmer
Khojki
Khudawadi
Lao
Latin
Lepcha
Limbu
Linear_A
Linear_B
Lisu
Lycian
Lydian
Mahajani
Makasar
Malayalam
Mandaic
Manichaean
Marchen
Masaram_Gondi
Medefaidrin
Meetei_Mayek
Mende_Kikakui
Meroitic_Cursive
Meroitic_Hieroglyphs
Miao
Modi
Mongolian
Mro
Multani
Myanmar
Nabataean
Nag_Mundari
Nandinagari
New_Tai_Lue
Newa
Nko
Nushu
Nyiakeng_Puachue_Hmong
Ogham
Ol_Chiki
Old_Hungarian
Old_Italic
Old_North_Arabian
Old_Permic
Old_Persian
Old_Sogdian
Old_South_Arabian
Old_Turkic
Old_Uyghur
Oriya
Osage
Osmanya
Pahawh_Hmong
Palmyrene
Pau_Cin_Hau
Phags_Pa
Phoenician
Psalter_Pahlavi
Rejang
Runic
Samaritan
Saurashtra
Sharada
Shavian
Siddham
SignWriting
Sinhala
Sogdian
Sora_Sompeng
Soyombo
Sundanese
Syloti_Nagri
Syriac
Tagalog
Tagbanwa
Tai_Le
Tai_Tham
Tai_Viet
Takri
Tamil
Tangsa
Tangut
Telugu
Thaana
Thai
Tibetan
Tifinagh
Tirhuta
Toto
Ugaritic
Vai
Vithkuqi
Wancho
Warang_Citi
Yezidi
Yi
Zanabazar_Square
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Vim character classes
\iidentifier character (NOT SUPPORTED) VIM
\I\i except digits (NOT SUPPORTED) VIM
\kkeyword character (NOT SUPPORTED) VIM
\K\k except digits (NOT SUPPORTED) VIM
\ffile name character (NOT SUPPORTED) VIM
\F\f except digits (NOT SUPPORTED) VIM
\pprintable character (NOT SUPPORTED) VIM
\P\p except digits (NOT SUPPORTED) VIM
\swhitespace character (≡ [ \t]) (NOT SUPPORTED) VIM
\Snon-white space character (≡ [^ \t]) (NOT SUPPORTED) VIM
\ddigits (≡ [0-9]) VIM
\Dnot \d VIM
\xhex digits (≡ [0-9A-Fa-f]) (NOT SUPPORTED) VIM
\Xnot \x (NOT SUPPORTED) VIM
\ooctal digits (≡ [0-7]) (NOT SUPPORTED) VIM
\Onot \o (NOT SUPPORTED) VIM
\wword character VIM
\Wnot \w VIM
\hhead of word character (NOT SUPPORTED) VIM
\Hnot \h (NOT SUPPORTED) VIM
\aalphabetic (NOT SUPPORTED) VIM
\Anot \a (NOT SUPPORTED) VIM
\llowercase (NOT SUPPORTED) VIM
\Lnot lowercase (NOT SUPPORTED) VIM
\uuppercase (NOT SUPPORTED) VIM
\Unot uppercase (NOT SUPPORTED) VIM
\_x\x plus newline, for any x (NOT SUPPORTED) VIM
\cignore case (NOT SUPPORTED) VIM
\Cmatch case (NOT SUPPORTED) VIM
\mmagic (NOT SUPPORTED) VIM
\Mnomagic (NOT SUPPORTED) VIM
\vverymagic (NOT SUPPORTED) VIM
\Vverynomagic (NOT SUPPORTED) VIM
\Zignore differences in Unicode combining characters (NOT SUPPORTED) VIM
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Magic
(?{code})arbitrary Perl code (NOT SUPPORTED) PERL
(??{code})postponed arbitrary Perl code (NOT SUPPORTED) PERL
(?n)recursive call to regexp capturing group n (NOT SUPPORTED)
(?+n)recursive call to relative group +n (NOT SUPPORTED)
(?-n)recursive call to relative group -n (NOT SUPPORTED)
(?C)PCRE callout (NOT SUPPORTED) PCRE
(?R)recursive call to entire regexp (≡ (?0)) (NOT SUPPORTED)
(?&name)recursive call to named group (NOT SUPPORTED)
(?P=name)named backreference (NOT SUPPORTED)
(?P>name)recursive call to named group (NOT SUPPORTED)
(?(cond)true|false)conditional branch (NOT SUPPORTED)
(?(cond)true)conditional branch (NOT SUPPORTED)
(*ACCEPT)make regexps more like Prolog (NOT SUPPORTED)
(*COMMIT)(NOT SUPPORTED)
(*F)(NOT SUPPORTED)
(*FAIL)(NOT SUPPORTED)
(*MARK)(NOT SUPPORTED)
(*PRUNE)(NOT SUPPORTED)
(*SKIP)(NOT SUPPORTED)
(*THEN)(NOT SUPPORTED)
(*ANY)set newline convention (NOT SUPPORTED)
(*ANYCRLF)(NOT SUPPORTED)
(*CR)(NOT SUPPORTED)
(*CRLF)(NOT SUPPORTED)
(*LF)(NOT SUPPORTED)
(*BSR_ANYCRLF)set \R convention (NOT SUPPORTED) PCRE
(*BSR_UNICODE)(NOT SUPPORTED) PCRE
+ +
+ +## re:search + +Scan through string looking for the first location where the regular expression pattern produces a match, and return a `dict` describing the match. Return `nil` if no position in the string matches the pattern. + +**Parameters** +- `regex`: String, regular expression to use for matching +- `input`: String to match on + +**Return value** `dict` or `nil`: a dict describing the match is returned, or nil if nothing matched + +**Author** +- [@SuperFola](https://github.com/SuperFola) + +**Example** +{{< highlight_arkscript >}} +(import std.re) +(print (re:search r"\bf[a-z]*" "which foot or hand fell fastest")) +# {match: foot, start: 6, end: 10, groups: []} +{{< /highlight_arkscript >}} + +## re:match + +If zero or more characters at the beginning of string match the regular expression pattern, return a `dict` describing the match. Return `nil` if the string does not match the pattern. + +**Parameters** +- `regex`: String, regular expression to use for matching +- `input`: String to match on + +**Return value** `dict` or `nil`: a dict describing the match is returned, or nil if nothing matched + +**Author** +- [@SuperFola](https://github.com/SuperFola) + +**Example** +{{< highlight_arkscript >}} +(import std.re) +(print (re:match r"(\w+) (\w+)" "Isaac Newton, physicist")) +# {match: Isaac Newton, start: 0, end: 12, groups: ["Isaac" "Newton"]} +{{< /highlight_arkscript >}} + +## re:fullMatch + +If the whole string matches the regular expression pattern, return a `dict` describing the match. Return `nil` if the string does not match the pattern. + +**Parameters** +- `regex`: String, regular expression to use for matching +- `input`: String to match on + +**Return value** `dict` or `nil`: a dict describing the match is returned, or nil if nothing matched + +**Author** +- [@SuperFola](https://github.com/SuperFola) + +**Example** +{{< highlight_arkscript >}} +(import std.re) +(print (re:fullMatch r"(?P\w+) (?P\w+)" "Malcolm Reynolds")) +# {match: Malcolm Reynolds, start: 0, end: 16, groups: ["Malcolm" "Reynolds"]} +{{< /highlight_arkscript >}} + +## re:findAll + +Return all non-overlapping matches of pattern in string, as a list of strings or tuples. The string is scanned left-to-right, and matches are returned in the order found. + +**Parameters** +- `regex`: String, regular expression to use for matching +- `input`: String to match on + +**Return value** `dict` or `nil`: a dict describing the match is returned, or nil if nothing matched + +**Author** +- [@SuperFola](https://github.com/SuperFola) + +**Example** +{{< highlight_arkscript >}} +(import std.re) +(print (re:findAll pattern text)) +# [ +# {match: foot, start: 6, end: 10, groups: []} +# {match: fell, start: 19, end: 23, groups: []} +# {match: fastest, start: 24, end: 31, groups: []} ] + +(print (re:findAll r"(\w+)=(\d+)" "set width=20 and height=10")) +# [ +# {match: width=20, start: 4, end: 12, groups: ["width" "20"]} +# {match: height=10, start: 17, end: 26, groups: ["height" "10"]} ] +{{< /highlight_arkscript >}} + +## re:sub + +Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. Backreferences, such as `\6`, are replaced with the substring matched by group 6 in the pattern. For example: + +{{< highlight_arkscript >}} +(print + (re:sub + r"def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):" + r"static PyObject* py_\1(void) {" + "def myfunc():")) +# static PyObject* py_myfunc(void) { +{{< /highlight_arkscript >}} + +**Parameters** +- `regex`: String, regular expression to use for matching +- `replacement`: String to use for the replacements +- `input`: String + +**Return value** `String` + +## re:split + +Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If `max_split` is nonzero, at most `max_split` splits occur, and the remainder of the string is returned as the final element of the list. + +**Parameters** +- `regex`: String, regular expression to use for splitting +- `input`: String to split +- `max_split`: Optional Number + +**Return value** `List`: list of Strings + +**Author** +- [@SuperFola](https://github.com/SuperFola) + +**Example** +{{< highlight_arkscript >}} +(import std.re) +(print (re:split r"\W+" "Words, def, ijkl.")) +# ["Words" "def" "ijkl"] +(print (re:split r"(\W+)" "Words, def, ijkl.")) +# ["Words" ", " "def" ", " "ijkl" "."] +(print (re:split r"\W+" "Words, def, ijkl." 1)) +# ["Words" "def, ijkl."] +{{< /highlight_arkscript >}} + +## re:escape + +Escape special characters in `text`. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. + +**Parameters** +- `text`: String to escape + +**Return value** `String` + +**Author** +- [@SuperFola](https://github.com/SuperFola) + +**Example** +{{< highlight_arkscript >}} +(import std.re) +(print (re:escape "1.5-2.0?")) +# "1\\.5\\-2\\.0\\?" +(print (re:escape "abcdefgh")) +# "abcdefgh" +(print (re:escape "")) +# """ +(print (re:escape "[")) +# "\\[" +{{< /highlight_arkscript >}} diff --git a/draft/re/include/module.hpp b/draft/re/include/module.hpp new file mode 100644 index 0000000..85f482e --- /dev/null +++ b/draft/re/include/module.hpp @@ -0,0 +1,19 @@ +#ifndef RE_REGEX_HPP +#define RE_REGEX_HPP + +#include + +namespace Regex +{ + using namespace Ark; + + Value search(std::vector& args, VM* vm); + Value match(std::vector& args, VM* vm); + Value full_match(std::vector& args, VM* vm); + Value find_all(std::vector& args, VM* vm); + Value sub(std::vector& args, VM* vm); + Value split(std::vector& args, VM* vm); + Value escape(std::vector& args, VM* vm); +} + +#endif // RE_REGEX_HPP diff --git a/draft/re/include/utils.hpp b/draft/re/include/utils.hpp new file mode 100644 index 0000000..97957b0 --- /dev/null +++ b/draft/re/include/utils.hpp @@ -0,0 +1,63 @@ +#ifndef RE_UTILS_HPP +#define RE_UTILS_HPP + +#include +#include + +#include +#include + +namespace Regex +{ + struct Span + { + long start; + std::size_t size; + bool empty; + }; + + struct Match + { + std::vector spans; + + [[nodiscard]] inline long start() const + { + return spans.front().start; + } + + [[nodiscard]] inline std::size_t end() const + { + return spans.front().start + spans.front().size; + } + + [[nodiscard]] inline std::size_t size() const + { + return spans.front().size; + } + }; + + struct MatchImpl_t + { + std::string text; + std::vector matches; + bool empty; + bool expect_many; + }; + + inline Span asSpan(const absl::string_view& sv, const std::string& text) + { + return Span { .start = static_cast(sv.data() - text.data()), .size = sv.size(), .empty = false }; + } + + inline Span getFirstSpan(const std::vector& groups, const std::string& text) + { + if (const auto& it = groups.front(); it.data() == nullptr) + return Span { .start = 0, .size = 0, .empty = true }; + else + return asSpan(it, text); + } + + MatchImpl_t matchImpl(RE2::Anchor anchor, const std::string& regex, const std::string& text, bool stop_at_first); +} + +#endif // RE_UTILS_HPP diff --git a/draft/re/src/impl.cpp b/draft/re/src/impl.cpp new file mode 100644 index 0000000..fb2e1f9 --- /dev/null +++ b/draft/re/src/impl.cpp @@ -0,0 +1,184 @@ +#include +#include +#include + +#include +#include + +#include + +namespace Regex +{ + using namespace Ark; + using namespace Ark::literals; + using namespace re2; + + Value toValue(const Match& match, const std::string& text) + { + internal::Dict dict; + const auto& span = match.spans.front(); + if (!span.empty) + dict.set(Value("match"), Value(text.substr(span.start, span.size))); + else + dict.set(Value("match"), Nil); + dict.set(Value("start"), Value(static_cast(span.start))); + dict.set(Value("end"), Value(static_cast(span.size + span.start))); + + Value groups(ValueType::List); + for (const Span& s : match.spans | std::ranges::views::drop(1)) + groups.push_back(Value(text.substr(s.start, s.size))); + dict.set(Value("groups"), groups); + + return Value(std::move(dict)); + } + + Value toValue(const MatchImpl_t& res) + { + if (res.empty) + return Nil; + + if (res.expect_many) + { + Value output(ValueType::List); + for (const Match& match : res.matches) + { + // todo: create a dict with all the groups + output.push_back(toValue(match, res.text)); + } + return output; + } + + const Match& match = res.matches.front(); + return toValue(match, res.text); + } + + Value search(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String, ValueType::String)) + throw types::TypeCheckingError( + "re:search", + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, + args); + const std::string& regex = args[0].string(); + const std::string& input = args[1].string(); + return toValue(matchImpl(RE2::UNANCHORED, regex, input, true)); + } + + Value match(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String, ValueType::String)) + throw types::TypeCheckingError( + "re:match", + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, + args); + const std::string& regex = args[0].string(); + const std::string& input = args[1].string(); + return toValue(matchImpl(RE2::ANCHOR_START, regex, input, true)); + } + + Value full_match(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String, ValueType::String)) + throw types::TypeCheckingError( + "re:fullMatch", + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, + args); + const std::string& regex = args[0].string(); + const std::string& input = args[1].string(); + return toValue(matchImpl(RE2::ANCHOR_BOTH, regex, input, true)); + } + + Value find_all(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String, ValueType::String)) + throw types::TypeCheckingError( + "re:findAll", + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, + args); + const std::string& regex = args[0].string(); + const std::string& input = args[1].string(); + return toValue(matchImpl(RE2::UNANCHORED, regex, input, false)); + } + + Value sub(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String, ValueType::String, ValueType::String)) + throw types::TypeCheckingError( + "re:sub", + { { types::Contract { { types::Typedef("regex", ValueType::String), + types::Typedef("replacement", ValueType::String), + types::Typedef("input", ValueType::String) } } } }, + args); + const std::string& regex = args[0].string(); + const std::string& replacement = args[1].string(); + std::string input = args[2].string(); + + // todo: control how many times we want to replace? + RE2::GlobalReplace(&input, regex, replacement); + return Value(input); + } + + Value split(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String, ValueType::String) && !types::check(args, ValueType::String, ValueType::String, ValueType::Number)) + throw types::TypeCheckingError( + "re:split", + { { types::Contract { + { types::Typedef("regex", ValueType::String), + types::Typedef("input", ValueType::String) } }, + types::Contract { { types::Typedef("regex", ValueType::String), + types::Typedef("input", ValueType::String), + types::Typedef("max_split", ValueType::Number) } } } }, + args); + const std::string& regex = args[0].string(); + const std::string& input = args[1].string(); + long max_split = 0; + if (args.size() == 3) + max_split = static_cast(args[2].number()); + + Value output(ValueType::List); + if (max_split < 0) + { + output.push_back(args[1]); + return output; + } + + long num_split = 0; + std::size_t end = 0; + const MatchImpl_t res = matchImpl(RE2::UNANCHORED, regex, input, false); + if (res.empty) + return Nil; + + for (const Match& match : res.matches) + { + output.push_back(Value(res.text.substr(end, match.start() - end))); + for (std::size_t i = 1; i < match.spans.size(); ++i) + { + if (!match.spans[i].empty) + output.push_back(Value(res.text.substr(match.spans[i].start, match.spans[i].size))); + else + output.push_back(Nil); + } + + end = match.end(); + num_split++; + + if (num_split == max_split) + break; + } + if (end < res.text.size()) + output.push_back(Value(res.text.substr(end))); + + return output; + } + + Value escape(std::vector& args, VM* vm) + { + if (!types::check(args, ValueType::String)) + throw types::TypeCheckingError( + "re:escape", + { { types::Contract { { types::Typedef("text", ValueType::String) } } } }, + args); + return Value(RE2::QuoteMeta(args[0].string())); + } +} diff --git a/draft/re/src/main.cpp b/draft/re/src/main.cpp new file mode 100644 index 0000000..62501e2 --- /dev/null +++ b/draft/re/src/main.cpp @@ -0,0 +1,17 @@ +#include + +ARK_API Ark::mapping* getFunctionsMapping() +{ + static Ark::mapping map[] = { + { "re:search", Regex::search }, + { "re:match", Regex::match }, + { "re:fullMatch", Regex::full_match }, + { "re:findAll", Regex::find_all }, + { "re:sub", Regex::sub }, + { "re:split", Regex::split }, + { "re:escape", Regex::escape }, + { nullptr, nullptr } + }; + + return map; +} diff --git a/draft/re/src/utils.cpp b/draft/re/src/utils.cpp new file mode 100644 index 0000000..ab6dec4 --- /dev/null +++ b/draft/re/src/utils.cpp @@ -0,0 +1,82 @@ +#include + +#include + +namespace Regex +{ + MatchImpl_t matchImpl(const RE2::Anchor anchor, const std::string& regex, const std::string& text, const bool stop_at_first) + { + constexpr std::size_t start_pos = 0; + const std::size_t end_pos = text.size(); + + auto options = RE2::Options(RE2::Quiet); + options.set_case_sensitive(true); + options.set_posix_syntax(false); + options.set_perl_classes(true); + + RE2 pattern(regex, options); + if (!pattern.ok()) + throw std::runtime_error(fmt::format("re: error while compiling pattern '{}', {}", regex, pattern.error())); + + const int num_groups = 1 + pattern.NumberOfCapturingGroups(); // +1 to have $0 as well + std::vector groups; + groups.resize(num_groups); + + if (!pattern.Match(text, start_pos, end_pos, anchor, groups.data(), num_groups)) + return { .empty = true }; + + MatchImpl_t output { + .text = text, + .empty = false, + .expect_many = !stop_at_first + }; + if (stop_at_first) + { + Match match; + for (const auto& it : groups) + { + if (it.data() == nullptr) + match.spans.emplace_back(Span { 0, 0, true }); + else + match.spans.emplace_back(asSpan(it, text)); + } + output.matches.emplace_back(match); + } + else + { + std::size_t pos = start_pos; + Span span = getFirstSpan(groups, text); + + while (true) + { + if (span.empty) + break; + + std::vector spans; + for (const auto& it : groups) + { + if (it.data() == nullptr) + spans.emplace_back(Span { 0, 0, true }); + else + spans.emplace_back(asSpan(it, text)); + } + output.matches.emplace_back(Match { .spans = spans }); + + if (pos == end_pos) + break; + if (pos == span.start + span.size) + // we matched the empty string at `pos` and would be stuck, so in order to make progress, + // increment the offset + pos++; + else + pos = span.start + span.size; + + // todo: should we reset `groups` each time? + if (!pattern.Match(text, pos, end_pos, anchor, groups.data(), num_groups)) + break; + span = getFirstSpan(groups, text); + } + } + return output; + } +} diff --git a/draft/re/tests/main.ark b/draft/re/tests/main.ark new file mode 100644 index 0000000..e69de29 diff --git a/src/hash/tests/run b/draft/re/tests/run similarity index 100% rename from src/hash/tests/run rename to draft/re/tests/run diff --git a/shell/createmodules/Template_CMakeLists.txt b/shell/createmodules/Template_CMakeLists.txt index b181365..c0068fb 100644 --- a/shell/createmodules/Template_CMakeLists.txt +++ b/shell/createmodules/Template_CMakeLists.txt @@ -19,4 +19,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${ark_SOURCE_DIR}/lib/${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 442be8b..41d1d68 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,12 @@ -add_subdirectory(bitwise) -add_subdirectory(console) -add_subdirectory(hash) -add_subdirectory(http) +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|bitwise,)") + add_subdirectory(bitwise) +endif () +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|console,)") + add_subdirectory(console) +endif () +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|(ark_)?hash,)") + add_subdirectory(ark_hash) +endif () +if (${ARK_REQUESTED_MODULES} MATCHES "(^\\*$|http,)") + add_subdirectory(http) +endif () diff --git a/src/hash/CMakeLists.txt b/src/ark_hash/CMakeLists.txt similarity index 85% rename from src/hash/CMakeLists.txt rename to src/ark_hash/CMakeLists.txt index aa17173..dc321d5 100644 --- a/src/hash/CMakeLists.txt +++ b/src/ark_hash/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(hash) +project(ark_hash) set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -21,4 +21,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${ark_SOURCE_DIR}/lib/${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/src/hash/documentation/README.md b/src/ark_hash/documentation/README.md similarity index 100% rename from src/hash/documentation/README.md rename to src/ark_hash/documentation/README.md diff --git a/src/hash/include/md5.hpp b/src/ark_hash/include/md5.hpp similarity index 100% rename from src/hash/include/md5.hpp rename to src/ark_hash/include/md5.hpp diff --git a/src/hash/src/main.cpp b/src/ark_hash/src/main.cpp similarity index 100% rename from src/hash/src/main.cpp rename to src/ark_hash/src/main.cpp diff --git a/src/hash/src/md5.cpp b/src/ark_hash/src/md5.cpp similarity index 100% rename from src/hash/src/md5.cpp rename to src/ark_hash/src/md5.cpp diff --git a/src/hash/tests/main.ark b/src/ark_hash/tests/main.ark similarity index 100% rename from src/hash/tests/main.ark rename to src/ark_hash/tests/main.ark diff --git a/src/ark_hash/tests/run b/src/ark_hash/tests/run new file mode 100644 index 0000000..7f7c4f5 --- /dev/null +++ b/src/ark_hash/tests/run @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +ARKSCRIPT=$1 +ARKLIB=$2 + +$ARKSCRIPT --lib $ARKLIB main.ark diff --git a/src/bitwise/CMakeLists.txt b/src/bitwise/CMakeLists.txt index 82d8023..41488e8 100644 --- a/src/bitwise/CMakeLists.txt +++ b/src/bitwise/CMakeLists.txt @@ -19,4 +19,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${ark_SOURCE_DIR}/lib/${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/src/console/CMakeLists.txt b/src/console/CMakeLists.txt index 283a784..167dd58 100644 --- a/src/console/CMakeLists.txt +++ b/src/console/CMakeLists.txt @@ -20,4 +20,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${ark_SOURCE_DIR}/lib/${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/src/http/CMakeLists.txt b/src/http/CMakeLists.txt index cf9f47d..3eb35a0 100644 --- a/src/http/CMakeLists.txt +++ b/src/http/CMakeLists.txt @@ -25,4 +25,4 @@ target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - "$" ${ark_SOURCE_DIR}/lib/${PROJECT_NAME}.arkm) + "$" ${ark_SOURCE_DIR}/lib/std/${PROJECT_NAME}.arkm) diff --git a/submodules/abseil b/submodules/abseil new file mode 160000 index 0000000..fe690e6 --- /dev/null +++ b/submodules/abseil @@ -0,0 +1 @@ +Subproject commit fe690e639469d2a3c86bc74dd2456371288d1f28 diff --git a/submodules/re2 b/submodules/re2 new file mode 160000 index 0000000..972a15c --- /dev/null +++ b/submodules/re2 @@ -0,0 +1 @@ +Subproject commit 972a15cedd008d846f1a39b2e88ce48d7f166cbd