saving the lexer

dev
Nick Dumas 2 years ago
parent c56128156e
commit 8098d75ae9

@ -3,12 +3,11 @@ package main
import ( import (
"log" "log"
"github.com/therealfakemoot/obsidian-parser" "github.com/therealfakemoot/wikilinks-parser/lexer"
) )
func main() { func main() {
l, tokens := lexer.Lex("[[foo/bar/butts.png]]") _, tokens := lexer.Lex(`[[foo/bar/butts.png]]`)
go l.Run()
for t := range tokens { for t := range tokens {
log.Printf("%#v\n", t) log.Printf("%#v\n", t)
} }

@ -1,3 +1,3 @@
module github.com/therealfakemoot/obsidian-parser module github.com/therealfakemoot/wikilinks-parser
go 1.19 go 1.19

@ -6,10 +6,21 @@ import (
"strings" "strings"
) )
type Token struct { type Token int
Raw string
} const (
LinkBeginToken = iota
LinkEndToken
LinkTextToken
LinkAliasBeginToken
LinkAliasTextToken
LinkSectionBeginToken
LinkSectionTextToken
LinkBlockBeginToken
LinkBlockTextToken
)
/*
var ( var (
LinkBeginToken = Token{Raw: "[["} LinkBeginToken = Token{Raw: "[["}
LinkEndToken = Token{Raw: "]]"} LinkEndToken = Token{Raw: "]]"}
@ -21,10 +32,12 @@ var (
LinkBlockBeginToken = Token{Raw: "^"} LinkBlockBeginToken = Token{Raw: "^"}
LinkBlockTextToken = Token{} LinkBlockTextToken = Token{}
) )
*/
type Lexer struct { type Lexer struct {
input []string input []string
pos int pos int
link bool
Tokens chan Token Tokens chan Token
} }
@ -79,13 +92,24 @@ func (l *Lexer) Run() {
} }
if s == "[" && l.Peek() == "[" { if s == "[" && l.Peek() == "[" {
l.link = true
l.Next() l.Next()
l.Emit(LinkBeginToken) l.Emit(LinkBeginToken)
switch s {
case "|":
l.Emit(LinkAliasBeginToken)
case "#":
l.Emit(LinkSectionBeginToken)
case "^":
l.Emit(LinkBlockBeginToken)
} }
if s == "]" && l.Peek() == "]" { if s == "]" && l.Peek() == "]" {
l.link = false
l.Next() l.Next()
l.Emit(LinkBeginToken) l.Emit(LinkEndToken)
}
} }
} }

@ -4,20 +4,40 @@ import (
"testing" "testing"
) )
func Test_Lexer(t *testing.T) { func Test_LexerSimple(t *testing.T) {
tt := []string{ tt := []string{
"[[Regular Link]]", "[[",
"]]",
"[[foo]]",
"[[foo]]",
"[[foo|bar]]",
}
for _, tc := range tt {
t.Run(tc, func(t *testing.T) {
t.Logf("checking %q", tc)
_, tokens := Lex(tc)
for tok := range tokens {
t.Logf("found token: %#v", tok)
}
})
}
}
/* /*
"![[Transcluded Link]]",
"[[Regular Link|Alias]]", func Test_LexerFull(t *testing.T) {
"[[Regular Link#Subsection of page]]", tt := []string{
"[[Regular Link^link to block]]", `[[Regular Link]]`,
"[[Regular Link#Subsection of page|Alias]]", `![[Transcluded Link]]`,
"[[Regular Link^link to block|Alias]]", `[[Regular Link|Alias]]`,
"[[Regular Link\|Alias]]", `[[Regular Link#Subsection of page]]`,
"[[Regular Link^link to block\|Alias]]", `[[Regular Link^link to block]]`,
"[[Regular Link#Subsection of page\|Alias]]", `[[Regular Link#Subsection of page|Alias]]`,
*/ `[[Regular Link^link to block|Alias]]`,
`[[Regular Link\|Alias]]`,
`[[Regular Link^link to block\|Alias]]`,
`[[Regular Link#Subsection of page\|Alias]]`,
} }
for _, tc := range tt { for _, tc := range tt {
@ -27,3 +47,4 @@ func Test_Lexer(t *testing.T) {
} }
} }
*/

@ -1,10 +0,0 @@
[[Regular Link]]
![[Transcluded Link]]
[[Regular Link|Alias]]
[[Regular Link#Subsection of page]]
[[Regular Link^link to block]]
[[Regular Link#Subsection of page|Alias]]
[[Regular Link^link to block|Alias]]
[[Regular Link\|Alias]]
[[Regular Link^link to block\|Alias]]
[[Regular Link#Subsection of page\|Alias]]
Loading…
Cancel
Save