Repository: goccy/go-yaml (go)

Issue:
Comments split files into documents
When using `parse.ParseFile()`, I find that my single-document yaml file will get split into multiple documents every time I hit a comment; is this intentional?

example YAML to reproduce this:
```yaml
key1: data
key2: data
# Comment
key3: data
```

Another question I have is regarding the comment parsing; the only `ast.Mode` available is ParseComments (which is fine), however... calling `.String()` on `*ast.File`, or `*ast.Document`, does not display comments.


Pull request:
Support parsing with comment
Fix #51 

Currently supported types are `map` or `sequence` or `anchor` or `alias` .
If you want to support other types , please submit issue .

## Example

### Map

```yaml
# commentA
a: #commentB
  # commentC
  b: c # commentD
  # commentE
  d: e # commentF
  # commentG
  f: g # commentH
# commentI
f: g # commentJ
# commentK
```

```go
f, _ := parser.ParseBytes([]byte(yml), parser.ParseComments)
```

<details><summary>Output</summary><div>

```
&ast.File{
  Name: "",
  Docs: []*ast.Document{
    &ast.Document{
      Comment: (*token.Token)(nil),
      Start:   (*token.Token)(nil),
      End:     (*token.Token)(nil),
      Body:    &ast.MappingNode{
        Comment: &token.Token{
          Type:          12,
          CharacterType: 0,
          Indicator:     3,
          Value:         "\n# commentA\n",
          Origin:        "\n# commentA\n",
          Position:      &token.Position{
            Line:        2,
            Column:      1,
            Offset:      2,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
        Start: &token.Token{
          Type:          5,
          CharacterType: 0,
          Indicator:     1,
          Value:         ":",
          Origin:        ":",
          Position:      &token.Position{
            Line:        3,
            Column:      2,
            Offset:      14,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
        End:         (*token.Token)(nil),
        IsFlowStyle: false,
        Values:      []*ast.MappingValueNode{
          &ast.MappingValueNode{
            Comment: (*token.Token)(nil),
            Start:   ...,
            Key:     &ast.StringNode{
              ScalarNode: nil,
              Comment:    &token.Token{
                Type:          12,
                CharacterType: 0,
                Indicator:     3,
                Value:         "commentB",
                Origin:        " #commentB\n",
                Position:      &token.Position{
                  Line:        3,
                  Column:      4,
                  Offset:      16,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Token: &token.Token{
                Type:          30,
                CharacterType: 2,
                Indicator:     0,
                Value:         "a",
                Origin:        "a",
                Position:      &token.Position{
                  Line:        3,
                  Column:      1,
                  Offset:      13,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Value: "a",
            },
            Value: &ast.MappingNode{
              Comment: &token.Token{
                Type:          12,
                CharacterType: 0,
                Indicator:     3,
                Value:         "  # commentC\n",
                Origin:        "  # commentC\n",
                Position:      &token.Position{
                  Line:        4,
                  Column:      3,
                  Offset:      28,
                  IndentNum:   2,
                  IndentLevel: 1,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Start: &token.Token{
                Type:          5,
                CharacterType: 0,
                Indicator:     1,
                Value:         ":",
                Origin:        ":",
                Position:      &token.Position{
                  Line:        5,
                  Column:      4,
                  Offset:      42,
                  IndentNum:   2,
                  IndentLevel: 1,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              End:         (*token.Token)(nil),
              IsFlowStyle: false,
              Values:      []*ast.MappingValueNode{
                &ast.MappingValueNode{
                  Comment: (*token.Token)(nil),
                  Start:   ...,
                  Key:     &ast.StringNode{
                    ScalarNode: nil,
                    Comment:    (*token.Token)(nil),
                    Token:      &token.Token{
                      Type:          30,
                      CharacterType: 2,
                      Indicator:     0,
                      Value:         "b",
                      Origin:        "  b",
                      Position:      &token.Position{
                        Line:        5,
                        Column:      3,
                        Offset:      41,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Value: "b",
                  },
                  Value: &ast.StringNode{
                    ScalarNode: nil,
                    Comment:    &token.Token{
                      Type:          12,
                      CharacterType: 0,
                      Indicator:     3,
                      Value:         " commentD",
                      Origin:        "# commentD\n",
                      Position:      &token.Position{
                        Line:        5,
                        Column:      8,
                        Offset:      46,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Token: &token.Token{
                      Type:          30,
                      CharacterType: 2,
                      Indicator:     0,
                      Value:         "c",
                      Origin:        " c ",
                      Position:      &token.Position{
                        Line:        5,
                        Column:      6,
                        Offset:      44,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Value: "c",
                  },
                },
                &ast.MappingValueNode{
                  Comment: &token.Token{
                    Type:          12,
                    CharacterType: 0,
                    Indicator:     3,
                    Value:         "  # commentE\n",
                    Origin:        "  # commentE\n",
                    Position:      &token.Position{
                      Line:        6,
                      Column:      3,
                      Offset:      59,
                      IndentNum:   2,
                      IndentLevel: 1,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Start: &token.Token{
                    Type:          5,
                    CharacterType: 0,
                    Indicator:     1,
                    Value:         ":",
                    Origin:        ":",
                    Position:      &token.Position{
                      Line:        7,
                      Column:      4,
                      Offset:      73,
                      IndentNum:   2,
                      IndentLevel: 1,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Key: &ast.StringNode{
                    ScalarNode: nil,
                    Comment:    (*token.Token)(nil),
                    Token:      &token.Token{
                      Type:          30,
                      CharacterType: 2,
                      Indicator:     0,
                      Value:         "d",
                      Origin:        "  d",
                      Position:      &token.Position{
                        Line:        7,
                        Column:      3,
                        Offset:      72,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Value: "d",
                  },
                  Value: &ast.StringNode{
                    ScalarNode: nil,
                    Comment:    &token.Token{
                      Type:          12,
                      CharacterType: 0,
                      Indicator:     3,
                      Value:         " commentF",
                      Origin:        "# commentF\n",
                      Position:      &token.Position{
                        Line:        7,
                        Column:      8,
                        Offset:      77,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Token: &token.Token{
                      Type:          30,
                      CharacterType: 2,
                      Indicator:     0,
                      Value:         "e",
                      Origin:        " e ",
                      Position:      &token.Position{
                        Line:        7,
                        Column:      6,
                        Offset:      75,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Value: "e",
                  },
                },
                &ast.MappingValueNode{
                  Comment: &token.Token{
                    Type:          12,
                    CharacterType: 0,
                    Indicator:     3,
                    Value:         "  # commentG\n",
                    Origin:        "  # commentG\n",
                    Position:      &token.Position{
                      Line:        8,
                      Column:      3,
                      Offset:      90,
                      IndentNum:   2,
                      IndentLevel: 1,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Start: &token.Token{
                    Type:          5,
                    CharacterType: 0,
                    Indicator:     1,
                    Value:         ":",
                    Origin:        ":",
                    Position:      &token.Position{
                      Line:        9,
                      Column:      4,
                      Offset:      104,
                      IndentNum:   2,
                      IndentLevel: 1,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Key: &ast.StringNode{
                    ScalarNode: nil,
                    Comment:    (*token.Token)(nil),
                    Token:      &token.Token{
                      Type:          30,
                      CharacterType: 2,
                      Indicator:     0,
                      Value:         "f",
                      Origin:        "  f",
                      Position:      &token.Position{
                        Line:        9,
                        Column:      3,
                        Offset:      103,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Value: "f",
                  },
                  Value: &ast.StringNode{
                    ScalarNode: nil,
                    Comment:    &token.Token{
                      Type:          12,
                      CharacterType: 0,
                      Indicator:     3,
                      Value:         " commentH",
                      Origin:        "# commentH\n",
                      Position:      &token.Position{
                        Line:        9,
                        Column:      8,
                        Offset:      108,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Token: &token.Token{
                      Type:          30,
                      CharacterType: 2,
                      Indicator:     0,
                      Value:         "g",
                      Origin:        " g ",
                      Position:      &token.Position{
                        Line:        9,
                        Column:      6,
                        Offset:      106,
                        IndentNum:   2,
                        IndentLevel: 1,
                      },
                      Next: (*token.Token)(nil),
                      Prev: (*token.Token)(nil),
                    },
                    Value: "g",
                  },
                },
              },
            },
          },
          &ast.MappingValueNode{
            Comment: &token.Token{
              Type:          12,
              CharacterType: 0,
              Indicator:     3,
              Value:         "# commentI\n",
              Origin:        "# commentI\n",
              Position:      &token.Position{
                Line:        10,
                Column:      1,
                Offset:      119,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            Start: &token.Token{
              Type:          5,
              CharacterType: 0,
              Indicator:     1,
              Value:         ":",
              Origin:        ":",
              Position:      &token.Position{
                Line:        11,
                Column:      2,
                Offset:      131,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            Key: &ast.StringNode{
              ScalarNode: nil,
              Comment:    (*token.Token)(nil),
              Token:      &token.Token{
                Type:          30,
                CharacterType: 2,
                Indicator:     0,
                Value:         "f",
                Origin:        "f",
                Position:      &token.Position{
                  Line:        11,
                  Column:      1,
                  Offset:      130,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Value: "f",
            },
            Value: &ast.StringNode{
              ScalarNode: nil,
              Comment:    &token.Token{
                Type:          12,
                CharacterType: 0,
                Indicator:     3,
                Value:         " commentJ",
                Origin:        "# commentJ\n",
                Position:      &token.Position{
                  Line:        11,
                  Column:      6,
                  Offset:      135,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Token: &token.Token{
                Type:          30,
                CharacterType: 2,
                Indicator:     0,
                Value:         "g",
                Origin:        " g ",
                Position:      &token.Position{
                  Line:        11,
                  Column:      4,
                  Offset:      133,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Value: "g",
            },
          },
        },
      },
    },
    &ast.Document{
      Comment: (*token.Token)(nil),
      Start:   (*token.Token)(nil),
      End:     (*token.Token)(nil),
      Body:    &ast.CommentNode{
        Comment: &token.Token{
          Type:          12,
          CharacterType: 0,
          Indicator:     3,
          Value:         "# commentK\n",
          Origin:        "# commentK\n",
          Position:      &token.Position{
            Line:        12,
            Column:      1,
            Offset:      146,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
      },
    },
  },
}
```

