Skip to content

Commit

Permalink
adhere to indent styling of two spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
leonfuss committed Sep 17, 2024
1 parent 05353a4 commit dd59c6d
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions libraries/js/text/regex.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def regex(pattern: String): Regex = regex(pattern, "g")
* If not present, the indices of capture groups will always be `None`.
*/
def regex(pattern: String, flags: String): Regex = {
extRegex(pattern, flags)
extRegex(pattern, flags)
}

extern pure def extRegex(str: String): Regex = js "new RegExp(${str})"
Expand Down Expand Up @@ -93,30 +93,30 @@ extern pure def extRegex(str: String, flags: String): Regex = js "new RegExp(${s
* val rx = regex("(\\w+)\\s(\\d+)", "g")
* val text = "Hello 123 World 456"
* while (true) {
* exec(rx, text) match = {
* Some(m) =>
* println("content: " ++ m.content ++ "range: " ++ m.start.show() ++ ", " ++ m.end.show()))
* None() => break
* exec(rx, text) match = {
* Some(m) =>
* println("content: " ++ m.content ++ "range: " ++ m.start.show() ++ ", " ++ m.end.show()))
* None() => break
* }
* ```
*/
def exec(reg: Regex, str: String): Option[Match] = {
def toGroup(g: GroupMatch): Group = Group(
content(g),
zip(undefinedToOption(start(g)), undefinedToOption(end(g))) { (s, e) => Range(s, e)}
)

undefinedToOption(reg.unsafeExec(str)) match {
case None() => None()
case Some(v) =>
val fullMatch = fullMatch(v)
Some(Match(
fullMatch.content,
fullMatch.start,
fullMatch.end,
v.subGroups.toList().map{toGroup}
))
}
def toGroup(g: GroupMatch): Group = Group(
content(g),
zip(undefinedToOption(start(g)), undefinedToOption(end(g))) { (s, e) => Range(s, e)}
)

undefinedToOption(reg.unsafeExec(str)) match {
case None() => None()
case Some(v) =>
val fullMatch = fullMatch(v)
Some(Match(
fullMatch.content,
fullMatch.start,
fullMatch.end,
v.subGroups.toList().map{toGroup}
))
}
}

// internal representation
Expand All @@ -138,20 +138,20 @@ function regex$exec(reg, str) {
var groups = [];
for (var i = 1; i < match.length; i++) {
if (match[i] !== undefined) {
if (match.indices !== undefined && match.indices[i] !== undefined) {
groups.push({
content: match[i],
start: match.indices[i][0],
end: match.indices[i][1]
});
} else {
groups.push({
content: match[i],
});
}
if (match.indices !== undefined && match.indices[i] !== undefined) {
groups.push({
content: match[i],
start: match.indices[i][0],
end: match.indices[i][1]
});
} else {
groups.push({
content: match[i],
});
}
}else {
// Push an empty string if an intermediate group is undefined
groups.push({content: ""});}
// Push an empty string if an intermediate group is undefined
groups.push({content: ""});}
}

return { fullMatch: fullMatch, groups: groups };
Expand All @@ -166,21 +166,21 @@ extern io def unsafeExec(reg: Regex, str: String): RegexMatch =

// Debug show
def debug(m: Match): String = {
val start = m.start.show()
val end = m.end.show()
val groups = m.groups.map{it => it.debug()}.join("\n")
val start = m.start.show()
val end = m.end.show()
val groups = m.groups.map{it => it.debug()}.join("\n")

"matched: " ++ m.content ++
"\nfrom: (" ++ start ++ ", " ++ end ++ ")" ++
"\nGroups: \n" ++ groups
"matched: " ++ m.content ++
"\nfrom: (" ++ start ++ ", " ++ end ++ ")" ++
"\nGroups: \n" ++ groups
}

def debug(g: Group): String = {
val range = g.index.map{ it => it.debug() }.getOrElse{"None"}
val range = g.index.map{ it => it.debug() }.getOrElse{"None"}

g.content ++ "\nfrom: " ++ range
g.content ++ "\nfrom: " ++ range
}

def debug(r: Range): String = {
"(" ++ r.start.show() ++ ", " ++ r.end.show() ++ ")"
"(" ++ r.start.show() ++ ", " ++ r.end.show() ++ ")"
}

0 comments on commit dd59c6d

Please sign in to comment.