@@ -0,0 +1,2 @@
|
|||||||
|
// Package parser implements some common logic of asciigoat based parsers
|
||||||
|
package parser
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"asciigoat.org/core/lexer"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TextParser ...
|
||||||
|
type TextParser struct {
|
||||||
|
src *lexer.Reader
|
||||||
|
pos lexer.Position
|
||||||
|
}
|
||||||
|
|
||||||
|
// Position ...
|
||||||
|
func (p *TextParser) Position() lexer.Position {
|
||||||
|
return p.pos
|
||||||
|
}
|
||||||
|
|
||||||
|
// StepLine ...
|
||||||
|
func (p *TextParser) StepLine() {
|
||||||
|
p.src.Discard()
|
||||||
|
p.pos.StepLine()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step ...
|
||||||
|
func (p *TextParser) Step() {
|
||||||
|
s := p.src.Emit()
|
||||||
|
p.pos.StepN(len(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit ...
|
||||||
|
func (p *TextParser) Emit() Token {
|
||||||
|
t := Token{
|
||||||
|
Value: p.src.Emit(),
|
||||||
|
Position: p.pos,
|
||||||
|
}
|
||||||
|
|
||||||
|
p.pos.StepN(t.Len())
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
// Peek ...
|
||||||
|
func (p *TextParser) Peek() string {
|
||||||
|
return p.src.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTextParser ...
|
||||||
|
func NewTextParser(r io.Reader) *TextParser {
|
||||||
|
if r == nil {
|
||||||
|
r = strings.NewReader("")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &TextParser{
|
||||||
|
src: lexer.NewReader(r),
|
||||||
|
}
|
||||||
|
p.pos.Reset()
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTextParserBytes ...
|
||||||
|
func NewTextParserBytes(b []byte) *TextParser {
|
||||||
|
return NewTextParser(bytes.NewReader(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTextParserString ...
|
||||||
|
func NewTextParserString(s string) *TextParser {
|
||||||
|
return NewTextParser(strings.NewReader(s))
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"asciigoat.org/core/lexer"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Token ...
|
||||||
|
type Token struct {
|
||||||
|
lexer.Position
|
||||||
|
|
||||||
|
Type uint
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len ...
|
||||||
|
func (t Token) Len() int {
|
||||||
|
return len(t.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// String generates the '%v' value
|
||||||
|
func (t Token) String() string {
|
||||||
|
return t.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// GoString generates the '%#v' value
|
||||||
|
func (t Token) GoString() string {
|
||||||
|
return fmt.Sprintf("Token{Position{%v,%v}, %v, %q}",
|
||||||
|
t.Line, t.Column, t.Type, t.Value)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user