diff --git a/README.md b/README.md index 69dd173..e7c19a4 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,14 @@ Forge is a configuration syntax and parser. The format was influenced a lot by nginx configuration file format. ```config +# Global settings global_key = "string value"; + +# Sub section sub_settings { sub_int = 500; sub_float = 80.80; + # Sub-Sub Section sub_sub_settings { sub_sub_sub_settings { key = "value"; @@ -23,10 +27,11 @@ sub_settings { } } +# Second section second { key = "value"; global_reference = sub_settings.sub_float; - local_reference = .key; + local_reference = .key; # References second.key } ``` @@ -36,6 +41,8 @@ For normal settings the format is the key followed by an equal sign followed by Sections (basically a map) is formatted as the section name with the section's settings wrapped in brackets. `
{ = ; }` +Comments start with a pound sign `#` and end with a newline. A comment can exist on the same line as settings/sections, but the comment must end the line. + ## Data types ### Boolean @@ -170,11 +177,6 @@ repo_url = domain + "/" + username + "/" + name; I'll probably revisit the API, I just threw it together quick, want to make sure it right. - -### Support for comments - -This is pretty lacking and should be added soon. - ### Documentation Documentation is a good thing. diff --git a/config/section.go b/config/section.go index 6660408..44db07f 100644 --- a/config/section.go +++ b/config/section.go @@ -3,13 +3,18 @@ package config import "encoding/json" type SectionValue struct { - Name string - Value map[string]ConfigValue + Name string + Value map[string]ConfigValue + Comments []string } func (this SectionValue) GetType() ConfigType { return SECTION } func (this SectionValue) GetValue() interface{} { return this.Value } +func (this SectionValue) AddComment(comment string) { + this.Comments = append(this.Comments, comment) +} + func (this SectionValue) Set(name string, value ConfigValue) { this.Value[name] = value } diff --git a/example/example.cfg b/example/example.cfg index d655528..362a18b 100644 --- a/example/example.cfg +++ b/example/example.cfg @@ -1,19 +1,23 @@ +# Global stuff global = "global value"; -master { - string = "master string value"; +# Primary stuff +primary { + string = "primary string value"; integer = 500; float = 80.80; boolean = true; negative = FALSE; nothing = NULL; + # Primary-sub stuff sub { - key = "master sub key value"; + key = "primary sub key value"; } } -slave { - another = "slave another value"; +secondary { + another = "secondary another value"; global_reference = global; - master_sub_key = master.sub.key; + primary_sub_key = primary.sub.key; + another_again = .another; # References secondary.another _under = 50; } diff --git a/parser/parser.go b/parser/parser.go index aa9adb8..bd98bd6 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -186,8 +186,9 @@ func (this *Parser) parseSetting(name string) error { func (this *Parser) parseSection(name string) error { section := config.SectionValue{ - Name: name, - Value: make(map[string]config.ConfigValue), + Name: name, + Value: make(map[string]config.ConfigValue), + Comments: make([]string, 0), } this.cur_section.Set(name, section) this.previous = append(this.previous, this.cur_section) @@ -216,6 +217,8 @@ func (this *Parser) Parse() error { tok := this.cur_tok this.readToken() switch tok.ID { + case token.COMMENT: + this.cur_section.AddComment(tok.Literal) case token.IDENTIFIER: if this.cur_tok.ID == token.LBRACKET { err := this.parseSection(tok.Literal) @@ -251,7 +254,8 @@ func ParseFile(filename string) (settings *config.SectionValue, err error) { func ParseReader(reader io.Reader) (*config.SectionValue, error) { settings := config.SectionValue{ - Value: make(map[string]config.ConfigValue), + Value: make(map[string]config.ConfigValue), + Comments: make([]string, 0), } parser := &Parser{ tokenizer: token.NewTokenizer(reader), diff --git a/token/tokenid.go b/token/tokenid.go index 694b2ad..a2b711f 100644 --- a/token/tokenid.go +++ b/token/tokenid.go @@ -18,6 +18,7 @@ const ( FLOAT STRING NULL + COMMENT ) var tokenNames = [...]string{ @@ -34,6 +35,7 @@ var tokenNames = [...]string{ FLOAT: "FLOAT", STRING: "STRING", NULL: "NULL", + COMMENT: "COMMENT", } func (this TokenID) String() string { diff --git a/token/tokenizer.go b/token/tokenizer.go index af3bfcf..7ad2ef4 100644 --- a/token/tokenizer.go +++ b/token/tokenizer.go @@ -118,6 +118,19 @@ func (this *Tokenizer) parseString() { this.readRune() } +func (this *Tokenizer) parseComment() { + this.cur_tok.ID = COMMENT + this.cur_tok.Literal = "" + for { + this.readRune() + if this.cur_ch == '\n' { + break + } + this.cur_tok.Literal += string(this.cur_ch) + } + this.readRune() +} + func (this *Tokenizer) skipWhitespace() { for { this.readRune() @@ -144,6 +157,8 @@ func (this *Tokenizer) NextToken() Token { this.parseIdentifier() case isDigit(ch): this.parseNumber() + case ch == '#': + this.parseComment() case ch == eof: this.cur_tok.ID = EOF this.cur_tok.Literal = "EOF"