Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfxb committed Nov 6, 2023
1 parent 368544a commit f3b9fd8
Show file tree
Hide file tree
Showing 22 changed files with 232 additions and 187 deletions.
4 changes: 2 additions & 2 deletions wright/src/bin/wright.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum Commands {
command: DebugCommands,
},

/// Subcommand to start an interactive repl.
/// Subcommand to start an interactive repl.
Repl,
}

Expand Down Expand Up @@ -65,7 +65,7 @@ fn main() -> Result<()> {
Ok(())
}

// Start an interactive repl.
// Start an interactive repl.
Some(Commands::Repl) => repl::start(),

_ => unimplemented!(),
Expand Down
2 changes: 1 addition & 1 deletion wright/src/filemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum FileName {
Real(PathBuf),
/// A named test-case in this crate's source code.
Test(&'static str),
/// The interactive Wright repl.
/// The interactive Wright repl.
#[display(fmt = "REPL:{}", line_number)]
Repl { line_number: usize },
/// An un-named test case in this crate's source code.
Expand Down
4 changes: 2 additions & 2 deletions wright/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

//! The wright programming language crate. This is being re-written from the ground up as of September 2022.
/// The version of this copy of Wright.
/// The version of this copy of Wright.
pub const WRIGHT_VERSION: &'static str = env!("CARGO_PKG_VERSION");

pub mod filemap;
pub mod parser;
pub mod solver;
pub mod repl;
pub mod solver;
5 changes: 2 additions & 3 deletions wright/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Parsers module, for all the parsers implemented by wright and necessary to parse wright source code.
pub mod ast;
pub mod error;
pub mod lexer;
pub mod util;
pub mod state;
pub mod error;

pub mod util;
6 changes: 2 additions & 4 deletions wright/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ pub mod expression;
pub mod identifier;
pub mod metadata;
pub mod path;
pub mod types;
pub mod statement;
pub mod types;

/// Trait implementd
pub trait AstNode<'src> {

}
pub trait AstNode<'src> {}
2 changes: 1 addition & 1 deletion wright/src/parser/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod primary;
/// Enumeration of all the different kinds of expression in wright.
#[derive(Debug)]
pub enum Expression<'src> {
/// A literal in source code.
/// A literal in source code.
Primary(Primary<'src>),
// Block(block::Block<'src>),
}
31 changes: 19 additions & 12 deletions wright/src/parser/ast/expression/literal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
//! Representation for literal expressions in wright source code.
use crate::parser::{state::ParserState, util::{NodeParserResult, map::map_node_type, NodeParserOption, BoxedParserFn}};

use self::{boolean::{BooleanLiteral, parse_boolean_literal}, integer::{IntegerLiteral, parse_integer_literal}};
use crate::parser::{
state::ParserState,
util::{map::map_node_type, BoxedParserFn, NodeParserOption, NodeParserResult},
};

use self::{
boolean::{parse_boolean_literal, BooleanLiteral},
integer::{parse_integer_literal, IntegerLiteral},
};

pub mod boolean;
pub mod integer;
Expand All @@ -14,27 +19,29 @@ pub mod integer;
pub enum Literal<'src> {
/// An integer literal in source code.
Integer(IntegerLiteral<'src>),
/// A boolean literal in source code.
/// A boolean literal in source code.
Boolean(BooleanLiteral<'src>),
}

/// Parse a literal from source code.
pub fn parse_literal<'src>(parser_state: ParserState<'src>) -> NodeParserOption<'src, Literal<'src>> {
// Make a list of the parsers to attempt in order on fresh clones of the parser state.
// Map each parser to the enum constructor to normalize types.
/// Parse a literal from source code.
pub fn parse_literal<'src>(
parser_state: ParserState<'src>,
) -> NodeParserOption<'src, Literal<'src>> {
// Make a list of the parsers to attempt in order on fresh clones of the parser state.
// Map each parser to the enum constructor to normalize types.
let literal_parsers: [BoxedParserFn<'src, NodeParserResult<'src, Literal<'src>>>; 2] = [
map_node_type(parse_integer_literal, Literal::Integer),
map_node_type(parse_boolean_literal, Literal::Boolean),
];

for parser_function in literal_parsers.into_iter() {
// Make a clean state to pass to the child parser.
// Make a clean state to pass to the child parser.
let clean_state = parser_state.clone();

// Call the parser and handle the result.
// Call the parser and handle the result.
match (parser_function)(clean_state) {
// Ignore/handle the output.
// Go to the next parser on errors.
// Ignore/handle the output.
// Go to the next parser on errors.
Err(_) => continue,
ok @ Ok(_) => return ok.ok(),
}
Expand Down
63 changes: 41 additions & 22 deletions wright/src/parser/ast/expression/literal/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
//! Boolean literal representation and parsing in Wright source.
use crate::parser::{ast::metadata::AstNodeMeta, util::{NodeParserResult, ParserSuccess}, state::ParserState, lexer::{IndexedToken, tokens::{Token, TokenTy}}, error::{ParserError, ParserErrorVariant}};
use crate::parser::{
ast::metadata::AstNodeMeta,
error::{ParserError, ParserErrorVariant},
lexer::{
tokens::{Token, TokenTy},
IndexedToken,
},
state::ParserState,
util::{NodeParserResult, ParserSuccess},
};

/// A boolean literal (true or false) in Wright source code.
#[derive(Debug)]
Expand All @@ -11,42 +20,54 @@ pub struct BooleanLiteral<'src> {
pub value: bool,
}

/// Attempt to parse a boolean literal from the lexer held by the parser state.
pub fn parse_boolean_literal<'src>(mut parser_state: ParserState<'src>) -> NodeParserResult<'src, BooleanLiteral<'src>> {
// Get the initial lexer index for later error reporting.
/// Attempt to parse a boolean literal from the lexer held by the parser state.
pub fn parse_boolean_literal<'src>(
mut parser_state: ParserState<'src>,
) -> NodeParserResult<'src, BooleanLiteral<'src>> {
// Get the initial lexer index for later error reporting.
let initial_index = parser_state.lexer.index;

// Match on the next token
// Match on the next token
match parser_state.lexer.next() {
Some(IndexedToken {
index,
token: Token {
variant: TokenTy::True,
length
}
token:
Token {
variant: TokenTy::True,
length,
},
}) => {
// Generate the AST node metadata for the parsed node.
// Generate the AST node metadata for the parsed node.
let ast_node_meta = parser_state.make_ast_node_meta(index, length);

Ok(ParserSuccess {
updated_parser_state: parser_state,
ast_node: BooleanLiteral { meta: ast_node_meta, value: true },
ast_node: BooleanLiteral {
meta: ast_node_meta,
value: true,
},
})
},
}

Some(IndexedToken {
token:
Token {
variant: TokenTy::False,
length
length,
},
index
index,
}) => {
// Make the AST node meta for the parsed node.
let ast_node_meta = parser_state.make_ast_node_meta(index, length);

Ok(ParserSuccess { updated_parser_state: parser_state, ast_node: BooleanLiteral { meta: ast_node_meta, value: false } })
},
Ok(ParserSuccess {
updated_parser_state: parser_state,
ast_node: BooleanLiteral {
meta: ast_node_meta,
value: false,
},
})
}

_ => Err(ParserError {
byte_range: initial_index..parser_state.lexer.index,
Expand All @@ -55,20 +76,18 @@ pub fn parse_boolean_literal<'src>(mut parser_state: ParserState<'src>) -> NodeP
}
}



