SymbolMap.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Symbol map classes.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {Attributes, Args, ParseMethod, ParseInput, ParseResult} from './Types.js';
  23. import {Symbol, Macro} from './Symbol.js';
  24. import {MapHandler} from './MapHandler.js';
  25. /**
  26. * SymbolMaps are the base components for the input parsers.
  27. *
  28. * They provide a contains method that checks if a map is applicable (contains)
  29. * a particular string. Implementing classes then perform the actual symbol
  30. * parsing, from simple regular expression test, straight forward symbol mapping
  31. * to transformational functionality on the parsed string.
  32. *
  33. * @interface
  34. */
  35. export interface SymbolMap {
  36. /**
  37. * @return {string} The name of the map.
  38. */
  39. name: string;
  40. /**
  41. * @return {ParseMethod} The default parsing method.
  42. */
  43. parser: ParseMethod;
  44. /**
  45. * @param {string} symbol A symbol to parse.
  46. * @return {boolean} True if the symbol map applies to the symbol.
  47. */
  48. contains(symbol: string): boolean;
  49. /**
  50. * @param {string} symbol A symbol to parse.
  51. * @return {ParseMethod} A parse method for the symbol.
  52. */
  53. parserFor(symbol: string): ParseMethod;
  54. /**
  55. * @param {TexParser} env The current parser.
  56. * @param {string} symbol A symbol to parse.
  57. * @return {ParseResult} The parsed symbol and the rest of the string.
  58. */
  59. parse([env, symbol]: ParseInput): ParseResult;
  60. }
  61. /**
  62. * @param {ParseResult} result The result to check
  63. * @return {ParseResult} True if result was void, result otherwise
  64. */
  65. export function parseResult(result: ParseResult): ParseResult {
  66. return result === void 0 ? true : result;
  67. }
  68. /**
  69. * Abstract implementation of symbol maps.
  70. * @template T
  71. */
  72. export abstract class AbstractSymbolMap<T> implements SymbolMap {
  73. /**
  74. * @constructor
  75. * @implements {SymbolMap}
  76. * @param {string} name Name of the mapping.
  77. * @param {ParseMethod} parser The parser for the mappiong.
  78. */
  79. constructor(private _name: string, private _parser: ParseMethod) {
  80. MapHandler.register(this);
  81. }
  82. /**
  83. * @override
  84. */
  85. public get name(): string {
  86. return this._name;
  87. }
  88. /**
  89. * @override
  90. */
  91. public abstract contains(symbol: string): boolean;
  92. /**
  93. * @override
  94. */
  95. public parserFor(symbol: string) {
  96. return this.contains(symbol) ? this.parser : null;
  97. }
  98. /**
  99. * @override
  100. */
  101. public parse([env, symbol]: ParseInput) {
  102. let parser = this.parserFor(symbol);
  103. let mapped = this.lookup(symbol);
  104. return (parser && mapped) ? parseResult(parser(env, mapped as any)) : null;
  105. }
  106. public set parser(parser: ParseMethod) {
  107. this._parser = parser;
  108. }
  109. public get parser(): ParseMethod {
  110. return this._parser;
  111. }
  112. /**
  113. * @param {string} symbol
  114. * @return {T}
  115. */
  116. public abstract lookup(symbol: string): T;
  117. }
  118. /**
  119. * Regular expressions used for parsing strings.
  120. */
  121. export class RegExpMap extends AbstractSymbolMap<string> {
  122. /**
  123. * @constructor
  124. * @extends {AbstractSymbolMap}
  125. * @param {string} name Name of the mapping.
  126. * @param {ParseMethod} parser The parser for the mappiong.
  127. * @param {RegExp} regexp The regular expression.
  128. */
  129. constructor(name: string, parser: ParseMethod, private _regExp: RegExp) {
  130. super(name, parser);
  131. }
  132. /**
  133. * @override
  134. */
  135. public contains(symbol: string) {
  136. return this._regExp.test(symbol);
  137. }
  138. /**
  139. * @override
  140. */
  141. public lookup(symbol: string): string {
  142. return this.contains(symbol) ? symbol : null;
  143. }
  144. }
  145. /**
  146. * Parse maps associate strings with parsing functionality.
  147. * @constructor
  148. * @extends {AbstractSymbolMap}
  149. * @template K
  150. */
  151. export abstract class AbstractParseMap<K> extends AbstractSymbolMap<K> {
  152. private map: Map<string, K> = new Map<string, K>();
  153. /**
  154. * @override
  155. */
  156. public lookup(symbol: string): K {
  157. return this.map.get(symbol);
  158. }
  159. /**
  160. * @override
  161. */
  162. public contains(symbol: string) {
  163. return this.map.has(symbol);
  164. }
  165. /**
  166. * Sets mapping for a symbol.
  167. * @param {string} symbol The symbol to map.
  168. * @param {K} object The symbols value in the mapping's codomain.
  169. */
  170. public add(symbol: string, object: K) {
  171. this.map.set(symbol, object);
  172. }
  173. /**
  174. * Removes a symbol from the map
  175. * @param {string} symbol The symbol to remove
  176. */
  177. public remove(symbol: string) {
  178. this.map.delete(symbol);
  179. }
  180. }
  181. /**
  182. * Maps symbols that can all be parsed with the same method.
  183. *
  184. * @constructor
  185. * @extends {AbstractParseMap}
  186. */
  187. export class CharacterMap extends AbstractParseMap<Symbol> {
  188. /**
  189. * @constructor
  190. * @param {string} name Name of the mapping.
  191. * @param {ParseMethod} parser The parser for the mapping.
  192. * @param {JSON} json The JSON representation of the character mapping.
  193. */
  194. constructor(name: string, parser: ParseMethod,
  195. json: {[index: string]: string | [string, Attributes]}) {
  196. super(name, parser);
  197. for (const key of Object.keys(json)) {
  198. let value = json[key];
  199. let [char, attrs] = (typeof(value) === 'string') ? [value, null] : value;
  200. let character = new Symbol(key, char, attrs);
  201. this.add(key, character);
  202. }
  203. }
  204. }
  205. /**
  206. * Maps symbols that are delimiters, that are all parsed with the same method.
  207. *
  208. * @constructor
  209. * @extends {CharacterMap}
  210. */
  211. export class DelimiterMap extends CharacterMap {
  212. /**
  213. * @override
  214. */
  215. public parse([env, symbol]: ParseInput) {
  216. return super.parse([env, '\\' + symbol]);
  217. }
  218. }
  219. /**
  220. * Maps macros that all bring their own parsing method.
  221. *
  222. * @constructor
  223. * @extends {AbstractParseMap}
  224. */
  225. export class MacroMap extends AbstractParseMap<Macro> {
  226. /**
  227. * @constructor
  228. * @param {string} name Name of the mapping.
  229. * @param {JSON} json The JSON representation of the macro map.
  230. * @param {Record<string, ParseMethod>} functionMap Collection of parse
  231. * functions for the single macros.
  232. */
  233. constructor(name: string,
  234. json: {[index: string]: string | Args[]},
  235. functionMap: Record<string, ParseMethod>) {
  236. super(name, null);
  237. for (const key of Object.keys(json)) {
  238. let value = json[key];
  239. let [func, ...attrs] = (typeof(value) === 'string') ? [value] : value;
  240. let character = new Macro(key, functionMap[func as string], attrs);
  241. this.add(key, character);
  242. }
  243. }
  244. /**
  245. * @override
  246. */
  247. public parserFor(symbol: string) {
  248. let macro = this.lookup(symbol);
  249. return macro ? macro.func : null;
  250. }
  251. /**
  252. * @override
  253. */
  254. public parse([env, symbol]: ParseInput) {
  255. let macro = this.lookup(symbol);
  256. let parser = this.parserFor(symbol);
  257. if (!macro || !parser) {
  258. return null;
  259. }
  260. return parseResult(parser(env, macro.symbol, ...macro.args));
  261. }
  262. }
  263. /**
  264. * Maps macros that all bring their own parsing method.
  265. *
  266. * @constructor
  267. * @extends {MacroMap}
  268. */
  269. export class CommandMap extends MacroMap {
  270. /**
  271. * @override
  272. */
  273. public parse([env, symbol]: ParseInput) {
  274. let macro = this.lookup(symbol);
  275. let parser = this.parserFor(symbol);
  276. if (!macro || !parser) {
  277. return null;
  278. }
  279. let saveCommand = env.currentCS;
  280. env.currentCS = '\\' + symbol;
  281. let result = parser(env, '\\' + macro.symbol, ...macro.args);
  282. env.currentCS = saveCommand;
  283. return parseResult(result);
  284. }
  285. }
  286. /**
  287. * Maps macros for environments. It has a general parsing method for
  288. * environments, i.e., one that deals with begin/end, and each environment has
  289. * its own parsing method returning the content.
  290. *
  291. * @constructor
  292. * @extends {MacroMap}
  293. */
  294. export class EnvironmentMap extends MacroMap {
  295. /**
  296. * @constructor
  297. * @param {string} name Name of the mapping.
  298. * @param {ParseMethod} parser The parser for the environments.
  299. * @param {JSON} json The JSON representation of the macro map.
  300. * @param {Record<string, ParseMethod>} functionMap Collection of parse
  301. * functions for the single macros.
  302. */
  303. constructor(name: string,
  304. parser: ParseMethod,
  305. json: {[index: string]: string | Args[]},
  306. functionMap: Record<string, ParseMethod>) {
  307. super(name, json, functionMap);
  308. this.parser = parser;
  309. }
  310. /**
  311. * @override
  312. */
  313. public parse([env, symbol]: ParseInput) {
  314. let macro = this.lookup(symbol);
  315. let envParser = this.parserFor(symbol);
  316. if (!macro || !envParser) {
  317. return null;
  318. }
  319. return parseResult(this.parser(env, macro.symbol, envParser, macro.args));
  320. }
  321. }