Mind_Reader/src/test/util.ts
Jake Grossman 887f88eba6
Add H5 & H4: Listen to Cursor/Line Context (#7)
* Update parser

Implied nodes caused ambiguity when querying for context. Now every line
is considered, even blank lines, to make the process much, *much* easier.

* Update pylex tests

* Add runCursorContext
2021-11-16 11:35:38 -06:00

69 lines
1.6 KiB
TypeScript

import * as vscode from 'vscode';
import { LineToken, PylexSymbol, LexNode} from '../pylex';
/**
* TODO: Eliminate need for me.
* Recursively deparents a LexNode tree. Needed
* because I wasn't able to iterate the circular parent-child
* relationship by hand
*/
function deparent(root: null): null;
function deparent(root: LexNode): LexNode;
function deparent(root: any): any {
if (root === null) {
return root;
} else {
if (root.children() !== null) {
return new LexNode(
root.label,
root.collapsibleState,
root.token,
root.children()!.map(deparent),
);
} else {
return new LexNode(
root.label,
root.collapsibleState,
root.token,
null,
null
);
}
}
}
/**
* "Roots" a list of lexNodes to match the parser
*
* Required to properly test the output of the parser,
* since the parent child-relationship can't be modeled
* exhaustively otherwise
*/
function root(nodes: LexNode[] | null): LexNode {
return new LexNode(
"root",
vscode.TreeItemCollapsibleState.None,
null,
nodes,
null
);
}
/* short hand for returning an indentation token for a certain line and indentation */
function indent(linenr: number, indentLevel: number): LexNode {
return new LexNode('INDENT', 0, new LineToken(PylexSymbol.INDENT, linenr, indentLevel));
}
/* short hand for returning an empty token for a certain line*/
function empty(linenr: number): LexNode {
return new LexNode('EMPTY', 0, new LineToken(PylexSymbol.EMPTY, linenr, 999999));
}
export {
deparent,
root,
indent,
empty
};