// impl<'a> Parse for ParseBooleanLiteral<'a> {
// type Success = BooleanLiteral<'a>;

// type Error = ParserError;

// /// Attempt to parse a boolean literal from the given parser. This will mutate the parser, and return either [`Ok`]
// /// with a [`BooleanLiteral`] or an [`Err`] containing a [`ParserError`]. The parser will have the front-most
// /// [`Token`] consumed from its lexer regardless.
// /// with a [`BooleanLiteral`] or an [`Err`] containing a [`ParserError`]. The parser will have the front-most
// /// [`Token`] consumed from its lexer regardless.
// fn parse<'src>(&self, parser: &mut Parser<'src>) -> Result<Self::Success, Self::Error> {
// // Get the initial lexer index for use in calculating the span of errors reported.
// // Get the initial lexer index for use in calculating the span of errors reported.
// let initial_index = parser.lexer.index;

//
//
// }
// }
28 changes: 19 additions & 9 deletions wright/src/parser/ast/expression/literal/integer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
//! Integer literal representation and parsing in wright source.
use crate::parser::{
ast::metadata::AstNodeMeta,
error::{ParserError, ParserErrorVariant},
lexer::{
tokens::{Token, TokenTy},
IndexedToken,
},
state::ParserState,
util::{NodeParserResult, ParserSuccess},
};
use num::{BigUint, Num};
use std::cmp;
use crate::parser::{state::ParserState, ast::metadata::AstNodeMeta, util::{NodeParserResult, ParserSuccess}, lexer::{IndexedToken, tokens::{Token, TokenTy}}, error::{ParserError, ParserErrorVariant}};


