Browse Source

get comments/includes to save

pull/16/head
Brett Langdon 11 years ago
parent
commit
360cc938a2
2 changed files with 23 additions and 24 deletions
  1. +16
    -16
      config/section.go
  2. +7
    -8
      parser/parser.go

+ 16
- 16
config/section.go View File

@ -9,8 +9,8 @@ type SectionValue struct {
Includes []string Includes []string
} }
func NewNamedSection(name string) SectionValue {
return SectionValue{
func NewNamedSection(name string) *SectionValue {
return &SectionValue{
Name: name, Name: name,
Value: make(map[string]ConfigValue), Value: make(map[string]ConfigValue),
Comments: make([]string, 0), Comments: make([]string, 0),
@ -18,8 +18,8 @@ func NewNamedSection(name string) SectionValue {
} }
} }
func NewAnonymousSection() SectionValue {
return SectionValue{
func NewAnonymousSection() *SectionValue {
return &SectionValue{
Value: make(map[string]ConfigValue), Value: make(map[string]ConfigValue),
Comments: make([]string, 0), Comments: make([]string, 0),
Includes: make([]string, 0), Includes: make([]string, 0),
@ -29,48 +29,48 @@ func NewAnonymousSection() SectionValue {
func (this SectionValue) GetType() ConfigType { return SECTION } func (this SectionValue) GetType() ConfigType { return SECTION }
func (this SectionValue) GetValue() interface{} { return this.Value } func (this SectionValue) GetValue() interface{} { return this.Value }
func (this SectionValue) AddComment(comment string) {
func (this *SectionValue) AddComment(comment string) {
this.Comments = append(this.Comments, comment) this.Comments = append(this.Comments, comment)
} }
func (this SectionValue) AddInclude(include string) {
func (this *SectionValue) AddInclude(include string) {
this.Includes = append(this.Includes, include) this.Includes = append(this.Includes, include)
} }
func (this SectionValue) Set(name string, value ConfigValue) {
func (this *SectionValue) Set(name string, value ConfigValue) {
this.Value[name] = value this.Value[name] = value
} }
func (this SectionValue) Get(name string) ConfigValue {
func (this *SectionValue) Get(name string) ConfigValue {
return this.Value[name] return this.Value[name]
} }
func (this SectionValue) GetSection(name string) SectionValue {
func (this *SectionValue) GetSection(name string) SectionValue {
value := this.Value[name] value := this.Value[name]
return value.(SectionValue) return value.(SectionValue)
} }
func (this SectionValue) GetString(name string) StringValue {
func (this *SectionValue) GetString(name string) StringValue {
value := this.Value[name] value := this.Value[name]
return value.(StringValue) return value.(StringValue)
} }
func (this SectionValue) GetInteger(name string) IntegerValue {
func (this *SectionValue) GetInteger(name string) IntegerValue {
value := this.Value[name] value := this.Value[name]
return value.(IntegerValue) return value.(IntegerValue)
} }
func (this SectionValue) GetFloat(name string) FloatValue {
func (this *SectionValue) GetFloat(name string) FloatValue {
value := this.Value[name] value := this.Value[name]
return value.(FloatValue) return value.(FloatValue)
} }
func (this SectionValue) Contains(name string) bool {
func (this *SectionValue) Contains(name string) bool {
_, ok := this.Value[name] _, ok := this.Value[name]
return ok return ok
} }
func (this SectionValue) ToJSON() ([]byte, error) {
func (this *SectionValue) ToJSON() ([]byte, error) {
data, err := this.ToMap() data, err := this.ToMap()
if err != nil { if err != nil {
return nil, err return nil, err
@ -78,11 +78,11 @@ func (this SectionValue) ToJSON() ([]byte, error) {
return json.Marshal(data) return json.Marshal(data)
} }
func (this SectionValue) ToMap() (map[string]interface{}, error) {
func (this *SectionValue) ToMap() (map[string]interface{}, error) {
settings := make(map[string]interface{}) settings := make(map[string]interface{})
for name, value := range this.Value { for name, value := range this.Value {
if value.GetType() == SECTION { if value.GetType() == SECTION {
data, err := value.(SectionValue).ToMap()
data, err := value.(*SectionValue).ToMap()
if err != nil { if err != nil {
return nil, err return nil, err
} }


+ 7
- 8
parser/parser.go View File

@ -14,11 +14,11 @@ import (
) )
type Parser struct { type Parser struct {
settings config.SectionValue
settings *config.SectionValue
tokenizer *token.Tokenizer tokenizer *token.Tokenizer
cur_tok token.Token cur_tok token.Token
cur_section config.SectionValue
previous []config.SectionValue
cur_section *config.SectionValue
previous []*config.SectionValue
} }
func (this *Parser) SyntaxError(msg string) error { func (this *Parser) SyntaxError(msg string) error {
@ -57,7 +57,7 @@ func (this *Parser) readToken() token.Token {
return this.cur_tok return this.cur_tok
} }
func (this *Parser) parseReference(starting_section config.SectionValue, period bool) (config.ConfigValue, error) {
func (this *Parser) parseReference(starting_section *config.SectionValue, period bool) (config.ConfigValue, error) {
names := []string{} names := []string{}
if period == false { if period == false {
names = append(names, this.cur_tok.Literal) names = append(names, this.cur_tok.Literal)
@ -98,7 +98,7 @@ func (this *Parser) parseReference(starting_section config.SectionValue, period
} }
name := names[0] name := names[0]
names = names[1:] names = names[1:]
section := reference.(config.SectionValue)
section := reference.(*config.SectionValue)
if section.Contains(name) == false { if section.Contains(name) == false {
return nil, this.ReferenceMissingError(visited, name) return nil, this.ReferenceMissingError(visited, name)
} }
@ -212,7 +212,6 @@ func (this *Parser) parseInclude() error {
this.tokenizer = token.NewTokenizer(reader) this.tokenizer = token.NewTokenizer(reader)
this.Parse() this.Parse()
} }
fmt.Println(this.cur_section.Includes)
this.tokenizer = old_tokenizer this.tokenizer = old_tokenizer
this.readToken() this.readToken()
return nil return nil
@ -290,7 +289,7 @@ func ParseReader(reader io.Reader) (*config.SectionValue, error) {
tokenizer: token.NewTokenizer(reader), tokenizer: token.NewTokenizer(reader),
settings: settings, settings: settings,
cur_section: settings, cur_section: settings,
previous: make([]config.SectionValue, 0),
previous: make([]*config.SectionValue, 0),
} }
err := parser.Parse() err := parser.Parse()
if err != nil { if err != nil {
@ -301,5 +300,5 @@ func ParseReader(reader io.Reader) (*config.SectionValue, error) {
return nil, parser.SyntaxError("expected end of section, instead found EOF") return nil, parser.SyntaxError("expected end of section, instead found EOF")
} }
return &settings, nil
return settings, nil
} }

Loading…
Cancel
Save