Skip to content

Commit

Permalink
Merge pull request #8 from kenniaa/multiple-errors
Browse files Browse the repository at this point in the history
Multiple errors
  • Loading branch information
mbStavola authored Nov 1, 2020
2 parents ef03950 + a22e90f commit d24259f
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 250 deletions.
18 changes: 7 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package main
import (
"flag"
"fmt"
"github.com/mbStavola/slydes/pkg/lang"
"github.com/mbStavola/slydes/render/html"
"github.com/mbStavola/slydes/render/native"
"os"
"strings"

"github.com/mbStavola/slydes/pkg/lang"
"github.com/mbStavola/slydes/render/html"
)

func main() {
filename := flag.String("file", "", "slide to open")
output := flag.String("out", "html", "method of display (html, native)")
output := flag.String("out", "noop", "method of display (noop, html)")
debug := flag.Bool("debug", false, "print debug info")

flag.Parse()
Expand All @@ -23,8 +23,8 @@ func main() {
} else if !strings.HasSuffix(*filename, ".sly") {
fmt.Print("Only .sly files are supported")
return
} else if *output != "native" && *output != "html" {
fmt.Print("Output must be either native or html")
} else if *output != "native" && *output != "html" && *output != "noop" {
fmt.Print("Output must be either noop or html")
return
}

Expand All @@ -47,11 +47,7 @@ func main() {
}

switch *output {
case "native":
if err := native.Render(show); err != nil {
fmt.Print(err)
}

case "noop":
break
case "html":
if err := html.Render(show); err != nil {
Expand Down
13 changes: 11 additions & 2 deletions pkg/lang/compile.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package lang

import (
"errors"
"fmt"
"github.com/mbStavola/slydes/pkg/types"
"image/color"

"github.com/mbStavola/slydes/pkg/types"
)

type Compiler interface {
Expand All @@ -18,13 +20,20 @@ func NewDefaultCompiler() DefaultCompiler {

func (comp DefaultCompiler) Compile(statements []Statement) (types.Show, error) {
state := newCompilationState()
errBundle := newErrorInfoBundle()

for _, statement := range statements {
if err := state.processStatement(statement); err != nil {
if err := state.processStatement(statement); err != nil && errors.As(err, &ErrorInfo{}) {
errBundle.Add(err.(ErrorInfo))
} else if err != nil {
return types.Show{}, err
}
}

if errBundle.HasErrors() {
return types.Show{}, errBundle
}

return state.finalizeCompilation(), nil
}

Expand Down
31 changes: 30 additions & 1 deletion pkg/lang/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
package lang

import "fmt"
import (
"fmt"
"strings"
)

type ErrorInfoBundle struct {
errors []ErrorInfo
}

func newErrorInfoBundle() ErrorInfoBundle {
return ErrorInfoBundle{errors: make([]ErrorInfo, 0)}
}

func (b *ErrorInfoBundle) Add(err ErrorInfo) {
b.errors = append(b.errors, err)
}

func (b ErrorInfoBundle) HasErrors() bool {
return len(b.errors) > 0
}

func (b ErrorInfoBundle) Error() string {
builder := strings.Builder{}
for _, err := range b.errors {
builder.WriteString(err.Error())
builder.WriteByte('\n')
}

return builder.String()
}

type ErrorInfo struct {
line uint
Expand Down
Loading

0 comments on commit d24259f

Please sign in to comment.