UNPKG

1.06 kBJavaScriptView Raw
1// @flow
2import defineFunction from "../defineFunction";
3import ParseError from "../ParseError";
4import {assertNodeType} from "../parseNode";
5
6// \@char is an internal function that takes a grouped decimal argument like
7// {123} and converts into symbol with code 123. It is used by the *macro*
8// \char defined in macros.js.
9defineFunction({
10 type: "textord",
11 names: ["\\@char"],
12 props: {
13 numArgs: 1,
14 allowedInText: true,
15 },
16 handler({parser}, args) {
17 const arg = assertNodeType(args[0], "ordgroup");
18 const group = arg.body;
19 let number = "";
20 for (let i = 0; i < group.length; i++) {
21 const node = assertNodeType(group[i], "textord");
22 number += node.text;
23 }
24 const code = parseInt(number);
25 if (isNaN(code)) {
26 throw new ParseError(`\\@char has non-numeric argument ${number}`);
27 }
28 return {
29 type: "textord",
30 mode: parser.mode,
31 text: String.fromCharCode(code),
32 };
33 },
34});