Skip to content

Commit 260e638

Browse files
committed
feat: parser
1 parent d7a97e1 commit 260e638

File tree

16 files changed

+883
-238
lines changed

16 files changed

+883
-238
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/custom-language-rust.iml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/discord.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/engine/src/component.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ pub trait ComponentErrors {
2020

2121
#[cfg(feature = "cli")]
2222
fn get_source_sliced(&self, start: crate::Cursor, end: crate::Cursor) -> crate::error::SourceFile {
23-
let (path, input) = *self.source().clone();
23+
let source_file = self.source();
2424

2525
let start_index = start.index() as usize;
2626
let end_index = end.index() as usize;
2727

28-
let source = &input[start_index..end_index];
28+
let source = &source_file.as_ref().1[start_index..end_index];
2929

30-
Box::from((path, source.to_string()))
30+
Box::from((source_file.as_ref().0.clone(), source.to_string()))
3131
}
3232
}
3333

3434
pub trait ComponentIter<'a, C, T, I> where
35+
C: PartialEq<T> + PartialEq,
3536
T: PartialEq<C> + PartialEq + Clone + 'a,
3637
I: Iterator<Item = T> + 'a {
3738

@@ -49,7 +50,7 @@ pub trait ComponentIter<'a, C, T, I> where
4950
}
5051
}
5152

52-
/// Iterates to the next character
53+
/// Iterates to the next item
5354
fn next(&mut self) -> Option<T> {
5455
if let Some(item) = self.get_iter().next() {
5556
self.cursor_next(&item);
@@ -59,7 +60,7 @@ pub trait ComponentIter<'a, C, T, I> where
5960
}
6061
}
6162

62-
/// Iterates to the next character if the next character is equal to the char argument
63+
/// Iterates to the next item if the next item is equal to the item argument
6364
fn next_if_eq(&mut self, item: &C) -> Option<T> {
6465
if self.peek_is(item) {
6566
self.next()
@@ -68,7 +69,16 @@ pub trait ComponentIter<'a, C, T, I> where
6869
}
6970
}
7071

71-
/// Expects a character to be there
72+
/// Iterates to the next item if the next item is equal to the item argument
73+
fn next_if_eq_mul(&mut self, items: &[C]) -> Option<T> {
74+
if self.peek().is_some_and(|t| items.iter().any(|c| c == t)) {
75+
self.next()
76+
} else {
77+
None
78+
}
79+
}
80+
81+
/// Expects a item to be there
7282
fn expect(&mut self, expected: &C) -> std::result::Result<T, Option<T>> {
7383
let Some(item) = self.next_if_eq(expected) else {
7484
return Err(None);
@@ -81,7 +91,7 @@ pub trait ComponentIter<'a, C, T, I> where
8191
}
8292
}
8393

84-
/// Checks if the next character is equal to the char argument
94+
/// Checks if the next item is equal to the item argument
8595
fn peek_is(&mut self, item: &C) -> bool {
8696
if let Some(peek) = self.peek() {
8797
peek == item
@@ -90,7 +100,7 @@ pub trait ComponentIter<'a, C, T, I> where
90100
}
91101
}
92102

93-
/// Returns the next character if exists without iterating
103+
/// Returns the next item if exists without iterating
94104
fn peek<'b>(&'b mut self) -> Option<&'b T>
95105
where I: 'b {
96106
self.get_iter().peek()

packages/engine/src/cursor.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,20 @@ impl From<Cursor> for CursorTuple {
8888
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8989
pub struct WithCursor<T> {
9090
pub value: T,
91-
pub cursor: Cursor
91+
pub start: Cursor,
92+
pub end: Cursor,
9293
}
9394

9495
impl<T> WithCursor<T> {
9596
pub fn create(value: T) -> Self {
97+
Self::create_with(Cursor::create(), Cursor::create(), value)
98+
}
99+
100+
pub fn create_with(start: Cursor, end: Cursor, value: T) -> Self {
96101
Self {
97102
value,
98-
cursor: Cursor::create()
103+
start,
104+
end,
99105
}
100106
}
101107
}

packages/engine/src/error.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#[cfg(feature = "cli")]
22
use colored::Colorize;
33

4-
#[derive(thiserror::Error, Debug, Clone)]
5-
pub enum EngineError {
4+
use crate::lexer::tokens::LexerTokenKind;
5+
6+
#[derive(thiserror::Error, PartialEq, Eq, Debug, Clone)]
7+
pub enum EngineErrorKind {
68
#[error("{0}")]
79
LexerError(#[from] crate::lexer::LexerError),
810
#[error("{0}")]
911
ParserError(#[from] crate::parser::ParserError),
12+
#[error("failed to get value as '{0}' from literal '{1}'")]
13+
LiteralExtractionError(LexerTokenKind, LexerTokenKind),
1014
#[error("an unreachable error has occurred. this shouldn't ever happen")]
1115
Unreachable,
1216
#[error("an unknown error has occurred")]
1317
UnknownError,
1418
}
1519

16-
pub type EngineResult<T> = std::result::Result<T, EngineError>;
17-
pub type ErrorList = Vec<EngineError>;
20+
pub type EngineResult<T> = std::result::Result<T, EngineErrorKind>;
21+
pub type ErrorList = Vec<EngineErrorKind>;
1822

1923
#[cfg(feature = "cli")]
2024
pub(super) type SourceFile = Box<(Option<std::path::PathBuf>, String)>;

0 commit comments

Comments
 (0)