Skip to content

Commit

Permalink
Merge pull request #505 from nevalang/parser_invalid_conn_err_msg
Browse files Browse the repository at this point in the history
fix(parser): err msg in case of invalid connection
  • Loading branch information
emil14 authored Mar 8, 2024
2 parents c46d95c + e681884 commit 18ac3bd
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/neva",
"cwd": "${workspaceFolder}/examples",
"args": ["run", "0_do_nothing"]
"cwd": "${workspaceFolder}/e2e/mod",
"args": ["run", "connection_with_only_port_addr"]
},
{
"name": "Neva CLI 10",
Expand Down
10 changes: 10 additions & 0 deletions e2e/mod/connection_with_only_port_addr/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://github.com/nevalang/neva/issues/503

const array list<int> = [1,2,3]
component Main(start any) (stop any) {
nodes { printer Printer<any>}
net {
:start -> ($array -> printer:data)
printer:sig
}
}
21 changes: 21 additions & 0 deletions e2e/run_tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,24 @@ func TestFloatConstWithIntLit(t *testing.T) {

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}

// Expect normal error message and not go panic trace in case of bad connection.
func TestConnectionWithOnlyPortAddr(t *testing.T) {
err := os.Chdir("./mod")
require.NoError(t, err)

cmd := exec.Command("neva", "run", "connection_with_only_port_addr")

out, err := cmd.CombinedOutput()
require.NoError(t, err)
expected := strings.TrimSpace(
"connection_with_only_port_addr/main.neva:8:2 Invalid connection, make sure you have both sender and receiver",
)
require.Equal(
t,
expected,
strings.TrimSpace(string(out)),
)

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
24 changes: 22 additions & 2 deletions internal/compiler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,28 @@ func (e Error) Wrap(child *Error) *Error {

func (e Error) unwrap() Error {
for e.child != nil {
e = *e.child
var loc *src.Location
if e.child.Location != nil {
loc = e.child.Location
} else {
loc = e.Location
}

var meta *src.Meta
if e.child.Meta != nil {
meta = e.child.Meta
} else {
meta = e.Meta
}

e = Error{
Err: e.child.Err,
Location: loc,
Meta: meta,
child: e.child.child,
}
}

return e
}

Expand All @@ -33,7 +53,7 @@ func (e Error) Error() string {
hasErr := e.Err != nil

if _, ok := e.Err.(*Error); ok {
panic("")
panic("internal error")
}

switch {
Expand Down
8 changes: 8 additions & 0 deletions internal/compiler/parser/listener_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ func parseConn(connDef generated.IConnDefContext) (src.Connection, *compiler.Err
}

arrBypassConn := connDef.ArrBypassConnDef()

if arrBypassConn == nil && connDef.NormConnDef() == nil {
panic(&compiler.Error{
Err: errors.New("Invalid connection, make sure you have both sender and receiver"),
Meta: &connMeta,
})
}

if arrBypassConn == nil {
parsedSenderSide := parseNormConnSenderSide(connDef.NormConnDef().SenderSide())

Expand Down
40 changes: 26 additions & 14 deletions internal/compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p Parser) ParseModules(
parsedMods := make(map[src.ModuleRef]src.Module, len(rawMods))

for modRef, rawMod := range rawMods {
parsedPkgs, err := p.ParsePackages(rawMod.Packages)
parsedPkgs, err := p.ParsePackages(modRef, rawMod.Packages)
if err != nil {
return nil, compiler.Error{
Err: errors.New("Parsing error"),
Expand All @@ -47,12 +47,16 @@ func (p Parser) ParseModules(
}

func (p Parser) ParsePackages(
modRef src.ModuleRef,
rawPkgs map[string]compiler.RawPackage,
) (map[string]src.Package, *compiler.Error) {
) (
map[string]src.Package,
*compiler.Error,
) {
packages := make(map[string]src.Package, len(rawPkgs))

for pkgName, pkgFiles := range rawPkgs {
parsedFiles, err := p.ParseFiles(pkgFiles)
parsedFiles, err := p.ParseFiles(modRef, pkgName, pkgFiles)
if err != nil {
return nil, compiler.Error{
Location: &src.Location{PkgName: pkgName},
Expand All @@ -65,31 +69,38 @@ func (p Parser) ParsePackages(
return packages, nil
}

func (p Parser) ParseFiles(files map[string][]byte) (map[string]src.File, *compiler.Error) {
func (p Parser) ParseFiles(
modRef src.ModuleRef,
pkgName string,
files map[string][]byte,
) (map[string]src.File, *compiler.Error) {
result := make(map[string]src.File, len(files))

for name, fileBytes := range files {
fileName := name
parsedFile, err := p.ParseFile(fileBytes)
for fileName, fileBytes := range files {
loc := src.Location{
ModRef: modRef,
PkgName: pkgName,
FileName: fileName,
}
parsedFile, err := p.parseFile(loc, fileBytes)
if err != nil {
return nil, compiler.Error{
Location: &src.Location{
FileName: fileName,
},
}.Wrap(err)
return nil, compiler.Error{Location: &loc}.Wrap(err)
}
result[fileName] = parsedFile
}

return result, nil
}

func (p Parser) ParseFile(bb []byte) (f src.File, err *compiler.Error) {
func (p Parser) parseFile(
loc src.Location,
bb []byte,
) (f src.File, err *compiler.Error) {
defer func() {
if e := recover(); e != nil {
compilerErr, ok := e.(*compiler.Error)
if ok {
err = compilerErr
err = compiler.Error{Location: &loc}.Wrap(compilerErr)
return
}
err = &compiler.Error{
Expand All @@ -98,6 +109,7 @@ func (p Parser) ParseFile(bb []byte) (f src.File, err *compiler.Error) {
e,
string(debug.Stack()),
),
Location: &loc,
}
}
}()
Expand Down
28 changes: 14 additions & 14 deletions internal/compiler/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package parser_test
package parser

import (
"testing"

"github.com/nevalang/neva/internal/compiler"
"github.com/nevalang/neva/internal/compiler/parser"
src "github.com/nevalang/neva/pkg/sourcecode"
"github.com/stretchr/testify/require"
)

Expand All @@ -14,9 +14,9 @@ func TestParser_ParseFile_Comments(t *testing.T) {
// comment
`)

p := parser.New(false)
p := New(false)

_, err := p.ParseFile(text)
_, err := p.parseFile(src.Location{}, text)
require.True(t, err == nil)
}

Expand Down Expand Up @@ -48,9 +48,9 @@ func TestParser_ParseFile_Directives(t *testing.T) {
}
`)

p := parser.New(false)
p := New(false)

got, err := p.ParseFile(text)
got, err := p.parseFile(src.Location{}, text)
require.True(t, err == nil)

d1 := got.Entities["C1"].Component.Directives[compiler.ExternDirective][0]
Expand Down Expand Up @@ -88,9 +88,9 @@ func TestParser_ParseFile_IONodes(t *testing.T) {
}
`)

p := parser.New(false)
p := New(false)

got, err := p.ParseFile(text)
got, err := p.parseFile(src.Location{}, text)
require.True(t, err == nil)

conn := got.Entities["C1"].Component.Net[0]
Expand All @@ -114,9 +114,9 @@ func TestParser_ParseFile_AnonymousNodes(t *testing.T) {
}
`)

p := parser.New(false)
p := New(false)

got, err := p.ParseFile(text)
got, err := p.parseFile(src.Location{}, text)
require.True(t, err == nil)

nodes := got.Entities["C1"].Component.Nodes
Expand All @@ -135,9 +135,9 @@ func TestParser_ParseFile_EnumLiterals(t *testing.T) {
const c1 pkg.Enum = pkg.Enum2::Bar
`)

p := parser.New(false)
p := New(false)

got, err := p.ParseFile(text)
got, err := p.parseFile(src.Location{}, text)
require.True(t, err == nil)

enum := got.Entities["c0"].Const.Message.Enum
Expand All @@ -162,9 +162,9 @@ func TestParser_ParseFile_EnumLiteralSenders(t *testing.T) {
}
`)

p := parser.New(false)
p := New(false)

got, err := p.ParseFile(text)
got, err := p.parseFile(src.Location{}, text)
require.True(t, err == nil)

conn := got.Entities["C1"].Component.Net[0]
Expand Down
2 changes: 1 addition & 1 deletion pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg

// Version is the current version of the language and stdlib
var Version = "0.10.1" //nolint:gochecknoglobals
var Version = "0.10.2" //nolint:gochecknoglobals

0 comments on commit 18ac3bd

Please sign in to comment.