mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
logic base
This commit is contained in:
parent
dc4f8289ad
commit
8f97c217a5
1 changed files with 133 additions and 114 deletions
|
@ -277,138 +277,157 @@ fn eat_line_comment<'a>(
|
||||||
) -> SpaceState<'a> {
|
) -> SpaceState<'a> {
|
||||||
use SpaceState::*;
|
use SpaceState::*;
|
||||||
|
|
||||||
let is_doc_comment = if let Some(b'#') = state.bytes().get(0) {
|
let mut index = 0;
|
||||||
match state.bytes().get(1) {
|
let bytes = state.bytes();
|
||||||
Some(b' ') => {
|
|
||||||
state = state.advance(2);
|
|
||||||
|
|
||||||
true
|
'outer: loop {
|
||||||
}
|
let is_doc_comment = if let Some(b'#') = bytes.get(index) {
|
||||||
Some(b'\n') => {
|
match bytes.get(index + 1) {
|
||||||
// consume the second # and the \n
|
Some(b' ') => {
|
||||||
state = state.advance(1);
|
state = state.advance(2);
|
||||||
state = state.advance_newline();
|
index += 2;
|
||||||
|
|
||||||
comments_and_newlines.push(CommentOrNewline::DocComment(""));
|
true
|
||||||
multiline = true;
|
}
|
||||||
|
Some(b'\n') => {
|
||||||
|
// consume the second # and the \n
|
||||||
|
state = state.advance(1);
|
||||||
|
state = state.advance_newline();
|
||||||
|
index += 2;
|
||||||
|
|
||||||
for c in state.bytes() {
|
comments_and_newlines.push(CommentOrNewline::DocComment(""));
|
||||||
match c {
|
multiline = true;
|
||||||
b' ' => {
|
|
||||||
state = state.advance(1);
|
for c in state.bytes() {
|
||||||
|
match c {
|
||||||
|
b' ' => {
|
||||||
|
state = state.advance(1);
|
||||||
|
}
|
||||||
|
b'\n' => {
|
||||||
|
state = state.advance_newline();
|
||||||
|
index += 1;
|
||||||
|
multiline = true;
|
||||||
|
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||||
|
}
|
||||||
|
b'\r' => {
|
||||||
|
state = state.advance_newline();
|
||||||
|
}
|
||||||
|
b'\t' => {
|
||||||
|
return HasTab(state);
|
||||||
|
}
|
||||||
|
b'#' => {
|
||||||
|
state = state.advance(1);
|
||||||
|
index += 1;
|
||||||
|
continue 'outer;
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
}
|
}
|
||||||
b'\n' => {
|
|
||||||
state = state.advance_newline();
|
index += 1;
|
||||||
multiline = true;
|
|
||||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
|
||||||
}
|
|
||||||
b'\r' => {
|
|
||||||
state = state.advance_newline();
|
|
||||||
}
|
|
||||||
b'\t' => {
|
|
||||||
return HasTab(state);
|
|
||||||
}
|
|
||||||
b'#' => {
|
|
||||||
state = state.advance(1);
|
|
||||||
return eat_line_comment(state, multiline, comments_and_newlines);
|
|
||||||
}
|
|
||||||
_ => break,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Good {
|
||||||
|
state,
|
||||||
|
multiline,
|
||||||
|
comments_and_newlines,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// consume the second #
|
||||||
|
state = state.advance(1);
|
||||||
|
|
||||||
|
return Good {
|
||||||
|
state,
|
||||||
|
multiline,
|
||||||
|
comments_and_newlines,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return Good {
|
_ => false,
|
||||||
state,
|
|
||||||
multiline,
|
|
||||||
comments_and_newlines,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
None => {
|
} else {
|
||||||
// consume the second #
|
false
|
||||||
state = state.advance(1);
|
};
|
||||||
|
|
||||||
return Good {
|
let loop_start = index;
|
||||||
state,
|
|
||||||
multiline,
|
|
||||||
comments_and_newlines,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => false,
|
let initial = state.bytes();
|
||||||
}
|
let mut it = initial.iter();
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
|
|
||||||
let initial = state.bytes();
|
while let Some(c) = it.next() {
|
||||||
let mut it = initial.iter();
|
match c {
|
||||||
|
b'\t' => return HasTab(state),
|
||||||
|
b'\n' => {
|
||||||
|
let delta = initial.len() - state.bytes().len();
|
||||||
|
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) };
|
||||||
|
|
||||||
while let Some(c) = it.next() {
|
if is_doc_comment {
|
||||||
match c {
|
comments_and_newlines.push(CommentOrNewline::DocComment(comment));
|
||||||
b'\t' => return HasTab(state),
|
} else {
|
||||||
b'\n' => {
|
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
||||||
let delta = initial.len() - state.bytes().len();
|
|
||||||
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) };
|
|
||||||
|
|
||||||
if is_doc_comment {
|
|
||||||
comments_and_newlines.push(CommentOrNewline::DocComment(comment));
|
|
||||||
} else {
|
|
||||||
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
|
||||||
}
|
|
||||||
state = state.advance_newline();
|
|
||||||
multiline = true;
|
|
||||||
|
|
||||||
while let Some(c) = it.next() {
|
|
||||||
match c {
|
|
||||||
b' ' => {
|
|
||||||
state = state.advance(1);
|
|
||||||
}
|
|
||||||
b'\n' => {
|
|
||||||
state = state.advance_newline();
|
|
||||||
multiline = true;
|
|
||||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
|
||||||
}
|
|
||||||
b'\r' => {
|
|
||||||
state = state.advance_newline();
|
|
||||||
}
|
|
||||||
b'\t' => {
|
|
||||||
return HasTab(state);
|
|
||||||
}
|
|
||||||
b'#' => {
|
|
||||||
state = state.advance(1);
|
|
||||||
return eat_line_comment(state, multiline, comments_and_newlines);
|
|
||||||
}
|
|
||||||
_ => break,
|
|
||||||
}
|
}
|
||||||
|
state = state.advance_newline();
|
||||||
|
multiline = true;
|
||||||
|
|
||||||
|
index += 1;
|
||||||
|
while let Some(c) = it.next() {
|
||||||
|
match c {
|
||||||
|
b' ' => {
|
||||||
|
state = state.advance(1);
|
||||||
|
}
|
||||||
|
b'\n' => {
|
||||||
|
state = state.advance_newline();
|
||||||
|
multiline = true;
|
||||||
|
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||||
|
}
|
||||||
|
b'\r' => {
|
||||||
|
state = state.advance_newline();
|
||||||
|
}
|
||||||
|
b'\t' => {
|
||||||
|
return HasTab(state);
|
||||||
|
}
|
||||||
|
b'#' => {
|
||||||
|
state = state.advance(1);
|
||||||
|
index += 1;
|
||||||
|
continue 'outer;
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Good {
|
||||||
|
state,
|
||||||
|
multiline,
|
||||||
|
comments_and_newlines,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
b'\r' => {
|
||||||
|
state = state.advance_newline();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
state = state.advance(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Good {
|
index += 1;
|
||||||
state,
|
|
||||||
multiline,
|
|
||||||
comments_and_newlines,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
b'\r' => {
|
|
||||||
state = state.advance_newline();
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
state = state.advance(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// We made it to the end of the bytes. This means there's a comment without a trailing newline.
|
// We made it to the end of the bytes. This means there's a comment without a trailing newline.
|
||||||
let delta = initial.len() - state.bytes().len();
|
let delta = initial.len() - state.bytes().len();
|
||||||
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) };
|
let comment = unsafe { std::str::from_utf8_unchecked(&bytes[loop_start..delta]) };
|
||||||
|
|
||||||
if is_doc_comment {
|
if is_doc_comment {
|
||||||
comments_and_newlines.push(CommentOrNewline::DocComment(comment));
|
comments_and_newlines.push(CommentOrNewline::DocComment(comment));
|
||||||
} else {
|
} else {
|
||||||
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
||||||
}
|
}
|
||||||
|
|
||||||
Good {
|
return Good {
|
||||||
state,
|
state,
|
||||||
multiline,
|
multiline,
|
||||||
comments_and_newlines,
|
comments_and_newlines,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue