mirror of
				https://github.com/goccy/go-yaml.git
				synced 2025-10-31 13:21:07 +00:00 
			
		
		
		
	Support parsing with comment
This commit is contained in:
		
							parent
							
								
									05f465a371
								
							
						
					
					
						commit
						b3d4080492
					
				
					 4 changed files with 461 additions and 41 deletions
				
			
		|  | @ -569,6 +569,30 @@ func TestSyntaxError(t *testing.T) { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestComment(t *testing.T) { | ||||
| 	yml := ` | ||||
| # commentA | ||||
| a: #commentB | ||||
|   # commentC | ||||
|   b: c # commentD | ||||
|   # commentE | ||||
|   d: e # commentF | ||||
|   # commentG | ||||
|   f: g # commentH | ||||
| # commentI | ||||
| f: g # commentJ | ||||
| # commentK | ||||
| ` | ||||
| 	f, err := parser.ParseBytes([]byte(yml), 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 +600,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 | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Masaaki Goshima
						Masaaki Goshima