From e649185b76ff2e5cd8d5e7bb19f92dbb65180564 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Mon, 15 Jun 2015 22:19:31 -0400 Subject: [PATCH] add boolean data type --- config/config.go | 10 ++++++++++ example/example.cfg | 2 ++ parser/parser.go | 9 +++++++++ token/tokenid.go | 2 ++ token/tokenizer.go | 10 ++++++++++ 5 files changed, 33 insertions(+) diff --git a/config/config.go b/config/config.go index 57c9874..4742682 100644 --- a/config/config.go +++ b/config/config.go @@ -5,12 +5,14 @@ type ConfigType int const ( SECTION ConfigType = iota INTEGER + BOOLEAN FLOAT STRING ) var configTypes = [...]string{ SECTION: "SECTION", + BOOLEAN: "BOOLEAN", INTEGER: "INTEGER", FLOAT: "FLOAT", STRING: "STRING", @@ -34,6 +36,14 @@ type ConfigValue interface { GetValue() interface{} } +type BooleanValue struct { + Name string + Value bool +} + +func (this BooleanValue) GetType() ConfigType { return BOOLEAN } +func (this BooleanValue) GetValue() interface{} { return this.Value } + type IntegerValue struct { Name string Value int64 diff --git a/example/example.cfg b/example/example.cfg index b3cb8db..239202a 100644 --- a/example/example.cfg +++ b/example/example.cfg @@ -3,6 +3,8 @@ master { string = "master string value"; integer = 500; float = 80.80; + boolean = true; + negative = FALSE; sub { key = "master sub key value"; } diff --git a/parser/parser.go b/parser/parser.go index d4f085a..5ee4636 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -119,6 +119,15 @@ func (this *Parser) parseSetting(name string) error { Name: name, Value: this.cur_tok.Literal, } + case token.BOOLEAN: + bool_val, err := strconv.ParseBool(this.cur_tok.Literal) + if err != nil { + return nil + } + value = config.BooleanValue{ + Name: name, + Value: bool_val, + } 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 f630818..2b8a920 100644 --- a/token/tokenid.go +++ b/token/tokenid.go @@ -13,6 +13,7 @@ const ( PERIOD IDENTIFIER + BOOLEAN INTEGER FLOAT STRING @@ -27,6 +28,7 @@ var tokenNames = [...]string{ SEMICOLON: "SEMICOLON", PERIOD: "PERIOD", IDENTIFIER: "IDENTIFIER", + BOOLEAN: "BOOLEAN", INTEGER: "INTEGER", FLOAT: "FLOAT", STRING: "STRING", diff --git a/token/tokenizer.go b/token/tokenizer.go index d9b1f55..988ee28 100644 --- a/token/tokenizer.go +++ b/token/tokenizer.go @@ -3,6 +3,7 @@ package token import ( "bufio" "io" + "strings" ) var eof = rune(0) @@ -19,6 +20,11 @@ func isWhitespace(ch rune) bool { return (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') } +func isBoolean(str string) bool { + lower := strings.ToLower(str) + return lower == "true" || lower == "false" +} + type Tokenizer struct { cur_line int cur_col int @@ -71,6 +77,10 @@ func (this *Tokenizer) parseIdentifier() { } this.cur_tok.Literal += string(this.cur_ch) } + + if isBoolean(this.cur_tok.Literal) { + this.cur_tok.ID = BOOLEAN + } } func (this *Tokenizer) parseNumber() {