Skip to content
This repository was archived by the owner on Aug 27, 2018. It is now read-only.

Commit 954e900

Browse files
authored
Merge pull request #125 from myitcv/syntax_viewer
examples/sites/syntaxviewer: initial commit
2 parents 2194de7 + 187f371 commit 954e900

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+22451
-13
lines changed

.vendor.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
./_vendor/src/honnef.co/go/js/xhr 00e3346113aed89b501ead4e863c7c3d04fa0c5b git@github.com:dominikh/go-js-xhr
2525
./_vendor/src/honnef.co/go/js/util 96b8dd9d16214b6cd9d8c8e84b3a375da4688108 git@github.com:dominikh/go-js-util
2626
./_vendor/src/honnef.co/go/js/dom 0a64a2db1425c1881d4ec762370250c65f44b85b git@github.com:dominikh/go-js-dom
27+
./_vendor/src/mvdan.cc/sh 0e64bc087f8341d164c07a5edba1e37417bd2df4 git@github.com:mvdan/sh
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: go
2+
3+
matrix:
4+
include:
5+
- os: linux
6+
dist: trusty
7+
sudo: false
8+
go: 1.9.x
9+
- os: linux
10+
dist: trusty
11+
sudo: false
12+
go: 1.10.x
13+
- os: osx
14+
osx_image: xcode9.2
15+
go: 1.10.x
16+
17+
go_import_path: mvdan.cc/sh
18+
19+
script:
20+
- go test ./...
21+
- go test -short -race ./...
22+
- shfmt -version

_vendor/src/mvdan.cc/sh/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2016, Daniel Martí. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of the copyright holder nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