</div></details>

### Sequence

```yaml
# commentA
- a # commentB
# commentC
- b: # commentD
  # commentE
  - d # commentF
  - e # commentG
# commentH
```

<details><summary>Output</summary><div>

```
&ast.File{
  Name: "",
  Docs: []*ast.Document{
    &ast.Document{
      Comment: (*token.Token)(nil),
      Start:   (*token.Token)(nil),
      End:     (*token.Token)(nil),
      Body:    &ast.SequenceNode{
        Comment: &token.Token{
          Type:          12,
          CharacterType: 0,
          Indicator:     3,
          Value:         "\n# commentA\n",
          Origin:        "\n# commentA\n",
          Position:      &token.Position{
            Line:        2,
            Column:      1,
            Offset:      2,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
        Start: &token.Token{
          Type:          3,
          CharacterType: 0,
          Indicator:     1,
          Value:         "-",
          Origin:        "-",
          Position:      &token.Position{
            Line:        3,
            Column:      1,
            Offset:      13,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
        End:         (*token.Token)(nil),
        IsFlowStyle: false,
        Values:      []ast.Node{
          &ast.StringNode{
            ScalarNode: nil,
            Comment:    &token.Token{
              Type:          12,
              CharacterType: 0,
              Indicator:     3,
              Value:         " commentB",
              Origin:        "# commentB\n",
              Position:      &token.Position{
                Line:        3,
                Column:      5,
                Offset:      17,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            Token: &token.Token{
              Type:          30,
              CharacterType: 2,
              Indicator:     0,
              Value:         "a",
              Origin:        " a ",
              Position:      &token.Position{
                Line:        3,
                Column:      3,
                Offset:      15,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            Value: "a",
          },
          &ast.SequenceNode{
            Comment: &token.Token{
              Type:          12,
              CharacterType: 0,
              Indicator:     3,
              Value:         "# commentC\n",
              Origin:        "# commentC\n",
              Position:      &token.Position{
                Line:        4,
                Column:      1,
                Offset:      28,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            Start: &token.Token{
              Type:          3,
              CharacterType: 0,
              Indicator:     1,
              Value:         "-",
              Origin:        "-",
              Position:      &token.Position{
                Line:        5,
                Column:      1,
                Offset:      39,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            End:         (*token.Token)(nil),
            IsFlowStyle: false,
            Values:      []ast.Node{
              &ast.MappingValueNode{
                Comment: (*token.Token)(nil),
                Start:   &token.Token{
                  Type:          5,
                  CharacterType: 0,
                  Indicator:     1,
                  Value:         ":",
                  Origin:        ":",
                  Position:      &token.Position{
                    Line:        5,
                    Column:      4,
                    Offset:      42,
                    IndentNum:   0,
                    IndentLevel: 0,
                  },
                  Next: (*token.Token)(nil),
                  Prev: (*token.Token)(nil),
                },
                Key: &ast.StringNode{
                  ScalarNode: nil,
                  Comment:    &token.Token{
                    Type:          12,
                    CharacterType: 0,
                    Indicator:     3,
                    Value:         " commentD",
                    Origin:        " # commentD\n",
                    Position:      &token.Position{
                      Line:        5,
                      Column:      6,
                      Offset:      44,
                      IndentNum:   0,
                      IndentLevel: 0,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Token: &token.Token{
                    Type:          30,
                    CharacterType: 2,
                    Indicator:     0,
                    Value:         "b",
                    Origin:        " b",
                    Position:      &token.Position{
                      Line:        5,
                      Column:      3,
                      Offset:      41,
                      IndentNum:   0,
                      IndentLevel: 0,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Value: "b",
                },
                Value: &ast.SequenceNode{
                  Comment: &token.Token{
                    Type:          12,
                    CharacterType: 0,
                    Indicator:     3,
                    Value:         "  # commentE\n",
                    Origin:        "  # commentE\n",
                    Position:      &token.Position{
                      Line:        6,
                      Column:      3,
                      Offset:      57,
                      IndentNum:   2,
                      IndentLevel: 1,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  Start: &token.Token{
                    Type:          3,
                    CharacterType: 0,
                    Indicator:     1,
                    Value:         "-",
                    Origin:        "  -",
                    Position:      &token.Position{
                      Line:        7,
                      Column:      3,
                      Offset:      70,
                      IndentNum:   2,
                      IndentLevel: 1,
                    },
                    Next: (*token.Token)(nil),
                    Prev: (*token.Token)(nil),
                  },
                  End:         (*token.Token)(nil),
                  IsFlowStyle: false,
                  Values:      []ast.Node{
                    &ast.StringNode{
                      ScalarNode: nil,
                      Comment:    &token.Token{
                        Type:          12,
                        CharacterType: 0,
                        Indicator:     3,
                        Value:         " commentF",
                        Origin:        "# commentF\n",
                        Position:      &token.Position{
                          Line:        7,
                          Column:      7,
                          Offset:      74,
                          IndentNum:   2,
                          IndentLevel: 1,
                        },
                        Next: (*token.Token)(nil),
                        Prev: (*token.Token)(nil),
                      },
                      Token: &token.Token{
                        Type:          30,
                        CharacterType: 2,
                        Indicator:     0,
                        Value:         "d",
                        Origin:        " d ",
                        Position:      &token.Position{
                          Line:        7,
                          Column:      5,
                          Offset:      72,
                          IndentNum:   2,
                          IndentLevel: 1,
                        },
                        Next: (*token.Token)(nil),
                        Prev: (*token.Token)(nil),
                      },
                      Value: "d",
                    },
                    &ast.StringNode{
                      ScalarNode: nil,
                      Comment:    &token.Token{
                        Type:          12,
                        CharacterType: 0,
                        Indicator:     3,
                        Value:         " commentG",
                        Origin:        "# commentG\n",
                        Position:      &token.Position{
                          Line:        8,
                          Column:      7,
                          Offset:      91,
                          IndentNum:   2,
                          IndentLevel: 1,
                        },
                        Next: (*token.Token)(nil),
                        Prev: (*token.Token)(nil),
                      },
                      Token: &token.Token{
                        Type:          30,
                        CharacterType: 2,
                        Indicator:     0,
                        Value:         "e",
                        Origin:        " e ",
                        Position:      &token.Position{
                          Line:        8,
                          Column:      5,
                          Offset:      89,
                          IndentNum:   2,
                          IndentLevel: 1,
                        },
                        Next: (*token.Token)(nil),
                        Prev: (*token.Token)(nil),
                      },
                      Value: "e",
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
    &ast.Document{
      Comment: (*token.Token)(nil),
      Start:   (*token.Token)(nil),
      End:     (*token.Token)(nil),
      Body:    &ast.CommentNode{
        Comment: &token.Token{
          Type:          12,
          CharacterType: 0,
          Indicator:     3,
          Value:         "# commentH\n",
          Origin:        "# commentH\n",
          Position:      &token.Position{
            Line:        9,
            Column:      1,
            Offset:      102,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
      },
    },
  },
}
```

