Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More updates to keyvalOptions, and add GetBrackets() option to match brackets. #1037

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions ts/input/tex/ParseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,15 @@ function readKeyval(text: string, l3keys: boolean = false): EnvList {
* @return {string} The cleaned string.
*/
function removeBraces(text: string, count: number): string {
if (count === 0) {
return text.replace(/^\s+/, '')
.replace(/([^\\\s]|^)((?:\\\\)*(?:\\\s)?)?\s+$/, '$1$2');
}
while (count > 0) {
text = text.trim().slice(1, -1);
count--;
}
return text.trim();
return text;
}


Expand All @@ -165,56 +169,46 @@ function readValue(text: string, end: string[],
let value = '';
let index = 0;
let start = 0; // Counter for the starting left braces.
let startCount = true; // Flag for counting starting left braces.
let stopCount = false; // If true right braces are found directly
let countBraces = true; // Flag for counting starting left braces.
// after starting braces, but no other char yet.
while (index < length) {
let c = text[index++];
switch (c) {
case '\\': // Handle control sequences (in particular, \{ and \})
value += c + text[index++];
startCount = stopCount = false;
value += c + (text[index++] || '');
countBraces = false;
continue;
case ' ': // Ignore spaces.
break;
case '{':
if (startCount) { // Count start left braces at start.
if (countBraces) { // Count open left braces at start.
start++;
} else {
stopCount = false;
}
braces++;
break;
case '}':
if (braces) { // Closing braces.
braces--;
}
if (startCount || stopCount) { // Closing braces at the start.
start--;
stopCount = true; // Continue to close braces.
if (!braces) { // Closing braces.
throw new TexError('ExtraCloseMissingOpen', 'Extra close brace or missing open brace');
}
startCount = false; // Stop counting start left braces.
braces--;
countBraces = false; // Stop counting start left braces.
break;
default:
if (!braces && end.indexOf(c) !== -1) { // End character reached.
return [stopCount ? 'true' : // If Stop count is true we
// have balanced braces, only.
removeBraces(value, l3keys ? Math.min(1, start) : start), c, text.slice(index)];
return [removeBraces(value, l3keys ? Math.min(1, start) : start), c, text.slice(index)];
}
if (start > braces) { // Some start left braces have been closed.
start = braces;
}
startCount = false;
stopCount = false;
countBraces = false;
}
value += c;
}
if (braces) {
throw new TexError('ExtraOpenMissingClose',
'Extra open brace or missing close brace');
throw new TexError('ExtraOpenMissingClose', 'Extra open brace or missing close brace');
}
return (dropBrace && !stopCount && start) ? ['', '', removeBraces(value, 1)] :
[stopCount ? 'true' : removeBraces(value, l3keys ? Math.min(1, start) : start), '', text.slice(index)];
return (dropBrace && start) ? ['', '', removeBraces(value, 1)] :
[removeBraces(value, l3keys ? Math.min(1, start) : start), '', text.slice(index)];
}

export const ParseUtil = {
Expand Down
15 changes: 10 additions & 5 deletions ts/input/tex/TexParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,16 @@ export default class TexParser {

/**
* Get an optional LaTeX argument in brackets.
* @param {string} name Name of the current control sequence.
* @param {string} def? The default value for the optional argument.
* @param {string} _name Name of the current control sequence.
* @param {string?} def The default value for the optional argument.
* @param {boolean=} matchBrackets True if indernal brackets must match.
* @return {string} The optional argument.
*/
public GetBrackets(_name: string, def?: string): string {
public GetBrackets(_name: string, def?: string, matchBrackets: boolean = false): string {
if (this.GetNext() !== '[') {
return def;
}
let j = ++this.i, parens = 0;
let j = ++this.i, parens = 0, brackets = 0;
while (this.i < this.string.length) {
switch (this.string.charAt(this.i++)) {
case '{': parens++; break;
Expand All @@ -366,9 +367,13 @@ export default class TexParser {
'Extra close brace while looking for %1', '\']\'');
}
break;
case '[': if (parens === 0) brackets++; break;
case ']':
if (parens === 0) {
return this.string.slice(j, this.i - 1);
if (!matchBrackets || brackets === 0) {
return this.string.slice(j, this.i - 1);
}
brackets--;
}
break;
}
Expand Down