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

Parser update #7

Merged
merged 4 commits into from
Jul 29, 2020
Merged

Parser update #7

merged 4 commits into from
Jul 29, 2020

Conversation

laurmaedje
Copy link
Member

@laurmaedje laurmaedje commented Jul 27, 2020

This PR gives the parser and some associated modules a facelift, mostly due to better parsing primitives.

  • Better parsing primitives leading to simpler code.
  • More consistent and simpler error handling methods.
  • The complex parse_collection function has been removed in favor of separate parsers for function arguments, tuples and objects. While this leads to (very) slight duplication, the code is much easier to understand and the error handling way less complicated.
  • The parser test code was cleaned up a bit, especially through the introduction of the pval! macro which reduces boilerplate for testing expression parsing.
  • The ParseContext struct was replaced by a ParseState which owns the function scope instead of borrowing it. This will later be useful to support function definitions (as they need to update the scope). As to the name: State fits better than context with the envisioned design of the layouter.
  • The tokenizer's function names have been changed from parse_<thing> to read_<thing> to better distinguish tokenizing from parsing.

Some extra small things:

  • The order of the items in the span module was brought into an order which is more consistent with the rest of the project (general to specific / big to small instead of the other way around).
  • The order of the arguments to the parse function was also updated to better reflect their importance.

Copy link
Member

@reknih reknih left a comment

Choose a reason for hiding this comment

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

src/syntax/parsing.rs seems to be a bit longer now, but also less nested and the functions make more sense (also, no take!)!
So, overall, I like it! Two minor change requests attached..

@@ -17,7 16,6 @@ pub mod prelude {
pub use crate::syntax::span::{Span, Spanned};
}


Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should really have automatic formatting to prevent changes like these

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, maybe, but it makes some code so horrible!

src/lib.rs Outdated
pub fn extend_offset(&mut self, offset: Position, other: Feedback) {
self.problems.extend(offset_spans(offset, other.problems));
self.decos.extend(offset_spans(offset, other.decos));
pub fn extend_offset(&mut self, other: Feedback, offset: Position) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm aware that this was here before, but since this PR changes the argument orders and other stylicstic stuff, allow me this remark: I'm not sure whether other is a great argument name. If it remains as such, the arguments should be more clearly explained in the Doc-Comment, especially since this is a public function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I propose changing the argument name to more.

/// The standard library scope.
scope: Scope,
/// The base parser state.
parse_state: ParseState,
Copy link
Member

Choose a reason for hiding this comment

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

Nice change, makes it really clear where stuff goes!

/// ```
/// try_or!(result, continue);
/// ```
macro_rules! try_or {
Copy link
Member

Choose a reason for hiding this comment

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

I really feel like this should be in the language.

pub struct ParseContext<'a> {
/// The scope containing function definitions.
pub scope: &'a Scope,
/// The state which can influence how a string of source code is parsed.
Copy link
Member

Choose a reason for hiding this comment

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

This is a major improvement over the last Doc-Comment

// Only at least two newlines mean a _real_ newline indicating a
// paragraph break.
// Starting from two newlines counts as a paragraph break, a single
// newline not.
Copy link
Member

Choose a reason for hiding this comment

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

Missing does

.map(|x| &**x)
.ok_or_else(|| &*self.fallback)
pub fn get_parser(&self, name: &str) -> Option<&Parser> {
self.parsers.get(name).map(AsRef::as_ref)
Copy link
Member

Choose a reason for hiding this comment

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

More rs::BlackMagic

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure :)

@@ -111,56 114,67 @@ impl Span {

impl Debug for Span {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "({:?} -> {:?})", self.start, self.end)
write!(f, "<{:?}-{:?}>", self.start, self.end)
Copy link
Member

Choose a reason for hiding this comment

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

Finally!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yay 🎉

@@ -367,12 368,12 @@ impl<'s> Tokens<'s> {
(&self.src[start .. end], terminated)
}

fn parse_string(&mut self) -> Token<'s> {
Copy link
Member

Choose a reason for hiding this comment

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

Good riddance of parse everywhere..

@laurmaedje
Copy link
Member Author

It think src/syntax/parsing.rs is now actually sixty lines shorter!

Copy link
Member

@reknih reknih left a comment

Choose a reason for hiding this comment

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

LGTM now

@laurmaedje laurmaedje merged commit f34ba3d into master Jul 29, 2020
@laurmaedje laurmaedje deleted the parser-update branch July 29, 2020 15:47
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.

2 participants