mathjslab
Version:
MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.
271 lines (270 loc) • 8.96 kB
TypeScript
import { Complex } from './Complex';
/**
* Single quote string character type.
*/
type SingleQuoteCharacter = "'";
/**
* Single quote string character literal.
*/
declare const singleQuoteCharacter: SingleQuoteCharacter;
/**
* Double quote string character type.
*/
type DoubleQuoteCharacter = '"';
/**
* Double quote string character literal.
*/
declare const doubleQuoteCharacter: DoubleQuoteCharacter;
/**
* String quote character type.
*/
type StringQuoteCharacter = SingleQuoteCharacter | DoubleQuoteCharacter;
/**
* Type number for `CharString` node.
*/
declare const stringClass = 3;
/**
* Runtime string value used by the AST and interpreter.
*
* `CharString` preserves the source quote style when the parser knows it. The
* value is stored as a row character vector, while `str` remains the canonical
* textual view. Truthiness follows MATLAB/Octave-like string behavior: empty
* strings are false and non-empty strings are true.
*/
declare class CharString {
/**
* Character vector backing the textual value.
*/
private characters;
/**
* Type of string quote (single or double).
*/
quote: StringQuoteCharacter;
/**
* String class type number.
*/
static readonly STRING = 3;
/**
* String class type property.
*/
type: number;
/**
* Parent node.
*/
parent?: unknown;
/**
* `CharString` constructor.
* @param str Runtime string value.
* @param quote Source quote style to preserve when unparsing.
*/
constructor(str: string, quote?: StringQuoteCharacter);
/**
* Textual scalar view of the character vector.
*
* This accessor preserves the long-standing public `str` contract while
* letting the runtime store strings as character vectors internally.
*/
get str(): string;
/**
* Replace the textual value and rebuild the backing character vector.
*
* @param value New textual value.
*/
set str(value: string);
/**
* Number of characters in the vector.
*/
get length(): number;
/**
* MATLAB/Octave-style row-vector dimensions for this character value.
*/
get dimension(): [number, number];
/**
* Creates a `CharString` instance.
* @param str String value.
* @param quote Quote character.
* @returns A new `CharString` instance.
*/
static readonly create: (str: string, quote?: StringQuoteCharacter) => CharString;
/**
* Test if an object is a instance of `CharString`.
* @param value Object to test.
* @returns `true` if `obj` is an instance of `CharString`. `false` otherwise.
*/
static readonly isInstanceOf: (obj: unknown) => obj is CharString;
/**
* Test whether a runtime text value represents a MATLAB/Octave char vector.
*
* Single-quoted literals and values produced with the single-quote style
* are classified as `char`.
*/
static readonly isChar: (obj: unknown) => obj is CharString;
/**
* Test whether a runtime text value represents a MATLAB string scalar.
*
* Double-quoted literals and values produced with the double-quote style
* are classified as `string`.
*/
static readonly isString: (obj: unknown) => obj is CharString;
/**
* Creates a copy of `CharString` `value`.
* @param value `CharString` to copy.
* @returns A copy of `value`.
*/
static readonly copy: (value: CharString) => CharString;
/**
* Creates a copy of `CharString` instance.
* @returns A copy of `this` `CharString`.
*/
copy(): CharString;
/**
* Return a copy of the backing character vector.
*
* @param value String wrapper.
* @returns Character array.
*/
static readonly vector: (value: CharString) => string[];
/**
* Return a copy of this value's character vector.
*
* @returns Character array.
*/
vector(): string[];
/**
* Convert this value to scalar `CharString` elements, one per character.
*
* @param value String wrapper.
* @returns Character scalar values preserving quote style.
*/
static readonly toCharacterScalars: (value: CharString) => CharString[];
/**
* Convert this value to scalar `CharString` elements.
*
* @returns Character scalar values preserving quote style.
*/
toCharacterScalars(): CharString[];
/**
* Build a string from scalar character values.
*
* @param values Scalar character values.
* @param quote Quote style for the resulting value.
* @returns A joined `CharString`.
*/
static readonly fromCharacterScalars: (values: CharString[], quote?: StringQuoteCharacter) => CharString;
/**
* Convert a numeric character code to a scalar `CharString`.
*
* MATLAB's `char` conversion truncates nonintegers toward zero and clamps
* values to the supported UTF-16 code-unit range.
*
* @param code Numeric Unicode code unit.
* @param quote Quote style for the resulting scalar.
* @returns Character scalar for the normalized code.
*/
static readonly fromNumericCode: (code: number, quote?: StringQuoteCharacter) => CharString;
/**
* Return the character at a zero-based position.
*
* @param index Zero-based character index.
* @returns Scalar character value, or `undefined` when out of range.
*/
characterAt(index: number): CharString | undefined;
/**
* Select characters by zero-based indices.
*
* @param indices Zero-based character indices.
* @returns New `CharString` containing selected characters.
*/
select(indices: number[]): CharString;
/**
* Parse a raw string token into a `CharString`.
*
* The current parser already supplies the decoded string content, so this
* helper simply wraps it with the default double-quote style.
*
* @param str Decoded string content.
* @returns Parsed `CharString`.
*/
static readonly parse: (str: string) => CharString;
/**
* Render the string as source text without adding quotes.
*
* `_parentPrecedence` is accepted for compatibility with other unparse
* helpers; string rendering does not need precedence.
*
* @param value String value to render.
* @param _parentPrecedence Parent operator precedence, unused.
* @returns Raw string content.
*/
static readonly unparse: (value: CharString, _parentPrecedence?: number) => string;
/**
* Convert a `CharString` to its runtime string value.
*
* @param value String wrapper.
* @returns Raw string content.
*/
static readonly toString: (value: CharString) => string;
/**
* Convert this instance to its runtime string value.
*
* @returns Raw string content.
*/
toString(): string;
/**
* Render the string as a double-quoted escaped source literal.
*
* The escaping mirrors MATLAB/Octave double-quote conventions used by the
* rest of the unparsing pipeline.
*
* @param value String wrapper.
* @returns Escaped source literal.
*/
static readonly unparseEscaped: (value: CharString) => string;
/**
* Render the string as MathML.
*
* `_parentPrecedence` is accepted for compatibility with other MathML
* helpers; string rendering does not need precedence.
*
* @param value String value to render.
* @param _parentPrecedence Parent operator precedence, unused.
* @returns MathML fragment.
*/
static readonly unparseMathML: (value: CharString, _parentPrecedence?: number) => string;
/**
* Render the string as an escaped MathML literal.
*
* @param value String value to render.
* @returns MathML fragment containing a quoted escaped string.
*/
static readonly unparseEscapedMathML: (value: CharString) => string;
/**
* Convert a string to a logical complex scalar.
*
* @param value String wrapper.
* @returns `true` for non-empty strings, `false` for empty strings.
*/
static readonly logical: (value: CharString) => Complex;
/**
* Alias for `logical` used by the common element interface.
*
* @param value String wrapper.
* @returns Logical complex scalar.
*/
static readonly toLogical: (value: CharString) => Complex;
/**
* Convert this instance to a logical complex scalar.
*
* @returns Logical complex scalar.
*/
toLogical(): Complex;
}
export type { SingleQuoteCharacter, DoubleQuoteCharacter, StringQuoteCharacter };
export { singleQuoteCharacter, doubleQuoteCharacter, stringClass, CharString };
declare const _default: {
singleQuoteCharacter: "'";
doubleQuoteCharacter: "\"";
stringClass: number;
CharString: typeof CharString;
};
export default _default;