2 Commits

Author SHA1 Message Date
amery c87cc3fe89 build-sys: use local asciigoat.org/core [DO-NOT-MERGE]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 02:08:05 +01:00
amery 6f10144b94 parser: implement initial tokeniser
only logging position, errors and non-whitespace elements

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 02:06:31 +01:00
5 changed files with 87 additions and 2 deletions
+2
View File
@@ -2,6 +2,8 @@ module asciigoat.org/ini
go 1.19
replace asciigoat.org/core => ../core
require (
asciigoat.org/core v0.3.4
github.com/mgechev/revive v1.3.3
-2
View File
@@ -1,5 +1,3 @@
asciigoat.org/core v0.3.4 h1:FQ4IvqQS7T1wke0dOeKUABtNY+JQHmccmedBjz18j9s=
asciigoat.org/core v0.3.4/go.mod h1:tXj+JUutxRbcO40ZQRuUVaZ4rnYz1kAZ0nblisV8u74=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab h1:5JxePczlyGAtj6R1MUEFZ/UFud6FfsOejq7xLC2ZIb0=
+69
View File
@@ -0,0 +1,69 @@
package parser
import (
"log"
"asciigoat.org/core/lexer"
)
// Run parses the source
func (p *Parser) Run() error {
p.pos.Reset()
return lexer.Run(p.lexStart)
}
func (p *Parser) lexStart() (lexer.StateFn, error) {
for {
r, _, err := p.src.ReadRune()
switch {
case err != nil:
// read error
log.Printf("%s: %s: %s", p.pos, "error", err)
return nil, err
case IsNewLine(r):
// new line
p.src.UnreadRune()
p.lexNewLine()
p.src.Discard()
p.pos.StepLine()
case IsSpace(r):
// whitespace
p.src.Discard()
p.pos.Step()
default:
// token
p.src.UnreadRune()
return p.lexToken, 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
}
func (p *Parser) lexNewLine() {
// r1 is warrantied to be either \n or \r
r1, _, _ := p.src.ReadRune()
r2, _, err := p.src.ReadRune()
switch {
case r1 == '\r' && r2 == '\n':
// CR LN
case r1 == '\r' && err == nil:
// CR
p.src.UnreadRune()
case r2 == '\r':
// LN CR
case err == nil:
// LN
p.src.UnreadRune()
}
}
+14
View File
@@ -0,0 +1,14 @@
package parser
import "asciigoat.org/core/lexer"
var (
// IsNewLine tells if a rune represents a line break or the start of one
IsNewLine = lexer.NewIsIn("\n\r")
// IsSpace tells if a rune is considered whitespace by unicode
IsSpace = lexer.IsSpace
// IsNotNewLine tells if a rune is anything other than line breaks
IsNotNewLine = lexer.NewIsNot(IsNewLine)
// IsNotSpace tells if a rune is anything other than whitespace
IsNotSpace = lexer.NewIsNot(IsSpace)
)
+2
View File
@@ -10,6 +10,8 @@ import (
// Parser parses a dosini-style document
type Parser struct {
src *lexer.Reader
pos lexer.Position
}
// NewParser creates a dosini-style parser using