Mind_Reader/src/pylex/token.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

68 lines
1.7 KiB
TypeScript

/* eslint-disable @typescript-eslint/naming-convention */
/* ^ allow uppercase enum */
/**
* LineToken Symbol Types
*/
export enum Symbol {
FUNCTION = "function",
CLASS = "class",
IF = "if",
ELSE = "else",
ELIF = "elif",
FOR = "for",
WHILE = "while",
TRY = "try",
EXCEPT = "except",
FINALLY = "finally",
WITH = "with",
INDENT = "INDENT", // Indent token, default if not EOF, only contains indent information
EMPTY = "EMPTY", // empty line, used only to associate with the previous line
EOF = "EOF"
}
/**
* @typedef {Object} TabInfo
* @prop {number} size // The width of a tab in spaces
* @prop {boolean} hard // Whether to use literal tab characters
*/
export type TabInfo = {
size: number,
hard: boolean,
};
/**
* A token for a line in a Python file
*/
export default class LineToken {
/**
* @param `type` The type of token for this line.
* @param `linenr` The line number (0-indexed)
* @param `indentLevel` The level of indentation.
* @param `attr` Additional item for tokens that might need it.
*/
constructor(
public readonly type: Symbol,
public readonly linenr: number,
public readonly indentLevel: number,
public readonly attr?: any // Any additional things a token might need (class name, control conidition)
) { }
/**
* @return A string representation of the token
*/
toString(): string {
return this.type + ", linenr:" + (this.linenr+1) + ", indentLevel: " + this.indentLevel + ", attr: " + this.attr;
}
}
/**
* The End-Of-File token
*
* EOFTOKEN is returned when `next()` is called
* while the lexer is on the last token in the stream.
*/
const EOFTOKEN = new LineToken(Symbol.EOF, -1, -1);
export { EOFTOKEN };