parse-cst.d.ts 4.3 KB

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