11 Commits

Author SHA1 Message Date
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 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 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
amery 6cca2996ca lexer: Implement Reader.UnreadRune() and Reader.PeekRune()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 02:00:43 +00:00
amery edcba80baa lexer: fix ReadRune() to actually move the cursor
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 02:00:38 +00:00
amery 7230a74f49 lexer: runes.Reader renamed to lexer.Reader
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 01:59:09 +00:00
5 changed files with 57 additions and 120 deletions
+45
View File
@@ -0,0 +1,45 @@
package lexer
import (
"fmt"
"strings"
)
var (
_ error = (*Error)(nil)
)
// Error represents a generic parsing error
type Error struct {
Filename string
Line int
Column int
Content string
Err error
}
func (err Error) Error() string {
var s []string
switch {
case err.Line > 0 || err.Column > 0:
s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column))
case err.Filename != "":
s = append(s, err.Filename)
}
if err.Err != nil {
s = append(s, err.Err.Error())
}
if err.Content != "" {
s = append(s, fmt.Sprintf("%q", err.Content))
}
return strings.Join(s, ": ")
}
func (err Error) Unwrap() error {
return err.Err
}
-104
View File
@@ -1,106 +1,2 @@
// Package lexer provides basic helpers to implement parsers
package lexer
import (
"io"
"asciigoat.org/core/runes"
)
// Lexer adds Accept and AcceptAll support to a
// [io.RuneScanner]
type Lexer struct {
io.RuneScanner
}
// 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 (p Lexer) Accept(cond func(rune) bool) bool {
return Accept(p, cond)
}
// 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 (p Lexer) AcceptAll(cond func(rune) bool) bool {
return AcceptAll(p, cond)
}
// NewLexer extends a [io.RuneScanner] with Accept()/AcceptAll()
// functionality
func NewLexer(src io.RuneScanner) *Lexer {
if src == nil {
return nil
}
return &Lexer{src}
}
// NewLexerReader creates a Lexer from an [io.Reader]
func NewLexerReader(r io.Reader) *Lexer {
if r == nil {
return nil
}
return &Lexer{runes.NewReader(r)}
}
// NewLexerBytes creates a Lexer from raw bytes
func NewLexerBytes(b []byte) *Lexer {
return &Lexer{runes.NewReaderBytes(b)}
}
// NewLexerString creates a Lexer from a data string
func NewLexerString(s string) *Lexer {
return &Lexer{runes.NewReaderString(s)}
}
// 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 {
var err error
for fn != nil && err == nil {
fn, err = fn()
}
return 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 Accept(src io.RuneScanner, cond func(r rune) bool) bool {
r, _, err := src.ReadRune()
switch {
case err != nil:
return false
case cond(r):
return true
default:
_ = src.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 AcceptAll(src io.RuneScanner, cond func(r rune) bool) bool {
var accepted bool
for {
r, _, err := src.ReadRune()
switch {
case err != nil:
return accepted
case cond(r):
accepted = true
default:
_ = src.UnreadRune()
return accepted
}
}
}
+11 -13
View File
@@ -41,28 +41,26 @@ func (p *Position) Step() {
p.Column++
}
// Next returns a new Position one rune forward
// on the line
func (p Position) Next() Position {
// StepN moves the column N places forward
func (p *Position) StepN(n int) {
if p.Line == 0 {
p.Reset()
}
return Position{
Line: p.Line,
Column: p.Column + 1,
switch {
case n > 0:
p.Column += n
default:
panic(fmt.Errorf("invalid %v increment", n))
}
}
// NextLine returns a new Position at the begining of the next
// line.
func (p Position) NextLine() Position {
// StepLine moves position to the start of the next line
func (p *Position) StepLine() {
if p.Line == 0 {
p.Reset()
}
return Position{
Line: p.Line + 1,
Column: 1,
}
p.Line++
p.Column = 1
}
+1 -1
View File
@@ -1,4 +1,4 @@
package runes
package lexer
import (
"bytes"
-2
View File
@@ -1,2 +0,0 @@
// Package runes helps us work with runes
package runes