Skip to content

Commit

Permalink
feat(scanner): add '?' and 'include' token
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 7, 2023
1 parent f5cb431 commit 34738da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func (s *Scanner) Parse() {
tokenKind = UNDERSCORE
case '*':
tokenKind = STAR
case '?':
tokenKind = QUESTIONMARK
case '\n':
s.addToken(NEWLINE, "")
s.advanceLine()
Expand All @@ -145,7 +147,7 @@ func (s *Scanner) Parse() {
out:
for {
switch s.curChar {
case '\n', '!', '#', '_', '*', '-', '[', ']', '(', ')', '`', '>':
case '\n', '!', '#', '_', '*', '-', '[', ']', '(', ')', '`', '>', '?':
break out
}

Expand All @@ -155,6 +157,14 @@ func (s *Scanner) Parse() {

// skip empty texts
if res.Len() != 0 {
result := res.String()
if result[0] == 'i' && strings.HasPrefix(result, "include") {
split := strings.Split(result, " ")
if len(split) >= 2 {
s.addToken(INCLUDE, split[1])
continue
}
}
s.addToken(TEXT, res.String())
}
// INFO: this skips adding the text again
Expand Down
9 changes: 9 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,22 @@ func TestHeadings(t *testing.T) {
STRAIGHTBRACECLOSE,
PARENOPEN,
TEXT,
QUESTIONMARK,
TEXT,
PARENCLOSE,
NEWLINE,
BACKTICK,
TEXT,
BACKTICK,
NEWLINE,
QUESTIONMARK,
INCLUDE,
NEWLINE,
QUESTIONMARK,
TEXT,
NEWLINE,
}
s.PrintTokens()
if len(tokens) != len(expectedTokens) {
t.Errorf("expected %d tokens, got: %d", len(expectedTokens), len(tokens))
}
Expand Down
3 changes: 3 additions & 0 deletions scanner/tests/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
![xnacly](https://avatars.githubusercontent.com/u/47723417?v=4)

`inline code`

?include test.md
?include
4 changes: 4 additions & 0 deletions scanner/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
BACKTICK
GREATERTHAN
BANG
QUESTIONMARK
INCLUDE
)

var TOKEN_LOOKUP_MAP = map[uint]string{
Expand All @@ -35,6 +37,8 @@ var TOKEN_LOOKUP_MAP = map[uint]string{
PARENCLOSE: "PARENCLOSE",
GREATERTHAN: "GREATERTHAN",
BACKTICK: "BACKTICK",
QUESTIONMARK: "QUESTIONMARK",
INCLUDE: "INCLUDE",
TEXT: "TEXT",
BANG: "BANG",
}

0 comments on commit 34738da

Please sign in to comment.