Skip to content

Commit 169a635

Browse files
committed
Add godoc comments
1 parent 05dcc69 commit 169a635

14 files changed

Lines changed: 41 additions & 0 deletions

File tree

ast/ast.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package ast defines the abstract syntax tree for the Scanspec language.
12
package ast
23

34
import (
@@ -6,10 +7,12 @@ import (
67
"github.com/alecthomas/participle/v2/lexer"
78
)
89

10+
// Node is the interface implemented by all AST nodes.
911
type Node interface {
1012
node()
1113
}
1214

15+
// Source is the root node of a Scanspec AST.
1316
type Source struct {
1417
Block Block `@@`
1518
}

ast/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var parser = participle.MustBuild[Source](participle.Lexer(lexer.MustSimple([]le
2222
participle.UseLookahead(2),
2323
)
2424

25+
// ParseString parses a Scanspec source string and returns the AST.
2526
func ParseString(filename string, s string) (*Source, error) {
2627
n, err := parser.ParseString(filename, s)
2728
if err != nil {

ast/walk.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package ast
22

33
import "fmt"
44

5+
// Visitor defines the interface for AST visitors. If Visit returns a
6+
// non-nil Visitor, Walk recurses into the node's children with that visitor.
57
type Visitor interface {
68
Visit(Node) Visitor
79
}
810

11+
// Walk traverses an AST in depth-first order. It calls v.Visit(n), and if that
12+
// returns a non-nil visitor, it recursively walks each child of n.
913
func Walk(v Visitor, n Node) {
1014
v = v.Visit(n)
1115
if v == nil {
@@ -198,6 +202,8 @@ func (f inspector) Visit(node Node) Visitor {
198202
return nil
199203
}
200204

205+
// Inspect traverses an AST, calling f for each node. If f returns false,
206+
// Inspect does not recurse into the node's children.
201207
func Inspect(node Node, f func(Node) bool) {
202208
Walk(inspector(f), node)
203209
}

eval/builtin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
)
99

10+
// Functions maps built-in function names to their implementations.
1011
var Functions = map[string]func(args ...interface{}) (interface{}, error){
1112
"len": func(args ...interface{}) (interface{}, error) {
1213
s, ok := args[0].(string)

eval/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func (e ErrBadParse) Error() string {
144144
return fmt.Sprintf("%d:%d~%d:%d: parse error: want %s, got %q", e.Pos.Line, e.Pos.Column, e.Cursor.Ln, e.Cursor.Col, e.Want, e.Got)
145145
}
146146

147+
// Cursor represents a position in the input stream.
147148
type Cursor struct {
148149
Ln, Col int
149150
}

eval/evaluator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package eval evaluates a Scanspec AST against input data, scanning values
2+
// and validating constraints.
13
package eval
24

35
import (
@@ -17,6 +19,8 @@ type evaluator struct {
1719
Values Values
1820
}
1921

22+
// Evaluate executes a Scanspec AST against the provided input, returning the
23+
// scanned values or an error if a constraint check fails or parsing fails.
2024
func Evaluate(source *ast.Source, input io.Reader, options ...Option) (values Values, err error) {
2125
e := evaluator{
2226
Source: source,

eval/option.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eval
22

3+
// Option configures the evaluator.
34
type Option interface {
45
apply(*evaluator)
56
}
@@ -10,6 +11,8 @@ func (f optionFunc) apply(e *evaluator) {
1011
f(e)
1112
}
1213

14+
// ScannerBuffer returns an Option that sets the initial buffer and maximum
15+
// buffer size for the input scanner.
1316
func ScannerBuffer(buf []byte, max int) Option {
1417
return optionFunc(func(e *evaluator) {
1518
e.Input.sc.Buffer(buf, max)

eval/type.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package eval
22

33
import "reflect"
44

5+
// Types maps Scanspec type names to their Go reflect.Type equivalents.
56
var Types = map[string]reflect.Type{
67
"bool": reflect.TypeOf(bool(false)),
78
"int": reflect.TypeOf(int(0)),
@@ -11,4 +12,5 @@ var Types = map[string]reflect.Type{
1112
"string": reflect.TypeOf(string("")),
1213
}
1314

15+
// Values holds the scanned variable values, keyed by variable name.
1416
type Values map[string]reflect.Value

gen/c99/c.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2020 Furqan Software Ltd. All rights reserved.
22

3+
// Package c99 generates C99 source code from a Scanspec AST.
34
package c99
45

56
import (
@@ -13,11 +14,14 @@ import (
1314
"git.furqansoftware.net/toph/scanlib/gen/code"
1415
)
1516

17+
// Generator walks a Scanspec AST and emits C99 source code.
1618
type Generator struct {
1719
ctx *Context
1820
err error
1921
}
2022

23+
// Generate generates C99 source code from a Scanspec AST. It returns
24+
// [gen.ErrUnsupportedType] if the Scanspec uses the string type.
2125
func Generate(n *ast.Source) ([]byte, error) {
2226
ctx := Context{
2327
types: map[string]string{},

gen/code/writer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2020 Furqan Software Ltd. All rights reserved.
22

3+
// Package code provides utilities for code generation.
34
package code
45

56
import (
@@ -8,6 +9,7 @@ import (
89
"strings"
910
)
1011

12+
// Writer is an indentation-aware code writer.
1113
type Writer struct {
1214
buf *bytes.Buffer
1315

@@ -17,6 +19,8 @@ type Writer struct {
1719
r, c int
1820
}
1921

22+
// NewWriter returns a new Writer that uses the given string for each
23+
// indentation level.
2024
func NewWriter(indent string) *Writer {
2125
return &Writer{
2226
buf: &bytes.Buffer{},

0 commit comments

Comments
 (0)