]> Cypherpunks repositories - gostls13.git/commitdiff
simplify extractEBNF code
authorRobert Griesemer <gri@golang.org>
Thu, 3 Sep 2009 17:53:27 +0000 (10:53 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 3 Sep 2009 17:53:27 +0000 (10:53 -0700)
R=rsc
DELTA=15  (3 added, 2 deleted, 10 changed)
OCL=34307
CL=34314

src/cmd/ebnflint/ebnflint.go

index 253f286d16ab393205517de387624429dec2743f..f6374214bd6f55449859a3aa4605a7adc5bb72da 100644 (file)
@@ -38,33 +38,34 @@ var (
 func extractEBNF(src []byte) []byte {
        var buf bytes.Buffer;
 
-       for i, j, n := 0, 0, len(src); ; {
-               // i = beginning of EBNF section
-               i = bytes.Index(src[j : n], open);
+       for {
+               // i = beginning of EBNF text
+               i := bytes.Index(src, open);
                if i < 0 {
-                       break;
+                       break;  // no EBNF found - we are done
                }
-               i += j+len(open);
+               i += len(open);
 
                // write as many newlines as found in the excluded text
                // to maintain correct line numbers in error messages 
-               for _, ch := range src[j : i] {
+               for _, ch := range src[0 : i] {
                        if ch == '\n' {
                                buf.WriteByte('\n');
                        }
                }
 
-               // j = end of EBNF section
-               j = bytes.Index(src[i : n], close);
+               // j = end of EBNF text (or end of source)
+               j := bytes.Index(src[i : len(src)], close);  // close marker
                if j < 0 {
-                       // missing closing
-                       // TODO(gri) should this be an error?
-                       j = n-i;
+                       j = len(src)-i;
                }
                j += i;
 
-               // copy EBNF section
+               // copy EBNF text
                buf.Write(src[i : j]);
+
+               // advance
+               src = src[j : len(src)];
        }
 
        return buf.Data();