Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw canonical ast separation #10

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

Conversation

zypeh
Copy link
Member

@zypeh zypeh commented Feb 25, 2020

In this branch:

  • Lexer that tokenizes the text stream
  • Parser that returns the raw ast.

@zypeh zypeh requested review from hch12907 and wongjiahau February 25, 2020 14:14
@@ -154,21 +154,50 @@ impl Parser {
}
}

pub fn build_pair(expressions: Vec<Sexp>) -> Sexp {
pub fn build_pair(mut expressions: Vec<Sexp>) -> Sexp {
expressions.reverse();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hacky yeah, because rust does not have foldr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

/// An s-expression is either an atom or a list of s-expressions. This is
/// similar to the data format used by lisp.
///
/// TODO: I don't know whether I need to add those seven Lisp primitives to

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at the moment no need as we want to keep the scope small.

@@ -1,3 +1,4 @@
pub mod node;
pub mod sexpr_parser;
pub mod sexpr_tokenizer;
pub mod sexpr_tokenizer_2;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put these modules into a separate directory?

None => vec![token]
}
})
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: return { .. }; vs { .. }

match tokens.last() {
Some(lastToken) => match (lastToken, token) {
(Token::Whitespace, Token::Whitespace) =>
[&tokens[0..lineNumber]].concat(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the concat is not needed here.

/// All strings must be valid utf-8.
#[derive(PartialEq, Clone, PartialOrd, Debug)]
pub enum Atom {
/// N stands for node

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that those are the short forms, but why short forms in the first place?

}

pub struct Parser {
tokens: Vec<Token>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pure nit, can be ignored: Box<[Token]> because tokens will never grow under Parser
(tokens.into_boxed_slice() will do that)

#[derive(Debug)]
pub struct TokenizerError {
/// The error message.
pub message: &'static str,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like an enum TokenizerErrorKind is appropriate here, but I digress..

src/sexpr_tokenizer.rs Show resolved Hide resolved
/// This will consume text stream and produces tokens one by one
fn next_token(&self, chars: &mut Peekable<Chars<'_>>) -> Result<Option<Token>, TokenizerError> {
match chars.peek() {
Some(&ch) => match ch {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing \n case (only \r\n is handled)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😨

src/sexpr_tokenizer.rs Show resolved Hide resolved
/// Read from `chars` until `predicate` returns `false` or EOF is hit.
/// Return the characters read as String, and keep the first non-matching
/// char available as `chars.next()`.
fn peeking_take_while(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about take_while() but Peekable has disappointed me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants