WIP: lexer: introduce StateFn, Accept()/AcceptAll() and Position to aid parsing #3
Closed
amery
wants to merge 3 commits from pr-amery-lexer
into main
2 changed files with 122 additions and 0 deletions
@ -0,0 +1,54 @@
|
||||
// Package lexer provides basic helpers to implement parsers
|
||||
package lexer |
||||
|
||||
import "io" |
||||
|
||||
// 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 |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,68 @@
|
||||
package lexer |
||||
|
||||
import "fmt" |
||||
|
||||
// Position indicates a line and column pair on a file.
|
||||
// Counting starts at 1.
|
||||
type Position struct { |
||||
Line int |
||||
Column int |
||||
} |
||||
|
||||
// String generates a pretty "(Line, Column)"" representation of the Position
|
||||
func (p Position) String() string { |
||||
if p.Line == 0 { |
||||
p.Reset() |
||||
} |
||||
|
||||
return fmt.Sprintf("(%v, %v)", p.Line, p.Column) |
||||
} |
||||
|
||||
// GoString generates a string representation of the Position for %#v usage
|
||||
func (p Position) GoString() string { |
||||
if p.Line == 0 { |
||||
p.Reset() |
||||
} |
||||
|
||||
return fmt.Sprintf("lexer.Position{%v, %v}", p.Line, p.Column) |
||||
} |
||||
|
||||
// Reset places a position at (1,1)
|
||||
func (p *Position) Reset() { |
||||
p.Line, p.Column = 1, 1 |
||||
} |
||||
|
||||
// Step moves the column one place
|
||||
func (p *Position) Step() { |
||||
if p.Line == 0 { |
||||
p.Reset() |
||||
} |
||||
|
||||
p.Column++ |
||||
} |
||||
|
||||
// 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, |
||||
} |
||||
} |
Loading…
Reference in new issue