Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f0b793ff25 | |||
| b697391f24 | |||
| 55f129f681 | |||
| dad0b2c7a4 | |||
| fee7165fbf | |||
| 1e75557bc3 | |||
| c1d3f61b73 | |||
| d081fe7d1c | |||
| 17ddff3527 |
@@ -7,6 +7,7 @@ replace asciigoat.org/core => ../core
|
|||||||
require (
|
require (
|
||||||
asciigoat.org/core v0.3.6
|
asciigoat.org/core v0.3.6
|
||||||
github.com/mgechev/revive v1.3.3
|
github.com/mgechev/revive v1.3.3
|
||||||
|
golang.org/x/tools v0.12.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -21,6 +22,6 @@ require (
|
|||||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
golang.org/x/mod v0.12.0 // indirect
|
||||||
golang.org/x/sys v0.11.0 // indirect
|
golang.org/x/sys v0.11.0 // indirect
|
||||||
golang.org/x/tools v0.12.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
|
|||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||||
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||||
|
|||||||
+13
-39
@@ -1,16 +1,14 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
"asciigoat.org/core/lexer"
|
"asciigoat.org/core/lexer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run parses the source
|
// Run parses the source
|
||||||
func (p *Parser) Run() error {
|
func (p *Parser) Run() error {
|
||||||
p.setDefaults()
|
p.setDefaults()
|
||||||
|
|
||||||
p.pos.Reset()
|
p.pos.Reset()
|
||||||
|
|
||||||
return lexer.Run(p.lexStart)
|
return lexer.Run(p.lexStart)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,29 +22,26 @@ func (p *Parser) lexStart() (lexer.StateFn, error) {
|
|||||||
case IsNewLine(r):
|
case IsNewLine(r):
|
||||||
// new line
|
// new line
|
||||||
p.lexNewLine(r)
|
p.lexNewLine(r)
|
||||||
p.src.Discard()
|
p.stepLine()
|
||||||
p.pos.StepLine()
|
|
||||||
case IsSpace(r):
|
case IsSpace(r):
|
||||||
// whitespace
|
// whitespace
|
||||||
p.src.Discard()
|
p.stepRune()
|
||||||
p.pos.Step()
|
|
||||||
case IsCommentStart(r):
|
|
||||||
// switch to comment lexer
|
|
||||||
p.src.UnreadRune()
|
|
||||||
return p.lexComment, nil
|
|
||||||
case IsSectionStart(r):
|
|
||||||
// section
|
|
||||||
p.src.Discard()
|
|
||||||
p.pos.Step()
|
|
||||||
return p.lexSectionStart, nil
|
|
||||||
default:
|
default:
|
||||||
// entry
|
// token
|
||||||
p.src.UnreadRune()
|
p.src.UnreadRune()
|
||||||
return p.lexEntryStart, nil
|
return p.lexToken, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Parser) lexToken() (lexer.StateFn, error) {
|
||||||
|
p.src.AcceptAll(IsNotSpace)
|
||||||
|
|
||||||
|
p.pushString(TokenUnknown)
|
||||||
|
|
||||||
|
return p.lexStart, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Parser) lexNewLine(r1 rune) {
|
func (p *Parser) lexNewLine(r1 rune) {
|
||||||
// r1 is warrantied to be either \n or \r
|
// r1 is warrantied to be either \n or \r
|
||||||
r2, _, err := p.src.ReadRune()
|
r2, _, err := p.src.ReadRune()
|
||||||
@@ -76,24 +71,3 @@ func (p *Parser) lexNewLine(r1 rune) {
|
|||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) lexComment() (lexer.StateFn, error) {
|
|
||||||
// until the end of the line
|
|
||||||
p.src.AcceptAll(IsNotNewLine)
|
|
||||||
|
|
||||||
err := p.OnComment(p.pos, p.src.Emit())
|
|
||||||
return p.lexStart, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Parser) lexSectionStart() (lexer.StateFn, error) { return nil, nil }
|
|
||||||
func (*Parser) lexEntryStart() (lexer.StateFn, error) { return nil, nil }
|
|
||||||
|
|
||||||
func (p *Parser) lexToken() (lexer.StateFn, error) {
|
|
||||||
p.src.AcceptAll(IsNotSpace)
|
|
||||||
|
|
||||||
s := p.src.Emit()
|
|
||||||
log.Printf("%s: %s: %q", p.pos, "token", s)
|
|
||||||
p.pos.StepN(len(s))
|
|
||||||
|
|
||||||
return p.lexStart, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,7 +13,3 @@ func (p *Parser) emitError(content string, err error) (lexer.StateFn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) emitInvalidRune(r rune) (lexer.StateFn, error) {
|
|
||||||
return p.emitError(string([]rune{r}), lexer.ErrUnacceptableRune)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
func (p *Parser) push(tok Token) {
|
||||||
|
n := len(p.queue)
|
||||||
|
p.queue = append(p.queue, tok)
|
||||||
|
|
||||||
|
log.Printf("queue[%v]: %s", n, tok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) pushString(typ TokenType) {
|
||||||
|
s := p.src.Emit()
|
||||||
|
|
||||||
|
el := Token{
|
||||||
|
Type: typ,
|
||||||
|
Value: s,
|
||||||
|
Position: p.pos,
|
||||||
|
}
|
||||||
|
|
||||||
|
p.pos.StepN(len(s))
|
||||||
|
|
||||||
|
p.push(el)
|
||||||
|
}
|
||||||
|
|
||||||
|
// stepLine discards the data and moves the position
|
||||||
|
// to the next line
|
||||||
|
func (p *Parser) stepLine() {
|
||||||
|
p.src.Discard()
|
||||||
|
p.pos.StepLine()
|
||||||
|
}
|
||||||
|
|
||||||
|
// stepRune discards the data and moves the position
|
||||||
|
// on rune forward on the same line
|
||||||
|
func (p *Parser) stepRune() {
|
||||||
|
p.src.Discard()
|
||||||
|
p.pos.Step()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) stepString() string {
|
||||||
|
s := p.src.Emit()
|
||||||
|
p.pos.StepN(len(s))
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -11,10 +11,9 @@ var (
|
|||||||
IsNotNewLine = lexer.NewIsNot(IsNewLine)
|
IsNotNewLine = lexer.NewIsNot(IsNewLine)
|
||||||
// IsNotSpace tells if a rune is anything other than whitespace
|
// IsNotSpace tells if a rune is anything other than whitespace
|
||||||
IsNotSpace = lexer.NewIsNot(IsSpace)
|
IsNotSpace = lexer.NewIsNot(IsSpace)
|
||||||
|
|
||||||
IsCommentStart = lexer.NewIsIn(";#")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsSectionStart(r rune) bool {
|
// IsSpaceNotNewLine indicates a rune is whitespace but not a new line
|
||||||
return r == '['
|
func IsSpaceNotNewLine(r rune) bool {
|
||||||
|
return IsSpace(r) && !IsNewLine(r)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -11,7 +11,8 @@ import (
|
|||||||
type Parser struct {
|
type Parser struct {
|
||||||
src *lexer.Reader
|
src *lexer.Reader
|
||||||
|
|
||||||
pos lexer.Position
|
pos lexer.Position
|
||||||
|
queue []Token
|
||||||
|
|
||||||
// OnSection is called after a [section] is parsed.
|
// OnSection is called after a [section] is parsed.
|
||||||
// Returning an error will abort the process.
|
// Returning an error will abort the process.
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
//go:generate go run golang.org/x/tools/cmd/stringer -type=TokenType
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"asciigoat.org/core/lexer"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A TokenType is a type of Token
|
||||||
|
type TokenType uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
// TokenUnknown represents a Token that hasn't been identified
|
||||||
|
TokenUnknown TokenType = iota
|
||||||
|
// TokenSectionName represents the section name between the squared brackets
|
||||||
|
TokenSectionName
|
||||||
|
// TokenSectionSubname represents a secondary name in the section represented
|
||||||
|
// between quotes after the section name.
|
||||||
|
// e.g.
|
||||||
|
// [section_name "section_subname"]
|
||||||
|
TokenSectionSubname
|
||||||
|
// TokenComment represents a comment, including the initial ';' or '#' until
|
||||||
|
// the end of the line
|
||||||
|
TokenComment
|
||||||
|
// TokenFieldKey represents a field name in a `key = value` entry
|
||||||
|
TokenFieldKey
|
||||||
|
// TokenFieldValue represents a field value in a `key = value` entry
|
||||||
|
TokenFieldValue
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Token is an element from the document
|
||||||
|
type Token struct {
|
||||||
|
Type TokenType
|
||||||
|
Position lexer.Position
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Token) String() string {
|
||||||
|
return fmt.Sprintf("%v:%v: %s: %q", t.Position.Line, t.Position.Column, t.Type, t.Value)
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// Code generated by "stringer -type=TokenType"; DO NOT EDIT.
|
||||||
|
|
||||||
|
package parser
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[TokenUnknown-0]
|
||||||
|
_ = x[TokenSectionName-1]
|
||||||
|
_ = x[TokenSectionSubname-2]
|
||||||
|
_ = x[TokenComment-3]
|
||||||
|
_ = x[TokenFieldKey-4]
|
||||||
|
_ = x[TokenFieldValue-5]
|
||||||
|
}
|
||||||
|
|
||||||
|
const _TokenType_name = "TokenUnknownTokenSectionNameTokenSectionSubnameTokenCommentTokenFieldKeyTokenFieldValue"
|
||||||
|
|
||||||
|
var _TokenType_index = [...]uint8{0, 12, 28, 47, 59, 72, 87}
|
||||||
|
|
||||||
|
func (i TokenType) String() string {
|
||||||
|
if i >= TokenType(len(_TokenType_index)-1) {
|
||||||
|
return "TokenType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
return _TokenType_name[_TokenType_index[i]:_TokenType_index[i+1]]
|
||||||
|
}
|
||||||
@@ -4,4 +4,5 @@ package tools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/mgechev/revive"
|
_ "github.com/mgechev/revive"
|
||||||
|
_ "golang.org/x/tools/cmd/stringer"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user