diff --git a/lib/js/src/Parser/Parser.bs.js b/lib/js/src/Parser/Parser.bs.js index f82f778b..60d5edb8 100644 --- a/lib/js/src/Parser/Parser.bs.js +++ b/lib/js/src/Parser/Parser.bs.js @@ -1,21 +1,17 @@ // Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; -var Js_array = require("rescript/lib/js/js_array.js"); -var Js_string = require("rescript/lib/js/js_string.js"); var Nodepath = require("node:path"); -var Belt_Array = require("rescript/lib/js/belt_Array.js"); -var Belt_Option = require("rescript/lib/js/belt_Option.js"); +var Core__Array = require("@rescript/core/lib/js/src/Core__Array.bs.js"); +var Core__Option = require("@rescript/core/lib/js/src/Core__Option.bs.js"); function splitToLines(s) { - return Belt_Array.keepMap(Belt_Array.map((function (__x) { - return Js_string.splitByRe(/\r\n|\n/, __x); - })(s), (function (x) { - if (x !== undefined && x !== "") { - return x; - } - - })), (function (x) { + return Core__Array.filterMap(s.split(/\r\n|\n/).map(function (x) { + if (x !== undefined && x !== "") { + return x; + } + + }), (function (x) { return x; })); } @@ -70,7 +66,7 @@ function make(initialContinuation, callback) { } function feed(self, input) { - var $$continue = Belt_Option.getWithDefault(self.continuation.contents, self.initialContinuation); + var $$continue = Core__Option.getOr(self.continuation.contents, self.initialContinuation); var err = $$continue(input); switch (err.TAG) { case "Error" : @@ -133,15 +129,15 @@ function toString$1(x) { if (x.TAG === "A") { return "\"" + (x._0 + "\""); } else { - return "[" + (Js_array.joinWith(", ", Belt_Array.map(x._0, toString$1)) + "]"); + return "[" + (x._0.map(toString$1).join(", ") + "]"); } } function preprocess(string) { - if (Js_string.substring(0, 13, string) === "cannot read: ") { + if (string.substring(0, 13) === "cannot read: ") { return { TAG: "Error", - _0: Js_string.sliceToEnd(12, string), + _0: string.slice(12), [Symbol.for("name")]: "Error" }; } else { @@ -157,7 +153,7 @@ function flatten(x) { if (x.TAG === "A") { return [x._0]; } else { - return Belt_Array.concatMany(Belt_Array.map(x._0, flatten)); + return x._0.map(flatten).flat(); } } @@ -169,7 +165,7 @@ function parseWithContinuation(string) { var stack = state.stack; var pushToTheTop = function (elem) { var index = stack.length - 1 | 0; - var expr = Belt_Array.get(stack, index); + var expr = stack[index]; if (expr === undefined) { return ; } @@ -185,11 +181,11 @@ function parseWithContinuation(string) { }; return ; } - Js_array.push(elem, xs._0); + xs._0.push(elem); }; var totalLength = string.length; for(var i = 0; i < totalLength; ++i){ - var $$char = Js_string.charAt(i, string); + var $$char = string.charAt(i); if (escaped.contents) { if ($$char === "n") { word.contents = word.contents + "\\"; @@ -198,13 +194,13 @@ function parseWithContinuation(string) { escaped.contents = false; } else if (!($$char === "\'" && !in_str.contents)) { if ($$char === "(" && !in_str.contents) { - Js_array.push({ + stack.push({ contents: { TAG: "L", _0: [], [Symbol.for("name")]: "L" } - }, stack); + }); } else if ($$char === ")" && !in_str.contents) { if (word.contents !== "") { pushToTheTop({ @@ -259,7 +255,7 @@ function parseWithContinuation(string) { [Symbol.for("name")]: "Continue" }; } - var v = Belt_Array.get(stack, 0); + var v = stack[0]; if (v === undefined) { return { TAG: "Error", @@ -281,7 +277,7 @@ function parseWithContinuation(string) { [Symbol.for("name")]: "Error" }; } - var w = Belt_Array.get(xs._0, 0); + var w = xs._0[0]; if (w !== undefined) { return { TAG: "Done", @@ -340,31 +336,31 @@ function parse(input) { var continuation = { contents: undefined }; - Belt_Array.forEach(splitToLines(input), (function (line) { - var $$continue = Belt_Option.getWithDefault(continuation.contents, parseWithContinuation); - var err = $$continue(line); - switch (err.TAG) { - case "Error" : - Js_array.push({ - TAG: "Error", - _0: err._0, - [Symbol.for("name")]: "Error" - }, resultAccum.contents); - return ; - case "Continue" : - continuation.contents = err._0; - return ; - case "Done" : - Js_array.push({ - TAG: "Ok", - _0: err._0, - [Symbol.for("name")]: "Ok" - }, resultAccum.contents); - continuation.contents = undefined; - return ; - - } - })); + splitToLines(input).forEach(function (line) { + var $$continue = Core__Option.getOr(continuation.contents, parseWithContinuation); + var err = $$continue(line); + switch (err.TAG) { + case "Error" : + resultAccum.contents.push({ + TAG: "Error", + _0: err._0, + [Symbol.for("name")]: "Error" + }); + return ; + case "Continue" : + continuation.contents = err._0; + return ; + case "Done" : + resultAccum.contents.push({ + TAG: "Ok", + _0: err._0, + [Symbol.for("name")]: "Ok" + }); + continuation.contents = undefined; + return ; + + } + }); return resultAccum.contents; } @@ -394,13 +390,13 @@ var $$Error = { }; function filepath(s) { - var removedBidi = Js_string.charCodeAt(0, s) === 8234.0 ? Js_string.sliceToEnd(1, s) : s; + var removedBidi = s.charCodeAt(0) === 8234.0 ? s.slice(1) : s; var normalized = Nodepath.normalize(removedBidi); - return Js_string.replaceByRe(/\\/g, "/", normalized); + return normalized.replace(/\\/g, "/"); } function $$escape(s) { - return Js_string.replaceByRe(/\n/g, "\\n", Js_string.replaceByRe(/\r\n/g, "\\r\\n", Js_string.replaceByRe(/\"/g, "\\\"", Js_string.replaceByRe(/\\/g, "\\\\", s)))).trim(); + return s.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"").replace(/\r\n/g, "\\r\\n").replace(/\n/g, "\\n"); } function unescapeEOL(s) { diff --git a/lib/js/src/Parser/SourceFile.bs.js b/lib/js/src/Parser/SourceFile.bs.js index efccbf45..ec37e23d 100644 --- a/lib/js/src/Parser/SourceFile.bs.js +++ b/lib/js/src/Parser/SourceFile.bs.js @@ -1,10 +1,9 @@ // Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; -var Js_string = require("rescript/lib/js/js_string.js"); -var Belt_Array = require("rescript/lib/js/belt_Array.js"); -var Belt_Option = require("rescript/lib/js/belt_Option.js"); var Caml_option = require("rescript/lib/js/caml_option.js"); +var Core__Array = require("@rescript/core/lib/js/src/Core__Array.bs.js"); +var Core__Option = require("@rescript/core/lib/js/src/Core__Option.bs.js"); var Parser$AgdaModeVscode = require("./Parser.bs.js"); function parse(filepath) { @@ -59,49 +58,47 @@ function lex(regex, source, target, tokens) { var cursor = { contents: token.range[0] }; - return Belt_Array.map(Belt_Array.keepMap((function (__x) { - return Js_string.splitByRe(regex, __x); - })(token.content), (function (x) { - return x; - })), (function (content) { - var kind = regex.test(content) ? target : source; - var cursorOld = cursor.contents; - cursor.contents = cursor.contents + content.length | 0; - return { - content: content, - range: [ - cursorOld, - cursor.contents - ], - kind: kind - }; - })); + return Core__Array.filterMap(token.content.split(regex), (function (x) { + return x; + })).map(function (content) { + var kind = regex.test(content) ? target : source; + var cursorOld = cursor.contents; + cursor.contents = cursor.contents + content.length | 0; + return { + content: content, + range: [ + cursorOld, + cursor.contents + ], + kind: kind + }; + }); }; - return Belt_Array.concatMany(Belt_Array.map(tokens, f)); + return tokens.map(f).flat(); } function map(f, self) { var delta = { contents: 0 }; - return Belt_Array.map(self, (function (token) { - var match = f(token); - var content = match.content; - var match$1 = token.range; - var lengthDiff = content.length - token.content.length | 0; - var result_range = [ - match$1[0] + delta.contents | 0, - (match$1[1] + delta.contents | 0) + lengthDiff | 0 - ]; - var result_kind = match.kind; - var result = { - content: content, - range: result_range, - kind: result_kind - }; - delta.contents = delta.contents + lengthDiff | 0; - return result; - })); + return self.map(function (token) { + var match = f(token); + var content = match.content; + var match$1 = token.range; + var lengthDiff = content.length - token.content.length | 0; + var result_range = [ + match$1[0] + delta.contents | 0, + (match$1[1] + delta.contents | 0) + lengthDiff | 0 + ]; + var result_kind = match.kind; + var result = { + content: content, + range: result_range, + kind: result_kind + }; + delta.contents = delta.contents + lengthDiff | 0; + return result; + }); } function mapOnly(kind, f, self) { @@ -164,27 +161,27 @@ function toTokens(raw) { var cursor = { contents: 0 }; - return Belt_Option.mapWithDefault(Js_string.match_(/(.*(?:\r\n|[\n\v\f\r\x85\u2028\u2029])?)/g, raw), [], (function (lines) { - return Belt_Array.map(Belt_Array.keep(Belt_Array.map(lines, (function (x) { - if (x !== undefined) { - return x; - } else { - return ""; - } - })), (function (s) { - return s !== ""; - })), (function (line) { - var cursorOld = cursor.contents; - cursor.contents = cursor.contents + line.length | 0; - return { - content: Js_string.substring(cursorOld, cursor.contents, raw), - range: [ - cursorOld, - cursor.contents - ], - kind: "Literate" - }; - })); + return Core__Option.mapOr(Caml_option.nullable_to_opt(raw.match(/(.*(?:\r\n|[\n\v\f\r\x85\u2028\u2029])?)/g)), [], (function (lines) { + return lines.map(function (x) { + if (x !== undefined) { + return x; + } else { + return ""; + } + }).filter(function (s) { + return s !== ""; + }).map(function (line) { + var cursorOld = cursor.contents; + cursor.contents = cursor.contents + line.length | 0; + return { + content: raw.substring(cursorOld, cursor.contents), + range: [ + cursorOld, + cursor.contents + ], + kind: "Literate" + }; + }); })); } @@ -195,22 +192,22 @@ function markWithRules(begin_, end_, raw) { var current = { contents: false }; - return Belt_Array.map(toTokens(raw), (function (token) { - var content = token.content; - previous.contents = current.contents; - if (begin_.test(content) && !current.contents) { - current.contents = true; - } else if (end_.test(content) && current.contents) { - current.contents = false; - } - var insideAgda = previous.contents && current.contents; - var kind = insideAgda ? "AgdaRaw" : "Literate"; - return { - content: content, - range: token.range, - kind: kind - }; - })); + return toTokens(raw).map(function (token) { + var content = token.content; + previous.contents = current.contents; + if (begin_.test(content) && !current.contents) { + current.contents = true; + } else if (end_.test(content) && current.contents) { + current.contents = false; + } + var insideAgda = previous.contents && current.contents; + var kind = insideAgda ? "AgdaRaw" : "Literate"; + return { + content: content, + range: token.range, + kind: kind + }; + }); } function markMarkdown(extra) { @@ -281,24 +278,16 @@ function parse$1(indices, filepath, raw) { }; }; var adjustGoalBracket = function (token) { - var idx = Belt_Array.get(indices, i.contents); + var idx = indices[i.contents]; var goalIndex = idx !== undefined ? String(idx) : "*"; var requiredSpaces = goalIndex.length; - var content = Belt_Option.getWithDefault(Belt_Option.flatMap(Caml_option.null_to_opt(goalBracketContent.exec(token.content)), (function (result) { - return Belt_Option.flatMap(Belt_Option.map(Belt_Array.get(result, 1), (function (prim) { - if (prim == null) { - return ; - } else { - return Caml_option.some(prim); - } - })), (function (x) { + var content = Core__Option.getOr(Core__Option.flatMap(Caml_option.nullable_to_opt(goalBracketContent.exec(token.content)), (function (result) { + return Core__Option.flatMap(result[1], (function (x) { return x; })); })), ""); - var actualSpaces = Belt_Option.getWithDefault(Belt_Option.flatMap((function (__x) { - return Js_string.match_(/\s*$/, __x); - })(content), (function (matches) { - var match = Belt_Array.get(matches, 0); + var actualSpaces = Core__Option.getOr(Core__Option.flatMap(Caml_option.nullable_to_opt(content.match(/\s*$/)), (function (matches) { + var match = matches[0]; if (match === undefined) { return ; } @@ -310,8 +299,8 @@ function parse$1(indices, filepath, raw) { })), 0); var newContent; if (actualSpaces < requiredSpaces) { - var padding = Js_string.repeat(requiredSpaces - actualSpaces | 0, ""); - newContent = Js_string.replaceByRe(/\{!.*!\}/, "{!" + content + padding + "!}", token.content); + var padding = " ".repeat(requiredSpaces - actualSpaces | 0); + newContent = token.content.replace(/\{!.*!\}/, "{!" + content + padding + "!}"); } else { newContent = token.content; } @@ -326,30 +315,30 @@ function parse$1(indices, filepath, raw) { }; }; var modified = mapOnly("GoalBracket", adjustGoalBracket, mapOnly("GoalQM", questionMark2GoalBracket, original)); - var originalHoles = Belt_Array.keep(original, isHole); - var modifiedHoles = Belt_Array.keep(modified, isHole); - return Belt_Array.keepMap(Belt_Array.mapWithIndex(originalHoles, (function (idx, token) { - var match = Belt_Array.get(modifiedHoles, idx); - var match$1 = Belt_Array.get(indices, idx); - if (match === undefined) { - return ; - } - if (match$1 === undefined) { - return ; - } - var match$2 = match.range; - var start = match$2[0]; - return { - index: match$1, - modifiedInterval: match.range, - originalInterval: [ - start, - start + token.content.length | 0 - ], - content: match.content, - changed: token.content !== match.content - }; - })), (function (x) { + var originalHoles = original.filter(isHole); + var modifiedHoles = modified.filter(isHole); + return Core__Array.filterMap(originalHoles.map(function (token, idx) { + var match = modifiedHoles[idx]; + var match$1 = indices[idx]; + if (match === undefined) { + return ; + } + if (match$1 === undefined) { + return ; + } + var match$2 = match.range; + var start = match$2[0]; + return { + index: match$1, + modifiedInterval: match.range, + originalInterval: [ + start, + start + token.content.length | 0 + ], + content: match.content, + changed: token.content !== match.content + }; + }), (function (x) { return x; })); } diff --git a/src/Parser/Parser.res b/src/Parser/Parser.res index 65b2d6a0..d1055eb9 100644 --- a/src/Parser/Parser.res +++ b/src/Parser/Parser.res @@ -1,9 +1,7 @@ -open Belt - let splitToLines = s => // RegEx updated to v10.1.4 s - ->(Js.String.splitByRe(%re("/\r\n|\n/"), _)) + ->String.splitByRegExp(%re("/\r\n|\n/")) ->Array.map(x => switch x { | None => None @@ -11,7 +9,7 @@ let splitToLines = s => | Some(chunk) => Some(chunk) } ) - ->Array.keepMap(x => x) + ->Array.filterMap(x => x) module Incr = { module Gen = { @@ -57,7 +55,7 @@ module Incr = { // parsing with continuation let feed = (self: t<'a, 'e>, input: string): unit => { // get the existing continuation or initialize a new one - let continue = self.continuation.contents->Option.getWithDefault(self.initialContinuation) + let continue = self.continuation.contents->Option.getOr(self.initialContinuation) // continue parsing with the given continuation switch continue(input) { @@ -96,7 +94,7 @@ module SExpression = { let rec toString = x => switch x { | A(s) => "\"" ++ (s ++ "\"") - | L(xs) => "[" ++ (Js.Array.joinWith(", ", Array.map(xs, toString)) ++ "]") + | L(xs) => "[" ++ (xs->Array.map(toString)->Array.join(", ") ++ "]") } type state = { @@ -107,8 +105,8 @@ module SExpression = { } let preprocess = (string: string): result => - if Js.String.substring(~from=0, ~to_=13, string) === "cannot read: " { - Error(Js.String.sliceToEnd(~from=12, string)) + if String.substring(~start=0, ~end=13, string) === "cannot read: " { + Error(String.sliceToEnd(~start=12, string)) } else { Ok(string) } @@ -116,7 +114,7 @@ module SExpression = { let rec flatten: t => array = x => switch x { | A(s) => [s] - | L(xs) => xs->Array.map(flatten)->Array.concatMany + | L(xs) => xs->Array.map(flatten)->Array.flat } let parseWithContinuation = (string: string): Incr.continuation< @@ -136,7 +134,7 @@ module SExpression = { | Some(expr) => switch expr.contents { | A(_) => expr := L([expr.contents, elem]) - | L(xs) => ignore(Js.Array.push(elem, xs)) + | L(xs) => xs->Array.push(elem) } | None => () } @@ -145,7 +143,7 @@ module SExpression = { let totalLength = String.length(string) for i in 0 to totalLength - 1 { - let char = Js.String.charAt(i, string) + let char = string->String.charAt(i) if escaped.contents { /* something was being escaped */ @@ -158,13 +156,13 @@ module SExpression = { } else if char == "\'" && !in_str.contents { () } else if char == "(" && !in_str.contents { - ignore(Js.Array.push(ref(L([])), stack)) + stack->Array.push(ref(L([]))) } else if char == ")" && !in_str.contents { if word.contents != "" { pushToTheTop(A(word.contents)) word := "" } - switch Js.Array.pop(stack) { + switch Array.pop(stack) { | Some(expr) => pushToTheTop(expr.contents) | None => () } @@ -222,14 +220,14 @@ module SExpression = { ->splitToLines ->Array.forEach(line => { // get the parsing continuation or initialize a new one - let continue = continuation.contents->Option.getWithDefault(parseWithContinuation) + let continue = continuation.contents->Option.getOr(parseWithContinuation) // continue parsing with the given continuation switch continue(line) { - | Error(err) => ignore(Js.Array.push(Error(err), resultAccum.contents)) + | Error(err) => resultAccum.contents->Array.push(Error(err)) | Continue(continue) => continuation := Some(continue) | Done(result) => - ignore(Js.Array.push(Ok(result), resultAccum.contents)) + resultAccum.contents->Array.push(Ok(result)) continuation := None } }) @@ -261,8 +259,8 @@ module Error = { let filepath = s => { // remove the Windows Bidi control character - let removedBidi = if Js.String.charCodeAt(0, s) === 8234.0 { - Js.String.sliceToEnd(~from=1, s) + let removedBidi = if String.charCodeAt(s, 0) === 8234.0 { + String.sliceToEnd(~start=1, s) } else { s } @@ -271,7 +269,7 @@ let filepath = s => { let normalized = NodeJs.Path.normalize(removedBidi) // replace Windows' stupid backslash with slash - let replaced = Js.String.replaceByRe(%re("/\\/g"), "/", normalized) + let replaced = normalized->String.replaceRegExp(%re("/\\/g"), "/") replaced } @@ -283,19 +281,12 @@ let filepath = s => { // CR LF => \r\n // " -> \" // \ -> \\ - let escape = (s: string): string => - Js.String.trim( - Js.String.replaceByRe( - %re("/\n/g"), - "\\n", - Js.String.replaceByRe( - %re("/\r\n/g"), - "\\r\\n", - Js.String.replaceByRe(%re("/\"/g"), "\\\"", Js.String.replaceByRe(%re("/\\/g"), "\\\\", s)), - ), - ), - ) + s + ->String.replaceRegExp(%re("/\\/g"), "\\\\") + ->String.replaceRegExp(%re("/\"/g"), "\\\"") + ->String.replaceRegExp(%re("/\r\n/g"), "\\r\\n") + ->String.replaceRegExp(%re("/\n/g"), "\\n") // Almost the inverse of escape, but only for EOL characters. // diff --git a/src/Parser/SourceFile.res b/src/Parser/SourceFile.res index 17f5ea8f..1dc75bb0 100644 --- a/src/Parser/SourceFile.res +++ b/src/Parser/SourceFile.res @@ -1,6 +1,5 @@ // All RegExps in this file has been updated to work with ReScript v10.1.4 -open Belt open Common module FileType = { @@ -11,13 +10,13 @@ module FileType = { | LiterateMarkdown | LiterateOrg let parse = filepath => - if Js.Re.test_(%re("/\.lagda\.rst$/i"), Parser.filepath(filepath)) { + if RegExp.test(%re("/\.lagda\.rst$/i"), Parser.filepath(filepath)) { LiterateRST - } else if Js.Re.test_(%re("/\.lagda\.md$/i"), Parser.filepath(filepath)) { + } else if RegExp.test(%re("/\.lagda\.md$/i"), Parser.filepath(filepath)) { LiterateMarkdown - } else if Js.Re.test_(%re("/\.lagda\.tex$|\.lagda$/i"), Parser.filepath(filepath)) { + } else if RegExp.test(%re("/\.lagda\.tex$|\.lagda$/i"), Parser.filepath(filepath)) { LiterateTeX - } else if Js.Re.test_(%re("/\.lagda\.org$/i"), Parser.filepath(filepath)) { + } else if RegExp.test(%re("/\.lagda\.org$/i"), Parser.filepath(filepath)) { LiterateOrg } else { Agda @@ -57,15 +56,15 @@ module Lexer = { regex : regex to perform split on a token sourceType: the type of token to look for and perform splitting targetType: the type of token given to the splitted tokens when identified */ - let lex = (regex: Js.Re.t, source: Token.kind, target: Token.kind, tokens): t => { + let lex = (regex: RegExp.t, source: Token.kind, target: Token.kind, tokens): t => { let f = (token: Token.t) => if token.kind === source { let cursor = ref(fst(token.range)) token.content - ->Js.String.splitByRe(regex, _) - ->Array.keepMap(x => x) + ->String.splitByRegExp(regex) + ->Array.filterMap(x => x) ->Array.map(content => { - let kind = Js.Re.test_(regex, content) ? target : source + let kind = RegExp.test(regex, content) ? target : source let cursorOld = cursor.contents cursor := cursor.contents + String.length(content) open Token @@ -74,7 +73,7 @@ module Lexer = { } else { [token] } - tokens->Array.map(f)->Array.concatMany + tokens->Array.map(f)->Array.flat } // transforms a list of tokens while preserving the ranges @@ -99,7 +98,7 @@ module Lexer = { // only apply map(f) on a specific tokenType let mapOnly = (kind: Token.kind, f: Token.t => Token.t, self): t => - self |> map(token => token.kind === kind ? f(token) : token) + map(token => token.kind === kind ? f(token) : token, self) } module Regex = { @@ -134,10 +133,9 @@ module Literate = { // split a single string into tokens (Literate) let toTokens = (raw: string): Lexer.t => { let cursor = ref(0) - Js.String.match_( - %re("/(.*(?:\r\n|[\n\v\f\r\x85\u2028\u2029])?)/g"), - raw, - )->Option.mapWithDefault([], lines => + raw + ->String.match(%re("/(.*(?:\r\n|[\n\v\f\r\x85\u2028\u2029])?)/g")) + ->Option.mapOr([], lines => lines ->Array.map(x => switch x { @@ -145,14 +143,14 @@ module Literate = { | Some(s) => s } ) - ->Array.keep(s => s != "") + ->Array.filter(s => s != "") ->Array.map(line => { // [\s\.\;\{\}\(\)\@] let cursorOld = cursor.contents cursor := cursor.contents + String.length(line) open Token { - content: Js.String.substring(~from=cursorOld, ~to_=cursor.contents, raw), + content: String.substring(~start=cursorOld, ~end=cursor.contents, raw), range: (cursorOld, cursor.contents), kind: Literate, } @@ -173,10 +171,10 @@ module Literate = { // update the previous line previous := current.contents - if Js.Re.test_(begin_, content) && !current.contents { + if RegExp.test(begin_, content) && !current.contents { // entering Agda code current := true - } else if Js.Re.test_(end_, content) && current.contents { + } else if RegExp.test(end_, content) && current.contents { // leaving Agda code current := false } @@ -231,12 +229,22 @@ let parse = (indices: array, filepath: string, raw: string): array | Agda => Lexer.make(raw) } /* just lexing, doesn't mess around with raw text, preserves positions */ - let original = - preprocessed - |> Lexer.lex(Regex.comment, AgdaRaw, Comment) - |> Lexer.lex(Regex.goalBracket, AgdaRaw, GoalBracket) - |> Lexer.lex(Regex.goalQuestionMarkRaw, AgdaRaw, GoalQMRaw) - |> Lexer.lex(Regex.goalQuestionMark, GoalQMRaw, GoalQM) + let original = Lexer.lex( + Regex.goalQuestionMark, + GoalQMRaw, + GoalQM, + Lexer.lex( + Regex.goalQuestionMarkRaw, + AgdaRaw, + GoalQMRaw, + Lexer.lex( + Regex.goalBracket, + AgdaRaw, + GoalBracket, + Lexer.lex(Regex.comment, AgdaRaw, Comment, preprocessed), + ), + ), + ) let questionMark2GoalBracket = token => { /* ? => {! !} */ @@ -265,28 +273,32 @@ let parse = (indices: array, filepath: string, raw: string): array /* calculate how much space we have */ let content: string = - Js.Re.exec_(Regex.goalBracketContent, token.content) - ->Option.flatMap(result => - Js.Re.captures(result)[1]->Option.map(Js.Nullable.toOption)->Option.flatMap(x => x) - ) - ->Option.getWithDefault("") + // Js.Re.exec_(Regex.goalBracketContent, token.content) + // ->Option.flatMap(result =>{ + // Js.Re.captures(result)[1]->Option.map(Js.Nullable.toOption)->Option.flatMap(x => x) + // }) + // ->Option.getOr("") + Regex.goalBracketContent + ->RegExp.exec(token.content) + ->Option.flatMap(result => result[1]->Option.flatMap(x => x)) + ->Option.getOr("") + let actualSpaces = content - ->Js.String.match_(%re("/\s*$/"), _) + ->String.match(%re("/\s*$/")) ->Option.flatMap(matches => switch matches[0] { | None => None | Some(None) => None - | Some(Some(s)) => Some(Js.String.length(s)) + | Some(Some(s)) => Some(String.length(s)) } ) - ->Option.getWithDefault(0) + ->Option.getOr(0) /* make room for the index, if there's not enough space */ let newContent = if actualSpaces < requiredSpaces { - let padding = Js.String.repeat(requiredSpaces - actualSpaces, "") - - Js.String.replaceByRe(%re("/\{!.*!\}/"), "{!" ++ content ++ padding ++ "!}", token.content) + let padding = " "->String.repeat(requiredSpaces - actualSpaces) + token.content->String.replaceRegExp(%re("/\{!.*!\}/"), "{!" ++ content ++ padding ++ "!}") } else { token.content } @@ -296,15 +308,16 @@ let parse = (indices: array, filepath: string, raw: string): array {content: newContent, kind: GoalBracket, range: (1, 2)} } - let modified = - original - |> Lexer.mapOnly(GoalQM, questionMark2GoalBracket) - |> Lexer.mapOnly(GoalBracket, adjustGoalBracket) - let originalHoles = original->Array.keep(isHole) - let modifiedHoles = modified->Array.keep(isHole) + let modified = Lexer.mapOnly( + GoalBracket, + adjustGoalBracket, + Lexer.mapOnly(GoalQM, questionMark2GoalBracket, original), + ) + let originalHoles = original->Array.filter(isHole) + let modifiedHoles = modified->Array.filter(isHole) originalHoles - ->Array.mapWithIndex((idx, token: Token.t) => + ->Array.mapWithIndex((token: Token.t, idx: int) => switch (modifiedHoles[idx], indices[idx]) { | (Some(modifiedHole), Some(index)) => let (start, _) = modifiedHole.range @@ -318,5 +331,5 @@ let parse = (indices: array, filepath: string, raw: string): array | _ => None } ) - ->Array.keepMap(x => x) + ->Array.filterMap(x => x) }