-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize parser and lexer to be separate features/modules.
- Loading branch information
Showing
26 changed files
with
120 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! [Abstract syntax tree] modeling. | ||
//! | ||
//! [Abstract syntax tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree | ||
pub mod identifier; | ||
pub mod path; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! [Identifier]s are used throughout wright as variable names, type names, function names, etc. | ||
//! Their modeling is pretty simple, and is defined here. | ||
//! | ||
//! [Identifier]: https://en.wikipedia.org/wiki/Identifier | ||
use crate::source_tracking::fragment::Fragment; | ||
|
||
/// Identifiers are used as names for variables, functions, modules, etc. | ||
/// These are defined using [Fragment]s of source code, which will contain the identifier itself. | ||
#[derive(Debug, Clone)] | ||
pub struct Identifier { | ||
/// The fragment of source code containing the identifier. | ||
pub fragment: Fragment, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! [Path]s are used in import statements, and can take the place of an [Identifier] in many people. | ||
use crate::source_tracking::fragment::Fragment; | ||
use super::identifier::Identifier; | ||
|
||
/// A double-colon separated path/reference to a module/function. This can be used in an `import` declaration and | ||
/// some other places. [Path]s with length of 1 are just [Identifier]s -- [Identifier]s can be considered paths in some | ||
/// instances. | ||
#[derive(Debug, Clone)] | ||
pub struct Path { | ||
/// The [Fragment] of source code containing the full source of this path (including the double-colon separators). | ||
pub full_path: Fragment, | ||
|
||
/// The first (left-most) identifier in this [Path]. This can also be considered the "root" of the path -- | ||
/// the module that the following item/identifier can be found in. | ||
pub head: Identifier, | ||
|
||
/// The rest of the [Path], following the first separator. | ||
pub tail: Option<Box<Path>> | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,40 @@ | ||
//! The wright lexer, parser, and AST representation. | ||
//! This parser module is responsible for turning the stream of [Token]s from the [Lexer] into a tree of [AST] nodes. | ||
//! | ||
//! [AST]: crate::ast | ||
// pub mod error; | ||
// pub mod state; | ||
// pub mod util; | ||
use super::lexer::{token::{Token, TokenTy}, Lexer}; | ||
|
||
// pub mod ast; | ||
pub mod lexer; | ||
mod identifier; | ||
|
||
/// Errors that can arise when parsing a source to an abstract syntax tree node. | ||
#[derive(Debug)] | ||
pub enum ParseError { | ||
/// Expected one type of token, found another | ||
Expected { | ||
/// The expected variant. | ||
expected: TokenTy, | ||
/// The token found from the lexer. | ||
found: Option<Token>, | ||
} | ||
} | ||
|
||
/// Trait implemented by all AST nodes that can be parsed. | ||
pub trait Parse: Sized { | ||
/// Attempt to parse a tree node of this type from a given [Lexer]. | ||
fn parse(lexer: &mut Lexer) -> Result<Self, ParseError>; | ||
} | ||
|
||
impl Lexer { | ||
/// Pull the next token from a lexer, and return an error if it's not of the given variant. | ||
pub fn expect(&mut self, token_ty: TokenTy) -> Result<Token, ParseError> { | ||
let next_token = self | ||
.next_token() | ||
.ok_or(ParseError::Expected { expected: token_ty, found: None })?; | ||
|
||
if next_token.variant != token_ty { | ||
return Err(ParseError::Expected { expected: token_ty, found: Some(next_token) }); | ||
} | ||
|
||
Ok(next_token) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::{ast::identifier::Identifier, lexer::{token::TokenTy, Lexer}}; | ||
use super::{Parse, ParseError}; | ||
|
||
impl Parse for Identifier { | ||
fn parse(lexer: &mut Lexer) -> Result<Self, ParseError> { | ||
let ident_token = lexer.expect(TokenTy::Identifier)?; | ||
Ok(Identifier { fragment: ident_token.fragment }) | ||
} | ||
} |