</div></details>

### Anchor/Alias

```yaml
a: &x b # commentA
c: *x # commentB
```

<details><summary>Output</summary><div>

```
&ast.File{
  Name: "",
  Docs: []*ast.Document{
    &ast.Document{
      Comment: (*token.Token)(nil),
      Start:   (*token.Token)(nil),
      End:     (*token.Token)(nil),
      Body:    &ast.MappingNode{
        Comment: (*token.Token)(nil),
        Start:   &token.Token{
          Type:          5,
          CharacterType: 0,
          Indicator:     1,
          Value:         ":",
          Origin:        ":",
          Position:      &token.Position{
            Line:        2,
            Column:      2,
            Offset:      3,
            IndentNum:   0,
            IndentLevel: 0,
          },
          Next: (*token.Token)(nil),
          Prev: (*token.Token)(nil),
        },
        End:         (*token.Token)(nil),
        IsFlowStyle: false,
        Values:      []*ast.MappingValueNode{
          &ast.MappingValueNode{
            Comment: (*token.Token)(nil),
            Start:   ...,
            Key:     &ast.StringNode{
              ScalarNode: nil,
              Comment:    (*token.Token)(nil),
              Token:      &token.Token{
                Type:          30,
                CharacterType: 2,
                Indicator:     0,
                Value:         "a",
                Origin:        "\na",
                Position:      &token.Position{
                  Line:        2,
                  Column:      1,
                  Offset:      2,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Value: "a",
            },
            Value: &ast.AnchorNode{
              Comment: (*token.Token)(nil),
              Start:   &token.Token{
                Type:          13,
                CharacterType: 0,
                Indicator:     4,
                Value:         "&",
                Origin:        " &",
                Position:      &token.Position{
                  Line:        2,
                  Column:      4,
                  Offset:      5,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Name: &ast.StringNode{
                ScalarNode: nil,
                Comment:    (*token.Token)(nil),
                Token:      &token.Token{
                  Type:          30,
                  CharacterType: 2,
                  Indicator:     0,
                  Value:         "x",
                  Origin:        "x",
                  Position:      &token.Position{
                    Line:        2,
                    Column:      5,
                    Offset:      6,
                    IndentNum:   0,
                    IndentLevel: 0,
                  },
                  Next: (*token.Token)(nil),
                  Prev: (*token.Token)(nil),
                },
                Value: "x",
              },
              Value: &ast.StringNode{
                ScalarNode: nil,
                Comment:    &token.Token{
                  Type:          12,
                  CharacterType: 0,
                  Indicator:     3,
                  Value:         " commentA",
                  Origin:        "# commentA\n",
                  Position:      &token.Position{
                    Line:        2,
                    Column:      9,
                    Offset:      10,
                    IndentNum:   0,
                    IndentLevel: 0,
                  },
                  Next: (*token.Token)(nil),
                  Prev: (*token.Token)(nil),
                },
                Token: &token.Token{
                  Type:          30,
                  CharacterType: 2,
                  Indicator:     0,
                  Value:         "b",
                  Origin:        "b ",
                  Position:      &token.Position{
                    Line:        2,
                    Column:      7,
                    Offset:      8,
                    IndentNum:   0,
                    IndentLevel: 0,
                  },
                  Next: (*token.Token)(nil),
                  Prev: (*token.Token)(nil),
                },
                Value: "b",
              },
            },
          },
          &ast.MappingValueNode{
            Comment: (*token.Token)(nil),
            Start:   &token.Token{
              Type:          5,
              CharacterType: 0,
              Indicator:     1,
              Value:         ":",
              Origin:        ":",
              Position:      &token.Position{
                Line:        3,
                Column:      2,
                Offset:      22,
                IndentNum:   0,
                IndentLevel: 0,
              },
              Next: (*token.Token)(nil),
              Prev: (*token.Token)(nil),
            },
            Key: &ast.StringNode{
              ScalarNode: nil,
              Comment:    (*token.Token)(nil),
              Token:      &token.Token{
                Type:          30,
                CharacterType: 2,
                Indicator:     0,
                Value:         "c",
                Origin:        "c",
                Position:      &token.Position{
                  Line:        3,
                  Column:      1,
                  Offset:      21,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Value: "c",
            },
            Value: &ast.AliasNode{
              Comment: (*token.Token)(nil),
              Start:   &token.Token{
                Type:          14,
                CharacterType: 0,
                Indicator:     4,
                Value:         "*",
                Origin:        " *",
                Position:      &token.Position{
                  Line:        3,
                  Column:      4,
                  Offset:      24,
                  IndentNum:   0,
                  IndentLevel: 0,
                },
                Next: (*token.Token)(nil),
                Prev: (*token.Token)(nil),
              },
              Value: &ast.StringNode{
                ScalarNode: nil,
                Comment:    &token.Token{
                  Type:          12,
                  CharacterType: 0,
                  Indicator:     3,
                  Value:         " commentB",
                  Origin:        "# commentB\n",
                  Position:      &token.Position{
                    Line:        3,
                    Column:      7,
                    Offset:      27,
                    IndentNum:   0,
                    IndentLevel: 0,
                  },
                  Next: (*token.Token)(nil),
                  Prev: (*token.Token)(nil),
                },
                Token: &token.Token{
                  Type:          30,
                  CharacterType: 2,
                  Indicator:     0,
                  Value:         "x",
                  Origin:        "x ",
                  Position:      &token.Position{
                    Line:        3,
                    Column:      5,
                    Offset:      25,
                    IndentNum:   0,
                    IndentLevel: 0,
                  },
                  Next: (*token.Token)(nil),
                  Prev: (*token.Token)(nil),
                },
                Value: "x",
              },
            },
          },
        },
      },
    },
  },
}
```

