12 Commits

Author SHA1 Message Date
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 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
amery 5a7b19bb06 Merge pull request 'build-sys: import build system from darvaza.org/core' (#7)
Reviewed-on: #7
2023-08-29 17:34:16 +02:00
amery 6c189fd87d build-sys: import build system from darvaza.org/core
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 15:23:25 +00:00
amery 1d62857e14 lexer: introduce StateFn and the basic state machine loop
v2: make the error break more explicit

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 15:11:51 +00:00
amery f7e13e0978 Merge pull request 'lexer: introduce Error{}' (#6)
Reviewed-on: #6
2023-08-29 17:00:09 +02:00
amery f67d8a2443 lexer: introduce Error{}
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 13:54:23 +00:00
amery d83b128c30 lexer: introduce Reader.Accept()/AcceptAll()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 13:26:49 +00:00
amery 76e6146e9e Merge pull request 'introduce NewReadCloser to allow byte and string buffers to offer io.ReadCloser' (#1
Reviewed-on: #1
2023-08-29 15:24:36 +02:00
amery f79e2bee9e Merge pull request 'lexer: rename runes.Reader to lexer.Reader and implement UnreadRune() and PeekRune()' (#4)
Reviewed-on: #4
2023-08-29 15:23:15 +02:00
2 changed files with 28 additions and 16 deletions
+17 -3
View File
@@ -1,17 +1,31 @@
// 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)
// Run runs a state machine until the state function either // Run runs a state machine until the state function either
// returns nil or an error // returns nil or an error
func Run(fn StateFn) error { func Run(fn StateFn) error {
var err error for fn != nil {
var err error
for fn != nil && err == nil {
fn, err = fn() fn, err = fn()
switch {
case errors.Is(err, io.EOF):
// EOF
return nil
case err != nil:
// failed
return err
}
} }
return err // ended
return nil
} }
+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,
}
} }