[6a3a178] | 1 | import { Type, YAMLSyntaxError } from './util'
|
---|
| 2 |
|
---|
| 3 | export default function parseCST(str: string): ParsedCST
|
---|
| 4 |
|
---|
| 5 | export interface ParsedCST extends Array<CST.Document> {
|
---|
| 6 | setOrigRanges(): boolean
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 | export namespace CST {
|
---|
| 10 | interface Range {
|
---|
| 11 | start: number
|
---|
| 12 | end: number
|
---|
| 13 | origStart?: number
|
---|
| 14 | origEnd?: number
|
---|
| 15 | isEmpty(): boolean
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | interface ParseContext {
|
---|
| 19 | /** Node starts at beginning of line */
|
---|
| 20 | atLineStart: boolean
|
---|
| 21 | /** true if currently in a collection context */
|
---|
| 22 | inCollection: boolean
|
---|
| 23 | /** true if currently in a flow context */
|
---|
| 24 | inFlow: boolean
|
---|
| 25 | /** Current level of indentation */
|
---|
| 26 | indent: number
|
---|
| 27 | /** Start of the current line */
|
---|
| 28 | lineStart: number
|
---|
| 29 | /** The parent of the node */
|
---|
| 30 | parent: Node
|
---|
| 31 | /** Source of the YAML document */
|
---|
| 32 | src: string
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | interface Node {
|
---|
| 36 | context: ParseContext | null
|
---|
| 37 | /** if not null, indicates a parser failure */
|
---|
| 38 | error: YAMLSyntaxError | null
|
---|
| 39 | /** span of context.src parsed into this node */
|
---|
| 40 | range: Range | null
|
---|
| 41 | valueRange: Range | null
|
---|
| 42 | /** anchors, tags and comments */
|
---|
| 43 | props: Range[]
|
---|
| 44 | /** specific node type */
|
---|
| 45 | type: Type
|
---|
| 46 | /** if non-null, overrides source value */
|
---|
| 47 | value: string | null
|
---|
| 48 |
|
---|
| 49 | readonly anchor: string | null
|
---|
| 50 | readonly comment: string | null
|
---|
| 51 | readonly hasComment: boolean
|
---|
| 52 | readonly hasProps: boolean
|
---|
| 53 | readonly jsonLike: boolean
|
---|
| 54 | readonly rangeAsLinePos: null | {
|
---|
| 55 | start: { line: number; col: number }
|
---|
| 56 | end?: { line: number; col: number }
|
---|
| 57 | }
|
---|
| 58 | readonly rawValue: string | null
|
---|
| 59 | readonly tag:
|
---|
| 60 | | null
|
---|
| 61 | | { verbatim: string }
|
---|
| 62 | | { handle: string; suffix: string }
|
---|
| 63 | readonly valueRangeContainsNewline: boolean
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | interface Alias extends Node {
|
---|
| 67 | type: Type.ALIAS
|
---|
| 68 | /** contain the anchor without the * prefix */
|
---|
| 69 | readonly rawValue: string
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | type Scalar = BlockValue | PlainValue | QuoteValue
|
---|
| 73 |
|
---|
| 74 | interface BlockValue extends Node {
|
---|
| 75 | type: Type.BLOCK_FOLDED | Type.BLOCK_LITERAL
|
---|
| 76 | chomping: 'CLIP' | 'KEEP' | 'STRIP'
|
---|
| 77 | blockIndent: number | null
|
---|
| 78 | header: Range
|
---|
| 79 | readonly strValue: string | null
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | interface BlockFolded extends BlockValue {
|
---|
| 83 | type: Type.BLOCK_FOLDED
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | interface BlockLiteral extends BlockValue {
|
---|
| 87 | type: Type.BLOCK_LITERAL
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | interface PlainValue extends Node {
|
---|
| 91 | type: Type.PLAIN
|
---|
| 92 | readonly strValue: string | null
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | interface QuoteValue extends Node {
|
---|
| 96 | type: Type.QUOTE_DOUBLE | Type.QUOTE_SINGLE
|
---|
| 97 | readonly strValue:
|
---|
| 98 | | null
|
---|
| 99 | | string
|
---|
| 100 | | { str: string; errors: YAMLSyntaxError[] }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | interface QuoteDouble extends QuoteValue {
|
---|
| 104 | type: Type.QUOTE_DOUBLE
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | interface QuoteSingle extends QuoteValue {
|
---|
| 108 | type: Type.QUOTE_SINGLE
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | interface Comment extends Node {
|
---|
| 112 | type: Type.COMMENT
|
---|
| 113 | readonly anchor: null
|
---|
| 114 | readonly comment: string
|
---|
| 115 | readonly rawValue: null
|
---|
| 116 | readonly tag: null
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | interface BlankLine extends Node {
|
---|
| 120 | type: Type.BLANK_LINE
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | interface MapItem extends Node {
|
---|
| 124 | type: Type.MAP_KEY | Type.MAP_VALUE
|
---|
| 125 | node: ContentNode | null
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | interface MapKey extends MapItem {
|
---|
| 129 | type: Type.MAP_KEY
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | interface MapValue extends MapItem {
|
---|
| 133 | type: Type.MAP_VALUE
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | interface Map extends Node {
|
---|
| 137 | type: Type.MAP
|
---|
| 138 | /** implicit keys are not wrapped */
|
---|
| 139 | items: Array<BlankLine | Comment | Alias | Scalar | MapItem>
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | interface SeqItem extends Node {
|
---|
| 143 | type: Type.SEQ_ITEM
|
---|
| 144 | node: ContentNode | null
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | interface Seq extends Node {
|
---|
| 148 | type: Type.SEQ
|
---|
| 149 | items: Array<BlankLine | Comment | SeqItem>
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | interface FlowChar {
|
---|
| 153 | char: '{' | '}' | '[' | ']' | ',' | '?' | ':'
|
---|
| 154 | offset: number
|
---|
| 155 | origOffset?: number
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | interface FlowCollection extends Node {
|
---|
| 159 | type: Type.FLOW_MAP | Type.FLOW_SEQ
|
---|
| 160 | items: Array<
|
---|
| 161 | FlowChar | BlankLine | Comment | Alias | Scalar | FlowCollection
|
---|
| 162 | >
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | interface FlowMap extends FlowCollection {
|
---|
| 166 | type: Type.FLOW_MAP
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | interface FlowSeq extends FlowCollection {
|
---|
| 170 | type: Type.FLOW_SEQ
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | type ContentNode = Alias | Scalar | Map | Seq | FlowCollection
|
---|
| 174 |
|
---|
| 175 | interface Directive extends Node {
|
---|
| 176 | type: Type.DIRECTIVE
|
---|
| 177 | name: string
|
---|
| 178 | readonly anchor: null
|
---|
| 179 | readonly parameters: string[]
|
---|
| 180 | readonly tag: null
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | interface Document extends Node {
|
---|
| 184 | type: Type.DOCUMENT
|
---|
| 185 | directives: Array<BlankLine | Comment | Directive>
|
---|
| 186 | contents: Array<BlankLine | Comment | ContentNode>
|
---|
| 187 | readonly anchor: null
|
---|
| 188 | readonly comment: null
|
---|
| 189 | readonly tag: null
|
---|
| 190 | }
|
---|
| 191 | }
|
---|