Skip to content

Commit 718d553

Browse files
committed
Started refactoring text streaming/peeking abstraction
1 parent 372f8ae commit 718d553

File tree

15 files changed

+73
-104
lines changed

15 files changed

+73
-104
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/build_pp_ast/src/lexer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod state;
55
use self::{lex_line::lex_line, state::State};
66
use super::{error::PreprocessorErrorKind, line_splice::LineSplicer};
77
use infinite_iterator::InfiniteIterator;
8-
use text::{IntoTextNoSend, Text};
8+
use text::{Text, TextPeeker};
99

1010
// Lexer for C preprocessor
1111
pub struct Lexer<I: Text> {
@@ -37,7 +37,7 @@ where
3737
loop {
3838
match self.line_splicer.next_line() {
3939
Ok(line) => {
40-
let mut line = line.into_text_no_send();
40+
let mut line = TextPeeker::new(line);
4141

4242
if line.peek().is_present() {
4343
let start_of_line = line.peek().source();

src/components/build_pp_ast/src/line_splice.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
Each line that ends in a backslash will be joined with the following line
77
*/
88

9+
use infinite_iterator::InfiniteIterator;
910
use source_files::Source;
1011
use std::{cell::RefCell, rc::Rc};
11-
use text::{Character, Text, TextStream};
12+
use text::{Character, Text};
1213

1314
#[derive(Debug)]
1415
pub struct LineSplicer<T: Text> {
@@ -61,8 +62,10 @@ where
6162
}
6263
}
6364

64-
impl<T: Text> TextStream for Line<T> {
65-
fn next(&mut self) -> Character {
65+
impl<T: Text> InfiniteIterator for Line<T> {
66+
type Item = Character;
67+
68+
fn next(&mut self) -> Self::Item {
6669
match &self.text {
6770
LineSource::Text(text) => {
6871
let character = loop {

src/components/build_workspace/src/compile/module.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use build_ast::{Input, Parser};
66
use build_token::Lexer;
77
use compiler::Compiler;
88
use data_units::ByteUnits;
9-
use diagnostics::{into_show, ErrorDiagnostic, Show};
9+
use diagnostics::{ErrorDiagnostic, Show, into_show};
1010
use fs_tree::FsNodeId;
1111
use infinite_iterator::{InfiniteIteratorPeeker, InfinitePeekable};
1212
use line_column::Location;
1313
use source_files::{Source, SourceFileKey};
1414
use std::path::Path;
15-
use text::{IntoText, IntoTextStream};
15+
use text::{TextPeeker, TextStreamer};
1616
use token::{Token, TokenKind};
1717

1818
pub struct CompiledModule<'a, I: InfinitePeekable<Token> + 'a> {
@@ -34,7 +34,7 @@ pub fn compile_module_file<'a>(
3434
let key = source_files.add(path.to_path_buf(), content);
3535
let content = source_files.get(key).content();
3636

37-
let text = content.chars().into_text_stream(key).into_text();
37+
let text = TextPeeker::new(TextStreamer::new(content.chars(), key));
3838
let lexer = InfiniteIteratorPeeker::new(Lexer::new(text));
3939
let mut input = Input::new(lexer, compiler.source_files, key);
4040
input.ignore_newlines();

src/components/build_workspace/src/compile/normal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use data_units::ByteUnits;
1010
use diagnostics::{ErrorDiagnostic, Show, into_show};
1111
use fs_tree::FsNodeId;
1212
use infinite_iterator::InfiniteIteratorPeeker;
13-
use text::{IntoText, IntoTextStream};
13+
use text::{TextPeeker, TextStreamer};
1414

1515
pub fn compile_normal_file(
1616
compiler: &Compiler,
@@ -26,7 +26,7 @@ pub fn compile_normal_file(
2626
let source_files = &compiler.source_files;
2727
let key = source_files.add(path.clone(), content);
2828
let content = source_files.get(key).content();
29-
let text = content.chars().into_text_stream(key).into_text();
29+
let text = TextPeeker::new(TextStreamer::new(content.chars(), key));
3030

3131
match &normal_file.kind {
3232
NormalFileKind::Adept => {

src/components/cli/src/build/invoke.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use compiler::Compiler;
66
use diagnostics::{DiagnosticFlags, Diagnostics, unerror};
77
use source_files::SourceFiles;
88
use std::{fs::metadata, path::Path};
9-
use text::{IntoText, IntoTextStream};
9+
use text::{TextPeeker, TextStreamer};
1010

1111
impl Invoke for BuildCommand {
1212
fn invoke(self) -> Result<(), ()> {
@@ -52,12 +52,10 @@ fn compile_header(compiler: &Compiler, filepath: &Path) -> Result<(), ()> {
5252

5353
let header_key = source_files.add(filepath.into(), content);
5454

55-
let header_contents = source_files
56-
.get(header_key)
57-
.content()
58-
.chars()
59-
.into_text_stream(header_key)
60-
.into_text();
55+
let header_contents = TextPeeker::new(TextStreamer::new(
56+
source_files.get(header_key).content().chars(),
57+
header_key,
58+
));
6159

6260
let preprocessed = unerror(
6361
preprocess(header_contents, &compiler.diagnostics),

src/support/line_column/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pub struct Location {
66
pub column: u32,
77
}
88

9+
unsafe impl<I> Send for LineColumn<I> where I: Iterator<Item = char> + Send {}
10+
911
pub struct LineColumn<I: Iterator<Item = char>> {
1012
iterator: Fuse<I>,
1113
line: u32,

src/support/text/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
infinite_iterator = { version = "0.1.0", path = "../infinite_iterator" }
78
line_column = { version = "0.1.0", path = "../line_column" }
89
source_files = { version = "0.1.0", path = "../../representations/source_files" }

src/support/text/src/character.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use infinite_iterator::InfiniteIteratorEnd;
12
use source_files::Source;
23

34
#[derive(Clone, Debug)]
@@ -99,3 +100,9 @@ impl Character {
99100
pub fn is_c_non_digit(c: char) -> bool {
100101
c.is_ascii_alphabetic() || c == '_' || c == '$'
101102
}
103+
104+
impl InfiniteIteratorEnd for Character {
105+
fn is_end(&self) -> bool {
106+
matches!(self, Self::End(..))
107+
}
108+
}

src/support/text/src/into_text/mod.rs

-36
This file was deleted.

src/support/text/src/into_text_stream/mod.rs

-18
This file was deleted.

src/support/text/src/lib.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
mod character;
22
mod eatable;
3-
mod into_text;
4-
mod into_text_stream;
5-
mod text_stream;
3+
mod peeker;
4+
mod text_streamer;
65

76
pub use character::{Character, is_c_non_digit};
87
pub use eatable::Eatable;
9-
pub use into_text::{IntoText, IntoTextNoSend};
10-
pub use into_text_stream::IntoTextStream;
8+
use infinite_iterator::InfiniteIterator;
9+
pub use peeker::TextPeeker;
1110
use source_files::Source;
12-
pub use text_stream::TextStream;
11+
pub use text_streamer::TextStreamer;
1312

14-
/*
15-
General representation of incoming text.
16-
17-
Generally, you don't implement this trait directly. Instead,
18-
you implement `TextStream`, and use the `IntoText` trait to
19-
create an easy to use text stream.
20-
21-
This trait just provides nice wrappers around `TextStream`
22-
*/
23-
pub trait Text: TextStream {
13+
pub trait Text: InfiniteIterator<Item = Character> {
2414
fn peek_nth(&mut self, n: usize) -> Character;
2515

2616
fn peek_n<const N: usize>(&mut self) -> [Character; N] {

src/support/text/src/into_text/peeker.rs src/support/text/src/peeker.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
use crate::{Character, Text, TextStream};
1+
use crate::{Character, Text};
2+
use infinite_iterator::InfiniteIterator;
23
use source_files::Source;
34
use std::collections::VecDeque;
45

5-
pub struct TextPeeker<S: TextStream> {
6-
stream: S,
6+
pub struct TextPeeker<I>
7+
where
8+
I: InfiniteIterator<Item = Character>,
9+
{
10+
stream: I,
711
queue: VecDeque<(char, Source)>,
812
}
913

10-
impl<S: TextStream> TextPeeker<S> {
11-
pub fn new(stream: S) -> Self {
14+
impl<I> TextPeeker<I>
15+
where
16+
I: InfiniteIterator<Item = Character>,
17+
{
18+
pub fn new(stream: I) -> Self {
1219
Self {
1320
stream,
1421
queue: VecDeque::new(),
1522
}
1623
}
1724
}
1825

19-
impl<S: TextStream> TextStream for TextPeeker<S> {
26+
impl<I> InfiniteIterator for TextPeeker<I>
27+
where
28+
I: InfiniteIterator<Item = Character>,
29+
{
30+
type Item = Character;
31+
2032
fn next(&mut self) -> Character {
2133
self.queue
2234
.pop_front()
@@ -25,7 +37,10 @@ impl<S: TextStream> TextStream for TextPeeker<S> {
2537
}
2638
}
2739

28-
impl<S: TextStream> Text for TextPeeker<S> {
40+
impl<I> Text for TextPeeker<I>
41+
where
42+
I: InfiniteIterator<Item = Character>,
43+
{
2944
fn peek_nth(&mut self, n: usize) -> Character {
3045
while self.queue.len() <= n {
3146
match self.stream.next() {

src/support/text/src/text_stream.rs

-5
This file was deleted.

src/support/text/src/into_text_stream/from_iterator.rs src/support/text/src/text_streamer.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
use crate::{Character, TextStream};
1+
use crate::Character;
2+
use infinite_iterator::InfiniteIterator;
23
use line_column::{LineColumn, Location};
34
use source_files::{Source, SourceFileKey};
45

5-
pub struct TextStreamFromIterator<I: Iterator<Item = char> + Send> {
6+
pub struct TextStreamer<I: Iterator<Item = char>> {
67
iterator: LineColumn<I>,
78
file_key: SourceFileKey,
89
last_location: Location,
910
}
1011

11-
impl<I: Iterator<Item = char> + Send> TextStreamFromIterator<I> {
12+
impl<I> TextStreamer<I>
13+
where
14+
I: Iterator<Item = char>,
15+
{
1216
pub fn new(iterator: I, file_key: SourceFileKey) -> Self {
1317
Self {
1418
iterator: LineColumn::new(iterator),
@@ -18,8 +22,13 @@ impl<I: Iterator<Item = char> + Send> TextStreamFromIterator<I> {
1822
}
1923
}
2024

21-
impl<I: Iterator<Item = char> + Send> TextStream for TextStreamFromIterator<I> {
22-
fn next(&mut self) -> Character {
25+
impl<I> InfiniteIterator for TextStreamer<I>
26+
where
27+
I: Iterator<Item = char>,
28+
{
29+
type Item = Character;
30+
31+
fn next(&mut self) -> Self::Item {
2332
match self.iterator.next() {
2433
Some((character, location)) => {
2534
self.last_location = location;
@@ -38,3 +47,5 @@ impl<I: Iterator<Item = char> + Send> TextStream for TextStreamFromIterator<I> {
3847
}
3948
}
4049
}
50+
51+
unsafe impl<I> Send for TextStreamer<I> where I: Iterator<Item = char> + Send {}

0 commit comments

Comments
 (0)