Skip to content

Parser update - #7

Merged
laurmaedje merged 4 commits into
masterfrom
parser-update
Jul 29, 2020
Merged

laurmaedje merged 4 commits into
masterfrom
parser-update

Conversation

@laurmaedje

@laurmaedje laurmaedje commented Jul 27, 2020 •

Copy link
Copy Markdown
Member

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.

@reknih reknih left a comment

Copy link
Copy Markdown
Member

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

Comment thread src/func.rs
pub use crate::syntax::span::{Span, Spanned};
}


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

Comment thread 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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/lib.rs
/// The standard library scope.
scope: Scope,
/// The base parser state.
parse_state: ParseState,

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

Comment thread src/macros.rs
/// ```
/// try_or!(result, continue);
/// ```
macro_rules! try_or {

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

Comment thread src/syntax/parsing.rs
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
Copy Markdown
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

Comment thread src/syntax/parsing.rs Outdated
// 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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missing does

Comment thread src/syntax/scope.rs
.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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure :)

Comment thread src/syntax/span.rs
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Finally!

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.

Yay 🎉

Comment thread src/syntax/tokens.rs
(&self.src[start .. end], terminated)
}

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

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

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

@reknih reknih left a comment

Copy link
Copy Markdown
Member

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
kan190729 pushed a commit to oss-experiment-uec/2024-k2210165-typst that referenced this pull request Dec 23, 2024
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
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