dockerscript: support '#' line comments

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-03-27 15:54:54 -07:00
parent 1a3c8051c5
commit 99bbfb108d

View file

@ -12,8 +12,13 @@ type Command struct {
Children []*Command Children []*Command
} }
type Scanner struct {
scanner.Scanner
commentLine bool
}
func Parse(src io.Reader) ([]*Command, error) { func Parse(src io.Reader) ([]*Command, error) {
s := &scanner.Scanner{} s := &Scanner{}
s.Init(src) s.Init(src)
s.Whitespace = 1<<'\t' | 1<<' ' s.Whitespace = 1<<'\t' | 1<<' '
s.Mode = scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanIdents s.Mode = scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanIdents
@ -45,7 +50,7 @@ func (cmd *Command) String() string {
return cmd.subString(0) return cmd.subString(0)
} }
func parseArgs(s *scanner.Scanner) ([]string, rune, error) { func parseArgs(s *Scanner) ([]string, rune, error) {
var parseError error var parseError error
// FIXME: we overwrite previously set error // FIXME: we overwrite previously set error
s.Error = func(s *scanner.Scanner, msg string) { s.Error = func(s *scanner.Scanner, msg string) {
@ -59,16 +64,25 @@ func parseArgs(s *scanner.Scanner) ([]string, rune, error) {
return args, tok, parseError return args, tok, parseError
} }
text := s.TokenText() text := s.TokenText()
// Toggle line comment
if strings.HasPrefix(text, "#") {
s.commentLine = true
} else if text == "\n" || text == "\r" {
s.commentLine = false
return args, tok, nil
}
if !s.commentLine {
if text == "{" || text == "}" || text == "\n" || text == "\r" || text == ";" { if text == "{" || text == "}" || text == "\n" || text == "\r" || text == ";" {
return args, tok, nil return args, tok, nil
} }
args = append(args, text) args = append(args, text)
}
tok = s.Scan() tok = s.Scan()
} }
return args, tok, nil return args, tok, nil
} }
func parse(s *scanner.Scanner, opener string) (expr []*Command, err error) { func parse(s *Scanner, opener string) (expr []*Command, err error) {
/* /*
defer func() { defer func() {
fmt.Printf("parse() returned %d commands:\n", len(expr)) fmt.Printf("parse() returned %d commands:\n", len(expr))