Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1import defineFunction from "../defineFunction";2import ParseError from "../ParseError";3import {assertNodeType} from "../parseNode";4import {Token} from "../Token";5 6import type Parser from "../Parser";7 8const globalMap: Record<string, string> = {9 "\\global": "\\global",10 "\\long": "\\\\globallong",11 "\\\\globallong": "\\\\globallong",12 "\\def": "\\gdef",13 "\\gdef": "\\gdef",14 "\\edef": "\\xdef",15 "\\xdef": "\\xdef",16 "\\let": "\\\\globallet",17 "\\futurelet": "\\\\globalfuture",18};19 20const checkControlSequence = (tok: Token): string => {21 const name = tok.text;22 if (/^(?:[\\{}$&#^_]|EOF)$/.test(name)) {23 throw new ParseError("Expected a control sequence", tok);24 }25 return name;26};27 28const getRHS = (parser: Parser): Token => {29 let tok = parser.gullet.popToken();30 if (tok.text === "=") { // consume optional equals31 tok = parser.gullet.popToken();32 if (tok.text === " ") { // consume one optional space33 tok = parser.gullet.popToken();34 }35 }36 return tok;37};38 39const letCommand = (parser: Parser, name: string, tok: Token, global: boolean) => {40 let macro = parser.gullet.macros.get(tok.text);41 if (macro == null) {42 // don't expand it later even if a macro with the same name is defined43 // e.g., \let\foo=\frac \def\frac{\relax} \frac1244 tok.noexpand = true;45 macro = {46 tokens: [tok],47 numArgs: 0,48 // reproduce the same behavior in expansion49 unexpandable: !parser.gullet.isExpandable(tok.text),50 };51 }52 parser.gullet.macros.set(name, macro, global);53};54 55// <assignment> -> <non-macro assignment>|<macro assignment>56// <non-macro assignment> -> <simple assignment>|\global<non-macro assignment>57// <macro assignment> -> <definition>|<prefix><macro assignment>58// <prefix> -> \global|\long|\outer59defineFunction({60 type: "internal",61 names: [62 "\\global", "\\long",63 "\\\\globallong", // can’t be entered directly64 ],65 props: {66 numArgs: 0,67 allowedInText: true,68 },69 handler({parser, funcName}) {70 parser.consumeSpaces();71 const token = parser.fetch();72 if (globalMap[token.text]) {73 // KaTeX doesn't have \par, so ignore \long74 if (funcName === "\\global" || funcName === "\\\\globallong") {75 token.text = globalMap[token.text];76 }77 return assertNodeType(parser.parseFunction(), "internal");78 }79 throw new ParseError(`Invalid token after macro prefix`, token);80 },81});82 83// Basic support for macro definitions: \def, \gdef, \edef, \xdef84// <definition> -> <def><control sequence><definition text>85// <def> -> \def|\gdef|\edef|\xdef86// <definition text> -> <parameter text><left brace><balanced text><right brace>87defineFunction({88 type: "internal",89 names: ["\\def", "\\gdef", "\\edef", "\\xdef"],90 props: {91 numArgs: 0,92 allowedInText: true,93 primitive: true,94 },95 handler({parser, funcName}) {96 let tok = parser.gullet.popToken();97 const name = tok.text;98 if (/^(?:[\\{}$&#^_]|EOF)$/.test(name)) {99 throw new ParseError("Expected a control sequence", tok);100 }101 102 let numArgs = 0;103 let insert: Token | undefined;104 const delimiters: string[][] = [[]];105 // <parameter text> contains no braces106 while (parser.gullet.future().text !== "{") {107 tok = parser.gullet.popToken();108 if (tok.text === "#") {109 // If the very last character of the <parameter text> is #, so that110 // this # is immediately followed by {, TeX will behave as if the {111 // had been inserted at the right end of both the parameter text112 // and the replacement text.113 if (parser.gullet.future().text === "{") {114 insert = parser.gullet.future();115 delimiters[numArgs].push("{");116 break;117 }118 119 // A parameter, the first appearance of # must be followed by 1,120 // the next by 2, and so on; up to nine #’s are allowed121 tok = parser.gullet.popToken();122 if (!(/^[1-9]$/.test(tok.text))) {123 throw new ParseError(`Invalid argument number "${tok.text}"`);124 }125 if (parseInt(tok.text) !== numArgs + 1) {126 throw new ParseError(127 `Argument number "${tok.text}" out of order`);128 }129 numArgs++;130 delimiters.push([]);131 } else if (tok.text === "EOF") {132 throw new ParseError("Expected a macro definition");133 } else {134 delimiters[numArgs].push(tok.text);135 }136 }137 // replacement text, enclosed in '{' and '}' and properly nested138 let {tokens} = parser.gullet.consumeArg();139 if (insert) {140 tokens.unshift(insert);141 }142 143 if (funcName === "\\edef" || funcName === "\\xdef") {144 tokens = parser.gullet.expandTokens(tokens);145 tokens.reverse(); // to fit in with stack order146 }147 // Final arg is the expansion of the macro148 parser.gullet.macros.set(name, {149 tokens,150 numArgs,151 delimiters,152 }, funcName === globalMap[funcName]);153 154 return {155 type: "internal",156 mode: parser.mode,157 };158 },159});160 161// <simple assignment> -> <let assignment>162// <let assignment> -> \futurelet<control sequence><token><token>163// | \let<control sequence><equals><one optional space><token>164// <equals> -> <optional spaces>|<optional spaces>=165defineFunction({166 type: "internal",167 names: [168 "\\let",169 "\\\\globallet", // can’t be entered directly170 ],171 props: {172 numArgs: 0,173 allowedInText: true,174 primitive: true,175 },176 handler({parser, funcName}) {177 const name = checkControlSequence(parser.gullet.popToken());178 parser.gullet.consumeSpaces();179 const tok = getRHS(parser);180 letCommand(parser, name, tok, funcName === "\\\\globallet");181 return {182 type: "internal",183 mode: parser.mode,184 };185 },186});187 188// ref: https://www.tug.org/TUGboat/tb09-3/tb22bechtolsheim.pdf189defineFunction({190 type: "internal",191 names: [192 "\\futurelet",193 "\\\\globalfuture", // can’t be entered directly194 ],195 props: {196 numArgs: 0,197 allowedInText: true,198 primitive: true,199 },200 handler({parser, funcName}) {201 const name = checkControlSequence(parser.gullet.popToken());202 const middle = parser.gullet.popToken();203 const tok = parser.gullet.popToken();204 letCommand(parser, name, tok, funcName === "\\\\globalfuture");205 parser.gullet.pushToken(tok);206 parser.gullet.pushToken(middle);207 return {208 type: "internal",209 mode: parser.mode,210 };211 },212});213 