Repository navigation
Parser update - #7
Conversation
reknih
left a comment
There was a problem hiding this comment.
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..
| pub use crate::syntax::span::{Span, Spanned}; | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Maybe we should really have automatic formatting to prevent changes like these
There was a problem hiding this comment.
Yes, maybe, but it makes some code so horrible!
| 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I propose changing the argument name to more.
| /// The standard library scope. | ||
| scope: Scope, | ||
| /// The base parser state. | ||
| parse_state: ParseState, |
There was a problem hiding this comment.
Nice change, makes it really clear where stuff goes!
| /// ``` | ||
| /// try_or!(result, continue); | ||
| /// ``` | ||
| macro_rules! try_or { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
| .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) |
| impl Debug for Span { | ||
| fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
| write!(f, "({:?} -> {:?})", self.start, self.end) | ||
| write!(f, "<{:?}-{:?}>", self.start, self.end) |
| (&self.src[start .. end], terminated) | ||
| } | ||
|
|
||
| fn parse_string(&mut self) -> Token<'s> { |
There was a problem hiding this comment.
Good riddance of parse everywhere..
|
It think |
Signed-off-by: Michael Irwin <[email protected]> Resolves typst#7
Parser update
This PR gives the parser and some associated modules a facelift, mostly due to better parsing primitives.
parse_collectionfunction 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.pval!macro which reduces boilerplate for testing expression parsing.ParseContextstruct was replaced by aParseStatewhich 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.parse_<thing>toread_<thing>to better distinguish tokenizing from parsing.Some extra small things: