-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a basic version of
%parse-param
.
We previously could parse `%parse_param` but not do anything with it. This commit adds support for `%parse-param` (using Bison's spelling) which, if defined, allows the user to pass an extra parameter to the parser. See `lrpar/cttests/src/parseparam.test` for an example: ``` %start S %parse-param p: &'input u64 %% S -> u64: 'INT' { *p + $lexer.span_str($1.unwrap().span()).parse::<u64>().unwrap() } ; %% ``` Now the generated parser can be called with two parameters `parse(&lexer, &1234)`. Since Rust has tuples, `parse-param` only needs to take a single paramater (whereas Bison allows multiple extra parameters), which is fortunate, because Rust doesn't have varargs, which would make dealing with multiple parameters extremely difficult. This commit is a bit simple in some ways: the type of the extra parameter must implement `Copy` and, if it's a reference, it must be tied to the `'input` lifetime. Perhaps these restrictions could be relaxed one day, but this is a start, and better than nothing.
- Loading branch information
Showing
10 changed files
with
169 additions
and
165 deletions.
There are no files selected for viewing
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
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,13 @@ | ||
name: Test %parse-param | ||
yacckind: Grmtools | ||
grammar: | | ||
%start S | ||
%parse-param p: &'input u64 | ||
%% | ||
S -> u64: | ||
'INT' { *p + $lexer.span_str($1.unwrap().span()).parse::<u64>().unwrap() } | ||
; | ||
%% | ||
lexer: | | ||
%% | ||
[0-9]+ 'INT' |
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
Oops, something went wrong.