Skip to content

Raw canonical ast separation#10

Open
zypeh wants to merge 17 commits into
masterfrom
raw-canonical-ast-separation
Open

Raw canonical ast separation#10
zypeh wants to merge 17 commits into
masterfrom
raw-canonical-ast-separation

Conversation

@zypeh

@zypeh zypeh commented Feb 25, 2020

Copy link
Copy Markdown
Member

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
Comment thread src/sexpr_parser.rs

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

Copy link
Copy Markdown
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yep.

Comment thread src/sexpr_parser.rs
/// 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

Copy link
Copy Markdown

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.

Comment thread src/lib.rs
pub mod node;
pub mod sexpr_parser;
pub mod sexpr_tokenizer;
pub mod sexpr_tokenizer_2; No newline at end of file

Copy link
Copy Markdown

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?

Comment thread src/sexpr_tokenizer_2.rs
None => vec![token]
}
})
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread src/sexpr_tokenizer_2.rs
match tokens.last() {
Some(lastToken) => match (lastToken, token) {
(Token::Whitespace, Token::Whitespace) =>
[&tokens[0..lineNumber]].concat(),

Copy link
Copy Markdown

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.

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

Copy link
Copy Markdown

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?

Comment thread src/sexpr_parser.rs
}

pub struct Parser {
tokens: Vec<Token>,

Copy link
Copy Markdown

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)

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

Copy link
Copy Markdown

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..

Comment thread src/sexpr_tokenizer.rs
Comment thread src/sexpr_tokenizer.rs
/// 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 {

Copy link
Copy Markdown

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

😨

Comment thread src/sexpr_tokenizer.rs
Comment thread src/sexpr_tokenizer.rs
/// 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(

Copy link
Copy Markdown

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