Browse Source

add support for negative numbers

fixes #7
pull/16/head
Brett Langdon 11 years ago
parent
commit
acba54d84d
3 changed files with 15 additions and 5 deletions
  1. +2
    -1
      example/example.cfg
  2. +3
    -2
      forge.go
  3. +10
    -2
      scanner.go

+ 2
- 1
example/example.cfg View File

@ -5,8 +5,9 @@ primary {
string = "primary string value";
integer = 500;
float = 80.80;
negative = -50;
boolean = true;
negative = FALSE;
not_true = FALSE;
nothing = NULL;
# Include external files
include "./*.cfg";


+ 3
- 2
forge.go View File

@ -26,11 +26,12 @@
// Config file format:
//
// IDENTIFIER: [_a-zA-Z]+
// NUMBERS: [0-9]+
//
// BOOL: 'true' | 'false'
// NULL: 'null'
// INTEGER: [0-9]+
// FLOAT: INTEGER '.' INTEGER
// INTEGER: NUMBERS | ('-' NUMBERS)
// FLOAT: (NUMBERS '.' NUMBERS) | ('-' NUMBERS . NUMBERS)
// STRING: '"' .* '"'
// REFERENCE: [IDENTIFIER] ('.' IDENTIFIER)+
// VALUE: BOOL | NULL | INTEGER | FLOAT | STRING | REFERENCE


+ 10
- 2
scanner.go View File

@ -100,9 +100,13 @@ func (scanner *Scanner) parseIdentifier() {
}
}
func (scanner *Scanner) parseNumber() {
func (scanner *Scanner) parseNumber(negative bool) {
scanner.curTok.ID = token.INTEGER
scanner.curTok.Literal = string(scanner.curCh)
if negative {
scanner.curTok.Literal = "-" + scanner.curTok.Literal
}
digit := false
for {
scanner.readRune()
@ -168,7 +172,7 @@ func (scanner *Scanner) NextToken() token.Token {
case isLetter(ch) || ch == '_':
scanner.parseIdentifier()
case isDigit(ch):
scanner.parseNumber()
scanner.parseNumber(false)
case ch == '#':
scanner.parseComment()
case ch == eof:
@ -190,6 +194,10 @@ func (scanner *Scanner) NextToken() token.Token {
scanner.curTok.ID = token.SEMICOLON
case '.':
scanner.curTok.ID = token.PERIOD
case '-':
if isDigit(scanner.curCh) {
scanner.parseNumber(true)
}
}
}


Loading…
Cancel
Save