Skip to content

Commit

Permalink
Merge pull request #14 from janpfeifer/errors
Browse files Browse the repository at this point in the history
Improved error reporting
  • Loading branch information
janpfeifer authored Apr 23, 2023
2 parents a53ab67 + e1fa072 commit c4a71e1
Show file tree
Hide file tree
Showing 17 changed files with 1,176 additions and 656 deletions.
38 changes: 38 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Package common holds functionality that is common to multiple other packages.
package common

import "sort"

// Set implements a Set for the key type T.
type Set[T comparable] map[T]struct{}

// MakeSet returns an empty Set of the given type. Size is optional, and if given
// will reserve the expected size.
func MakeSet[T comparable](size ...int) Set[T] {
if len(size) == 0 {
return make(Set[T])
}
return make(Set[T], size[0])
}

// Has returns true if Set s has the given key.
func (s Set[T]) Has(key T) bool {
_, found := s[key]
return found
}

// Insert key into set.
func (s Set[T]) Insert(key T) {
s[key] = struct{}{}
}

// SortedKeys enumerate keys from a string map and sort them.
// TODO: make it for any key type.
func SortedKeys[T any](m map[string]T) (keys []string) {
keys = make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return
}
13 changes: 7 additions & 6 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package dispatcher

import (
"fmt"
. "github.com/janpfeifer/gonb/common"
"github.com/janpfeifer/gonb/goexec"
"github.com/janpfeifer/gonb/gonbui/protocol"
"github.com/janpfeifer/gonb/kernel"
Expand Down Expand Up @@ -163,14 +164,14 @@ func handleExecuteRequest(msg kernel.Message, goExec *goexec.State) error {
// Dispatch to various executors.
msg.Kernel().Interrupted.Store(false)
lines := strings.Split(code, "\n")
usedLines := make(map[int]bool)
usedLines := MakeSet[int]()
var executionErr error
if err := specialcmd.Parse(msg, goExec, true, lines, usedLines); err != nil {
executionErr = errors.WithMessagef(err, "executing special commands in cell")
}
hasMoreToRun := len(usedLines) < len(lines)
if executionErr == nil && !msg.Kernel().Interrupted.Load() && hasMoreToRun {
executionErr = goExec.ExecuteCell(msg, lines, usedLines)
executionErr = goExec.ExecuteCell(msg, msg.Kernel().ExecCounter, lines, usedLines)
}

// Final execution result.
Expand Down Expand Up @@ -210,14 +211,14 @@ func HandleInspectRequest(msg kernel.Message, goExec *goexec.State) error {
lines, cursorLine, cursorCol := kernel.JupyterToLinesAndCursor(code, cursorPos)

// Separate special commands from Go commands.
usedLines := make(map[int]bool)
usedLines := MakeSet[int]()
if err := specialcmd.Parse(msg, goExec, false, lines, usedLines); err != nil {
return errors.WithMessagef(err, "parsing special commands in cell")
}

// Get data contents for reply.
var data kernel.MIMEMap
if usedLines[cursorLine] {
if usedLines.Has(cursorLine) {
// If special command, use our help message as inspect content.
data = kernel.MIMEMap{protocol.MIMETextPlain: any(specialcmd.HelpMessage)}
} else {
Expand Down Expand Up @@ -281,12 +282,12 @@ func handleCompleteRequest(msg kernel.Message, goExec *goexec.State) (err error)
lines, cursorLine, cursorCol := kernel.JupyterToLinesAndCursor(code, cursorPos)

// Separate special commands from Go commands.
usedLines := make(map[int]bool)
usedLines := MakeSet[int]()
if err = specialcmd.Parse(msg, goExec, false, lines, usedLines); err != nil {
err = errors.WithMessagef(err, "parsing special commands in cell")
return
}
if usedLines[cursorLine] {
if usedLines.Has(cursorLine) {
// No auto-complete for special commands.
return
}
Expand Down
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# GoNB Changelog

## v0.5

* Improved error reporting, including indication of line number in cell.
* Parse error output of the execution of a cell, and if it contains a stack-trace, add a reference to the cell
code (cell id and line number).
* Cleaned up, improved code documentation and testing for `goexec` package.

## v0.4.1

* Added support for Mac installation.
Expand Down
124 changes: 77 additions & 47 deletions examples/tutorial.ipynb

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions goexec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Package `goexec`

The package `goexec` is responsible for executing the notebook cells using Go, as well as keeping the
state of all current declarations (functions, variables, types, constants, imports) that once
declared, stay alive for subsequent cell executions.

The code is organized in the following files:

* `goexec.go`: definition of the main `State` object and the various Go structs for the various declarations
(functions, variables, types, constants, imports), including the concept of cursor.
* `execcode.go`: implements `State.ExecuteCell()`, the main functionality offered by the package.
* `composer.go`: generate dynamically a `main.go` file from pre-parsed declarations. It includes some
the code that renders teh various types of declarations, and a writer that keep tabs on the cursor position.
* `parser.go`: methods and objects used to parse the Go code from the cell, and again after `goimports` is run.
Loading

0 comments on commit c4a71e1

Please sign in to comment.