ast.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // Package ast defines AST nodes that represent markdown elements.
  2. package ast
  3. import (
  4. "bytes"
  5. "fmt"
  6. "strings"
  7. textm "github.com/yuin/goldmark/text"
  8. "github.com/yuin/goldmark/util"
  9. )
  10. // A NodeType indicates what type a node belongs to.
  11. type NodeType int
  12. const (
  13. // TypeBlock indicates that a node is kind of block nodes.
  14. TypeBlock NodeType = iota + 1
  15. // TypeInline indicates that a node is kind of inline nodes.
  16. TypeInline
  17. // TypeDocument indicates that a node is kind of document nodes.
  18. TypeDocument
  19. )
  20. // NodeKind indicates more specific type than NodeType.
  21. type NodeKind int
  22. func (k NodeKind) String() string {
  23. return kindNames[k]
  24. }
  25. var kindMax NodeKind
  26. var kindNames = []string{""}
  27. // NewNodeKind returns a new Kind value.
  28. func NewNodeKind(name string) NodeKind {
  29. kindMax++
  30. kindNames = append(kindNames, name)
  31. return kindMax
  32. }
  33. // An Attribute is an attribute of the Node.
  34. type Attribute struct {
  35. Name []byte
  36. Value interface{}
  37. }
  38. // A Node interface defines basic AST node functionalities.
  39. type Node interface {
  40. // Type returns a type of this node.
  41. Type() NodeType
  42. // Kind returns a kind of this node.
  43. Kind() NodeKind
  44. // NextSibling returns a next sibling node of this node.
  45. NextSibling() Node
  46. // PreviousSibling returns a previous sibling node of this node.
  47. PreviousSibling() Node
  48. // Parent returns a parent node of this node.
  49. Parent() Node
  50. // SetParent sets a parent node to this node.
  51. SetParent(Node)
  52. // SetPreviousSibling sets a previous sibling node to this node.
  53. SetPreviousSibling(Node)
  54. // SetNextSibling sets a next sibling node to this node.
  55. SetNextSibling(Node)
  56. // HasChildren returns true if this node has any children, otherwise false.
  57. HasChildren() bool
  58. // ChildCount returns a total number of children.
  59. ChildCount() int
  60. // FirstChild returns a first child of this node.
  61. FirstChild() Node
  62. // LastChild returns a last child of this node.
  63. LastChild() Node
  64. // AppendChild append a node child to the tail of the children.
  65. AppendChild(self, child Node)
  66. // RemoveChild removes a node child from this node.
  67. // If a node child is not children of this node, RemoveChild nothing to do.
  68. RemoveChild(self, child Node)
  69. // RemoveChildren removes all children from this node.
  70. RemoveChildren(self Node)
  71. // SortChildren sorts childrens by comparator.
  72. SortChildren(comparator func(n1, n2 Node) int)
  73. // ReplaceChild replace a node v1 with a node insertee.
  74. // If v1 is not children of this node, ReplaceChild append a insetee to the
  75. // tail of the children.
  76. ReplaceChild(self, v1, insertee Node)
  77. // InsertBefore inserts a node insertee before a node v1.
  78. // If v1 is not children of this node, InsertBefore append a insetee to the
  79. // tail of the children.
  80. InsertBefore(self, v1, insertee Node)
  81. // InsertAfterinserts a node insertee after a node v1.
  82. // If v1 is not children of this node, InsertBefore append a insetee to the
  83. // tail of the children.
  84. InsertAfter(self, v1, insertee Node)
  85. // OwnerDocument returns this node's owner document.
  86. // If this node is not a child of the Document node, OwnerDocument
  87. // returns nil.
  88. OwnerDocument() *Document
  89. // Dump dumps an AST tree structure to stdout.
  90. // This function completely aimed for debugging.
  91. // level is a indent level. Implementer should indent informations with
  92. // 2 * level spaces.
  93. Dump(source []byte, level int)
  94. // Text returns text values of this node.
  95. Text(source []byte) []byte
  96. // HasBlankPreviousLines returns true if the row before this node is blank,
  97. // otherwise false.
  98. // This method is valid only for block nodes.
  99. HasBlankPreviousLines() bool
  100. // SetBlankPreviousLines sets whether the row before this node is blank.
  101. // This method is valid only for block nodes.
  102. SetBlankPreviousLines(v bool)
  103. // Lines returns text segments that hold positions in a source.
  104. // This method is valid only for block nodes.
  105. Lines() *textm.Segments
  106. // SetLines sets text segments that hold positions in a source.
  107. // This method is valid only for block nodes.
  108. SetLines(*textm.Segments)
  109. // IsRaw returns true if contents should be rendered as 'raw' contents.
  110. IsRaw() bool
  111. // SetAttribute sets the given value to the attributes.
  112. SetAttribute(name []byte, value interface{})
  113. // SetAttributeString sets the given value to the attributes.
  114. SetAttributeString(name string, value interface{})
  115. // Attribute returns a (attribute value, true) if an attribute
  116. // associated with the given name is found, otherwise
  117. // (nil, false)
  118. Attribute(name []byte) (interface{}, bool)
  119. // AttributeString returns a (attribute value, true) if an attribute
  120. // associated with the given name is found, otherwise
  121. // (nil, false)
  122. AttributeString(name string) (interface{}, bool)
  123. // Attributes returns a list of attributes.
  124. // This may be a nil if there are no attributes.
  125. Attributes() []Attribute
  126. // RemoveAttributes removes all attributes from this node.
  127. RemoveAttributes()
  128. }
  129. // A BaseNode struct implements the Node interface partialliy.
  130. type BaseNode struct {
  131. firstChild Node
  132. lastChild Node
  133. parent Node
  134. next Node
  135. prev Node
  136. childCount int
  137. attributes []Attribute
  138. }
  139. func ensureIsolated(v Node) {
  140. if p := v.Parent(); p != nil {
  141. p.RemoveChild(p, v)
  142. }
  143. }
  144. // HasChildren implements Node.HasChildren .
  145. func (n *BaseNode) HasChildren() bool {
  146. return n.firstChild != nil
  147. }
  148. // SetPreviousSibling implements Node.SetPreviousSibling .
  149. func (n *BaseNode) SetPreviousSibling(v Node) {
  150. n.prev = v
  151. }
  152. // SetNextSibling implements Node.SetNextSibling .
  153. func (n *BaseNode) SetNextSibling(v Node) {
  154. n.next = v
  155. }
  156. // PreviousSibling implements Node.PreviousSibling .
  157. func (n *BaseNode) PreviousSibling() Node {
  158. return n.prev
  159. }
  160. // NextSibling implements Node.NextSibling .
  161. func (n *BaseNode) NextSibling() Node {
  162. return n.next
  163. }
  164. // RemoveChild implements Node.RemoveChild .
  165. func (n *BaseNode) RemoveChild(self, v Node) {
  166. if v.Parent() != self {
  167. return
  168. }
  169. n.childCount--
  170. prev := v.PreviousSibling()
  171. next := v.NextSibling()
  172. if prev != nil {
  173. prev.SetNextSibling(next)
  174. } else {
  175. n.firstChild = next
  176. }
  177. if next != nil {
  178. next.SetPreviousSibling(prev)
  179. } else {
  180. n.lastChild = prev
  181. }
  182. v.SetParent(nil)
  183. v.SetPreviousSibling(nil)
  184. v.SetNextSibling(nil)
  185. }
  186. // RemoveChildren implements Node.RemoveChildren .
  187. func (n *BaseNode) RemoveChildren(self Node) {
  188. for c := n.firstChild; c != nil; {
  189. c.SetParent(nil)
  190. c.SetPreviousSibling(nil)
  191. next := c.NextSibling()
  192. c.SetNextSibling(nil)
  193. c = next
  194. }
  195. n.firstChild = nil
  196. n.lastChild = nil
  197. n.childCount = 0
  198. }
  199. // SortChildren implements Node.SortChildren.
  200. func (n *BaseNode) SortChildren(comparator func(n1, n2 Node) int) {
  201. var sorted Node
  202. current := n.firstChild
  203. for current != nil {
  204. next := current.NextSibling()
  205. if sorted == nil || comparator(sorted, current) >= 0 {
  206. current.SetNextSibling(sorted)
  207. if sorted != nil {
  208. sorted.SetPreviousSibling(current)
  209. }
  210. sorted = current
  211. sorted.SetPreviousSibling(nil)
  212. } else {
  213. c := sorted
  214. for c.NextSibling() != nil && comparator(c.NextSibling(), current) < 0 {
  215. c = c.NextSibling()
  216. }
  217. current.SetNextSibling(c.NextSibling())
  218. current.SetPreviousSibling(c)
  219. if c.NextSibling() != nil {
  220. c.NextSibling().SetPreviousSibling(current)
  221. }
  222. c.SetNextSibling(current)
  223. }
  224. current = next
  225. }
  226. n.firstChild = sorted
  227. for c := n.firstChild; c != nil; c = c.NextSibling() {
  228. n.lastChild = c
  229. }
  230. }
  231. // FirstChild implements Node.FirstChild .
  232. func (n *BaseNode) FirstChild() Node {
  233. return n.firstChild
  234. }
  235. // LastChild implements Node.LastChild .
  236. func (n *BaseNode) LastChild() Node {
  237. return n.lastChild
  238. }
  239. // ChildCount implements Node.ChildCount .
  240. func (n *BaseNode) ChildCount() int {
  241. return n.childCount
  242. }
  243. // Parent implements Node.Parent .
  244. func (n *BaseNode) Parent() Node {
  245. return n.parent
  246. }
  247. // SetParent implements Node.SetParent .
  248. func (n *BaseNode) SetParent(v Node) {
  249. n.parent = v
  250. }
  251. // AppendChild implements Node.AppendChild .
  252. func (n *BaseNode) AppendChild(self, v Node) {
  253. ensureIsolated(v)
  254. if n.firstChild == nil {
  255. n.firstChild = v
  256. v.SetNextSibling(nil)
  257. v.SetPreviousSibling(nil)
  258. } else {
  259. last := n.lastChild
  260. last.SetNextSibling(v)
  261. v.SetPreviousSibling(last)
  262. }
  263. v.SetParent(self)
  264. n.lastChild = v
  265. n.childCount++
  266. }
  267. // ReplaceChild implements Node.ReplaceChild .
  268. func (n *BaseNode) ReplaceChild(self, v1, insertee Node) {
  269. n.InsertBefore(self, v1, insertee)
  270. n.RemoveChild(self, v1)
  271. }
  272. // InsertAfter implements Node.InsertAfter .
  273. func (n *BaseNode) InsertAfter(self, v1, insertee Node) {
  274. n.InsertBefore(self, v1.NextSibling(), insertee)
  275. }
  276. // InsertBefore implements Node.InsertBefore .
  277. func (n *BaseNode) InsertBefore(self, v1, insertee Node) {
  278. n.childCount++
  279. if v1 == nil {
  280. n.AppendChild(self, insertee)
  281. return
  282. }
  283. ensureIsolated(insertee)
  284. if v1.Parent() == self {
  285. c := v1
  286. prev := c.PreviousSibling()
  287. if prev != nil {
  288. prev.SetNextSibling(insertee)
  289. insertee.SetPreviousSibling(prev)
  290. } else {
  291. n.firstChild = insertee
  292. insertee.SetPreviousSibling(nil)
  293. }
  294. insertee.SetNextSibling(c)
  295. c.SetPreviousSibling(insertee)
  296. insertee.SetParent(self)
  297. }
  298. }
  299. // OwnerDocument implements Node.OwnerDocument.
  300. func (n *BaseNode) OwnerDocument() *Document {
  301. d := n.Parent()
  302. for {
  303. p := d.Parent()
  304. if p == nil {
  305. if v, ok := d.(*Document); ok {
  306. return v
  307. }
  308. break
  309. }
  310. d = p
  311. }
  312. return nil
  313. }
  314. // Text implements Node.Text .
  315. func (n *BaseNode) Text(source []byte) []byte {
  316. var buf bytes.Buffer
  317. for c := n.firstChild; c != nil; c = c.NextSibling() {
  318. buf.Write(c.Text(source))
  319. }
  320. return buf.Bytes()
  321. }
  322. // SetAttribute implements Node.SetAttribute.
  323. func (n *BaseNode) SetAttribute(name []byte, value interface{}) {
  324. if n.attributes == nil {
  325. n.attributes = make([]Attribute, 0, 10)
  326. } else {
  327. for i, a := range n.attributes {
  328. if bytes.Equal(a.Name, name) {
  329. n.attributes[i].Name = name
  330. n.attributes[i].Value = value
  331. return
  332. }
  333. }
  334. }
  335. n.attributes = append(n.attributes, Attribute{name, value})
  336. }
  337. // SetAttributeString implements Node.SetAttributeString.
  338. func (n *BaseNode) SetAttributeString(name string, value interface{}) {
  339. n.SetAttribute(util.StringToReadOnlyBytes(name), value)
  340. }
  341. // Attribute implements Node.Attribute.
  342. func (n *BaseNode) Attribute(name []byte) (interface{}, bool) {
  343. if n.attributes == nil {
  344. return nil, false
  345. }
  346. for i, a := range n.attributes {
  347. if bytes.Equal(a.Name, name) {
  348. return n.attributes[i].Value, true
  349. }
  350. }
  351. return nil, false
  352. }
  353. // AttributeString implements Node.AttributeString.
  354. func (n *BaseNode) AttributeString(s string) (interface{}, bool) {
  355. return n.Attribute(util.StringToReadOnlyBytes(s))
  356. }
  357. // Attributes implements Node.Attributes.
  358. func (n *BaseNode) Attributes() []Attribute {
  359. return n.attributes
  360. }
  361. // RemoveAttributes implements Node.RemoveAttributes.
  362. func (n *BaseNode) RemoveAttributes() {
  363. n.attributes = nil
  364. }
  365. // DumpHelper is a helper function to implement Node.Dump.
  366. // kv is pairs of an attribute name and an attribute value.
  367. // cb is a function called after wrote a name and attributes.
  368. func DumpHelper(v Node, source []byte, level int, kv map[string]string, cb func(int)) {
  369. name := v.Kind().String()
  370. indent := strings.Repeat(" ", level)
  371. fmt.Printf("%s%s {\n", indent, name)
  372. indent2 := strings.Repeat(" ", level+1)
  373. if v.Type() == TypeBlock {
  374. fmt.Printf("%sRawText: \"", indent2)
  375. for i := 0; i < v.Lines().Len(); i++ {
  376. line := v.Lines().At(i)
  377. fmt.Printf("%s", line.Value(source))
  378. }
  379. fmt.Printf("\"\n")
  380. fmt.Printf("%sHasBlankPreviousLines: %v\n", indent2, v.HasBlankPreviousLines())
  381. }
  382. for name, value := range kv {
  383. fmt.Printf("%s%s: %s\n", indent2, name, value)
  384. }
  385. if cb != nil {
  386. cb(level + 1)
  387. }
  388. for c := v.FirstChild(); c != nil; c = c.NextSibling() {
  389. c.Dump(source, level+1)
  390. }
  391. fmt.Printf("%s}\n", indent)
  392. }
  393. // WalkStatus represents a current status of the Walk function.
  394. type WalkStatus int
  395. const (
  396. // WalkStop indicates no more walking needed.
  397. WalkStop WalkStatus = iota + 1
  398. // WalkSkipChildren indicates that Walk wont walk on children of current
  399. // node.
  400. WalkSkipChildren
  401. // WalkContinue indicates that Walk can continue to walk.
  402. WalkContinue
  403. )
  404. // Walker is a function that will be called when Walk find a
  405. // new node.
  406. // entering is set true before walks children, false after walked children.
  407. // If Walker returns error, Walk function immediately stop walking.
  408. type Walker func(n Node, entering bool) (WalkStatus, error)
  409. // Walk walks a AST tree by the depth first search algorithm.
  410. func Walk(n Node, walker Walker) error {
  411. _, err := walkHelper(n, walker)
  412. return err
  413. }
  414. func walkHelper(n Node, walker Walker) (WalkStatus, error) {
  415. status, err := walker(n, true)
  416. if err != nil || status == WalkStop {
  417. return status, err
  418. }
  419. if status != WalkSkipChildren {
  420. for c := n.FirstChild(); c != nil; c = c.NextSibling() {
  421. if st, err := walkHelper(c, walker); err != nil || st == WalkStop {
  422. return WalkStop, err
  423. }
  424. }
  425. }
  426. status, err = walker(n, false)
  427. if err != nil || status == WalkStop {
  428. return WalkStop, err
  429. }
  430. return WalkContinue, nil
  431. }