Browse Source

Work on parser a bit.

Beoran 5 years ago
parent
commit
b15c3c6e2a
3 changed files with 28 additions and 33 deletions
  1. 1 30
      go.mod
  2. 7 0
      parser.go
  3. 20 3
      parser_test.go

+ 1 - 30
go.mod

@@ -1,32 +1,3 @@
 module muesli
 
-require gitlab.com/beoran/monolog v0.0.0
-
-require (
-	github.com/cosiner/argv v0.0.1 // indirect
-	github.com/cpuguy83/go-md2man v1.0.10 // indirect
-	github.com/go-delve/delve v1.2.0 // indirect
-	github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
-	github.com/mattn/go-colorable v0.1.1 // indirect
-	github.com/mattn/go-isatty v0.0.7 // indirect
-	github.com/mattn/go-runewidth v0.0.4 // indirect
-	github.com/peterh/liner v1.1.0 // indirect
-	github.com/pkg/profile v1.3.0 // indirect
-	github.com/russross/blackfriday v2.0.0+incompatible // indirect
-	github.com/sirupsen/logrus v1.4.0 // indirect
-	github.com/spf13/cobra v0.0.3 // indirect
-	github.com/spf13/pflag v1.0.3 // indirect
-	github.com/stretchr/testify v1.3.0 // indirect
-	gitlab.com/beoran/gdast v0.0.0
-	golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c // indirect
-	golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c // indirect
-	golang.org/x/net v0.0.0-20190328230028-74de082e2cca // indirect
-	golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 // indirect
-	golang.org/x/tools v0.0.0-20190330180304-aef51cc3777c // indirect
-	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
-	gopkg.in/yaml.v2 v2.2.2 // indirect
-)
-
-replace gitlab.com/beoran/monolog => ../monolog
-
-replace gitlab.com/beoran/gdast => ../gdast
+go 1.12

+ 7 - 0
parser.go

@@ -389,6 +389,9 @@ func (parser *Parser) ParseEmptyStatement() *Ast {
 }
 
 func (parser Parser) NextIs(kinds ...TokenKind) bool {
+    if (parser.current.TokenKind == TokenKindNone) {
+		parser.Advance()
+	}    
     for _, kind := range kinds {
         if kind == parser.current.TokenKind  {
             return true
@@ -474,6 +477,10 @@ func (parser *Parser) ParseProgram() *Ast {
 }
 
 func (parser *Parser) Parse() *Ast {
+	defer func() {		
+		err := recover()
+		fmt.Printf("Parse error: %s\n", err)
+	} ()
 	ast := parser.ParseProgram()
 	return ast
 }

+ 20 - 3
parser_test.go

@@ -6,6 +6,10 @@ import (
 )
 
 func HelperFailOnErrors(ast * Ast, expected int, test *testing.T) {
+	if ast == nil { 
+		test.Errorf("Parse failed, AST is nil.")
+		return
+	}	
 	if ast.IsNone() {
 		test.Errorf("Parse failed, %d parse errors expected", expected)
 	}
@@ -21,11 +25,20 @@ func HelperFailOnErrors(ast * Ast, expected int, test *testing.T) {
 
 func HelperParseAndFailOnErrors(prog string, expected int,
 	parsefunc func(*Parser) *Ast, test *testing.T) {
+		defer func(){
+			err := recover()
+			if err != nil {
+				test.Errorf("Parse error: %s", err)
+			}
+		}()
+	
 	parser := NewParserFromString(prog)
 	parser.SetLogger(&testLogger{})
 	ast := parsefunc(parser)
 	HelperFailOnErrors(ast, expected, test)
-	ast.Display()
+	if ast != nil { 
+		ast.Display()
+	}
 }
 
 func TestParser(test *testing.T) {
@@ -33,7 +46,9 @@ func TestParser(test *testing.T) {
 	say ( add 5 10 ) .`
 	parser := NewParserFromString(com)
 	ast := parser.Parse()
-	ast.Display()
+	if ast != nil { 
+		ast.Display()
+	}	
 }
 
 func TestParser2(test *testing.T) {
@@ -70,7 +85,9 @@ func TestParseDesignFile(test *testing.T) {
     } 
     parser.SetLogger(&testLogger{})
     ast := parser.Parse()
-    ast.Display()
+    if ast != nil { 
+		ast.Display()
+	}
     
 }