~mna/snow

e2a898005ef6d0950501490492e723c3a9e11a73 — Martin Angers 1 year, 10 months ago 0ce020e
test some invalid cases
48 files changed, 53 insertions(+), 17 deletions(-)

M pkg/compiler/parser.go
M pkg/compiler/parser_test.go
D pkg/compiler/testdata/comments.snow.err
R pkg/compiler/testdata/{comment.snow => parser/comment.snow}
R pkg/compiler/testdata/{comment.snow.err => parser/comment.snow.err}
R pkg/compiler/testdata/{comment.snow.want => parser/comment.snow.want}
R pkg/compiler/testdata/{comments.snow => parser/comments.snow}
A pkg/compiler/testdata/parser/comments.snow.err
R pkg/compiler/testdata/{comments.snow.want => parser/comments.snow.want}
R pkg/compiler/testdata/{empty.snow => parser/empty.snow}
R pkg/compiler/testdata/{empty.snow.err => parser/empty.snow.err}
R pkg/compiler/testdata/{empty.snow.want => parser/empty.snow.want}
R pkg/compiler/testdata/{fn.snow => parser/fn.snow}
R pkg/compiler/testdata/{fn.snow.err => parser/fn.snow.err}
R pkg/compiler/testdata/{fn.snow.want => parser/fn.snow.want}
R pkg/compiler/testdata/{fn_add.snow => parser/fn_add.snow}
R pkg/compiler/testdata/{fn_add.snow.err => parser/fn_add.snow.err}
R pkg/compiler/testdata/{fn_add.snow.want => parser/fn_add.snow.want}
R pkg/compiler/testdata/{fn_naked_return.snow => parser/fn_naked_return.snow}
R pkg/compiler/testdata/{fn_naked_return.snow.err => parser/fn_naked_return.snow.err}
R pkg/compiler/testdata/{fn_naked_return.snow.want => parser/fn_naked_return.snow.want}
A pkg/compiler/testdata/parser/fn_return_return.snow
A pkg/compiler/testdata/parser/fn_return_return.snow.err
A pkg/compiler/testdata/parser/fn_return_return.snow.want
R pkg/compiler/testdata/{let.snow => parser/let.snow}
R pkg/compiler/testdata/{let.snow.err => parser/let.snow.err}
R pkg/compiler/testdata/{let.snow.want => parser/let.snow.want}
R pkg/compiler/testdata/{let_full.snow => parser/let_full.snow}
R pkg/compiler/testdata/{let_full.snow.err => parser/let_full.snow.err}
R pkg/compiler/testdata/{let_full.snow.want => parser/let_full.snow.want}
R pkg/compiler/testdata/{let_infer.snow => parser/let_infer.snow}
R pkg/compiler/testdata/{let_infer.snow.err => parser/let_infer.snow.err}
R pkg/compiler/testdata/{let_infer.snow.want => parser/let_infer.snow.want}
A pkg/compiler/testdata/parser/return.snow
A pkg/compiler/testdata/parser/return.snow.err
A pkg/compiler/testdata/parser/return.snow.want
A pkg/compiler/testdata/parser/return_return.snow
A pkg/compiler/testdata/parser/return_return.snow.err
A pkg/compiler/testdata/parser/return_return.snow.want
R pkg/compiler/testdata/{var.snow => parser/var.snow}
R pkg/compiler/testdata/{var.snow.err => parser/var.snow.err}
R pkg/compiler/testdata/{var.snow.want => parser/var.snow.want}
R pkg/compiler/testdata/{var_full.snow => parser/var_full.snow}
R pkg/compiler/testdata/{var_full.snow.err => parser/var_full.snow.err}
R pkg/compiler/testdata/{var_full.snow.want => parser/var_full.snow.want}
R pkg/compiler/testdata/{var_infer.snow => parser/var_infer.snow}
R pkg/compiler/testdata/{var_infer.snow.err => parser/var_infer.snow.err}
R pkg/compiler/testdata/{var_infer.snow.want => parser/var_infer.snow.want}
M pkg/compiler/parser.go => pkg/compiler/parser.go +23 -8
@@ 90,12 90,21 @@ func (p *parser) expectSemi() {
	}
}

var stmtStart = map[token]bool{
	Var:    true,
	Let:    true,
	Fn:     true,
	Return: true,
}
// sets of tokens that can be sync'd to (in a call to p.advance)
var (
	topLevelStmtStart = map[token]bool{
		Var: true,
		Let: true,
		Fn:  true,
	}

	stmtStart = map[token]bool{
		Var:    true,
		Let:    true,
		Fn:     true,
		Return: true,
	}
)

