8 Commits

Author SHA1 Message Date
amery 3bd2c58fc3 Merge pull request 'lexer: constants and helpers' (#9)
Reviewed-on: #9
2023-08-30 19:44:26 +02:00
amery f8f6ff9e11 Merge pull request 'lexer: Reader.Accept(), AcceptAll(), and StateFn' (#5)
Reviewed-on: #5
2023-08-30 19:02:15 +02:00
amery 05d504346e lexer: extend Run() to treat io.EOF as non-error termination
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 01:50:12 +01:00
amery eb9261b478 lexer: NewIsNot(), NewIsIn(), NewIsOneOf()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 23:30:51 +00:00
amery 0098052142 lexer: IsSpace()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 23:30:47 +00:00
amery a02326a0a6 lexer: ErrUnacceptableRune and ErrNotImplemented
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 23:30:44 +00:00
amery 93c58cdc26 Merge pull request 'lexer: introduce a Position (Line, Column) handler' (#8)
Reviewed-on: #8
2023-08-29 22:14:57 +02:00
amery 9425ba0f7c lexer: introduce a Position (Line, Column) handler
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 16:22:59 +00:00
4 changed files with 71 additions and 14 deletions
+9
View File
@@ -1,6 +1,7 @@
package lexer package lexer
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@@ -9,6 +10,14 @@ var (
_ error = (*Error)(nil) _ error = (*Error)(nil)
) )
var (
// ErrUnacceptableRune indicates the read rune isn't acceptable in the context
ErrUnacceptableRune = errors.New("rune not acceptable in context")
// ErrNotImplemented indicates something hasn't been implemented yet
ErrNotImplemented = errors.New("not implemented")
)
// Error represents a generic parsing error // Error represents a generic parsing error
type Error struct { type Error struct {
Filename string Filename string
+10 -1
View File
@@ -1,6 +1,11 @@
// Package lexer provides basic helpers to implement parsers // Package lexer provides basic helpers to implement parsers
package lexer package lexer
import (
"errors"
"io"
)
// StateFn is a State Function of the parser // StateFn is a State Function of the parser
type StateFn func() (StateFn, error) type StateFn func() (StateFn, error)
@@ -11,7 +16,11 @@ func Run(fn StateFn) error {
var err error var err error
fn, err = fn() fn, err = fn()
if err != nil { switch {
case errors.Is(err, io.EOF):
// EOF
return nil
case err != nil:
// failed // failed
return err return err
} }
+11 -13
View File
@@ -41,28 +41,26 @@ func (p *Position) Step() {
p.Column++ p.Column++
} }
// Next returns a new Position one rune forward // StepN moves the column N places forward
// on the line func (p *Position) StepN(n int) {
func (p Position) Next() Position {
if p.Line == 0 { if p.Line == 0 {
p.Reset() p.Reset()
} }
return Position{ switch {
Line: p.Line, case n > 0:
Column: p.Column + 1, p.Column += n
default:
panic(fmt.Errorf("invalid %v increment", n))
} }
} }
// NextLine returns a new Position at the begining of the next // StepLine moves position to the start of the next line
// line. func (p *Position) StepLine() {
func (p Position) NextLine() Position {
if p.Line == 0 { if p.Line == 0 {
p.Reset() p.Reset()
} }
return Position{ p.Line++
Line: p.Line + 1, p.Column = 1
Column: 1,
}
} }
+41
View File
@@ -0,0 +1,41 @@
package lexer
import (
"strings"
"unicode"
)
// NewIsNot generates a rune condition checker that reverses the
// decision of the given checker.
func NewIsNot(cond func(rune) bool) func(rune) bool {
return func(r rune) bool {
return !cond(r)
}
}
// NewIsIn generates a rune condition checker that accepts runes
// contained on the provided string
func NewIsIn(s string) func(rune) bool {
return func(r rune) bool {
return strings.ContainsRune(s, r)
}
}
// NewIsOneOf generates a run condition checker that accepts runes
// accepted by any of the given checkers
func NewIsOneOf(s ...func(rune) bool) func(rune) bool {
return func(r rune) bool {
for _, cond := range s {
if cond(r) {
return true
}
}
return false
}
}
// IsSpace reports whether the rune is a space character as
// defined by Unicode's White Space property
func IsSpace(r rune) bool {
return unicode.IsSpace(r)
}