docs: document remaining packages and simplify AST types #45
@@ -1,7 +1,7 @@
|
||||
package saccharine
|
||||
|
||||
type Expression interface {
|
||||
IsExpression()
|
||||
isExpression()
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
@@ -25,10 +25,10 @@ type Clause struct {
|
||||
Returns Expression
|
||||
}
|
||||
|
||||
func (Abstraction) IsExpression() {}
|
||||
func (Application) IsExpression() {}
|
||||
func (Atom) IsExpression() {}
|
||||
func (Clause) IsExpression() {}
|
||||
func (Abstraction) isExpression() {}
|
||||
func (Application) isExpression() {}
|
||||
func (Atom) isExpression() {}
|
||||
func (Clause) isExpression() {}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||
"git.maximhutz.com/max/lambda/pkg/trace"
|
||||
)
|
||||
|
||||
type TokenIterator = iterator.Iterator[Token]
|
||||
@@ -42,7 +41,7 @@ func parseToken(i *TokenIterator, expected TokenType, ignoreSoftBreaks bool) (*T
|
||||
|
||||
func parseString(i *TokenIterator) (string, error) {
|
||||
if tok, err := parseToken(i, TokenAtom, true); err != nil {
|
||||
return "", trace.Wrap(err, "no variable (col %d)", i.Index())
|
||||
return "", fmt.Errorf("no variable (col %d): %w", i.Index(), err)
|
||||
} else {
|
||||
return tok.Value, nil
|
||||
}
|
||||
@@ -64,7 +63,7 @@ func parseList[U any](i *TokenIterator, fn func(*TokenIterator) (U, error), mini
|
||||
for {
|
||||
if u, err := fn(i); err != nil {
|
||||
if len(results) < minimum {
|
||||
return nil, trace.Wrap(err, "expected at least '%v' items, got only '%v'", minimum, len(results))
|
||||
return nil, fmt.Errorf("expected at least '%v' items, got only '%v': %w", minimum, len(results), err)
|
||||
}
|
||||
return results, nil
|
||||
} else {
|
||||
@@ -76,11 +75,11 @@ func parseList[U any](i *TokenIterator, fn func(*TokenIterator) (U, error), mini
|
||||
func parseAbstraction(i *TokenIterator) (*Abstraction, error) {
|
||||
return iterator.Do(i, func(i *TokenIterator) (*Abstraction, error) {
|
||||
if _, err := parseToken(i, TokenSlash, true); err != nil {
|
||||
return nil, trace.Wrap(err, "no function slash (col %d)", i.MustGet().Column)
|
||||
return nil, fmt.Errorf("no function slash (col %d): %w", i.MustGet().Column, err)
|
||||
} else if parameters, err := parseList(i, parseString, 0); err != nil {
|
||||
return nil, err
|
||||
} else if _, err = parseToken(i, TokenDot, true); err != nil {
|
||||
return nil, trace.Wrap(err, "no function dot (col %d)", i.MustGet().Column)
|
||||
return nil, fmt.Errorf("no function dot (col %d): %w", i.MustGet().Column, err)
|
||||
} else if body, err := parseExpression(i); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
@@ -92,11 +91,11 @@ func parseAbstraction(i *TokenIterator) (*Abstraction, error) {
|
||||
func parseApplication(i *TokenIterator) (*Application, error) {
|
||||
return iterator.Do(i, func(i *TokenIterator) (*Application, error) {
|
||||
if _, err := parseToken(i, TokenOpenParen, true); err != nil {
|
||||
return nil, trace.Wrap(err, "no openning brackets (col %d)", i.MustGet().Column)
|
||||
return nil, fmt.Errorf("no openning brackets (col %d): %w", i.MustGet().Column, err)
|
||||
} else if expressions, err := parseList(i, parseExpression, 1); err != nil {
|
||||
return nil, err
|
||||
} else if _, err := parseToken(i, TokenCloseParen, true); err != nil {
|
||||
return nil, trace.Wrap(err, "no closing brackets (col %d)", i.MustGet().Column)
|
||||
return nil, fmt.Errorf("no closing brackets (col %d): %w", i.MustGet().Column, err)
|
||||
} else {
|
||||
return NewApplication(expressions[0], expressions[1:]), nil
|
||||
}
|
||||
@@ -105,7 +104,7 @@ func parseApplication(i *TokenIterator) (*Application, error) {
|
||||
|
||||
func parseAtom(i *TokenIterator) (*Atom, error) {
|
||||
if tok, err := parseToken(i, TokenAtom, true); err != nil {
|
||||
return nil, trace.Wrap(err, "no variable (col %d)", i.Index())
|
||||
return nil, fmt.Errorf("no variable (col %d): %w", i.Index(), err)
|
||||
} else {
|
||||
return NewAtom(tok.Value), nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"unicode"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||
"git.maximhutz.com/max/lambda/pkg/trace"
|
||||
)
|
||||
|
||||
// isVariables determines whether a rune can be a valid variable.
|
||||
@@ -51,7 +50,7 @@ func scanToken(i *iterator.Iterator[rune]) (*Token, error) {
|
||||
|
||||
letter, err := i.Next()
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err, "cannot produce next token")
|
||||
return nil, fmt.Errorf("cannot produce next token: %w", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
@@ -82,7 +81,7 @@ func scanToken(i *iterator.Iterator[rune]) (*Token, error) {
|
||||
for !i.Done() {
|
||||
r, err := i.Next()
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err, "error while parsing comment")
|
||||
return nil, fmt.Errorf("error while parsing comment: %w", err)
|
||||
}
|
||||
|
||||
if r == '\n' {
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Indent(s string, size int) string {
|
||||
lines := strings.Lines(s)
|
||||
indent := strings.Repeat(" ", size)
|
||||
|
||||
indented := ""
|
||||
for line := range lines {
|
||||
indented += indent + line
|
||||
}
|
||||
|
||||
return indented
|
||||
}
|
||||
|
||||
func Wrap(child error, format string, a ...any) error {
|
||||
parent := fmt.Errorf(format, a...)
|
||||
childErrString := Indent(child.Error(), 4)
|
||||
return errors.New(parent.Error() + "\n" + childErrString)
|
||||
}
|
||||
Reference in New Issue
Block a user