Browse code

dockerscript: support '#' line comments

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)

Solomon Hykes authored on 2014/03/28 07:54:54
Showing 1 changed files
... ...
@@ -12,8 +12,13 @@ type Command struct {
12 12
 	Children []*Command
13 13
 }
14 14
 
15
+type Scanner struct {
16
+	scanner.Scanner
17
+	commentLine bool
18
+}
19
+
15 20
 func Parse(src io.Reader) ([]*Command, error) {
16
-	s := &scanner.Scanner{}
21
+	s := &Scanner{}
17 22
 	s.Init(src)
18 23
 	s.Whitespace = 1<<'\t' | 1<<' '
19 24
 	s.Mode = scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanIdents
... ...
@@ -45,7 +50,7 @@ func (cmd *Command) String() string {
45 45
 	return cmd.subString(0)
46 46
 }
47 47
 
48
-func parseArgs(s *scanner.Scanner) ([]string, rune, error) {
48
+func parseArgs(s *Scanner) ([]string, rune, error) {
49 49
 	var parseError error
50 50
 	// FIXME: we overwrite previously set error
51 51
 	s.Error = func(s *scanner.Scanner, msg string) {
... ...
@@ -59,16 +64,25 @@ func parseArgs(s *scanner.Scanner) ([]string, rune, error) {
59 59
 			return args, tok, parseError
60 60
 		}
61 61
 		text := s.TokenText()
62
-		if text == "{" || text == "}" || text == "\n" || text == "\r" || text == ";" {
62
+		// Toggle line comment
63
+		if strings.HasPrefix(text, "#") {
64
+			s.commentLine = true
65
+		} else if text == "\n" || text == "\r" {
66
+			s.commentLine = false
63 67
 			return args, tok, nil
64 68
 		}
65
-		args = append(args, text)
69
+		if !s.commentLine {
70
+			if text == "{" || text == "}" || text == "\n" || text == "\r" || text == ";" {
71
+				return args, tok, nil
72
+			}
73
+			args = append(args, text)
74
+		}
66 75
 		tok = s.Scan()
67 76
 	}
68 77
 	return args, tok, nil
69 78
 }
70 79
 
71
-func parse(s *scanner.Scanner, opener string) (expr []*Command, err error) {
80
+func parse(s *Scanner, opener string) (expr []*Command, err error) {
72 81
 	/*
73 82
 	defer func() {
74 83
 		fmt.Printf("parse() returned %d commands:\n", len(expr))