[d24f17c] | 1 | import { Token } from './cst.js';
|
---|
| 2 | /**
|
---|
| 3 | * A YAML concrete syntax tree (CST) parser
|
---|
| 4 | *
|
---|
| 5 | * ```ts
|
---|
| 6 | * const src: string = ...
|
---|
| 7 | * for (const token of new Parser().parse(src)) {
|
---|
| 8 | * // token: Token
|
---|
| 9 | * }
|
---|
| 10 | * ```
|
---|
| 11 | *
|
---|
| 12 | * To use the parser with a user-provided lexer:
|
---|
| 13 | *
|
---|
| 14 | * ```ts
|
---|
| 15 | * function* parse(source: string, lexer: Lexer) {
|
---|
| 16 | * const parser = new Parser()
|
---|
| 17 | * for (const lexeme of lexer.lex(source))
|
---|
| 18 | * yield* parser.next(lexeme)
|
---|
| 19 | * yield* parser.end()
|
---|
| 20 | * }
|
---|
| 21 | *
|
---|
| 22 | * const src: string = ...
|
---|
| 23 | * const lexer = new Lexer()
|
---|
| 24 | * for (const token of parse(src, lexer)) {
|
---|
| 25 | * // token: Token
|
---|
| 26 | * }
|
---|
| 27 | * ```
|
---|
| 28 | */
|
---|
| 29 | export declare class Parser {
|
---|
| 30 | private onNewLine?;
|
---|
| 31 | /** If true, space and sequence indicators count as indentation */
|
---|
| 32 | private atNewLine;
|
---|
| 33 | /** If true, next token is a scalar value */
|
---|
| 34 | private atScalar;
|
---|
| 35 | /** Current indentation level */
|
---|
| 36 | private indent;
|
---|
| 37 | /** Current offset since the start of parsing */
|
---|
| 38 | offset: number;
|
---|
| 39 | /** On the same line with a block map key */
|
---|
| 40 | private onKeyLine;
|
---|
| 41 | /** Top indicates the node that's currently being built */
|
---|
| 42 | stack: Token[];
|
---|
| 43 | /** The source of the current token, set in parse() */
|
---|
| 44 | private source;
|
---|
| 45 | /** The type of the current token, set in parse() */
|
---|
| 46 | private type;
|
---|
| 47 | /**
|
---|
| 48 | * @param onNewLine - If defined, called separately with the start position of
|
---|
| 49 | * each new line (in `parse()`, including the start of input).
|
---|
| 50 | */
|
---|
| 51 | constructor(onNewLine?: (offset: number) => void);
|
---|
| 52 | /**
|
---|
| 53 | * Parse `source` as a YAML stream.
|
---|
| 54 | * If `incomplete`, a part of the last line may be left as a buffer for the next call.
|
---|
| 55 | *
|
---|
| 56 | * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
|
---|
| 57 | *
|
---|
| 58 | * @returns A generator of tokens representing each directive, document, and other structure.
|
---|
| 59 | */
|
---|
| 60 | parse(source: string, incomplete?: boolean): Generator<Token, void, unknown>;
|
---|
| 61 | /**
|
---|
| 62 | * Advance the parser by the `source` of one lexical token.
|
---|
| 63 | */
|
---|
| 64 | next(source: string): Generator<Token, void, unknown>;
|
---|
| 65 | private lexer;
|
---|
| 66 | /** Call at end of input to push out any remaining constructions */
|
---|
| 67 | end(): Generator<Token, void, unknown>;
|
---|
| 68 | private get sourceToken();
|
---|
| 69 | private step;
|
---|
| 70 | private peek;
|
---|
| 71 | private pop;
|
---|
| 72 | private stream;
|
---|
| 73 | private document;
|
---|
| 74 | private scalar;
|
---|
| 75 | private blockScalar;
|
---|
| 76 | private blockMap;
|
---|
| 77 | private blockSequence;
|
---|
| 78 | private flowCollection;
|
---|
| 79 | private flowScalar;
|
---|
| 80 | private startBlockValue;
|
---|
| 81 | private atIndentedComment;
|
---|
| 82 | private documentEnd;
|
---|
| 83 | private lineEnd;
|
---|
| 84 | }
|
---|