_vendor/src/mvdan.cc/sh/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# sh
2+
3+
[![GoDoc](https://godoc.org/mvdan.cc/sh?status.svg)](https://godoc.org/mvdan.cc/sh)
4+
[![Linux & OSX build](https://travis-ci.org/mvdan/sh.svg?branch=master)](https://travis-ci.org/mvdan/sh)
5+
[![Windows build](https://ci.appveyor.com/api/projects/status/rxxs08v65aj2fqof?svg=true)](https://ci.appveyor.com/project/mvdan/sh)
6+
[![Coverage Status](https://coveralls.io/repos/github/mvdan/sh/badge.svg?branch=master)](https://coveralls.io/github/mvdan/sh)
7+
8+
A shell parser, formatter and interpreter. Supports [POSIX Shell],
9+
[Bash] and [mksh]. Requires Go 1.9 or later.
10+
11+
### shfmt
12+
13+
go get -u mvdan.cc/sh/cmd/shfmt
14+
15+
`shfmt` formats shell programs. It can use tabs or any number of spaces
16+
to indent. See [canonical.sh](syntax/canonical.sh) for a quick look at
17+
its default style.
18+
19+
You can feed it standard input, any number of files or any number of
20+
directories to recurse into. When recursing, it will operate on `.sh`
21+
and `.bash` files and ignore files starting with a period. It will also
22+
operate on files with no extension and a shell shebang.
23+
24+
shfmt -l -w script.sh
25+
26+
Use `-i N` to indent with a number of spaces instead of tabs. There are
27+
other formatting options - see `shfmt -h`. For example, to get the
28+
formatting appropriate for [Google's Style][google-style] guide, use
29+
`shfmt -i 2 -ci`.
30+
31+
Packages are available for [Arch], [CRUX], [Homebrew], [NixOS] and [Void].
32+
33+
#### Advantages over `bash -n`
34+
35+
`bash -n` can be useful to check for syntax errors in shell scripts.
36+
However, `shfmt >/dev/null` can do a better job as it checks for invalid
37+
UTF-8 and does all parsing statically, including checking POSIX Shell
38+
validity:
39+
40+
```
41+
$ echo '${foo:1 2}' | bash -n
42+
$ echo '${foo:1 2}' | shfmt
43+
1:9: not a valid arithmetic operator: 2
44+
$ echo 'foo=(1 2)' | bash --posix -n
45+
$ echo 'foo=(1 2)' | shfmt -p
46+
1:5: arrays are a bash feature
47+
```
48+
49+
### gosh
50+
51+
go get -u mvdan.cc/sh/cmd/gosh
52+
53+
Experimental shell that uses `interp`. Work in progress, so don't expect
54+
stability just yet.
55+
56+
### Fuzzing
57+
58+
This project makes use of [go-fuzz] to find crashes and hangs in both
59+
the parser and the printer. To get started, run:
60+
61+
git checkout fuzz
62+
./fuzz
63+
64+
### Caveats
65+
66+
* Bash index expressions must be an arithmetic expression or a quoted
67+
string. This is because the static parser can't know whether the array
68+
is an associative array (string keys) since that depends on having
69+
called or not `declare -A`.
70+
71+
```
72+
$ echo '${array[spaced string]}' | shfmt
73+
1:16: not a valid arithmetic operator: string
74+
```
75+
76+
* `$((` and `((` ambiguity is not suported. Backtracking would greatly
77+
complicate the parser and make stream support - `io.Reader` -
78+
impossible. In practice, the POSIX spec recommends to [space the
79+
operands][posix-ambiguity] if `$( (` is meant.
80+
81+
```
82+
$ echo '$((foo); (bar))' | shfmt
83+
1:1: reached ) without matching $(( with ))
84+
```
85+
86+
* Some builtins like `export` and `let` are parsed as keywords. This is
87+
to allow statically parsing them and building their AST, as opposed to
88+
just keeping the arguments as a slice of arguments.
89+
90+
### Related projects
91+
92+
* [dockerised-shfmt] - A docker image of `shfmt`
93+
* [format-shell] - Atom plugin for `shfmt`
94+
* [micro] - Editor with a built-in plugin for `shfmt`
95+
* [shell-format] - VS Code plugin for `shfmt`
96+
* [vim-shfmt] - Vim plugin for `shfmt`
97+
98+
[arch]: https://aur.archlinux.org/packages/shfmt/
99+
[bash]: https://www.gnu.org/software/bash/
100+
[crux]: https://github.com/6c37/crux-ports-git/tree/3.3/shfmt
101+
[dockerised-shfmt]: https://hub.docker.com/r/jamesmstone/shfmt/
102+
[examples]: https://godoc.org/mvdan.cc/sh/syntax#pkg-examples
103+
[format-shell]: https://atom.io/packages/format-shell
104+
[go-fuzz]: https://github.com/dvyukov/go-fuzz
105+
[google-style]: https://google.github.io/styleguide/shell.xml
106+
[homebrew]: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/shfmt.rb
107+
[micro]: https://micro-editor.github.io/
108+
[mksh]: https://www.mirbsd.org/mksh.htm
109+
[nixos]: https://github.com/NixOS/nixpkgs/blob/HEAD/pkgs/tools/text/shfmt/default.nix
110+
[posix shell]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
111+
[posix-ambiguity]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03
112+
[shell-format]: https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format
113+
[vim-shfmt]: https://github.com/z0mbix/vim-shfmt
114+
[void]: https://github.com/voidlinux/void-packages/blob/HEAD/srcpkgs/shfmt/template
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "{build}"
2+
3+
clone_folder: c:\gopath\src\mvdan.cc\sh
4+
5+
environment:
6+
GOPATH: c:\gopath
7+
GOVERSION: 1.10
8+
9+
install:
10+
- rmdir c:\go /s /q
11+
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.msi
12+
- msiexec /i go%GOVERSION%.windows-amd64.msi /q
13+
- go version
14+
15+
build: false
16+
deploy: false
17+
18+
test_script:
19+
- go get -d -t ./...
20+
- go install -v ./...
21+
- go test ./...
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
2+
// See LICENSE for licensing information
3+
4+
package main // import "mvdan.cc/sh/cmd/gosh"
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"io"
10+
"os"
11+
"strings"
12+
13+
"golang.org/x/crypto/ssh/terminal"
14+
15+
"mvdan.cc/sh/interp"
16+
"mvdan.cc/sh/syntax"
17+
)
18+
19+
var (
20+
command = flag.String("c", "", "command to be executed")
21+
22+
parser *syntax.Parser
23+
24+
runner = interp.Runner{
25+
Stdin: os.Stdin,
26+
Stdout: os.Stdout,
27+
Stderr: os.Stderr,
28+
}
29+
)
30+
31+
func main() {
32+
flag.Parse()
33+
if err := runAll(); err != nil {
34+
fmt.Fprintln(os.Stderr, err)
35+
os.Exit(1)
36+
}
37+
}
38+
39+
func runAll() error {
40+
parser = syntax.NewParser()
41+
if *command != "" {
42+
return run(strings.NewReader(*command), "")
43+
}
44+
if flag.NArg() == 0 {
45+
if terminal.IsTerminal(int(os.Stdin.Fd())) {
46+
return interactive()
47+
}
48+
return run(os.Stdin, "")
49+
}
50+
for _, path := range flag.Args() {
51+
if err := runPath(path); err != nil {
52+
return err
53+
}
54+
}
55+
return nil
56+
}
57+
58+
func runPath(path string) error {
59+
f, err := os.Open(path)
60+
if err != nil {
61+
return err
62+
}
63+
defer f.Close()
64+
return run(f, path)
65+
}
66+
67+
func run(reader io.Reader, name string) error {
68+
prog, err := parser.Parse(reader, name)
69+
if err != nil {
70+
return err
71+
}
72+
runner.Reset()
73+
return runner.Run(prog)
74+
}
75+
76+
type promptReader struct {
77+
io.Reader
78+
first bool
79+
}
80+
81+
func (pr *promptReader) Read(p []byte) (int, error) {
82+
if pr.first {
83+
fmt.Printf("$ ")
84+
pr.first = false
85+
} else {
86+
fmt.Printf("> ")
87+
}
88+
return pr.Reader.Read(p)
89+
}
90+
91+
func interactive() error {
92+
r := &promptReader{os.Stdin, true}
93+
runner.Reset()
94+
fn := func(s *syntax.Stmt) bool {
95+
if err := runner.Stmt(s); err != nil {
96+
code, ok := err.(interp.ExitCode)
97+
if ok {
98+
os.Exit(int(code))
99+
}
100+
fmt.Fprintln(os.Stderr, err)
101+
os.Exit(1)
102+
}
103+
r.first = true
104+
return true
105+
}
106+
return parser.Stmts(r, fn)
107+
}

0 commit comments

Comments
 (0)