From 9e330194b8f728b65d301c6c768c438f0612aa96 Mon Sep 17 00:00:00 2001 From: Ashish Myles Date: Sat, 9 Sep 2017 17:54:47 -0400 Subject: [PATCH] Port symbol.js to @flow. (#858) * Port symbol.js to @flow. * Add a `Mode` @flow type and use it in `ParseNode`. --- src/ParseNode.js | 5 +++-- src/symbols.js | 36 +++++++++++++++++++++++++----------- src/types.js | 9 +++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 src/types.js diff --git a/src/ParseNode.js b/src/ParseNode.js index f399ae1c..7479f41a 100644 --- a/src/ParseNode.js +++ b/src/ParseNode.js @@ -1,5 +1,6 @@ // @flow import {LexerInterface, Token} from "./Token"; +import type {Mode} from "./types"; /** * The resulting parse tree nodes of the parse tree. @@ -12,7 +13,7 @@ import {LexerInterface, Token} from "./Token"; export default class ParseNode { type: *; value: *; - mode: *; + mode: Mode; // TODO: We should combine these to ({lexer, start, end}|void) as they // should all exist together or not exist at all. That way, only a single // void check needs to be done to see if we have metadata. @@ -23,7 +24,7 @@ export default class ParseNode { constructor( type: string, // type of node, like e.g. "ordgroup" value: mixed, // type-specific representation of the node - mode: string, // parse mode in action for this node, "math" or "text" + mode: Mode, // parse mode in action for this node, "math" or "text" firstToken?: Token, // first token of the input for this node, // will omit position information if unset lastToken?: Token, // last token of the input for this node, diff --git a/src/symbols.js b/src/symbols.js index 5adf6600..a1151a42 100644 --- a/src/symbols.js +++ b/src/symbols.js @@ -1,3 +1,4 @@ +// @flow /** * This file holds a list of all no-argument functions and single-character * symbols (like 'a' or ';'). @@ -16,20 +17,33 @@ * accepted in (e.g. "math" or "text"). */ -module.exports = { - math: {}, - text: {}, +import type {Mode} from "./types"; + +type Font = "main" | "ams"; +type Group = + "accent" | "bin" | "close" | "inner" | "mathord" | "op" | "open" | "punct" | + "rel" | "spacing" | "textord"; +type CharInfoMap = {[string]: {font: Font, group: Group, replace: ?string}}; + +const symbols: {[Mode]: CharInfoMap} = { + "math": {}, + "text": {}, }; +export default symbols; -function defineSymbol(mode, font, group, replace, name, acceptUnicodeChar) { - module.exports[mode][name] = { - font: font, - group: group, - replace: replace, - }; +/** `acceptUnicodeChar = true` is only applicable if `replace` is set. */ +function defineSymbol( + mode: Mode, + font: Font, + group: Group, + replace: ?string, + name: string, + acceptUnicodeChar?: boolean, +) { + symbols[mode][name] = {font, group, replace}; - if (acceptUnicodeChar) { - module.exports[mode][replace] = module.exports[mode][name]; + if (acceptUnicodeChar && replace) { + symbols[mode][replace] = symbols[mode][name]; } } diff --git a/src/types.js b/src/types.js new file mode 100644 index 00000000..067de700 --- /dev/null +++ b/src/types.js @@ -0,0 +1,9 @@ +// @flow + +/** + * This file consists only of basic flow types used in multiple places. + * For types with javascript, create separate files by themselves. + */ + +export type Mode = "math" | "text"; +