diff --git a/example/example.cfg b/example/example.cfg index d956b5e..c4e7a57 100644 --- a/example/example.cfg +++ b/example/example.cfg @@ -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"; diff --git a/forge.go b/forge.go index 6bc4fa7..c5a3ab1 100644 --- a/forge.go +++ b/forge.go @@ -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 diff --git a/scanner.go b/scanner.go index 89e0372..eb7c0b9 100644 --- a/scanner.go +++ b/scanner.go @@ -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) + } } }