Files
KaTeX/src/Token.js
ylemkimon 9917d1ce84 Add support for \expandafter, \noexpand, \edef, \let, and \long (#2122)
* Add support for \expandafter

* Add support for \noexpand

* Add support for \edef

* Update comments

* Allow \long before macro definition

* Update documentation

* Update comments

* Fix defPrefix

* Add support for \let

* Update documentation

* Print error token

* Update documentation

* Check whether command is expandable

* Add tests

* Fix token order

* Make noexpand a Token property

* Throw error if control sequence is undefined when expanding

* Rename expandableOnly to expandOnly

* Make unexpandable macro property

* Move \expandafter to macros.js

* Add TODO

* Fix merge conflict

* Update a test case

* Remove unused functions in MacroContextInterface

* Update comments

* Refactor code

* Move \noexpand to macros

* Update MacroExpander.js

* Add a test case

* Separate control sequence check to a function

* Add support for \futurelet

* Separate RHS getter to a function

* Update documentation

* Move expandOnly logic to expandOnce

* Refactor code and update comments

Co-authored-by: Kevin Barabash <kevinb@khanacademy.org>
2020-03-11 12:14:34 +09:00

48 lines
1.5 KiB
JavaScript

// @flow
import SourceLocation from "./SourceLocation";
/**
* Interface required to break circular dependency between Token, Lexer, and
* ParseError.
*/
export interface LexerInterface {input: string, tokenRegex: RegExp}
/**
* The resulting token returned from `lex`.
*
* It consists of the token text plus some position information.
* The position information is essentially a range in an input string,
* but instead of referencing the bare input string, we refer to the lexer.
* That way it is possible to attach extra metadata to the input string,
* like for example a file name or similar.
*
* The position information is optional, so it is OK to construct synthetic
* tokens if appropriate. Not providing available position information may
* lead to degraded error reporting, though.
*/
export class Token {
text: string;
loc: ?SourceLocation;
noexpand: ?boolean; // don't expand the token
treatAsRelax: ?boolean; // used in \noexpand
constructor(
text: string, // the text of this token
loc: ?SourceLocation,
) {
this.text = text;
this.loc = loc;
}
/**
* Given a pair of tokens (this and endToken), compute a `Token` encompassing
* the whole input range enclosed by these two.
*/
range(
endToken: Token, // last token of the range, inclusive
text: string, // the text of the newly constructed token
) {
return new Token(text, SourceLocation.range(this, endToken));
}
}