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 (
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


+ 2
- 0
example/example.cfg View File

@ -3,6 +3,8 @@ master {
string = "master string value";
integer = 500;
float = 80.80;
boolean = true;
negative = FALSE;
sub {
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,
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 {


+ 2
- 0
token/tokenid.go View File

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


+ 10
- 0
token/tokenizer.go View File

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


Loading…
Cancel
Save