-
Notifications
You must be signed in to change notification settings - Fork 14
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
feat(air-parser): canon stream syntax #618
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,11 +30,12 @@ pub(super) fn try_parse_call_variable( | |
CallVariableParser::try_parse(string_to_parse, start_pos) | ||
} | ||
|
||
#[derive(Debug)] | ||
#[derive(Debug, Clone, Copy)] | ||
enum MetTag { | ||
None, | ||
Stream, | ||
StreamMap, | ||
Canon, | ||
CanonStream, | ||
} | ||
|
||
|
@@ -175,7 +176,10 @@ impl<'input> CallVariableParser<'input> { | |
} | ||
|
||
fn try_parse_as_variable(&mut self) -> LexerResult<()> { | ||
if self.try_parse_as_stream_start()? || self.try_parse_as_json_path_start()? { | ||
if self.try_parse_as_canon()? | ||
|| self.try_parse_as_stream()? | ||
|| self.try_parse_as_json_path_start()? | ||
{ | ||
return Ok(()); | ||
} else if self.is_json_path_started() { | ||
self.try_parse_as_json_path()?; | ||
|
@@ -186,15 +190,31 @@ impl<'input> CallVariableParser<'input> { | |
Ok(()) | ||
} | ||
|
||
fn try_parse_as_stream_start(&mut self) -> LexerResult<bool> { | ||
let stream_tag = MetTag::from_tag(self.current_char()); | ||
if self.current_offset() == 0 && stream_tag.is_tag() { | ||
fn try_parse_as_stream(&mut self) -> LexerResult<bool> { | ||
let tag = MetTag::from_tag(self.current_char()); | ||
if self.current_offset() == 0 && tag.is_tag() { | ||
if self.string_to_parse.len() == 1 { | ||
let error_pos = self.pos_in_string_to_parse(); | ||
return Err(LexerError::empty_stream_name(error_pos..error_pos)); | ||
return Err(LexerError::empty_tagged_name(error_pos..error_pos)); | ||
} | ||
|
||
self.state.met_tag = stream_tag; | ||
self.state.met_tag = tag; | ||
return Ok(true); | ||
} | ||
|
||
Ok(false) | ||
} | ||
|
||
fn try_parse_as_canon(&mut self) -> LexerResult<bool> { | ||
let tag = self.state.met_tag.deduce_tag(self.current_char()); | ||
|
||
if self.current_offset() == 1 && tag.is_canon_stream() { | ||
if self.string_to_parse.len() == 2 && tag.is_tag() { | ||
let error_pos = self.pos_in_string_to_parse(); | ||
return Err(LexerError::empty_canon_name(error_pos..error_pos)); | ||
} | ||
|
||
self.state.met_tag = tag; | ||
return Ok(true); | ||
} | ||
|
||
|
@@ -238,6 +258,9 @@ impl<'input> CallVariableParser<'input> { | |
return Err(LexerError::leading_dot( | ||
self.start_pos..self.pos_in_string_to_parse(), | ||
)); | ||
} else if self.state.met_tag.is_tag() && self.current_offset() <= 2 { | ||
let prev_pos = self.pos_in_string_to_parse() - 1; | ||
return Err(LexerError::empty_canon_name(prev_pos..prev_pos)); | ||
} | ||
self.state.first_dot_met_pos = Some(self.current_offset()); | ||
return Ok(true); | ||
|
@@ -288,7 +311,7 @@ impl<'input> CallVariableParser<'input> { | |
name, | ||
position: self.start_pos, | ||
}, | ||
MetTag::CanonStream => Token::CanonStream { | ||
MetTag::CanonStream | MetTag::Canon => Token::CanonStream { | ||
name, | ||
position: self.start_pos, | ||
}, | ||
|
@@ -311,7 +334,7 @@ impl<'input> CallVariableParser<'input> { | |
lambda, | ||
position: self.start_pos, | ||
}, | ||
MetTag::CanonStream => Token::CanonStreamWithLambda { | ||
MetTag::CanonStream | MetTag::Canon => Token::CanonStreamWithLambda { | ||
name, | ||
lambda, | ||
position: self.start_pos, | ||
|
@@ -383,16 +406,35 @@ impl<'input> CallVariableParser<'input> { | |
} | ||
} | ||
|
||
/// There are two kinds of tags ATM, namely tag and canon tag. | ||
/// Tag defines the first level and comes first in a variable name, e.g. $stream. | ||
/// Canon tag is the only tag that ATM defines the second level. | ||
/// Canon tag comes second in a variable name, e.g. #$canon_stream. | ||
impl MetTag { | ||
fn from_tag(tag: char) -> Self { | ||
match tag { | ||
'$' => Self::Stream, | ||
'#' => Self::CanonStream, | ||
'#' => Self::Canon, | ||
'%' => Self::StreamMap, | ||
_ => Self::None, | ||
} | ||
} | ||
|
||
fn deduce_tag(&self, tag: char) -> Self { | ||
match tag { | ||
'$' if self.is_canon() => Self::CanonStream, | ||
_ => self.to_owned(), | ||
} | ||
} | ||
|
||
fn is_canon(&self) -> bool { | ||
matches!(self, Self::Canon) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you derive It worth deriving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is a benefit comparing with the current code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simple equality seems to be more straightforward. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tend to stick to whatever is used now and see no real benefit changing this to equality b/c it is internally the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code style and idioms are not about internal changes at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO it is unwise to use Eq here given that is_tag() uses !matches and I am not changing is_tag b/c it is outside the scope of this patch. You suggest to break the style from my point of view. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a PR for small style/idiomatic changes is OK, otherwise they will never be changed.
|
||
} | ||
|
||
fn is_canon_stream(&self) -> bool { | ||
matches!(self, Self::CanonStream) | ||
} | ||
|
||
fn is_tag(&self) -> bool { | ||
!matches!(self, Self::None) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it could be expressed in a bit different way, please don't merge the PR, I'll take a look later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to rename try_parse_as_tagged_token to try_parse_as_stream as was discussed.