Compare commits
2 Commits
ef0e093aaa
..
v0.3.5
| Author | SHA1 | Date | |
|---|---|---|---|
| f8f6ff9e11 | |||
| 05d504346e |
@@ -1,7 +1,6 @@
|
|||||||
package lexer
|
package lexer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -10,14 +9,6 @@ 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
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,29 +64,3 @@ 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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
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