forked from mvdan/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 13bfa73
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2015, Daniel Martí. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# sh | ||
|
||
A shell parser. Not yet usable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2016, Daniel Martí <[email protected]> | ||
// See LICENSE for licensing information | ||
|
||
package sh | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
const ( | ||
_ = -iota | ||
EOF | ||
IDENT | ||
) | ||
|
||
func parse(r io.Reader) error { | ||
p := &parser{ | ||
r: bufio.NewReader(r), | ||
} | ||
return p.program() | ||
} | ||
|
||
type parser struct { | ||
r *bufio.Reader | ||
tok int32 | ||
} | ||
|
||
func isIdentChar(r rune) bool { | ||
return r == '_' || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') | ||
} | ||
|
||
func (p *parser) next() error { | ||
r, _, err := p.r.ReadRune() | ||
if err == io.EOF { | ||
p.tok = EOF | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if r == ' ' { | ||
return p.next() | ||
} | ||
if isIdentChar(r) { | ||
for isIdentChar(r) { | ||
r, _, err = p.r.ReadRune() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
p.tok = IDENT | ||
return nil | ||
} | ||
p.tok = r | ||
return nil | ||
} | ||
|
||
func (p *parser) discardLine() error { | ||
if _, err := p.r.ReadBytes('\n'); err != nil { | ||
return err | ||
} | ||
return p.next() | ||
} | ||
|
||
func (p *parser) got(tok int32) bool { | ||
if p.tok == tok { | ||
p.next() | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (p *parser) program() error { | ||
if err := p.next(); err != nil { | ||
return err | ||
} | ||
for p.tok != EOF { | ||
switch { | ||
case p.got('#'): | ||
if err := p.discardLine(); err != nil { | ||
return err | ||
} | ||
case p.got(IDENT): | ||
default: | ||
return fmt.Errorf("unexpected token") | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2016, Daniel Martí <[email protected]> | ||
// See LICENSE for licensing information | ||
|
||
package sh | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
paths, err := filepath.Glob("testdata/*.sh") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for _, path := range paths { | ||
testParse(t, path) | ||
} | ||
} | ||
|
||
func testParse(t *testing.T, path string) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer f.Close() | ||
if err := parse(f); err != nil { | ||
t.Fatalf("Error in %s: %v", path, err) | ||
} | ||
} |
Empty file.