/// An integer in Wright source code.
#[derive(Debug)]
Expand All @@ -14,12 +22,14 @@ pub struct IntegerLiteral<'src> {
pub value: BigUint,
}

/// Parse an [`IntegerLiteral`] from source code.
pub fn parse_integer_literal<'src>(mut parser_state: ParserState<'src>) -> NodeParserResult<'src, IntegerLiteral<'src>> {
// Get the initial index of the lexer for later error reporting.
/// Parse an [`IntegerLiteral`] from source code.
pub fn parse_integer_literal<'src>(
mut parser_state: ParserState<'src>,
) -> NodeParserResult<'src, IntegerLiteral<'src>> {
// Get the initial index of the lexer for later error reporting.
let initial_index = parser_state.lexer.index;

// Match on the next token from the lexer, erroring if it's anything but an integer literal.
// Match on the next token from the lexer, erroring if it's anything but an integer literal.
match parser_state.lexer.next() {
Some(IndexedToken {
index,
Expand Down Expand Up @@ -58,12 +68,12 @@ pub fn parse_integer_literal<'src>(mut parser_state: ParserState<'src>) -> NodeP
// Make the AST node metadata for the parsed value
let ast_node_meta = parser_state.make_ast_node_meta(index, length);

Ok(ParserSuccess {
updated_parser_state: parser_state,
Ok(ParserSuccess {
updated_parser_state: parser_state,
ast_node: IntegerLiteral {
meta: ast_node_meta,
value,
}
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion wright/src/parser/ast/expression/parentheses.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! An expression in parentheses in Wright source code.
//! An expression in parentheses in Wright source code.
use crate::parser::ast::metadata::AstNodeMeta;

Expand Down
19 changes: 9 additions & 10 deletions wright/src/parser/ast/expression/primary.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
//! Primary expressions in Qright source code.
//! Primary expressions in Qright source code.
use crate::parser::ast::path::Path;
use super::{literal::Literal, parentheses::ParenthesesExpression};
use crate::parser::ast::path::Path;

/// A primary expression is a special type of low-level expression that can appear in places where other expressions
/// (such as blocks or conditionals) are not allowed.
/// A primary expression is a special type of low-level expression that can appear in places where other expressions
/// (such as blocks or conditionals) are not allowed.
#[derive(Debug)]
pub enum Primary<'src> {
/// A literal in source code.
Literal(Literal<'src>),
/// A path to an item/symbol/constant value.
///
/// This includes identifiers as single element paths.
/// A path to an item/symbol/constant value.
///
/// This includes identifiers as single element paths.
Path(Path<'src>),
/// An expression in parentheses.
Parentheses(ParenthesesExpression<'src>)
/// An expression in parentheses.
Parentheses(ParenthesesExpression<'src>),
}

4 changes: 2 additions & 2 deletions wright/src/parser/ast/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub struct Identifier<'src> {

// impl<'src> Parser<'src> {
// /// Parse an identifier in source code or error.
// ///
// /// If the parse is unsuccessful, return an error and do not update the parser state.
// ///
// /// If the parse is unsuccessful, return an error and do not update the parser state.
// pub fn parse_identifier(&mut self) -> ParserResult<Identifier<'src>> {
// // Clone the lexer to try to parse an identifier.
// let mut lexer = self.lexer.clone();
Expand Down
Loading

0 comments on commit f3b9fd8

Please sign in to comment.