summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Burke <rich.g.burke@gmail.com>2018-12-29 16:58:05 +0000
committerRichard Burke <rich.g.burke@gmail.com>2018-12-29 16:58:05 +0000
commit5e2d226c081300d1fcadc40504be89e2ae192b5c (patch)
tree85770c8f9d7fbb6def1fe2b81c3505a0c0c34091
parenta7e2e1a003dcf3e44a784d81c39cffd33965d056 (diff)
Def command parsing improvements
-rw-r--r--cmd/grv/config_parse.go29
-rw-r--r--cmd/grv/config_parse_test.go14
2 files changed, 36 insertions, 7 deletions
diff --git a/cmd/grv/config_parse.go b/cmd/grv/config_parse.go
index 1aec5d8..46dc238 100644
--- a/cmd/grv/config_parse.go
+++ b/cmd/grv/config_parse.go
@@ -34,6 +34,15 @@ const (
)
var isIdentifier = regexp.MustCompile(`[[:alnum:]]+`).MatchString
+var whiteSpaceTokens = map[ConfigTokenType]bool{
+ CtkWhiteSpace: true,
+ CtkComment: true,
+}
+var whiteSpaceAndTerminatorTokens = map[ConfigTokenType]bool{
+ CtkWhiteSpace: true,
+ CtkComment: true,
+ CtkTerminator: true,
+}
type commandConstructor func(parser *ConfigParser, commandToken *ConfigToken, tokens []*ConfigToken) (ConfigCommand, error)
@@ -355,14 +364,12 @@ func (parser *ConfigParser) InputSource() string {
return parser.inputSource
}
-func (parser *ConfigParser) scan() (token *ConfigToken, err error) {
+func (parser *ConfigParser) scanAndIgnore(ignoreTokens map[ConfigTokenType]bool) (token *ConfigToken, err error) {
for {
token, err = parser.scanner.Scan()
if err != nil {
return
- }
-
- if token.tokenType != CtkWhiteSpace && token.tokenType != CtkComment {
+ } else if _, ignore := ignoreTokens[token.tokenType]; !ignore {
break
}
}
@@ -370,6 +377,14 @@ func (parser *ConfigParser) scan() (token *ConfigToken, err error) {
return
}
+func (parser *ConfigParser) scan() (token *ConfigToken, err error) {
+ return parser.scanAndIgnore(whiteSpaceTokens)
+}
+
+func (parser *ConfigParser) scanIgnoringTerminators() (token *ConfigToken, err error) {
+ return parser.scanAndIgnore(whiteSpaceAndTerminatorTokens)
+}
+
func (parser *ConfigParser) scanRaw() (token *ConfigToken, err error) {
return parser.scanner.Scan()
}
@@ -478,7 +493,7 @@ OuterLoop:
}
func parseDefCommand(parser *ConfigParser) (tokens []*ConfigToken, err error) {
- commandNameToken, err := parser.scan()
+ commandNameToken, err := parser.scanIgnoringTerminators()
if err != nil {
return
} else if commandNameToken.err != nil {
@@ -494,7 +509,7 @@ func parseDefCommand(parser *ConfigParser) (tokens []*ConfigToken, err error) {
tokens = append(tokens, commandNameToken)
- openingBraceToken, err := parser.scan()
+ openingBraceToken, err := parser.scanIgnoringTerminators()
if err != nil {
return
} else if openingBraceToken.err != nil {
@@ -656,7 +671,7 @@ func defCommandConstructor(parser *ConfigParser, commandToken *ConfigToken, toke
var functionBodyBuffer bytes.Buffer
for i := 2; i < len(tokens)-1; i++ {
- functionBodyBuffer.WriteString(tokens[i].value)
+ functionBodyBuffer.WriteString(tokens[i].rawValue)
}
functionBody := functionBodyBuffer.String()
diff --git a/cmd/grv/config_parse_test.go b/cmd/grv/config_parse_test.go
index 22e2338..0a0bfd2 100644
--- a/cmd/grv/config_parse_test.go
+++ b/cmd/grv/config_parse_test.go
@@ -399,6 +399,20 @@ func TestParseSingleCommand(t *testing.T) {
functionBody: " ",
},
},
+ {
+ input: "def myFunc { addtab \\\n\t\"Test Tab\" }",
+ expectedCommand: &DefCommandValues{
+ commandName: "myFunc",
+ functionBody: " addtab \\\n\t\"Test Tab\" ",
+ },
+ },
+ {
+ input: "def\n myFunc \n{ addtab Main }",
+ expectedCommand: &DefCommandValues{
+ commandName: "myFunc",
+ functionBody: " addtab Main ",
+ },
+ },
}
for _, singleCommandTest := range singleCommandTests {