-
-
Notifications
You must be signed in to change notification settings - Fork 1
[codex] Add stream-based parse convenience #244
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
src/bin/snapshots/antlr4_rust_gen__tests__parser_parse_convenience.snap
This file contains hidden or 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,85 @@ | ||
| --- | ||
| source: src/bin/antlr4-rust-gen.rs | ||
| expression: "render_parser_parse_convenience(\"TParser\")" | ||
| --- | ||
| /// Result from [`parse_with_parser`] or [`parse_stream_with_parser`]. | ||
| /// | ||
| /// Keeps the generated parser available after the entry rule runs so callers | ||
| /// can inspect diagnostics or recover the parser-owned token stream. | ||
| #[derive(Debug)] | ||
| pub struct TParserParseOutput<R, L> | ||
| where | ||
| L: TokenSource, | ||
| { | ||
| pub result: R, | ||
| pub parser: TParser<L>, | ||
| } | ||
|
|
||
| /// Parses UTF-8 text by constructing the lexer, token stream, parser, and | ||
| /// caller-selected entry rule in one call. | ||
| /// | ||
| /// Pass the generated lexer constructor and a parser entry rule, for example | ||
| /// `parse(src, MyGrammarLexer::new, TParser::file)`. | ||
| /// | ||
| /// The returned [`antlr4_runtime::ParsedFile`] owns the canonical token store, | ||
| /// flat CST storage, and entry-rule root. | ||
| /// Use [`parse_with_parser`] instead when the caller also needs parser | ||
| /// diagnostics after the entry rule runs. | ||
| pub fn parse<L: TokenSource>( | ||
| input: impl AsRef<str>, | ||
| lexer: impl FnOnce(antlr4_runtime::InputStream) -> L, | ||
| entry: impl FnOnce(&mut TParser<L>) -> Result<antlr4_runtime::NodeId, antlr4_runtime::AntlrError>, | ||
| ) -> Result<antlr4_runtime::ParsedFile, antlr4_runtime::AntlrError> | ||
| { | ||
| parse_stream(antlr4_runtime::InputStream::new(input.as_ref()), lexer, entry) | ||
| } | ||
|
|
||
| /// Parses UTF-8 text like [`parse`] while returning the parser after the entry | ||
| /// rule has run. | ||
| /// | ||
| /// This keeps the compact generated setup path available for callers that also | ||
| /// need `Parser::number_of_syntax_errors()` or `TParser::into_token_stream()`. | ||
| pub fn parse_with_parser<L: TokenSource, R>( | ||
| input: impl AsRef<str>, | ||
| lexer: impl FnOnce(antlr4_runtime::InputStream) -> L, | ||
| entry: impl FnOnce(&mut TParser<L>) -> Result<R, antlr4_runtime::AntlrError>, | ||
| ) -> Result<TParserParseOutput<R, L>, antlr4_runtime::AntlrError> | ||
| { | ||
| parse_stream_with_parser( | ||
| antlr4_runtime::InputStream::new(input.as_ref()), | ||
| lexer, | ||
| entry, | ||
| ) | ||
| } | ||
|
|
||
| /// Parses a caller-provided character stream by constructing the lexer, token | ||
| /// stream, parser, and caller-selected entry rule in one call. | ||
| /// | ||
| /// Unlike [`parse`], this accepts any [`antlr4_runtime::CharStream`], including | ||
| /// a named [`antlr4_runtime::InputStream`] or a byte-oriented | ||
| /// [`antlr4_runtime::ByteStream`]. | ||
| pub fn parse_stream<I: antlr4_runtime::CharStream, L: TokenSource>( | ||
| input: I, | ||
| lexer: impl FnOnce(I) -> L, | ||
| entry: impl FnOnce(&mut TParser<L>) -> Result<antlr4_runtime::NodeId, antlr4_runtime::AntlrError>, | ||
| ) -> Result<antlr4_runtime::ParsedFile, antlr4_runtime::AntlrError> | ||
| { | ||
| let TParserParseOutput { result, parser } = | ||
| parse_stream_with_parser(input, lexer, entry)?; | ||
| Ok(parser.into_parsed_file(result)) | ||
| } | ||
|
|
||
| /// Parses a caller-provided character stream like [`parse_stream`] while | ||
| /// returning the parser after the entry rule has run. | ||
| pub fn parse_stream_with_parser<I: antlr4_runtime::CharStream, L: TokenSource, R>( | ||
| input: I, | ||
| lexer: impl FnOnce(I) -> L, | ||
| entry: impl FnOnce(&mut TParser<L>) -> Result<R, antlr4_runtime::AntlrError>, | ||
| ) -> Result<TParserParseOutput<R, L>, antlr4_runtime::AntlrError> | ||
| { | ||
| let lexer = lexer(input); | ||
| let tokens = CommonTokenStream::new(lexer); | ||
| let mut parser = TParser::new(tokens); | ||
| let result = entry(&mut parser)?; | ||
| Ok(TParserParseOutput { result, parser }) | ||
| } |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.