From 7c67612b8a894187fa3b64725531a5459f9211bf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Aug 2018 22:33:29 +0300 Subject: organizize --- crates/libsyntax2/src/lexer/comments.rs | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 crates/libsyntax2/src/lexer/comments.rs (limited to 'crates/libsyntax2/src/lexer/comments.rs') diff --git a/crates/libsyntax2/src/lexer/comments.rs b/crates/libsyntax2/src/lexer/comments.rs new file mode 100644 index 000000000..01acb6515 --- /dev/null +++ b/crates/libsyntax2/src/lexer/comments.rs @@ -0,0 +1,57 @@ +use lexer::ptr::Ptr; + +use SyntaxKind::{self, *}; + +pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool { + if ptr.next_is('!') && ptr.nnext_is('/') { + ptr.bump(); + ptr.bump(); + bump_until_eol(ptr); + true + } else { + false + } +} + +fn scan_block_comment(ptr: &mut Ptr) -> Option { + if ptr.next_is('*') { + ptr.bump(); + let mut depth: u32 = 1; + while depth > 0 { + if ptr.next_is('*') && ptr.nnext_is('/') { + depth -= 1; + ptr.bump(); + ptr.bump(); + } else if ptr.next_is('/') && ptr.nnext_is('*') { + depth += 1; + ptr.bump(); + ptr.bump(); + } else if ptr.bump().is_none() { + break; + } + } + Some(COMMENT) + } else { + None + } +} + +pub(crate) fn scan_comment(ptr: &mut Ptr) -> Option { + if ptr.next_is('/') { + bump_until_eol(ptr); + Some(COMMENT) + } else { + scan_block_comment(ptr) + } +} + +fn bump_until_eol(ptr: &mut Ptr) { + loop { + if ptr.next_is('\n') || ptr.next_is('\r') && ptr.nnext_is('\n') { + return; + } + if ptr.bump().is_none() { + break; + } + } +} -- cgit v1.2.3