Browse Source

add boolean data type

pull/16/head
Brett Langdon 11 years ago
parent
commit
e649185b76
5 changed files with 33 additions and 0 deletions
  1. +10
    -0
      config/config.go
  2. +2
    -0
      example/example.cfg
  3. +9
    -0
      parser/parser.go
  4. +2
    -0
      token/tokenid.go
  5. +10
    -0
      token/tokenizer.go

+ 10
- 0
config/config.go View File

@ -5,12 +5,14 @@ type ConfigType int
const ( const (
SECTION ConfigType = iota SECTION ConfigType = iota
INTEGER INTEGER
BOOLEAN
FLOAT FLOAT
STRING STRING
) )
var configTypes = [...]string{ var configTypes = [...]string{
SECTION: "SECTION", SECTION: "SECTION",
BOOLEAN: "BOOLEAN",
INTEGER: "INTEGER", INTEGER: "INTEGER",
FLOAT: "FLOAT", FLOAT: "FLOAT",
STRING: "STRING", STRING: "STRING",
@ -34,6 +36,14 @@ type ConfigValue interface {
GetValue() 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 { type IntegerValue struct {
Name string Name string
Value int64 Value int64


+ 2
- 0
example/example.cfg View File

@ -3,6 +3,8 @@ master {
string = "master string value"; string = "master string value";
integer = 500; integer = 500;
float = 80.80; float = 80.80;
boolean = true;
negative = FALSE;
sub { sub {
key = "master sub key value"; key = "master sub key value";
} }


+ 9
- 0
parser/parser.go View File

@ -119,6 +119,15 @@ func (this *Parser) parseSetting(name string) error {
Name: name, Name: name,
Value: this.cur_tok.Literal, 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: case token.INTEGER:
int_val, err := strconv.ParseInt(this.cur_tok.Literal, 10, 64) int_val, err := strconv.ParseInt(this.cur_tok.Literal, 10, 64)
if err != nil { if err != nil {


+ 2
- 0
token/tokenid.go View File

@ -13,6 +13,7 @@ const (
PERIOD PERIOD
IDENTIFIER IDENTIFIER
BOOLEAN
INTEGER INTEGER
FLOAT FLOAT
STRING STRING
@ -27,6 +28,7 @@ var tokenNames = [...]string{
SEMICOLON: "SEMICOLON", SEMICOLON: "SEMICOLON",
PERIOD: "PERIOD", PERIOD: "PERIOD",
IDENTIFIER: "IDENTIFIER", IDENTIFIER: "IDENTIFIER",
BOOLEAN: "BOOLEAN",
INTEGER: "INTEGER", INTEGER: "INTEGER",
FLOAT: "FLOAT", FLOAT: "FLOAT",
STRING: "STRING", STRING: "STRING",


+ 10
- 0
token/tokenizer.go View File

@ -3,6 +3,7 @@ package token
import ( import (
"bufio" "bufio"
"io" "io"
"strings"
) )
var eof = rune(0) var eof = rune(0)
@ -19,6 +20,11 @@ func isWhitespace(ch rune) bool {
return (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') 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 { type Tokenizer struct {
cur_line int cur_line int
cur_col int cur_col int
@ -71,6 +77,10 @@ func (this *Tokenizer) parseIdentifier() {
} }
this.cur_tok.Literal += string(this.cur_ch) this.cur_tok.Literal += string(this.cur_ch)
} }
if isBoolean(this.cur_tok.Literal) {
this.cur_tok.ID = BOOLEAN
}
} }
func (this *Tokenizer) parseNumber() { func (this *Tokenizer) parseNumber() {


Loading…
Cancel
Save