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 // @flow
import {LexerInterface, Token} from "./Token"; import {LexerInterface, Token} from "./Token";
import type {Mode} from "./types";
/** /**
* The resulting parse tree nodes of the parse tree. * The resulting parse tree nodes of the parse tree.
@@ -12,7 +13,7 @@ import {LexerInterface, Token} from "./Token";
export default class ParseNode { export default class ParseNode {
type: *; type: *;
value: *; value: *;
mode: *; mode: Mode;
// TODO: We should combine these to ({lexer, start, end}|void) as they // 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 // 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. // void check needs to be done to see if we have metadata.
@@ -23,7 +24,7 @@ export default class ParseNode {
constructor( constructor(
type: string, // type of node, like e.g. "ordgroup" type: string, // type of node, like e.g. "ordgroup"
value: mixed, // type-specific representation of the node 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, firstToken?: Token, // first token of the input for this node,
// will omit position information if unset // will omit position information if unset
lastToken?: Token, // last token of the input for this node, 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 * This file holds a list of all no-argument functions and single-character
* symbols (like 'a' or ';'). * symbols (like 'a' or ';').
@@ -16,20 +17,33 @@
* accepted in (e.g. "math" or "text"). * accepted in (e.g. "math" or "text").
*/ */
module.exports = { import type {Mode} from "./types";
math: {},
text: {}, 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) { /** `acceptUnicodeChar = true` is only applicable if `replace` is set. */
module.exports[mode][name] = { function defineSymbol(
font: font, mode: Mode,
group: group, font: Font,
replace: replace, group: Group,
}; replace: ?string,
name: string,
acceptUnicodeChar?: boolean,
) {
symbols[mode][name] = {font, group, replace};
if (acceptUnicodeChar) { if (acceptUnicodeChar && replace) {
module.exports[mode][replace] = module.exports[mode][name]; 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";