diff options
Diffstat (limited to 'src/pkg')
| -rw-r--r-- | src/pkg/Makefile | 6 | ||||
| -rw-r--r-- | src/pkg/exp/ebnf/Makefile (renamed from src/pkg/ebnf/Makefile) | 6 | ||||
| -rw-r--r-- | src/pkg/exp/ebnf/ebnf.go (renamed from src/pkg/ebnf/ebnf.go) | 0 | ||||
| -rw-r--r-- | src/pkg/exp/ebnf/ebnf_test.go (renamed from src/pkg/ebnf/ebnf_test.go) | 0 | ||||
| -rw-r--r-- | src/pkg/exp/ebnf/parser.go (renamed from src/pkg/ebnf/parser.go) | 0 | ||||
| -rw-r--r-- | src/pkg/exp/ebnflint/Makefile | 15 | ||||
| -rw-r--r-- | src/pkg/exp/ebnflint/doc.go | 22 | ||||
| -rw-r--r-- | src/pkg/exp/ebnflint/ebnflint.go | 109 |
8 files changed, 152 insertions, 6 deletions
diff --git a/src/pkg/Makefile b/src/pkg/Makefile index e833fcfbba..321b463b13 100644 --- a/src/pkg/Makefile +++ b/src/pkg/Makefile @@ -68,7 +68,6 @@ DIRS=\ debug/elf\ debug/gosym\ debug/pe\ - ebnf\ encoding/ascii85\ encoding/base32\ encoding/base64\ @@ -78,6 +77,8 @@ DIRS=\ encoding/pem\ exec\ exp/datafmt\ + exp/ebnf\ + exp/ebnflint\ exp/gui\ exp/gui/x11\ exp/norm\ @@ -173,7 +174,6 @@ DIRS=\ websocket\ xml\ ../cmd/cgo\ - ../cmd/ebnflint\ ../cmd/godoc\ ../cmd/gofix\ ../cmd/gofmt\ @@ -201,6 +201,7 @@ NOTEST+=\ crypto\ crypto/openpgp/error\ crypto/x509/pkix\ + exp/ebnflint\ exp/gui\ exp/gui/x11\ exp/sql/driver\ @@ -220,7 +221,6 @@ NOTEST+=\ testing\ testing/iotest\ ../cmd/cgo\ - ../cmd/ebnflint\ ../cmd/godoc\ ../cmd/gotest\ ../cmd/goyacc\ diff --git a/src/pkg/ebnf/Makefile b/src/pkg/exp/ebnf/Makefile index f5555d2720..844de675cb 100644 --- a/src/pkg/ebnf/Makefile +++ b/src/pkg/exp/ebnf/Makefile @@ -2,11 +2,11 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../Make.inc +include ../../../Make.inc -TARG=ebnf +TARG=exp/ebnf GOFILES=\ ebnf.go\ parser.go\ -include ../../Make.pkg +include ../../../Make.pkg diff --git a/src/pkg/ebnf/ebnf.go b/src/pkg/exp/ebnf/ebnf.go index 2ec7f00800..2ec7f00800 100644 --- a/src/pkg/ebnf/ebnf.go +++ b/src/pkg/exp/ebnf/ebnf.go diff --git a/src/pkg/ebnf/ebnf_test.go b/src/pkg/exp/ebnf/ebnf_test.go index 8cfd6b9c37..8cfd6b9c37 100644 --- a/src/pkg/ebnf/ebnf_test.go +++ b/src/pkg/exp/ebnf/ebnf_test.go diff --git a/src/pkg/ebnf/parser.go b/src/pkg/exp/ebnf/parser.go index 2dbbefb751..2dbbefb751 100644 --- a/src/pkg/ebnf/parser.go +++ b/src/pkg/exp/ebnf/parser.go diff --git a/src/pkg/exp/ebnflint/Makefile b/src/pkg/exp/ebnflint/Makefile new file mode 100644 index 0000000000..2057b07d58 --- /dev/null +++ b/src/pkg/exp/ebnflint/Makefile @@ -0,0 +1,15 @@ +# Copyright 2009 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +include ../../../Make.inc + +TARG=ebnflint +GOFILES=\ + ebnflint.go\ + +include ../../../Make.cmd + +test: $(TARG) + $(TARG) -start="SourceFile" "$(GOROOT)"/doc/go_spec.html + diff --git a/src/pkg/exp/ebnflint/doc.go b/src/pkg/exp/ebnflint/doc.go new file mode 100644 index 0000000000..f35976eea7 --- /dev/null +++ b/src/pkg/exp/ebnflint/doc.go @@ -0,0 +1,22 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +/* + +Ebnflint verifies that EBNF productions are consistent and gramatically correct. +It reads them from an HTML document such as the Go specification. + +Grammar productions are grouped in boxes demarcated by the HTML elements + <pre class="ebnf"> + </pre> + + +Usage: + ebnflint [--start production] [file] + +The --start flag specifies the name of the start production for +the grammar; it defaults to "Start". + +*/ +package documentation diff --git a/src/pkg/exp/ebnflint/ebnflint.go b/src/pkg/exp/ebnflint/ebnflint.go new file mode 100644 index 0000000000..c827716c44 --- /dev/null +++ b/src/pkg/exp/ebnflint/ebnflint.go @@ -0,0 +1,109 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "bytes" + "exp/ebnf" + "flag" + "fmt" + "go/scanner" + "go/token" + "io/ioutil" + "os" + "path/filepath" +) + +var fset = token.NewFileSet() +var start = flag.String("start", "Start", "name of start production") + +func usage() { + fmt.Fprintf(os.Stderr, "usage: ebnflint [flags] [filename]\n") + flag.PrintDefaults() + os.Exit(1) +} + +// Markers around EBNF sections in .html files +var ( + open = []byte(`<pre class="ebnf">`) + close = []byte(`</pre>`) +) + +func report(err os.Error) { + scanner.PrintError(os.Stderr, err) + os.Exit(1) +} + +func extractEBNF(src []byte) []byte { + var buf bytes.Buffer + + for { + // i = beginning of EBNF text + i := bytes.Index(src, open) + if i < 0 { + break // no EBNF found - we are done + } + 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[0:i] { + if ch == '\n' { + buf.WriteByte('\n') + } + } + + // j = end of EBNF text (or end of source) + j := bytes.Index(src[i:], close) // close marker + if j < 0 { + j = len(src) - i + } + j += i + + // copy EBNF text + buf.Write(src[i:j]) + + // advance + src = src[j:] + } + + return buf.Bytes() +} + +func main() { + flag.Parse() + + var ( + filename string + src []byte + err os.Error + ) + switch flag.NArg() { + case 0: + filename = "<stdin>" + src, err = ioutil.ReadAll(os.Stdin) + case 1: + filename = flag.Arg(0) + src, err = ioutil.ReadFile(filename) + default: + usage() + } + if err != nil { + report(err) + } + + if filepath.Ext(filename) == ".html" || bytes.Index(src, open) >= 0 { + src = extractEBNF(src) + } + + grammar, err := ebnf.Parse(filename, bytes.NewBuffer(src)) + if err != nil { + report(err) + } + + if err = ebnf.Verify(grammar, *start); err != nil { + report(err) + } +} |