func (p *parser) advance(to map[token]bool) {
	for p.tok != EOF {


@@ 133,8 142,14 @@ func (p *parser) parseStmt(topLevel bool) stmt {
		return p.parseFuncDef()
	default:
		pos := p.pos
		p.errorExpected(pos, "statement")
		p.advance(stmtStart)
		lbl := "statement"
		sync := stmtStart
		if topLevel {
			lbl = "top-level statement"
			sync = topLevelStmtStart
		}
		p.errorExpected(pos, lbl)
		p.advance(sync)
		return &badStmt{from: pos, to: p.pos}
	}
}

M pkg/compiler/parser_test.go => pkg/compiler/parser_test.go +9 -8
@@ 16,7 16,8 @@ import (
var testUpdateParserTests = flag.Bool("test.update-parser-tests", false, "If set, replace expected parser test results with actual results.")

func TestParser(t *testing.T) {
	fis, err := ioutil.ReadDir("testdata")
	baseDir := filepath.Join("testdata", "parser")
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}


@@ 32,7 33,7 @@ func TestParser(t *testing.T) {
		t.Run(fi.Name(), func(t *testing.T) {
			var p parser

			b, err := ioutil.ReadFile(filepath.Join("testdata", fi.Name()))
			b, err := ioutil.ReadFile(filepath.Join(baseDir, fi.Name()))
			if err != nil {
				t.Fatal(err)
			}


@@ 54,8 55,8 @@ func TestParser(t *testing.T) {
				t.Fatal(err)
			}

			wantFile := filepath.Join("testdata", fi.Name()+".want")
			errFile := filepath.Join("testdata", fi.Name()+".err")
			wantFile := filepath.Join(baseDir, fi.Name()+".want")
			errFile := filepath.Join(baseDir, fi.Name()+".err")
			if *testUpdateParserTests {
				if err := ioutil.WriteFile(wantFile, buf.Bytes(), 0666); err != nil {
					t.Fatal(err)


@@ 72,9 73,9 @@ func TestParser(t *testing.T) {
			if err != nil && !os.IsNotExist(err) {
				t.Fatal(err)
			}
			t.Logf("got:\n%s\n", buf.String())
			t.Logf("want:\n%s\n", string(want))
			if patch := diff.Diff(buf.String(), string(want)); patch != "" {
				t.Logf("got:\n%s\n", buf.String())
				t.Logf("want:\n%s\n", string(want))
				t.Errorf("diff:\n%s\n", patch)
			}



@@ 83,9 84,9 @@ func TestParser(t *testing.T) {
			if err != nil && !os.IsNotExist(err) {
				t.Fatal(err)
			}
			t.Logf("got error(s):\n%s\n", ebuf.String())
			t.Logf("want error(s):\n%s\n", string(ewant))
			if patch := diff.Diff(ebuf.String(), string(ewant)); patch != "" {
				t.Logf("got error(s):\n%s\n", ebuf.String())
				t.Logf("want error(s):\n%s\n", string(ewant))
				t.Errorf("diff error(s):\n%s\n", patch)
			}
		})

D pkg/compiler/testdata/comments.snow.err => pkg/compiler/testdata/comments.snow.err +0 -1
@@ 1,1 0,0 @@
comments.snow:7:1: expected statement, found 'illegal'

R pkg/compiler/testdata/comment.snow => pkg/compiler/testdata/parser/comment.snow +0 -0
R pkg/compiler/testdata/comment.snow.err => pkg/compiler/testdata/parser/comment.snow.err +0 -0
R pkg/compiler/testdata/comment.snow.want => pkg/compiler/testdata/parser/comment.snow.want +0 -0
R pkg/compiler/testdata/comments.snow => pkg/compiler/testdata/parser/comments.snow +0 -0
A pkg/compiler/testdata/parser/comments.snow.err => pkg/compiler/testdata/parser/comments.snow.err +1 -0
@@ 0,0 1,1 @@
comments.snow:7:1: expected top-level statement, found 'illegal'

R pkg/compiler/testdata/comments.snow.want => pkg/compiler/testdata/parser/comments.snow.want +0 -0
R pkg/compiler/testdata/empty.snow => pkg/compiler/testdata/parser/empty.snow +0 -0
R pkg/compiler/testdata/empty.snow.err => pkg/compiler/testdata/parser/empty.snow.err +0 -0
R pkg/compiler/testdata/empty.snow.want => pkg/compiler/testdata/parser/empty.snow.want +0 -0
R pkg/compiler/testdata/fn.snow => pkg/compiler/testdata/parser/fn.snow +0 -0
R pkg/compiler/testdata/fn.snow.err => pkg/compiler/testdata/parser/fn.snow.err +0 -0
R pkg/compiler/testdata/fn.snow.want => pkg/compiler/testdata/parser/fn.snow.want +0 -0
R pkg/compiler/testdata/fn_add.snow => pkg/compiler/testdata/parser/fn_add.snow +0 -0
R pkg/compiler/testdata/fn_add.snow.err => pkg/compiler/testdata/parser/fn_add.snow.err +0 -0
R pkg/compiler/testdata/fn_add.snow.want => pkg/compiler/testdata/parser/fn_add.snow.want +0 -0
R pkg/compiler/testdata/fn_naked_return.snow => pkg/compiler/testdata/parser/fn_naked_return.snow +0 -0
R pkg/compiler/testdata/fn_naked_return.snow.err => pkg/compiler/testdata/parser/fn_naked_return.snow.err +0 -0
R pkg/compiler/testdata/fn_naked_return.snow.want => pkg/compiler/testdata/parser/fn_naked_return.snow.want +0 -0
A pkg/compiler/testdata/parser/fn_return_return.snow => pkg/compiler/testdata/parser/fn_return_return.snow +3 -0
@@ 0,0 1,3 @@
fn test() {
  return return
}

A pkg/compiler/testdata/parser/fn_return_return.snow.err => pkg/compiler/testdata/parser/fn_return_return.snow.err +1 -0
@@ 0,0 1,1 @@
fn_return_return.snow:2:10: expected ';', found 'return'

A pkg/compiler/testdata/parser/fn_return_return.snow.want => pkg/compiler/testdata/parser/fn_return_return.snow.want +8 -0
@@ 0,0 1,8 @@
file [0:29]
  fn [0:29]
    ident [test] [3:7]
    sig [7:9]
    block [10:29]
      return [14:21]
        bad expr [21:21]
      return [21:27]

R pkg/compiler/testdata/let.snow => pkg/compiler/testdata/parser/let.snow +0 -0
R pkg/compiler/testdata/let.snow.err => pkg/compiler/testdata/parser/let.snow.err +0 -0
R pkg/compiler/testdata/let.snow.want => pkg/compiler/testdata/parser/let.snow.want +0 -0
R pkg/compiler/testdata/let_full.snow => pkg/compiler/testdata/parser/let_full.snow +0 -0
R pkg/compiler/testdata/let_full.snow.err => pkg/compiler/testdata/parser/let_full.snow.err +0 -0
R pkg/compiler/testdata/let_full.snow.want => pkg/compiler/testdata/parser/let_full.snow.want +0 -0
R pkg/compiler/testdata/let_infer.snow => pkg/compiler/testdata/parser/let_infer.snow +0 -0
R pkg/compiler/testdata/let_infer.snow.err => pkg/compiler/testdata/parser/let_infer.snow.err +0 -0
R pkg/compiler/testdata/let_infer.snow.want => pkg/compiler/testdata/parser/let_infer.snow.want +0 -0
A pkg/compiler/testdata/parser/return.snow => pkg/compiler/testdata/parser/return.snow +1 -0
@@ 0,0 1,1 @@
return

A pkg/compiler/testdata/parser/return.snow.err => pkg/compiler/testdata/parser/return.snow.err +1 -0
@@ 0,0 1,1 @@
return.snow:1:1: expected top-level statement, found 'return'

A pkg/compiler/testdata/parser/return.snow.want => pkg/compiler/testdata/parser/return.snow.want +2 -0
@@ 0,0 1,2 @@
file [0:7]
  bad stmt [0:7]

A pkg/compiler/testdata/parser/return_return.snow => pkg/compiler/testdata/parser/return_return.snow +1 -0
@@ 0,0 1,1 @@
return return

A pkg/compiler/testdata/parser/return_return.snow.err => pkg/compiler/testdata/parser/return_return.snow.err +1 -0
@@ 0,0 1,1 @@
return_return.snow:1:1: expected top-level statement, found 'return'

A pkg/compiler/testdata/parser/return_return.snow.want => pkg/compiler/testdata/parser/return_return.snow.want +2 -0
@@ 0,0 1,2 @@
file [0:14]
  bad stmt [0:14]

R pkg/compiler/testdata/var.snow => pkg/compiler/testdata/parser/var.snow +0 -0
R pkg/compiler/testdata/var.snow.err => pkg/compiler/testdata/parser/var.snow.err +0 -0
R pkg/compiler/testdata/var.snow.want => pkg/compiler/testdata/parser/var.snow.want +0 -0
R pkg/compiler/testdata/var_full.snow => pkg/compiler/testdata/parser/var_full.snow +0 -0
R pkg/compiler/testdata/var_full.snow.err => pkg/compiler/testdata/parser/var_full.snow.err +0 -0
R pkg/compiler/testdata/var_full.snow.want => pkg/compiler/testdata/parser/var_full.snow.want +0 -0
R pkg/compiler/testdata/var_infer.snow => pkg/compiler/testdata/parser/var_infer.snow +0 -0
R pkg/compiler/testdata/var_infer.snow.err => pkg/compiler/testdata/parser/var_infer.snow.err +0 -0
R pkg/compiler/testdata/var_infer.snow.want => pkg/compiler/testdata/parser/var_infer.snow.want +0 -0