Browse Source

add support for comments

pull/16/head
Brett Langdon 11 years ago
parent
commit
17ba1aaaf6
6 changed files with 49 additions and 17 deletions
  1. +8
    -6
      README.md
  2. +7
    -2
      config/section.go
  3. +10
    -6
      example/example.cfg
  4. +7
    -3
      parser/parser.go
  5. +2
    -0
      token/tokenid.go
  6. +15
    -0
      token/tokenizer.go

+ 8
- 6
README.md View File

@ -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.
`<section> { <key> = <value>; }`
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.

+ 7
- 2
config/section.go View File

@ -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
}


+ 10
- 6
example/example.cfg View File

@ -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;
}

+ 7
- 3
parser/parser.go View File

@ -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),


+ 2
- 0
token/tokenid.go View File

@ -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 {


+ 15
- 0
token/tokenizer.go View File

@ -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"


Loading…
Cancel
Save