From 123770119e00fa00fcfd3108c08fca1c9a8c6f0e Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 2 Sep 2023 22:03:06 +0000 Subject: [PATCH] parser: TextParser [WIP] Signed-off-by: Alejandro Mery --- parser/parser.go | 2 ++ parser/text.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ parser/token.go | 31 +++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 parser/parser.go create mode 100644 parser/text.go create mode 100644 parser/token.go diff --git a/parser/parser.go b/parser/parser.go new file mode 100644 index 0000000..04bd993 --- /dev/null +++ b/parser/parser.go @@ -0,0 +1,2 @@ +// Package parser implements some common logic of asciigoat based parsers +package parser diff --git a/parser/text.go b/parser/text.go new file mode 100644 index 0000000..d463a95 --- /dev/null +++ b/parser/text.go @@ -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)) +} diff --git a/parser/token.go b/parser/token.go new file mode 100644 index 0000000..9cc4405 --- /dev/null +++ b/parser/token.go @@ -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) +}