-
Notifications
You must be signed in to change notification settings - Fork 112
/
parser.vim
296 lines (276 loc) · 11 KB
/
parser.vim
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: June 23, 2013
" URL: http://peterodding.com/code/vim/notes/
function! xolox#notes#parser#parse_note(text) " {{{1
" Parser for the note taking syntax used by vim-notes.
let starttime = xolox#misc#timer#start()
let context = s:create_parse_context(a:text)
let note_title = context.next_line()
let blocks = [{'type': 'title', 'text': note_title}]
while context.has_more()
let chr = context.peek(1)
call xolox#misc#msg#debug("notes.vim %s: Peeking at character %s ..", g:xolox#notes#version, string(chr))
if chr == "\n"
" Ignore empty lines.
call context.next(1)
continue
elseif chr == '#'
let block = s:parse_heading(context)
elseif chr == '{' && context.peek(3) == "\{\{\{"
let block = s:parse_code_block(context)
else
let lookahead = s:match_list_item(context, 0)
if lookahead =~ 'list'
let block = s:parse_list(context)
elseif lookahead == 'divider'
let block = s:parse_divider(context)
elseif !empty(lookahead)
let msg = "Programming error! Unsupported lookahead type: %s."
throw printf(msg, string(lookahead))
else
let block = s:parse_paragraph(context)
endif
endif
" Don't include empty blocks in the output.
if !empty(block)
call add(blocks, block)
endif
endwhile
call xolox#misc#timer#stop("notes.vim %s: Parsed note into %i blocks in %s.", g:xolox#notes#version, len(blocks), starttime)
return blocks
endfunction
function! xolox#notes#parser#view_parse_nodes() " {{{1
" Parse the current note and show the parse nodes in a temporary buffer.
let note_text = join(getline(1, '$'), "\n")
let parse_nodes = xolox#notes#parser#parse_note(note_text)
vnew
call setline(1, map(parse_nodes, 'string(v:val)'))
setlocal filetype=vim nomodified nowrap
endfunction
function! s:create_parse_context(text) " {{{1
" Create an object to encapsulate the lowest level of parser state.
let context = {'text': a:text, 'index': 0}
" The has_more() method returns 1 (true) when more input is available, 0
" (false) otherwise.
function context.has_more()
return self.index < len(self.text)
endfunction
" The peek() method returns the next character without consuming it.
function context.peek(n)
if self.has_more()
return self.text[self.index : self.index + (a:n - 1)]
endif
return ''
endfunction
" The next() method returns the next character and consumes it.
function context.next(n)
let result = self.peek(a:n)
let self.index += a:n
return result
endfunction
" The next_line() method returns the current line and consumes it.
function context.next_line()
let line = ''
while self.has_more()
let chr = self.next(1)
if chr == "\n" || chr == ""
" We hit the end of line or input.
return line
else
" The line continues.
let line .= chr
endif
endwhile
return line
endfunction
return context
endfunction
function! s:match_list_item(context, consume_lookahead) " {{{1
" Check whether the current line starts with a list bullet.
let context = copy(a:context)
let line = context.next_line()
let bullet = matchstr(line, s:bullet_pattern)
if !empty(bullet)
call xolox#misc#msg#debug("notes.vim %s: Matched list item bullet '%s' ..", g:xolox#notes#version, bullet)
" Disambiguate list bullets from horizontal dividers.
if line =~ '^\s\+\*\s\*\s\*$'
return 'divider'
endif
" We matched a bullet! Now we still need to distinguish ordered from
" unordered list items.
if a:consume_lookahead
let a:context.index += len(bullet)
endif
return (bullet =~ '\d') ? 'ordered-list' : 'unordered-list'
endif
return ''
endfunction
function! s:match_line(context) " {{{1
" Get the text of the current line, stopping at end of the line or just
" before the start of a code block marker, whichever comes first.
let line = ''
while a:context.has_more()
let chr = a:context.peek(1)
if chr == '{' && a:context.peek(3) == "\{\{\{"
" XXX The start of a code block implies the end of whatever came before.
" The marker above contains back slashes so that Vim doesn't apply
" folding because of the marker :-).
return line
elseif chr == "\n"
call a:context.next(1)
return line . "\n"
else
let line .= a:context.next(1)
endif
endwhile
" We hit the end of the input.
return line
endfunction
function! s:parse_heading(context) " {{{1
" Parse the upcoming heading in the input stream.
let level = 0
while a:context.peek(1) == '#'
let level += 1
call a:context.next(1)
endwhile
let text = xolox#misc#str#trim(s:match_line(a:context))
return {'type': 'heading', 'level': level, 'text': text}
endfunction
function! s:parse_code_block(context) " {{{1
" Parse the upcoming code block in the input stream.
let language = ''
let text = ''
" Skip the start marker.
call a:context.next(3)
" Get the optional language name.
while a:context.peek(1) =~ '\w'
let language .= a:context.next(1)
endwhile
" Skip the whitespace separating the start marker and/or language name from
" the text.
while a:context.peek(1) =~ '[ \t]'
call a:context.next(1)
endwhile
" Get the text inside the code block.
while a:context.has_more()
let chr = a:context.next(1)
if chr == '}' && a:context.peek(2) == '}}'
call a:context.next(2)
break
endif
let text .= chr
endwhile
" Strip trailing whitespace.
let text = substitute(text, '\_s\+$', '', '')
return {'type': 'code', 'language': language, 'text': text}
endfunction
function! s:parse_divider(context) " {{{1
" Parse the upcoming horizontal divider in the input stream.
call a:context.next_line()
return {'type': 'divider'}
endfunction
function! s:parse_list(context) " {{{1
" Parse the upcoming sequence of list items in the input stream.
let list_type = 'unknown'
let items = []
let lines = []
" Outer loop to consume one or more list items.
while a:context.has_more()
let type = s:match_list_item(a:context, 1)
if !empty(type)
" The current line starts with a list bullet.
if list_type == 'unknown'
" The first bullet determines the type of list.
let list_type = type
endif
" Save the previous list item?
call s:save_item(items, lines)
let lines = []
endif
let line = s:match_line(a:context)
if line[-1:] == "\n"
call add(lines, line)
elseif !empty(line)
call add(lines, line)
else
" FIXME What happens when we find an empty line? Here's what:
" 1. If the line after that starts without indentation, we found
" the end of the list.
" 2. If the line starts with indentation, we are dealing with a
" list item that contains multiple paragraphs...
endif
endwhile
call s:save_item(items, lines)
return {'type': 'list', 'ordered': (list_type == 'ordered-list'), 'items': items}
endfunction
function! s:save_item(items, lines)
let text = join(a:lines, "\n")
if text =~ '\S'
call add(a:items, xolox#misc#str#compact(text))
endif
endfunction
function! s:parse_paragraph(context) " {{{1
" Parse the upcoming paragraph in the input stream.
let lines = []
while a:context.has_more()
let line = s:match_line(a:context)
call xolox#misc#msg#debug("notes.vim %s: Matched line in paragraph: %s.", g:xolox#notes#version, string(line))
call add(lines, line)
if line =~ '^\_s*$'
" An empty line marks the end of the paragraph.
call xolox#misc#msg#debug("notes.vim %s: Paragraph ends in empty line.", g:xolox#notes#version)
break
elseif line[-1:] != "\n"
" XXX When match_line() returns a line that doesn't end in a newline
" character, it means either we hit the end of the input or the current
" line continues in a code block (which is not ours to parse :-).
call xolox#misc#msg#debug("notes.vim %s: Paragraph ends in code block?", g:xolox#notes#version)
break
endif
endwhile
" Don't include empty paragraphs in the output.
let text = join(lines, "\n")
if text =~ '\S'
return {'type': 'paragraph', 'text': xolox#misc#str#compact(text)}
else
return {}
endif
endfunction
function! s:generate_list_item_bullet_pattern() " {{{1
" Generate a regular expression that matches any kind of list bullet.
let choices = copy(g:notes_unicode_bullets)
for bullet in g:notes_ascii_bullets
call add(choices, xolox#misc#escape#pattern(bullet))
endfor
call add(choices, '\d\+[[:punct:]]\?')
return join(choices, '\|')
endfunction
let s:bullet_pattern = '^\s*\(' . s:generate_list_item_bullet_pattern() . '\)\s\+'
function! xolox#notes#parser#run_tests() " {{{1
" Tests for the note taking syntax parser.
call xolox#misc#test#reset()
call xolox#misc#test#wrap('xolox#notes#parser#test_parsing_of_note_titles')
call xolox#misc#test#wrap('xolox#notes#parser#test_parsing_of_headings')
call xolox#misc#test#wrap('xolox#notes#parser#test_parsing_of_paragraphs')
call xolox#misc#test#wrap('xolox#notes#parser#test_parsing_of_code_blocks')
call xolox#misc#test#wrap('xolox#notes#parser#test_parsing_of_list_items')
call xolox#misc#test#summarize()
endfunction
function! xolox#notes#parser#test_parsing_of_note_titles()
call xolox#misc#test#assert_equals([{'type': 'title', 'text': 'Just the title'}], xolox#notes#parser#parse_note('Just the title'))
endfunction
function! xolox#notes#parser#test_parsing_of_headings()
call xolox#misc#test#assert_equals([{'type': 'title', 'text': 'Just the title'}, {'type': 'heading', 'level': 1, 'text': 'This is a heading'}], xolox#notes#parser#parse_note("Just the title\n\n# This is a heading"))
endfunction
function! xolox#notes#parser#test_parsing_of_paragraphs()
call xolox#misc#test#assert_equals([{'type': 'title', 'text': 'Just the title'}, {'type': 'paragraph', 'text': 'This is a paragraph'}], xolox#notes#parser#parse_note("Just the title\n\nThis is a paragraph"))
call xolox#misc#test#assert_equals([{'type': 'title', 'text': 'Just the title'}, {'type': 'paragraph', 'text': 'This is a paragraph'}, {'type': 'paragraph', 'text': "And here's another paragraph!"}], xolox#notes#parser#parse_note("Just the title\n\nThis is a paragraph\n\n\n\nAnd here's another paragraph!"))
endfunction
function! xolox#notes#parser#test_parsing_of_code_blocks()
call xolox#misc#test#assert_equals([{'type': 'title', 'text': 'Just the title'}, {'type': 'code', 'language': '', 'text': "This is a code block\nwith two lines"}], xolox#notes#parser#parse_note("Just the title\n\n{{{ This is a code block\nwith two lines }}}"))
endfunction
function! xolox#notes#parser#test_parsing_of_list_items()
call xolox#misc#test#assert_equals([{'type': 'title', 'text': 'Just the title'}, {'type': 'list', 'ordered': 1, 'items': ['item one', 'item two', 'item three']}], xolox#notes#parser#parse_note("Just the title\n\n1. item one\n2. item two\n3. item three"))
endfunction
call xolox#notes#parser#run_tests()