Skip to content

Commit

Permalink
implement logic for unknownMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
qwinsi committed Sep 8, 2024
1 parent 3da187f commit 8028e91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { symbolMap } from "./map";

export function tex2typst(tex: string, options?: Tex2TypstOptions): string {
const opt: Tex2TypstOptions = {
nonStrict: false,
nonStrict: true,
preferTypstIntrinsic: true,
customTexMacros: {}
};
Expand Down
60 changes: 34 additions & 26 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { symbolMap } from "./map";
import { TexNode, TexSupsubData, Token, TokenType } from "./types";


Expand Down Expand Up @@ -517,35 +518,42 @@ export class LatexParser {
throw new LatexParserError('Unexpected command: ' + command);
}


const paramNum = get_command_param_num(command.slice(1));
if (paramNum === 0) {
return [{ type: 'symbol', content: command }, pos];
} else if (paramNum === 1) {
if (command === '\\sqrt' && pos < tokens.length && token_eq(tokens[pos], LEFT_SQUARE_BRACKET)) {
const posLeftSquareBracket = pos;
const posRightSquareBracket = find_closing_square_bracket(tokens, pos);
const exprInside = tokens.slice(posLeftSquareBracket + 1, posRightSquareBracket);
const exponent = this.parse(exprInside);
const [arg1, newPos] = this.parseNextExprWithoutSupSub(tokens, posRightSquareBracket + 1);
return [{ type: 'unaryFunc', content: command, args: [arg1], data: exponent }, newPos];
} else if (command === '\\text') {
if (pos + 2 >= tokens.length) {
throw new LatexParserError('Expecting content for \\text command');
switch (paramNum) {
case 0:
if (!symbolMap.has(command.slice(1))) {
return [{ type: 'unknownMacro', content: command }, pos];
}
return [{ type: 'symbol', content: command }, pos];
case 1: {
if (command === '\\sqrt' && pos < tokens.length && token_eq(tokens[pos], LEFT_SQUARE_BRACKET)) {
const posLeftSquareBracket = pos;
const posRightSquareBracket = find_closing_square_bracket(tokens, pos);
const exprInside = tokens.slice(posLeftSquareBracket + 1, posRightSquareBracket);
const exponent = this.parse(exprInside);
const [arg1, newPos] = this.parseNextExprWithoutSupSub(tokens, posRightSquareBracket + 1);
return [{ type: 'unaryFunc', content: command, args: [arg1], data: exponent }, newPos];
} else if (command === '\\text') {
if (pos + 2 >= tokens.length) {
throw new LatexParserError('Expecting content for \\text command');
}
assert(token_eq(tokens[pos], LEFT_CURLY_BRACKET));
assert(tokens[pos + 1].type === TokenType.TEXT);
assert(token_eq(tokens[pos + 2], RIGHT_CURLY_BRACKET));
const text = tokens[pos + 1].value;
return [{ type: 'text', content: text }, pos + 3];
}
assert(token_eq(tokens[pos], LEFT_CURLY_BRACKET));
assert(tokens[pos + 1].type === TokenType.TEXT);
assert(token_eq(tokens[pos + 2], RIGHT_CURLY_BRACKET));
const text = tokens[pos + 1].value;
return [{ type: 'text', content: text }, pos + 3];
let [arg1, newPos] = this.parseNextExprWithoutSupSub(tokens, pos);
return [{ type: 'unaryFunc', content: command, args: [arg1] }, newPos];
}
let [arg1, newPos] = this.parseNextExprWithoutSupSub(tokens, pos);
return [{ type: 'unaryFunc', content: command, args: [arg1] }, newPos];
} else if (paramNum === 2) {
const [arg1, pos1] = this.parseNextExprWithoutSupSub(tokens, pos);
const [arg2, pos2] = this.parseNextExprWithoutSupSub(tokens, pos1);
return [{ type: 'binaryFunc', content: command, args: [arg1, arg2] }, pos2];
} else {
throw new Error( 'Invalid number of parameters');
case 2: {
const [arg1, pos1] = this.parseNextExprWithoutSupSub(tokens, pos);
const [arg2, pos2] = this.parseNextExprWithoutSupSub(tokens, pos1);
return [{ type: 'binaryFunc', content: command, args: [arg1, arg2] }, pos2];
}
default:
throw new Error( 'Invalid number of parameters');
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ export class TypstWriter {
this.insideFunctionDepth --;
}
} else if (node.type === 'unknownMacro') {
// TODO: At present, the parser does not produce nodes with type 'unknownMacro'.
// This is a placeholder for future implementation.
if (this.nonStrict) {
this.queue.push({ type: 'symbol', content: node.content });
} else {
Expand Down Expand Up @@ -330,7 +328,7 @@ export class TypstWriter {
}

private appendWithBracketsIfNeeded(node: TexNode): boolean {
const is_single = ['symbol', 'element', 'unaryFunc', 'binaryFunc', 'leftright'].includes(node.type);
const is_single = ['symbol', 'unknownMacro', 'element', 'unaryFunc', 'binaryFunc', 'leftright'].includes(node.type);
if (is_single) {
this.append(node);
} else {
Expand Down

0 comments on commit 8028e91

Please sign in to comment.