Pass some docs tests
This commit is contained in:
parent
ab53df56bc
commit
e45ac042b9
@ -55,11 +55,11 @@ pub fn lex<'chars, 'src: 'chars>(
|
|||||||
/// ```
|
/// ```
|
||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let input = "x = 1 + 2";
|
/// let input = "x = 1 + 2";
|
||||||
/// let mut lexer = Lexer::new();
|
/// let mut lexer = Lexer::new(input);
|
||||||
/// let mut tokens = Vec::new();
|
/// let mut tokens = Vec::new();
|
||||||
///
|
///
|
||||||
/// loop {
|
/// loop {
|
||||||
/// let (token, span) = lexer.next_token(input).unwrap();
|
/// let (token, span) = lexer.next_token().unwrap();
|
||||||
/// let is_eof = matches!(token, Token::Eof);
|
/// let is_eof = matches!(token, Token::Eof);
|
||||||
///
|
///
|
||||||
/// tokens.push((token, span));
|
/// tokens.push((token, span));
|
||||||
|
@ -32,7 +32,7 @@ pub mod value;
|
|||||||
pub mod vm;
|
pub mod vm;
|
||||||
|
|
||||||
pub use analyzer::{analyze, AnalysisError, Analyzer};
|
pub use analyzer::{analyze, AnalysisError, Analyzer};
|
||||||
pub use ast::{AbstractSyntaxTree, Expression, Span, Statement};
|
pub use ast::{AbstractSyntaxTree, AstError, Expression, Node, Span, Statement};
|
||||||
pub use built_in_function::{BuiltInFunction, BuiltInFunctionError};
|
pub use built_in_function::{BuiltInFunction, BuiltInFunctionError};
|
||||||
pub use constructor::Constructor;
|
pub use constructor::Constructor;
|
||||||
pub use context::{Context, ContextData, ContextError};
|
pub use context::{Context, ContextData, ContextError};
|
||||||
|
@ -16,31 +16,21 @@ use crate::{ast::*, DustError, Identifier, LexError, Lexer, Token, TokenKind, To
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let tree = parse("x + 42").unwrap();
|
/// let source = "42.to_string()";
|
||||||
///
|
///
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// tree,
|
/// parse(source),
|
||||||
/// AbstractSyntaxTree {
|
/// Ok(AbstractSyntaxTree::with_statements([
|
||||||
/// nodes: [
|
/// Statement::Expression(Expression::call(
|
||||||
/// Node::new(
|
/// Expression::field_access(
|
||||||
/// Statement::BinaryOperation {
|
/// Expression::literal(42, (0, 2)),
|
||||||
/// left: Box::new(Node::new(
|
/// Node::new(Identifier::new("to_string"), (3, 12)),
|
||||||
/// Statement::Identifier(Identifier::new("x")),
|
/// (0, 12)
|
||||||
/// (0, 1),
|
/// ),
|
||||||
/// )),
|
/// vec![],
|
||||||
/// operator: Node::new(
|
/// (0, 14)
|
||||||
/// BinaryOperator::Add,
|
/// ))
|
||||||
/// (2, 3)
|
/// ]))
|
||||||
/// ),
|
|
||||||
/// right: Box::new(Node::new(
|
|
||||||
/// Statement::Constant(Value::integer(42)),
|
|
||||||
/// (4, 6),
|
|
||||||
/// ))
|
|
||||||
/// },
|
|
||||||
/// (0, 6),
|
|
||||||
/// )
|
|
||||||
/// ].into(),
|
|
||||||
/// },
|
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse(source: &str) -> Result<AbstractSyntaxTree, DustError> {
|
pub fn parse(source: &str) -> Result<AbstractSyntaxTree, DustError> {
|
||||||
@ -55,7 +45,8 @@ pub fn parse_into<'src>(
|
|||||||
source: &'src str,
|
source: &'src str,
|
||||||
tree: &mut AbstractSyntaxTree,
|
tree: &mut AbstractSyntaxTree,
|
||||||
) -> Result<(), DustError<'src>> {
|
) -> Result<(), DustError<'src>> {
|
||||||
let mut parser = Parser::new(source);
|
let lexer = Lexer::new(source);
|
||||||
|
let mut parser = Parser::new(lexer);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let node = parser
|
let node = parser
|
||||||
@ -82,21 +73,21 @@ pub fn parse_into<'src>(
|
|||||||
/// # use std::collections::VecDeque;
|
/// # use std::collections::VecDeque;
|
||||||
/// # use dust_lang::*;
|
/// # use dust_lang::*;
|
||||||
/// let source = "x = 42";
|
/// let source = "x = 42";
|
||||||
/// let lexer = Lexer::new();
|
/// let lexer = Lexer::new(source);
|
||||||
/// let mut parser = Parser::new(input, lexer);
|
/// let mut parser = Parser::new(lexer);
|
||||||
/// let mut nodes = VecDeque::new();
|
/// let mut statements = VecDeque::new();
|
||||||
///
|
///
|
||||||
/// loop {
|
/// loop {
|
||||||
/// let node = parser.parse().unwrap();
|
/// let statement = parser.parse_statement().unwrap();
|
||||||
///
|
///
|
||||||
/// nodes.push_back(node);
|
/// statements.push_back(statement);
|
||||||
///
|
///
|
||||||
/// if let Token::Eof = parser.current().0 {
|
/// if parser.is_eof() {
|
||||||
/// break;
|
/// break;
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// let tree = AbstractSyntaxTree { nodes };
|
/// let tree = AbstractSyntaxTree { statements };
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Parser<'src> {
|
pub struct Parser<'src> {
|
||||||
@ -107,8 +98,7 @@ pub struct Parser<'src> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'src> Parser<'src> {
|
impl<'src> Parser<'src> {
|
||||||
pub fn new(source: &'src str) -> Self {
|
pub fn new(mut lexer: Lexer<'src>) -> Self {
|
||||||
let mut lexer = Lexer::new(source);
|
|
||||||
let (current_token, current_position) = lexer.next_token().unwrap_or((Token::Eof, (0, 0)));
|
let (current_token, current_position) = lexer.next_token().unwrap_or((Token::Eof, (0, 0)));
|
||||||
|
|
||||||
Parser {
|
Parser {
|
||||||
@ -119,6 +109,10 @@ impl<'src> Parser<'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_eof(&self) -> bool {
|
||||||
|
matches!(self.current_token, Token::Eof)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_statement(&mut self) -> Result<Statement, ParseError> {
|
pub fn parse_statement(&mut self) -> Result<Statement, ParseError> {
|
||||||
let start_position = self.current_position;
|
let start_position = self.current_position;
|
||||||
|
|
||||||
@ -1295,16 +1289,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(source),
|
parse(source),
|
||||||
Ok(AbstractSyntaxTree {
|
Ok(AbstractSyntaxTree::with_statements([Statement::Let(
|
||||||
statements: [Statement::Let(Node::new(
|
Node::new(
|
||||||
LetStatement::Let {
|
LetStatement::Let {
|
||||||
identifier: Node::new(Identifier::new("x"), (4, 5)),
|
identifier: Node::new(Identifier::new("x"), (4, 5)),
|
||||||
value: Expression::literal(42, (8, 10)),
|
value: Expression::literal(42, (8, 10)),
|
||||||
},
|
},
|
||||||
(0, 11),
|
(0, 11),
|
||||||
))]
|
)
|
||||||
.into()
|
)]))
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user