</div></details>

Patch:
diff --git a/ast/ast.go b/ast/ast.go
index a16c5ef..b6827aa 100644
--- a/ast/ast.go
+++ b/ast/ast.go
@@ -7,6 +7,11 @@ import (
 	"strings"
 
 	"github.com/goccy/go-yaml/token"
+	"golang.org/x/xerrors"
+)
+
+var (
+	ErrInvalidTokenType = xerrors.New("invalid token type")
 )
 
 // NodeType type identifier of node
@@ -49,6 +54,8 @@ const (
 	DirectiveType
 	// TagType type identifier for tag node
 	TagType
+	// CommentType type identifier for comment node
+	CommentType
 )
 
 // String node type identifier to text
@@ -90,6 +97,8 @@ func (t NodeType) String() string {
 		return "Directive"
 	case TagType:
 		return "Tag"
+	case CommentType:
+		return "Comment"
 	}
 	return ""
 }
@@ -104,6 +113,10 @@ type Node interface {
 	Type() NodeType
 	// AddColumn add column number to child nodes recursively
 	AddColumn(int)
+	// SetComment set comment token to node
+	SetComment(*token.Token) error
+	// Comment returns comment token instance
+	GetComment() *token.Token
 }
 
 // File contains all documents in YAML file
@@ -123,9 +136,10 @@ func (f *File) String() string {
 
 // Document type of Document
 type Document struct {
-	Start *token.Token // position of DocumentHeader ( `---` )
-	End   *token.Token // position of DocumentEnd ( `...` )
-	Body  Node
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token // position of DocumentHeader ( `---` )
+	End     *token.Token // position of DocumentEnd ( `...` )
+	Body    Node
 }
 
 // GetToken returns token instance
@@ -133,6 +147,11 @@ func (d *Document) GetToken() *token.Token {
 	return d.Body.GetToken()
 }
 
+// GetComment returns comment token instance
+func (d *Document) GetComment() *token.Token {
+	return d.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (d *Document) AddColumn(col int) {
 	if d.Body != nil {
@@ -140,6 +159,15 @@ func (d *Document) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (d *Document) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	d.Comment = tk
+	return nil
+}
+
 // Type returns DocumentType
 func (d *Document) Type() NodeType { return DocumentType }
 
@@ -280,6 +308,11 @@ func String(tk *token.Token) Node {
 	}
 }
 
+// Comment create node for comment
+func Comment(tk *token.Token) Node {
+	return &CommentNode{Comment: tk}
+}
+
 // MergeKey create node for merge key ( << )
 func MergeKey(tk *token.Token) Node {
 	return &MergeKeyNode{
@@ -308,7 +341,8 @@ func Sequence(tk *token.Token, isFlowStyle bool) *SequenceNode {
 // NullNode type of null node
 type NullNode struct {
 	ScalarNode
-	Token *token.Token
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
 }
 
 // Type returns NullType
@@ -319,11 +353,25 @@ func (n *NullNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *NullNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *NullNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *NullNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns nil value
 func (n *NullNode) GetValue() interface{} {
 	return nil
@@ -337,8 +385,9 @@ func (n *NullNode) String() string {
 // IntegerNode type of integer node
 type IntegerNode struct {
 	ScalarNode
-	Token *token.Token
-	Value interface{} // int64 or uint64 value
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
+	Value   interface{} // int64 or uint64 value
 }
 
 // Type returns IntegerType
@@ -349,11 +398,25 @@ func (n *IntegerNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *IntegerNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *IntegerNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *IntegerNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns int64 value
 func (n *IntegerNode) GetValue() interface{} {
 	return n.Value
@@ -367,6 +430,7 @@ func (n *IntegerNode) String() string {
 // FloatNode type of float node
 type FloatNode struct {
 	ScalarNode
+	Comment   *token.Token // position of Comment ( `#comment` )
 	Token     *token.Token
 	Precision int
 	Value     float64
@@ -380,11 +444,25 @@ func (n *FloatNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *FloatNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *FloatNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *FloatNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns float64 value
 func (n *FloatNode) GetValue() interface{} {
 	return n.Value
@@ -398,8 +476,9 @@ func (n *FloatNode) String() string {
 // StringNode type of string node
 type StringNode struct {
 	ScalarNode
-	Token *token.Token
-	Value string
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
+	Value   string
 }
 
 // Type returns StringType
@@ -410,11 +489,25 @@ func (n *StringNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *StringNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *StringNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *StringNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns string value
 func (n *StringNode) GetValue() interface{} {
 	return n.Value
@@ -450,8 +543,9 @@ func (n *StringNode) String() string {
 // LiteralNode type of literal node
 type LiteralNode struct {
 	ScalarNode
-	Start *token.Token
-	Value *StringNode
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token
+	Value   *StringNode
 }
 
 // Type returns LiteralType
@@ -462,6 +556,11 @@ func (n *LiteralNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *LiteralNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *LiteralNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -470,6 +569,15 @@ func (n *LiteralNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *LiteralNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns string value
 func (n *LiteralNode) GetValue() interface{} {
 	return n.String()
@@ -484,7 +592,8 @@ func (n *LiteralNode) String() string {
 // MergeKeyNode type of merge key node
 type MergeKeyNode struct {
 	ScalarNode
-	Token *token.Token
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
 }
 
 // Type returns MergeKeyType
@@ -495,6 +604,11 @@ func (n *MergeKeyNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *MergeKeyNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // GetValue returns '<<' value
 func (n *MergeKeyNode) GetValue() interface{} {
 	return n.Token.Value
@@ -510,11 +624,21 @@ func (n *MergeKeyNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *MergeKeyNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // BoolNode type of boolean node
 type BoolNode struct {
 	ScalarNode
-	Token *token.Token
-	Value bool
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
+	Value   bool
 }
 
 // Type returns BoolType
@@ -525,11 +649,25 @@ func (n *BoolNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *BoolNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *BoolNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *BoolNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns boolean value
 func (n *BoolNode) GetValue() interface{} {
 	return n.Value
@@ -543,8 +681,9 @@ func (n *BoolNode) String() string {
 // InfinityNode type of infinity node
 type InfinityNode struct {
 	ScalarNode
-	Token *token.Token
-	Value float64
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
+	Value   float64
 }
 
 // Type returns InfinityType
@@ -555,11 +694,25 @@ func (n *InfinityNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *InfinityNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *InfinityNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *InfinityNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns math.Inf(0) or math.Inf(-1)
 func (n *InfinityNode) GetValue() interface{} {
 	return n.Value
@@ -573,7 +726,8 @@ func (n *InfinityNode) String() string {
 // NanNode type of nan node
 type NanNode struct {
 	ScalarNode
-	Token *token.Token
+	Comment *token.Token // position of Comment ( `#comment` )
+	Token   *token.Token
 }
 
 // Type returns NanType
@@ -584,11 +738,25 @@ func (n *NanNode) GetToken() *token.Token {
 	return n.Token
 }
 
+// GetComment returns comment token instance
+func (n *NanNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *NanNode) AddColumn(col int) {
 	n.Token.AddColumn(col)
 }
 
+// SetComment set comment token
+func (n *NanNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // GetValue returns math.NaN()
 func (n *NanNode) GetValue() interface{} {
 	return math.NaN()
@@ -634,6 +802,7 @@ func (m *MapNodeIter) Value() Node {
 
 // MappingNode type of mapping node
 type MappingNode struct {
+	Comment     *token.Token // position of Comment ( `#comment` )
 	Start       *token.Token
 	End         *token.Token
 	IsFlowStyle bool
@@ -648,6 +817,11 @@ func (n *MappingNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *MappingNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *MappingNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -657,6 +831,15 @@ func (n *MappingNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *MappingNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 func (n *MappingNode) flowStyleString() string {
 	if len(n.Values) == 0 {
 		return "{}"
@@ -697,9 +880,10 @@ func (n *MappingNode) MapRange() *MapNodeIter {
 
 // MappingValueNode type of mapping value
 type MappingValueNode struct {
-	Start *token.Token
-	Key   Node
-	Value Node
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token
+	Key     Node
+	Value   Node
 }
 
 // Type returns MappingValueType
@@ -710,6 +894,11 @@ func (n *MappingValueNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *MappingValueNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *MappingValueNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -721,6 +910,15 @@ func (n *MappingValueNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *MappingValueNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // String mapping value to text
 func (n *MappingValueNode) String() string {
 	space := strings.Repeat(" ", n.Key.GetToken().Position.Column-1)
@@ -781,6 +979,7 @@ func (m *ArrayNodeIter) Len() int {
 
 // SequenceNode type of sequence node
 type SequenceNode struct {
+	Comment     *token.Token // position of Comment ( `#comment` )
 	Start       *token.Token
 	End         *token.Token
 	IsFlowStyle bool
@@ -795,6 +994,11 @@ func (n *SequenceNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *SequenceNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *SequenceNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -804,6 +1008,15 @@ func (n *SequenceNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *SequenceNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 func (n *SequenceNode) flowStyleString() string {
 	values := []string{}
 	for _, value := range n.Values {
@@ -849,9 +1062,10 @@ func (n *SequenceNode) ArrayRange() *ArrayNodeIter {
 
 // AnchorNode type of anchor node
 type AnchorNode struct {
-	Start *token.Token
-	Name  Node
-	Value Node
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token
+	Name    Node
+	Value   Node
 }
 
 // Type returns AnchorType
@@ -862,6 +1076,11 @@ func (n *AnchorNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *AnchorNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *AnchorNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -873,6 +1092,15 @@ func (n *AnchorNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *AnchorNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // String anchor to text
 func (n *AnchorNode) String() string {
 	value := n.Value.String()
@@ -888,8 +1116,9 @@ func (n *AnchorNode) String() string {
 
 // AliasNode type of alias node
 type AliasNode struct {
-	Start *token.Token
-	Value Node
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token
+	Value   Node
 }
 
 // Type returns AliasType
@@ -900,6 +1129,11 @@ func (n *AliasNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *AliasNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *AliasNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -908,6 +1142,15 @@ func (n *AliasNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *AliasNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // String alias to text
 func (n *AliasNode) String() string {
 	return fmt.Sprintf("*%s", n.Value.String())
@@ -915,8 +1158,9 @@ func (n *AliasNode) String() string {
 
 // DirectiveNode type of directive node
 type DirectiveNode struct {
-	Start *token.Token
-	Value Node
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token
+	Value   Node
 }
 
 // Type returns DirectiveType
@@ -927,6 +1171,11 @@ func (n *DirectiveNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *DirectiveNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *DirectiveNode) AddColumn(col int) {
 	if n.Value != nil {
@@ -934,6 +1183,15 @@ func (n *DirectiveNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *DirectiveNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // String directive to text
 func (n *DirectiveNode) String() string {
 	return fmt.Sprintf("%s%s", n.Start.Value, n.Value.String())
@@ -941,8 +1199,9 @@ func (n *DirectiveNode) String() string {
 
 // TagNode type of tag node
 type TagNode struct {
-	Start *token.Token
-	Value Node
+	Comment *token.Token // position of Comment ( `#comment` )
+	Start   *token.Token
+	Value   Node
 }
 
 // Type returns TagType
@@ -953,6 +1212,11 @@ func (n *TagNode) GetToken() *token.Token {
 	return n.Start
 }
 
+// GetComment returns comment token instance
+func (n *TagNode) GetComment() *token.Token {
+	return n.Comment
+}
+
 // AddColumn add column number to child nodes recursively
 func (n *TagNode) AddColumn(col int) {
 	n.Start.AddColumn(col)
@@ -961,11 +1225,53 @@ func (n *TagNode) AddColumn(col int) {
 	}
 }
 
+// SetComment set comment token
+func (n *TagNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
 // String tag to text
 func (n *TagNode) String() string {
 	return fmt.Sprintf("%s %s", n.Start.Value, n.Value.String())
 }
 
+// CommentNode type of comment node
+type CommentNode struct {
+	Comment *token.Token // position of Comment ( `#comment` )
+}
+
+// Type returns TagType
+func (n *CommentNode) Type() NodeType { return CommentType }
+
+// GetToken returns token instance
+func (n *CommentNode) GetToken() *token.Token { return n.Comment }
+
+// GetComment returns comment token instance
+func (n *CommentNode) GetComment() *token.Token { return n.Comment }
+
+// AddColumn add column number to child nodes recursively
+func (n *CommentNode) AddColumn(col int) {
+	n.Comment.AddColumn(col)
+}
+
+// SetComment set comment token
+func (n *CommentNode) SetComment(tk *token.Token) error {
+	if tk.Type != token.CommentType {
+		return ErrInvalidTokenType
+	}
+	n.Comment = tk
+	return nil
+}
+
+// String comment to text
+func (n *CommentNode) String() string {
+	return n.Comment.Value
+}
+
 // Visitor has Visit method that is invokded for each node encountered by Walk.
 // If the result visitor w is not nil, Walk visits each of the children of node with the visitor w,
 // followed by a call of w.Visit(nil).
diff --git a/parser/context.go b/parser/context.go
index f208d10..2f64021 100644
--- a/parser/context.go
+++ b/parser/context.go
@@ -29,15 +29,41 @@ func (c *context) currentToken() *token.Token {
 }
 
 func (c *context) nextToken() *token.Token {
-	if c.size > c.idx+1 {
-		return c.tokens[c.idx+1]
+	if c.idx+1 >= c.size {
+		return nil
 	}
-	return nil
+	return c.tokens[c.idx+1]
 }
 
 func (c *context) afterNextToken() *token.Token {
-	if c.size > c.idx+2 {
-		return c.tokens[c.idx+2]
+	if c.idx+2 >= c.size {
+		return nil
+	}
+	return c.tokens[c.idx+2]
+}
+
+func (c *context) nextNotCommentToken() *token.Token {
+	for i := c.idx + 1; i < c.size; i++ {
+		tk := c.tokens[i]
+		if tk.Type == token.CommentType {
+			continue
+		}
+		return tk
+	}
+	return nil
+}
+
+func (c *context) afterNextNotCommentToken() *token.Token {
+	notCommentTokenCount := 0
+	for i := c.idx + 1; i < c.size; i++ {
+		tk := c.tokens[i]
+		if tk.Type == token.CommentType {
+			continue
+		}
+		notCommentTokenCount++
+		if notCommentTokenCount == 2 {
+			return tk
+		}
 	}
 	return nil
 }
@@ -46,7 +72,15 @@ func (c *context) enabledComment() bool {
 	return c.mode&ParseComments != 0
 }
 
-func (c *context) progress(num int) {
+func (c *context) isCurrentCommentToken() bool {
+	tk := c.currentToken()
+	if tk == nil {
+		return false
+	}
+	return tk.Type == token.CommentType
+}
+
+func (c *context) progressIgnoreComment(num int) {
 	if c.size <= c.idx+num {
 		c.idx = c.size
 	} else {
@@ -54,6 +88,13 @@ func (c *context) progress(num int) {
 	}
 }
 
+func (c *context) progress(num int) {
+	if c.isCurrentCommentToken() {
+		return
+	}
+	c.progressIgnoreComment(num)
+}
+
 func newContext(tokens token.Tokens, mode Mode) *context {
 	filteredTokens := token.Tokens{}
 	if mode&ParseComments != 0 {
diff --git a/parser/parser.go b/parser/parser.go
index ea14aab..2f000cd 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -113,6 +113,14 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 	ctx.progress(1)          // progress to mapping value token
 	tk := ctx.currentToken() // get mapping value token
 	ctx.progress(1)          // progress to value token
+	if err := p.setSameLineCommentIfExists(ctx, key); err != nil {
+		return nil, errors.Wrapf(err, "failed to set same line comment to node")
+	}
+	if key.GetComment() != nil {
+		// if current token is comment, GetComment() is not nil.
+		// then progress to value token
+		ctx.progressIgnoreComment(1)
+	}
 	var value ast.Node
 	if vtk := ctx.currentToken(); vtk == nil {
 		value = ast.Null(token.New("null", "null", tk.Position))
@@ -138,15 +146,15 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 		Key:   key,
 		Value: value,
 	}
-	ntk := ctx.nextToken()
-	antk := ctx.afterNextToken()
+	ntk := ctx.nextNotCommentToken()
+	antk := ctx.afterNextNotCommentToken()
 	node := &ast.MappingNode{
 		Start:  tk,
 		Values: []*ast.MappingValueNode{mvnode},
 	}
 	for antk != nil && antk.Type == token.MappingValueType &&
 		ntk.Position.Column == key.GetToken().Position.Column {
-		ctx.progress(1)
+		ctx.progressIgnoreComment(1)
 		value, err := p.parseToken(ctx, ctx.currentToken())
 		if err != nil {
 			return nil, errors.Wrapf(err, "failed to parse mapping node")
@@ -154,7 +162,13 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 		switch value.Type() {
 		case ast.MappingType:
 			c := value.(*ast.MappingNode)
-			for _, v := range c.Values {
+			comment := c.GetComment()
+			for idx, v := range c.Values {
+				if idx == 0 && comment != nil {
+					if err := v.SetComment(comment); err != nil {
+						return nil, errors.Wrapf(err, "failed to set comment token to node")
+					}
+				}
 				node.Values = append(node.Values, v)
 			}
 		case ast.MappingValueType:
@@ -162,8 +176,8 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 		default:
 			return nil, xerrors.Errorf("failed to parse mapping value node node is %s", value.Type())
 		}
-		ntk = ctx.nextToken()
-		antk = ctx.afterNextToken()
+		ntk = ctx.nextNotCommentToken()
+		antk = ctx.afterNextNotCommentToken()
 	}
 	if len(node.Values) == 1 {
 		return mvnode, nil
@@ -185,7 +199,7 @@ func (p *parser) parseSequenceEntry(ctx *context) (ast.Node, error) {
 			return nil, errors.Wrapf(err, "failed to parse sequence")
 		}
 		sequenceNode.Values = append(sequenceNode.Values, value)
-		tk = ctx.nextToken()
+		tk = ctx.nextNotCommentToken()
 		if tk == nil {
 			break
 		}
@@ -195,7 +209,7 @@ func (p *parser) parseSequenceEntry(ctx *context) (ast.Node, error) {
 		if tk.Position.Column != curColumn {
 			break
 		}
-		ctx.progress(1)
+		ctx.progressIgnoreComment(1)
 	}
 	return sequenceNode, nil
 }
@@ -265,6 +279,20 @@ func (p *parser) parseStringValue(tk *token.Token) ast.Node {
 	return nil
 }
 
+func (p *parser) parseScalarValueWithComment(ctx *context, tk *token.Token) (ast.Node, error) {
+	node := p.parseScalarValue(tk)
+	if node == nil {
+		return nil, nil
+	}
+	if p.isSameLineComment(ctx.nextToken(), node) {
+		ctx.progress(1)
+		if err := p.setSameLineCommentIfExists(ctx, node); err != nil {
+			return nil, errors.Wrapf(err, "failed to set same line comment to node")
+		}
+	}
+	return node, nil
+}
+
 func (p *parser) parseScalarValue(tk *token.Token) ast.Node {
 	if node := p.parseStringValue(tk); node != nil {
 		return node
@@ -319,6 +347,27 @@ func (p *parser) parseLiteral(ctx *context) (ast.Node, error) {
 	return node, nil
 }
 
+func (p *parser) isSameLineComment(tk *token.Token, node ast.Node) bool {
+	if tk == nil {
+		return false
+	}
+	if tk.Type != token.CommentType {
+		return false
+	}
+	return tk.Position.Line == node.GetToken().Position.Line
+}
+
+func (p *parser) setSameLineCommentIfExists(ctx *context, node ast.Node) error {
+	tk := ctx.currentToken()
+	if !p.isSameLineComment(tk, node) {
+		return nil
+	}
+	if err := node.SetComment(tk); err != nil {
+		return errors.Wrapf(err, "failed to set comment token to ast.Node")
+	}
+	return nil
+}
+
 func (p *parser) parseDocument(ctx *context) (*ast.Document, error) {
 	node := &ast.Document{Start: ctx.currentToken()}
 	ctx.progress(1) // skip document header token
@@ -334,17 +383,60 @@ func (p *parser) parseDocument(ctx *context) (*ast.Document, error) {
 	return node, nil
 }
 
+func (p *parser) parseComment(ctx *context) (ast.Node, error) {
+	commentTokens := []*token.Token{}
+	for {
+		tk := ctx.currentToken()
+		if tk == nil {
+			break
+		}
+		if tk.Type != token.CommentType {
+			break
+		}
+		commentTokens = append(commentTokens, tk)
+		ctx.progressIgnoreComment(1) // skip comment token
+	}
+	// TODO: support token group. currently merge tokens to one token
+	firstToken := commentTokens[0]
+	values := []string{}
+	origins := []string{}
+	for _, tk := range commentTokens {
+		values = append(values, tk.Value)
+		origins = append(origins, tk.Origin)
+	}
+	firstToken.Value = strings.Join(values, "")
+	firstToken.Value = strings.Join(origins, "")
+	node, err := p.parseToken(ctx, ctx.currentToken())
+	if err != nil {
+		return nil, errors.Wrapf(err, "failed to parse node after comment")
+	}
+	if node == nil {
+		return ast.Comment(firstToken), nil
+	}
+	if err := node.SetComment(firstToken); err != nil {
+		return nil, errors.Wrapf(err, "failed to set comment token to node")
+	}
+	return node, nil
+}
+
 func (p *parser) parseToken(ctx *context, tk *token.Token) (ast.Node, error) {
 	if tk == nil {
 		return nil, nil
 	}
 	if tk.NextType() == token.MappingValueType {
-		return p.parseMappingValue(ctx)
+		node, err := p.parseMappingValue(ctx)
+		return node, err
 	}
-	if node := p.parseScalarValue(tk); node != nil {
+	node, err := p.parseScalarValueWithComment(ctx, tk)
+	if err != nil {
+		return nil, errors.Wrapf(err, "failed to parse scalar value")
+	}
+	if node != nil {
 		return node, nil
 	}
 	switch tk.Type {
+	case token.CommentType:
+		return p.parseComment(ctx)
 	case token.DocumentHeaderType:
 		return p.parseDocument(ctx)
 	case token.MappingStartType:
@@ -375,7 +467,7 @@ func (p *parser) parse(tokens token.Tokens, mode Mode) (*ast.File, error) {
 		if err != nil {
 			return nil, errors.Wrapf(err, "failed to parse")
 		}
-		ctx.progress(1)
+		ctx.progressIgnoreComment(1)
 		if node == nil {
 			continue
 		}


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index e08e208..88ccb8f 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -569,6 +569,62 @@ func TestSyntaxError(t *testing.T) {
 	}
 }
 
+func TestComment(t *testing.T) {
+	tests := []struct {
+		name string
+		yaml string
+	}{
+		{
+			name: "map with comment",
+			yaml: `
+# commentA
+a: #commentB
+  # commentC
+  b: c # commentD
+  # commentE
+  d: e # commentF
+  # commentG
+  f: g # commentH
+# commentI
+f: g # commentJ
+# commentK
+`,
+		},
+		{
+			name: "sequence with comment",
+			yaml: `
+# commentA
+- a # commentB
+# commentC
+- b: # commentD
+  # commentE
+  - d # commentF
+  - e # commentG
+# commentH
+`,
+		},
+		{
+			name: "anchor and alias",
+			yaml: `
+a: &x b # commentA
+c: *x # commentB
+`,
+		},
+	}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			f, err := parser.ParseBytes([]byte(test.yaml), parser.ParseComments)
+			if err != nil {
+				t.Fatalf("%+v", err)
+			}
+			var v Visitor
+			for _, doc := range f.Docs {
+				ast.Walk(&v, doc.Body)
+			}
+		})
+	}
+}
+
 type Visitor struct {
 }
 
@@ -576,5 +632,9 @@ func (v *Visitor) Visit(node ast.Node) ast.Visitor {
 	tk := node.GetToken()
 	tk.Prev = nil
 	tk.Next = nil
+	if comment := node.GetComment(); comment != nil {
+		comment.Prev = nil
+		comment.Next = nil
+	}
 	return v
 }