This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cfg_reader | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
type Emitter struct { | ||
Stream string | ||
} | ||
|
||
func NewEmitter() *Emitter { | ||
return &Emitter{Stream: ""} | ||
} | ||
|
||
func (e *Emitter) Emit(node *Node, indent uint, isLast bool, useCommas bool, useApostrophes bool) error { | ||
_indent := strings.Repeat(" ", int(indent * 2)) | ||
|
||
if node.IsScalar() { | ||
str, ok := node.ToString() | ||
if !ok { | ||
return errors.New("failed to emit") | ||
} | ||
specialChars := strings.Count(str, " ") | ||
if useApostrophes || specialChars > 0 { | ||
e.Stream += "'" + Escape(str) + "'\n" | ||
} else { | ||
e.Stream += Escape(str) + "\n" | ||
} | ||
} else if node.IsList() { | ||
e.Stream += "[\n" | ||
|
||
list, ok := node.ToList() | ||
if !ok { | ||
return errors.New("failed to emit list") | ||
} | ||
end := len(list) - 1 | ||
for i, node := range list { | ||
e.Stream += _indent | ||
e.Emit(node, indent + 1, i == end, useCommas, useApostrophes) | ||
} | ||
e.Stream += strings.Repeat(" ", int((indent - 1) * 2)) | ||
if isLast || !useCommas { | ||
e.Stream += "]\n" | ||
} else { | ||
e.Stream += "],\n" | ||
} | ||
} else if node.IsDict() { | ||
if indent > 0 { | ||
e.Stream += "{\n" | ||
} | ||
|
||
dict, ok := node.ToDict() | ||
if !ok { | ||
return errors.New("failed to emit dict") | ||
} | ||
i := 0 | ||
endIdx := len(dict) - 1 | ||
for key, node := range dict { | ||
if node.IsNone() { | ||
continue | ||
} | ||
e.Stream += _indent + key + ":" | ||
e.Emit(node, indent + 1, i == endIdx, useCommas, useApostrophes) | ||
i++ | ||
} | ||
|
||
if indent > 0 { | ||
e.Stream += strings.Repeat(" ", int((indent - 1) * 2)) | ||
if isLast || !useCommas { | ||
e.Stream += "}\n" | ||
} else { | ||
e.Stream += "},\n" | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *Emitter) String() string { | ||
return e.Stream | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters