diff --git a/README.md b/README.md index f49f5df..69dd173 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ A boolean value is either `true` or `false` of any case. `TRUE`, `true`, `True`, `FALSE`, `False`, `false`. +### Null + +A null value is allowed as `null` of any case. + +`NULL`, `Null`, `null`. + ### String A string value is wrapped by double quotes (single quotes will not work). diff --git a/config/config.go b/config/config.go index 4742682..e3e5d83 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ const ( BOOLEAN FLOAT STRING + NULL ) var configTypes = [...]string{ @@ -16,6 +17,7 @@ var configTypes = [...]string{ INTEGER: "INTEGER", FLOAT: "FLOAT", STRING: "STRING", + NULL: "NULL", } func (this ConfigType) String() string { @@ -44,6 +46,14 @@ type BooleanValue struct { func (this BooleanValue) GetType() ConfigType { return BOOLEAN } func (this BooleanValue) GetValue() interface{} { return this.Value } +type NullValue struct { + Name string + Value interface{} +} + +func (this NullValue) GetType() ConfigType { return NULL } +func (this NullValue) GetValue() interface{} { return nil } + type IntegerValue struct { Name string Value int64 diff --git a/example/example.cfg b/example/example.cfg index 239202a..d655528 100644 --- a/example/example.cfg +++ b/example/example.cfg @@ -5,6 +5,7 @@ master { float = 80.80; boolean = true; negative = FALSE; + nothing = NULL; sub { key = "master sub key value"; } diff --git a/parser/parser.go b/parser/parser.go index 5ee4636..64ae202 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -128,6 +128,11 @@ func (this *Parser) parseSetting(name string) error { Name: name, Value: bool_val, } + case token.NULL: + value = config.NullValue{ + Name: name, + Value: nil, + } case token.INTEGER: int_val, err := strconv.ParseInt(this.cur_tok.Literal, 10, 64) if err != nil { diff --git a/token/tokenid.go b/token/tokenid.go index 2b8a920..694b2ad 100644 --- a/token/tokenid.go +++ b/token/tokenid.go @@ -17,6 +17,7 @@ const ( INTEGER FLOAT STRING + NULL ) var tokenNames = [...]string{ @@ -32,6 +33,7 @@ var tokenNames = [...]string{ INTEGER: "INTEGER", FLOAT: "FLOAT", STRING: "STRING", + NULL: "NULL", } func (this TokenID) String() string { diff --git a/token/tokenizer.go b/token/tokenizer.go index 988ee28..af3bfcf 100644 --- a/token/tokenizer.go +++ b/token/tokenizer.go @@ -25,6 +25,10 @@ func isBoolean(str string) bool { return lower == "true" || lower == "false" } +func isNull(str string) bool { + return strings.ToLower(str) == "null" +} + type Tokenizer struct { cur_line int cur_col int @@ -80,6 +84,8 @@ func (this *Tokenizer) parseIdentifier() { if isBoolean(this.cur_tok.Literal) { this.cur_tok.ID = BOOLEAN + } else if isNull(this.cur_tok.Literal) { + this.cur_tok.ID = NULL } }