MCPcopy Create free account
hub / github.com/NativeScript/SimDeck / readBalanced

Function readBalanced

scripts/experimental/swiftui-preview.mjs:1053–1080  ·  view source on GitHub ↗
(source, start, open, close)

Source from the content-addressed store, hash-verified

1051}
1052
1053function readBalanced(source, start, open, close) {
1054 let depth = 0;
1055 let textStart = start + 1;
1056 for (let index = start; index < source.length; index += 1) {
1057 const char = source[index];
1058 if (char === '"' || char === "'") {
1059 index = skipQuoted(source, index);
1060 continue;
1061 }
1062 if (char === "/" && source[index + 1] === "/") {
1063 index = skipLineComment(source, index);
1064 continue;
1065 }
1066 if (char === "/" && source[index + 1] === "*") {
1067 index = skipBlockComment(source, index);
1068 continue;
1069 }
1070 if (char === open) {
1071 depth += 1;
1072 } else if (char === close) {
1073 depth -= 1;
1074 if (depth === 0) {
1075 return { end: index, text: source.slice(textStart, index) };
1076 }
1077 }
1078 }
1079 throw new Error(`Unbalanced ${open}${close} block near offset ${start}.`);
1080}
1081
1082function firstStringLiteral(text) {
1083 const match = text.match(/"((?:\\"|[^"])*)"/);

Callers 1

extractPreviewsFunction · 0.85

Calls 3

skipQuotedFunction · 0.85
skipLineCommentFunction · 0.85
skipBlockCommentFunction · 0.85

Tested by

no test coverage detected