Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Performance, No Breaking] Optimize checkers and stylistic refactoring. #201

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions src/scanner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,43 +352,49 @@ end
# Checkers
# --------

const whitespace = "\0 \t\r\n\u0085\u2028\u2029"
yaml_1_1_is_whitespace(c::Char) =
c == '\0' || c == ' ' || c == '\t' ||
c == '\r' || c == '\n' ||
c == '\u85' || c == '\u2028' || c == '\u2029'


function check_directive(stream::TokenStream)
check_directive(stream::TokenStream) =
stream.column == 0
end

function check_document_start(stream::TokenStream)
check_document_start(stream::TokenStream) =
stream.column == 0 &&
prefix(stream.input, 3) == "---" &&
in(peek(stream.input, 3), whitespace)
end
prefix(stream.input, 3) == "---" &&
yaml_1_1_is_whitespace(peek(stream.input, 3))

function check_document_end(stream::TokenStream)
stream.column == 0 &&
prefix(stream.input, 3) == "..." &&
(in(peek(stream.input, 3), whitespace) || peek(stream.input, 3) === nothing)
end
check_document_end(stream::TokenStream) =
stream.column == 0 &&
prefix(stream.input, 3) == "..." && begin
c = peek(stream.input, 3)
yaml_1_1_is_whitespace(c) || c === nothing
end

function check_block_entry(stream::TokenStream)
in(peek(stream.input, 1), whitespace)
cnext = peek(stream.input, 1)
yaml_1_1_is_whitespace(cnext)
end

function check_key(stream::TokenStream)
stream.flow_level > 0 || in(peek(stream.input, 1), whitespace)
end
check_key(stream::TokenStream) =
stream.flow_level > 0 || begin
cnext = peek(stream.input, 1)
yaml_1_1_is_whitespace(cnext)
end

function check_value(stream::TokenStream)
cnext = peek(stream.input, 1)
stream.flow_level > 0 || in(cnext, whitespace) || cnext === nothing
end
check_value(stream::TokenStream) =
stream.flow_level > 0 || begin
cnext = peek(stream.input, 1)
yaml_1_1_is_whitespace(cnext) || cnext === nothing
end

function check_plain(stream::TokenStream)
!in(peek(stream.input), "\0 \t\r\n\u0085\u2028\u2029-?:,[]{}#&*!|>\'\"%@`\uFEFF") ||
(!in(peek(stream.input, 1), whitespace) &&
(peek(stream.input) == '-' || (stream.flow_level == 0 &&
in(peek(stream.input), "?:"))))
c = peek(stream.input)
!(yaml_1_1_is_whitespace(c) || c in "-?:,[]{}#&*!|>\'\"%@`\uFEFF") || begin
cnext = peek(stream.input, 1)
!yaml_1_1_is_whitespace(cnext) && (c == '-' || stream.flow_level == 0 && (c == '?' || c == ':'))
end
end


Expand Down Expand Up @@ -1411,10 +1417,10 @@ function scan_plain(stream::TokenStream)
while true
c = peek(stream.input, length)
cnext = peek(stream.input, length + 1)
if in(c, whitespace) ||
if yaml_1_1_is_whitespace(c) ||
c === nothing ||
(stream.flow_level == 0 && c == ':' &&
(cnext === nothing || in(cnext, whitespace))) ||
(cnext === nothing || yaml_1_1_is_whitespace(cnext))) ||
(stream.flow_level != 0 && in(c, ",:?[]{}"))
break
end
Expand Down
Loading