-
Notifications
You must be signed in to change notification settings - Fork 39
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
refactor: parse pragmas with monch #224
Draft
dsherret
wants to merge
5
commits into
denoland:main
Choose a base branch
from
dsherret:refactor_parse_deno_types_with_monch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5b4976
refactor: parse @deno-types directives with monch
dsherret 7c174aa
Wasm
dsherret 8909ec0
remove allowing whitespace surrounding the equals sign -- nobody does…
dsherret 4028d95
Merge branch 'main' into refactor_parse_deno_types_with_monch
dsherret 1c2c4ba
More monch parsing.
dsherret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use monch::*; | ||
|
||
pub struct ParsedDenoTypes<'a> { | ||
pub text: &'a str, | ||
pub quote_start: usize, | ||
pub quote_end: usize, | ||
} | ||
|
||
pub fn parse_deno_types(input: &str) -> Result<ParsedDenoTypes, ParseError> { | ||
let original_input = input; | ||
let (input, _) = skip_whitespace(input)?; | ||
let (input, _) = tag("@deno-types")(input)?; | ||
let (input, _) = ch('=')(input)?; | ||
let quote_start_input = input; | ||
let (input, quote_char) = or(ch('"'), ch('\"'))(input)?; | ||
let (input, text) = take_while(|c| c != quote_char)(input)?; | ||
let (input, _) = ch(quote_char)(input)?; | ||
Ok(ParsedDenoTypes { | ||
text, | ||
quote_start: original_input.len() - quote_start_input.len(), | ||
quote_end: original_input.len() - input.len(), | ||
}) | ||
} | ||
|
||
pub struct ParsedTripleSlashReference<'a> { | ||
pub text: &'a str, | ||
pub quote_start: usize, | ||
pub quote_end: usize, | ||
} | ||
|
||
/// Matches a `/// <reference ... />` comment reference based on the kind (ex. path or types). | ||
pub fn parse_triple_slash_reference<'a>( | ||
kind: &str, | ||
input: &'a str, | ||
) -> Result<ParsedTripleSlashReference<'a>, ParseError<'a>> { | ||
// regex in TS codebase: /^(\/\/\/\s*<reference\s+path\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/ | ||
|
||
let original_input = input; | ||
let (input, _) = ch('/')(input)?; // only one, because we're starting from within a comment line | ||
let (input, _) = skip_whitespace(input)?; | ||
let (input, _) = tag("<reference")(input)?; | ||
let (input, _) = skip_whitespace(input)?; | ||
let (input, _) = tag(kind)(input)?; // "path" or "types" | ||
let (input, _) = skip_whitespace(input)?; | ||
let (input, _) = ch('=')(input)?; | ||
let (input, _) = skip_whitespace(input)?; | ||
let quote_start_input = input; | ||
let (input, quote_char) = or(ch('"'), ch('\"'))(input)?; | ||
let (input, text) = take_while(|c| c != quote_char)(input)?; | ||
let (input, _) = ch(quote_char)(input)?; | ||
let quote_end_input = input; | ||
if !input.contains("/>") { | ||
return Err(monch::ParseError::Backtrace); | ||
} | ||
Ok(ParsedTripleSlashReference { | ||
text, | ||
quote_start: original_input.len() - quote_start_input.len(), | ||
quote_end: original_input.len() - quote_end_input.len(), | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
This would go across newlines, which is not good.