Skip to content

Commit bd0c420

Browse files
committed
Implement fixed width lexing
1 parent 72d2dbf commit bd0c420

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TODO
5353
| DiarySexp | | X | | | | |
5454
| Planning | | X | | | | |
5555
| Comment | | X | | | | |
56-
| FixedWidth | | | | | | |
56+
| FixedWidth | | X | | | | |
5757
| HorizontalRule | | | | | | |
5858
| Keyword | | X | | | | |
5959
| Affiliated Keywords | | - | | | | |

src/lexer.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ function lexnext(state::LexerState, bytes::DenseVector{UInt8}, start::UInt32)::T
9393
elseif chr == UInt8('*') && pos == linestart && ischarat(bytes, pos + countsame(bytes, pos, '*'), ' ')
9494
lex_heading(state, bytes, pos)
9595
elseif chr == UInt8(':')
96-
lex_drawer(state, bytes, pos)
96+
if length(bytes) > pos && iswhitespace(bytes, pos + 0x1) || islineend(bytes, pos + 0x1)
97+
lex_fixedwidth(state, bytes, pos)
98+
else
99+
lex_drawer(state, bytes, pos)
100+
end
97101
elseif chr == UInt8('[') && pos == linestart
98102
fndef = lex_footnotedef(state, bytes, pos)
99103
if fndef != NONE_TOKEN && K"footnote_definition" state.ctx
@@ -428,7 +432,19 @@ function lex_comment(::LexerState, bytes::DenseVector{UInt8}, start::UInt32)
428432
Token(K"comment", start, pos), nextpos
429433
end
430434

431-
# TODO: Fixed width
435+
function lex_fixedwidth(::LexerState, bytes::DenseVector{UInt8}, start::UInt32)
436+
pos, nextpos = start, start
437+
while pos <= length(bytes)
438+
nextpos = skipspaces(bytes, nextpos).stop
439+
ischarat(bytes, nextpos, ':') &&
440+
(islineend(bytes, nextpos + 0x1) ||
441+
nextpos < length(bytes) && iswhitespace(bytes, nextpos + 0x1)) ||
442+
break
443+
pos = lineend(bytes, nextpos) - 0x1
444+
nextpos = pos + 0x2
445+
end
446+
Token(K"fixedwidth", start, pos), nextpos
447+
end
432448

433449
# TODO: Horizontal rules
434450

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ end
266266
@test collect(Lexer("# comment\n # \n# more\n")) ==
267267
[Token(K"comment", 1, 20)]
268268
end
269+
@testset "Fixed width" begin
270+
@test collect(Lexer(":")) ==
271+
[Token(K"fixedwidth", 1, 1)]
272+
@test collect(Lexer(": fixed")) ==
273+
[Token(K"fixedwidth", 1, 7)]
274+
@test collect(Lexer(": fixed\n: more")) ==
275+
[Token(K"fixedwidth", 1, 14)]
276+
@test collect(Lexer(": fixed\n:\n: more")) ==
277+
[Token(K"fixedwidth", 1, 16)]
278+
@test collect(Lexer(": fixed\n : \n: more\n")) ==
279+
[Token(K"fixedwidth", 1, 18)]
280+
end
269281
@testset "Type inference" begin
270282
@testset "Utilities" begin
271283
bytes, pos = codeunits("abc"), UInt32(1)
@@ -302,6 +314,7 @@ end
302314
@inferred Tuple{Token, UInt32} Org.lex_diarysexp(lstate, bytes, pos)
303315
@inferred Tuple{Token, UInt32} Org.lex_planning(lstate, bytes, pos)
304316
@inferred Tuple{Token, UInt32} Org.lex_comment(lstate, bytes, pos)
317+
@inferred Tuple{Token, UInt32} Org.lex_fixedwidth(lstate, bytes, pos)
305318
end
306319
end
307320
@testset "Unhandled errors" begin
@@ -340,6 +353,7 @@ end
340353
@test_call Org.lex_diarysexp(lstate, bytes, pos)
341354
@test_call Org.lex_planning(lstate, bytes, pos)
342355
@test_call Org.lex_comment(lstate, bytes, pos)
356+
@test_call Org.lex_fixedwidth(lstate, bytes, pos)
343357
end
344358
@testset "Iteration" begin
345359
@test_call iterate(Lexer("abc"), LexerState())
@@ -381,6 +395,7 @@ end
381395
@test_opt Org.lex_diarysexp(lstate, bytes, pos)
382396
@test_opt Org.lex_planning(lstate, bytes, pos)
383397
@test_opt Org.lex_comment(lstate, bytes, pos)
398+
@test_opt Org.lex_fixedwidth(lstate, bytes, pos)
384399
end
385400
@testset "Iteration" begin
386401
@test_opt iterate(Lexer("abc"), LexerState())

0 commit comments

Comments
 (0)