pub struct Codebase { /* private fields */ }
Expand description
Codebase. A struct that holds all your code in memory (codespan forces this)
Implementations§
Source§impl Codebase
impl Codebase
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new codebase.
Examples found in repository?
examples/simple_c_lexer.rs (line 12)
4pub fn main() {
5 let args = std::env::args().collect::<Vec<_>>();
6 if args.len() != 2 {
7 eprintln!("Usage: cargo run --example simple_c_lexer -- sample.c");
8 return;
9 }
10
11 let code = std::fs::read_to_string(&args[1]).unwrap();
12 let mut codebase = Codebase::new();
13 let key = codebase.add(args[1].clone(), code);
14
15 let mut reader = TokenReader::new(codebase.get(key).unwrap().source().clone());
16 while let Some(char) = reader.next_char() {
17 if chars::is_digit(char) {
18 // * Number
19 println!("Number {}", reader.next_token(chars::is_digit, char));
20 } else if chars::is_ident_start(char) {
21 // * Identifier
22 println!(
23 "Ident {}",
24 reader.next_token(chars::is_ident_continue, char)
25 );
26 } else if char == '"' {
27 // * String
28 let mut string = String::new();
29 loop {
30 match reader.next_char() {
31 // Escape sequences
32 Some('\\') => match reader.next_char() {
33 Some('n') => string.push('\n'),
34 Some(char) => string.push(char),
35 None => codebase.emit( // Technically won't happen
36 Diagnostic::error()
37 .with_message("expected escape code")
38 .with_labels(vec![Label::primary(
39 key,
40 reader.cursor..reader.cursor + 1,
41 )
42 .with_message("here")]),
43 ),
44 },
45
46 // Quote
47 Some('"') => break,
48
49 // Char
50 Some(char) if char != '\n' => string.push(char),
51 _ => {
52 codebase.emit(
53 Diagnostic::error()
54 .with_message("unterminated string")
55 .with_labels(vec![Label::primary(
56 key,
57 reader.cursor..reader.cursor + 1,
58 )
59 .with_message("here")]),
60 );
61 break;
62 }
63 }
64 }
65 println!("String {:?}", string);
66 } else if ['(', ')', '{', '}', ';'].contains(&char) {
67 // * Operator
68 println!("Operator {}", char);
69 }
70 }
71}
Sourcepub fn add(&mut self, name: String, source: String) -> usize
pub fn add(&mut self, name: String, source: String) -> usize
Add a file to the codebase, returning the handle that can be used to refer to it again.
Examples found in repository?
examples/simple_c_lexer.rs (line 13)
4pub fn main() {
5 let args = std::env::args().collect::<Vec<_>>();
6 if args.len() != 2 {
7 eprintln!("Usage: cargo run --example simple_c_lexer -- sample.c");
8 return;
9 }
10
11 let code = std::fs::read_to_string(&args[1]).unwrap();
12 let mut codebase = Codebase::new();
13 let key = codebase.add(args[1].clone(), code);
14
15 let mut reader = TokenReader::new(codebase.get(key).unwrap().source().clone());
16 while let Some(char) = reader.next_char() {
17 if chars::is_digit(char) {
18 // * Number
19 println!("Number {}", reader.next_token(chars::is_digit, char));
20 } else if chars::is_ident_start(char) {
21 // * Identifier
22 println!(
23 "Ident {}",
24 reader.next_token(chars::is_ident_continue, char)
25 );
26 } else if char == '"' {
27 // * String
28 let mut string = String::new();
29 loop {
30 match reader.next_char() {
31 // Escape sequences
32 Some('\\') => match reader.next_char() {
33 Some('n') => string.push('\n'),
34 Some(char) => string.push(char),
35 None => codebase.emit( // Technically won't happen
36 Diagnostic::error()
37 .with_message("expected escape code")
38 .with_labels(vec![Label::primary(
39 key,
40 reader.cursor..reader.cursor + 1,
41 )
42 .with_message("here")]),
43 ),
44 },
45
46 // Quote
47 Some('"') => break,
48
49 // Char
50 Some(char) if char != '\n' => string.push(char),
51 _ => {
52 codebase.emit(
53 Diagnostic::error()
54 .with_message("unterminated string")
55 .with_labels(vec![Label::primary(
56 key,
57 reader.cursor..reader.cursor + 1,
58 )
59 .with_message("here")]),
60 );
61 break;
62 }
63 }
64 }
65 println!("String {:?}", string);
66 } else if ['(', ')', '{', '}', ';'].contains(&char) {
67 // * Operator
68 println!("Operator {}", char);
69 }
70 }
71}
Sourcepub fn get(&self, file_id: usize) -> Result<&SimpleFile<String, Rc<str>>, Error>
pub fn get(&self, file_id: usize) -> Result<&SimpleFile<String, Rc<str>>, Error>
Get the file corresponding to the given id.
Examples found in repository?
examples/simple_c_lexer.rs (line 15)
4pub fn main() {
5 let args = std::env::args().collect::<Vec<_>>();
6 if args.len() != 2 {
7 eprintln!("Usage: cargo run --example simple_c_lexer -- sample.c");
8 return;
9 }
10
11 let code = std::fs::read_to_string(&args[1]).unwrap();
12 let mut codebase = Codebase::new();
13 let key = codebase.add(args[1].clone(), code);
14
15 let mut reader = TokenReader::new(codebase.get(key).unwrap().source().clone());
16 while let Some(char) = reader.next_char() {
17 if chars::is_digit(char) {
18 // * Number
19 println!("Number {}", reader.next_token(chars::is_digit, char));
20 } else if chars::is_ident_start(char) {
21 // * Identifier
22 println!(
23 "Ident {}",
24 reader.next_token(chars::is_ident_continue, char)
25 );
26 } else if char == '"' {
27 // * String
28 let mut string = String::new();
29 loop {
30 match reader.next_char() {
31 // Escape sequences
32 Some('\\') => match reader.next_char() {
33 Some('n') => string.push('\n'),
34 Some(char) => string.push(char),
35 None => codebase.emit( // Technically won't happen
36 Diagnostic::error()
37 .with_message("expected escape code")
38 .with_labels(vec![Label::primary(
39 key,
40 reader.cursor..reader.cursor + 1,
41 )
42 .with_message("here")]),
43 ),
44 },
45
46 // Quote
47 Some('"') => break,
48
49 // Char
50 Some(char) if char != '\n' => string.push(char),
51 _ => {
52 codebase.emit(
53 Diagnostic::error()
54 .with_message("unterminated string")
55 .with_labels(vec![Label::primary(
56 key,
57 reader.cursor..reader.cursor + 1,
58 )
59 .with_message("here")]),
60 );
61 break;
62 }
63 }
64 }
65 println!("String {:?}", string);
66 } else if ['(', ')', '{', '}', ';'].contains(&char) {
67 // * Operator
68 println!("Operator {}", char);
69 }
70 }
71}
Sourcepub fn emit(&mut self, diagnostic: Diagnostic<usize>)
pub fn emit(&mut self, diagnostic: Diagnostic<usize>)
Emit a diagnostic
Examples found in repository?
examples/simple_c_lexer.rs (lines 35-43)
4pub fn main() {
5 let args = std::env::args().collect::<Vec<_>>();
6 if args.len() != 2 {
7 eprintln!("Usage: cargo run --example simple_c_lexer -- sample.c");
8 return;
9 }
10
11 let code = std::fs::read_to_string(&args[1]).unwrap();
12 let mut codebase = Codebase::new();
13 let key = codebase.add(args[1].clone(), code);
14
15 let mut reader = TokenReader::new(codebase.get(key).unwrap().source().clone());
16 while let Some(char) = reader.next_char() {
17 if chars::is_digit(char) {
18 // * Number
19 println!("Number {}", reader.next_token(chars::is_digit, char));
20 } else if chars::is_ident_start(char) {
21 // * Identifier
22 println!(
23 "Ident {}",
24 reader.next_token(chars::is_ident_continue, char)
25 );
26 } else if char == '"' {
27 // * String
28 let mut string = String::new();
29 loop {
30 match reader.next_char() {
31 // Escape sequences
32 Some('\\') => match reader.next_char() {
33 Some('n') => string.push('\n'),
34 Some(char) => string.push(char),
35 None => codebase.emit( // Technically won't happen
36 Diagnostic::error()
37 .with_message("expected escape code")
38 .with_labels(vec![Label::primary(
39 key,
40 reader.cursor..reader.cursor + 1,
41 )
42 .with_message("here")]),
43 ),
44 },
45
46 // Quote
47 Some('"') => break,
48
49 // Char
50 Some(char) if char != '\n' => string.push(char),
51 _ => {
52 codebase.emit(
53 Diagnostic::error()
54 .with_message("unterminated string")
55 .with_labels(vec![Label::primary(
56 key,
57 reader.cursor..reader.cursor + 1,
58 )
59 .with_message("here")]),
60 );
61 break;
62 }
63 }
64 }
65 println!("String {:?}", string);
66 } else if ['(', ')', '{', '}', ';'].contains(&char) {
67 // * Operator
68 println!("Operator {}", char);
69 }
70 }
71}
pub fn files(&self) -> &Vec<SimpleFile<String, Rc<str>>>
Trait Implementations§
Source§impl<'a> Files<'a> for Codebase
impl<'a> Files<'a> for Codebase
Source§type FileId = usize
type FileId = usize
A unique identifier for files in the file provider. This will be used
for rendering
diagnostic::Label
s in the corresponding source files.Source§fn line_index(&self, file_id: usize, byte_index: usize) -> Result<usize, Error>
fn line_index(&self, file_id: usize, byte_index: usize) -> Result<usize, Error>
The index of the line at the given byte index.
If the byte index is past the end of the file, returns the maximum line index in the file.
This means that this function only fails if the file is not present. Read more
Source§fn line_range(
&self,
file_id: usize,
line_index: usize,
) -> Result<Range<usize>, Error>
fn line_range( &self, file_id: usize, line_index: usize, ) -> Result<Range<usize>, Error>
The byte range of line in the source of the file.
Source§fn line_number(
&'a self,
id: Self::FileId,
line_index: usize,
) -> Result<usize, Error>
fn line_number( &'a self, id: Self::FileId, line_index: usize, ) -> Result<usize, Error>
The user-facing line number at the given line index.
It is not necessarily checked that the specified line index
is actually in the file. Read more
Auto Trait Implementations§
impl Freeze for Codebase
impl RefUnwindSafe for Codebase
impl !Send for Codebase
impl !Sync for Codebase
impl Unpin for Codebase
impl UnwindSafe for Codebase
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more