Compare commits
8 Commits
v0.3.4
...
ef0e093aaa
| Author | SHA1 | Date | |
|---|---|---|---|
| ef0e093aaa | |||
| 16f39fb87b | |||
| eb9261b478 | |||
| 0098052142 | |||
| a02326a0a6 | |||
| 1247683981 | |||
| 1d62857e14 | |||
| d83b128c30 |
@@ -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
|
||||||
|
|||||||
@@ -1,2 +1,22 @@
|
|||||||
// Package lexer provides basic helpers to implement parsers
|
// Package lexer provides basic helpers to implement parsers
|
||||||
package lexer
|
package lexer
|
||||||
|
|
||||||
|
// StateFn is a State Function of the parser
|
||||||
|
type StateFn func() (StateFn, error)
|
||||||
|
|
||||||
|
// Run runs a state machine until the state function either
|
||||||
|
// returns nil or an error
|
||||||
|
func Run(fn StateFn) error {
|
||||||
|
for fn != nil {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
fn, err = fn()
|
||||||
|
if err != nil {
|
||||||
|
// failed
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ended
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -64,3 +64,29 @@ func (p *Position) StepLine() {
|
|||||||
p.Line++
|
p.Line++
|
||||||
p.Column = 1
|
p.Column = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Next returns a new Position one rune forward
|
||||||
|
// on the line
|
||||||
|
func (p Position) Next() Position {
|
||||||
|
if p.Line == 0 {
|
||||||
|
p.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position{
|
||||||
|
Line: p.Line,
|
||||||
|
Column: p.Column + 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextLine returns a new Position at the begining of the next
|
||||||
|
// line.
|
||||||
|
func (p Position) NextLine() Position {
|
||||||
|
if p.Line == 0 {
|
||||||
|
p.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position{
|
||||||
|
Line: p.Line + 1,
|
||||||
|
Column: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -198,6 +198,41 @@ func (b *Reader) PeekRune() (rune, int, error) {
|
|||||||
return r, l, err
|
return r, l, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Accept consumes a rune from the source if it meets the condition.
|
||||||
|
// it returns true if the condition was met and false if it wasn't.
|
||||||
|
func (b *Reader) Accept(cond func(r rune) bool) bool {
|
||||||
|
r, _, err := b.ReadRune()
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return false
|
||||||
|
case cond(r):
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
_ = b.UnreadRune()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AcceptAll consumes runes from the source as long as they meet the
|
||||||
|
// condition. it returns true if the condition was met for at least one rune,
|
||||||
|
// and false if it wasn't.
|
||||||
|
func (b *Reader) AcceptAll(cond func(r rune) bool) bool {
|
||||||
|
var accepted bool
|
||||||
|
|
||||||
|
for {
|
||||||
|
r, _, err := b.ReadRune()
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return accepted
|
||||||
|
case cond(r):
|
||||||
|
accepted = true
|
||||||
|
default:
|
||||||
|
_ = b.UnreadRune()
|
||||||
|
return accepted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewReader creates a new runes [Reader] using the given [io.Reader]
|
// NewReader creates a new runes [Reader] using the given [io.Reader]
|
||||||
func NewReader(r io.Reader) *Reader {
|
func NewReader(r io.Reader) *Reader {
|
||||||
if r == nil {
|
if r == nil {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user