Browse Source

Merge pull request #16 from brettlangdon/dev/escape.quotes

Allow escaped double quotes in strings
pull/21/head
Brett Langdon 11 years ago
parent
commit
c6b8488ade
3 changed files with 25 additions and 2 deletions
  1. +2
    -0
      forge_test.go
  2. +22
    -2
      scanner.go
  3. +1
    -0
      test.cfg

+ 2
- 0
forge_test.go View File

@ -14,6 +14,7 @@ global = "global value";
# Primary stuff
primary {
string = "primary string value";
string_with_quote = "some \"quoted\" str\\ing";
integer = 500;
float = 80.80;
negative = -50;
@ -54,6 +55,7 @@ func assertDirectives(values map[string]interface{}, t *testing.T) {
// Primary
primary := values["primary"].(map[string]interface{})
assertEqual(primary["string"], "primary string value", t)
assertEqual(primary["string_with_quote"], "some \"quoted\" str\\ing", t)
assertEqual(primary["integer"], int64(500), t)
assertEqual(primary["float"], float64(80.80), t)
assertEqual(primary["negative"], int64(-50), t)


+ 22
- 2
scanner.go View File

@ -14,6 +14,10 @@ func isLetter(ch rune) bool {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
}
func isEscapeCharacter(ch rune) bool {
return ch == '\\' || ch == '"'
}
func isDigit(ch rune) bool {
return ('0' <= ch && ch <= '9')
}
@ -123,10 +127,26 @@ func (scanner *Scanner) parseNumber(negative bool) {
func (scanner *Scanner) parseString() {
scanner.curTok.ID = token.STRING
scanner.curTok.Literal = string(scanner.curCh)
// Whether or not we are trying to escape a character
escape := false
for {
scanner.readRune()
if scanner.curCh == '"' {
break
if escape == false {
// We haven't seen a backslash yet
if scanner.curCh == '\\' {
// A new backslash
escape = true
continue
} else if scanner.curCh == '"' {
// An unescaped quote, time to bail out
break
}
} else if isEscapeCharacter(scanner.curCh) {
// A valid escape character, continue as normal
escape = false
} else {
// We had a backslash, but an invalid escape character
// TODO: "Unsupported escape character found"
}
scanner.curTok.Literal += string(scanner.curCh)
}


+ 1
- 0
test.cfg View File

@ -3,6 +3,7 @@ global = "global value";
# Primary stuff
primary {
string = "primary string value";
string_with_quote = "some \"quoted\" str\\ing";
integer = 500;
float = 80.80;
negative = -50;


Loading…
Cancel
Save