Port symbol.js to @flow. (#858)

* Port symbol.js to @flow.

* Add a `Mode` @flow type and use it in `ParseNode`.
This commit is contained in:
Ashish Myles
2017-09-09 17:54:47 -04:00
committed by GitHub
parent 0f9fb0a1ce
commit 9e330194b8
3 changed files with 37 additions and 13 deletions

View File

@@ -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,

View File

@@ -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];
}
}

9
src/types.js Normal file
View File

@@ -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";