-
Notifications
You must be signed in to change notification settings - Fork 1
/
MarkdownToMaml.Tests.ps1
181 lines (157 loc) · 4.44 KB
/
MarkdownToMaml.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Set-StrictMode -Version Latest
enum TokenType
{
HeaderL1
HeaderL2
HeaderL3
HeaderL4
Code
Word
HyperLink
}
function GetHeaderByLevel([int]$level)
{
switch ($level)
{
1 { return [TokenType]::HeaderL1 }
2 { return [TokenType]::HeaderL2 }
3 { return [TokenType]::HeaderL3 }
4 { return [TokenType]::HeaderL4 }
default {
throw "Illegal header level: $level"
}
}
}
class Extent
{
[int]$startOffset
[int]$endOffset
}
class Token
{
[TokenType] $tokenType
[string] $text
[Extent] $extent
}
class Tokenizer
{
hidden [string] $inputText;
hidden [int] $offset;
Tokenizer([string]$inputText)
{
$this.inputText = $inputText;
$this.offset = 0;
}
hidden [void] skipLineBreak()
{
#todo
}
hidden [string] readWord()
{
$startOffset = $this.offset
while ($this.offset -lt $this.inputText.Length -and (-not ($this.inputText[$this.offset] -match '\s')))
{
$this.offset++
}
while ($this.offset -lt $this.inputText.Length -and ($this.inputText[$this.offset] -match '\s'))
{
$this.offset++
}
$word = $this.inputText.Substring($startOffset, $this.offset - $startOffset)
return $word.TrimEnd(@("`r", "`n"))
}
hidden [Token] readHeader()
{
$startOffset = $this.offset
$level = 0
while($this.inputText[$this.offset] -eq '#')
{
$level++
$this.offset++
}
return [Token] @{
TokenType = GetHeaderByLevel $level
Text = $this.readWord()
Extent = @{
startOffset = $startOffset
endOffset = $this.offset
}
}
}
[Token] nextToken()
{
$startOffset = $this.offset
if ($startOffset -ge $this.inputText.Length) {
return $null
}
switch($this.inputText[$this.offset])
{
# TODO: This is a cheap man version. We need more graceful handling for line-endings.
#"`r" {
# $this.offset++
# return $this.nextToken()
#}
#"`n" {
# $this.offset++
# return $this.nextToken()
#}
'#' { return $this.readHeader() }
'`' {
if ($this.inputText.Substring($this.offset, 3) -eq '```') {
return $this.readCode()
}
}
'[' {
return $this.readHyperLink()
}
default {
return [Token] @{
TokenType = [TokenType]::Word
Text = $this.readWord()
Extent = @{
startOffset = $startOffset
endOffset = $this.offset
}
}
}
}
}
}
Describe 'Tokenizer' {
Context 'Simple inputs' {
It 'should parse HeaderL1' {
$inputText = '#Hello'
$tokenizer = [Tokenizer]::new($inputText);
$t = $tokenizer.nextToken()
$t.tokenType | Should be ([TokenType]::HeaderL1)
$t.text | Should be 'Hello'
$t.extent.startOffset | Should be 0
$t.extent.endOffset | Should be 6
$tokenizer.nextToken() | Should be $null
}
It 'should parse name and SYNOPSIS' {
$inputText = @'
##Get-Foo
###SYNOPSIS
This is synopsis.
'@
$tokenizer = [Tokenizer]::new($inputText);
$t = $tokenizer.nextToken()
$t.tokenType | Should be ([TokenType]::HeaderL2)
$t.text | Should be 'Get-Foo'
$t = $tokenizer.nextToken()
$t.text | Should be 'SYNOPSIS'
$t.tokenType | Should be ([TokenType]::HeaderL3)
$t = $tokenizer.nextToken()
$t.tokenType | Should be ([TokenType]::Word)
$t.text | Should be 'This '
$t = $tokenizer.nextToken()
$t.tokenType | Should be ([TokenType]::Word)
$t.text | Should be 'is '
$t = $tokenizer.nextToken()
$t.tokenType | Should be ([TokenType]::Word)
$t.text | Should be 'synopsis.'
$tokenizer.nextToken() | Should be $null
}
}
}