-
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.
Merge pull request #471 from ratmice/clone_parse_param
Relax `%parse-param` bounds from `Copy` to `Clone`
- Loading branch information
Showing
12 changed files
with
174 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Test %parse-param copy | ||
yacckind: Grmtools | ||
grammar: | | ||
%start S | ||
%parse-param p: u64 | ||
%% | ||
S -> u64: | ||
// Previously %parse-param required a `Copy` bounds. | ||
// Since then we relaxed the bounds to require `Clone`. | ||
// This tests backwards compatibility of actions that | ||
// rely on the older copy bounds. | ||
'INT' { (move |_| {})(p); check_copy(p); p + $lexer.span_str($1.unwrap().span()).parse::<u64>().unwrap() } | ||
; | ||
%% | ||
fn check_copy<T: Copy>(_: T){} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "clone_param" | ||
version = "0.1.0" | ||
authors = ["Matt Rice <[email protected]>"] | ||
edition = "2021" | ||
license = "Apache-2.0/MIT" | ||
|
||
[[bin]] | ||
doc = false | ||
name = "clone_param" | ||
|
||
[build-dependencies] | ||
cfgrammar = { path="../../../cfgrammar" } | ||
lrlex = { path="../../../lrlex" } | ||
lrpar = { path="../.." } | ||
|
||
[dependencies] | ||
cfgrammar = { path="../../../cfgrammar" } | ||
lrlex = { path="../../../lrlex" } | ||
lrpar = { path="../.." } |
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,20 @@ | ||
# `clone_param` | ||
|
||
## Description | ||
Example which shows how to use interior mutability with the `%parse-param` directive. | ||
As a parameter the parse function accepts a `Rc<RefCell<u64>>`. | ||
|
||
## Input | ||
For input the parser accepts a positive or negative integer e.g. `-1`, `42`, etc followed | ||
by any sequence of `+`, or `-` characters. Except for the initial `-` on a negative integer, | ||
`+` or `-` are treated as `Increment` and `Decrement` operators. | ||
|
||
## Evaluation | ||
Rather than building an AST, the param is directly mutated by the actions. | ||
As such an input sequence like `-3++-` will evalute to `-2`. | ||
|
||
## Example | ||
``` | ||
>>> -3++- | ||
Evaluated: RefCell { value: -2 } | ||
``` |
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,20 @@ | ||
#![deny(rust_2018_idioms)] | ||
use cfgrammar::yacc::YaccKind; | ||
use lrlex::CTLexerBuilder; | ||
|
||
fn main() { | ||
// Since we're using both lrlex and lrpar, we use lrlex's `lrpar_config` convenience function | ||
// that makes it easy to a) create a lexer and parser and b) link them together. | ||
CTLexerBuilder::new() | ||
.rust_edition(lrlex::RustEdition::Rust2021) | ||
.lrpar_config(|ctp| { | ||
ctp.yacckind(YaccKind::Grmtools) | ||
.rust_edition(lrpar::RustEdition::Rust2021) | ||
.grammar_in_src_dir("param.y") | ||
.unwrap() | ||
}) | ||
.lexer_in_src_dir("param.l") | ||
.unwrap() | ||
.build() | ||
.unwrap(); | ||
} |
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,40 @@ | ||
#![allow(clippy::unnecessary_wraps)] | ||
|
||
use lrlex::lrlex_mod; | ||
use lrpar::lrpar_mod; | ||
use std::io::{self, BufRead, Write}; | ||
use std::{cell::RefCell, rc::Rc}; | ||
|
||
// Using `lrlex_mod!` brings the lexer for `param.l` into scope. By default the module name will be | ||
// `param_l` (i.e. the file name, minus any extensions, with a suffix of `_l`). | ||
lrlex_mod!("param.l"); | ||
// Using `lrpar_mod!` brings the parser for `param.y` into scope. By default the module name will be | ||
// `param_y` (i.e. the file name, minus any extensions, with a suffix of `_y`). | ||
lrpar_mod!("param.y"); | ||
|
||
fn main() { | ||
// Get the `LexerDef` for the `param` language. | ||
let lexerdef = param_l::lexerdef(); | ||
let stdin = io::stdin(); | ||
loop { | ||
print!(">>> "); | ||
io::stdout().flush().ok(); | ||
match stdin.lock().lines().next() { | ||
Some(Ok(ref l)) => { | ||
if l.trim().is_empty() { | ||
continue; | ||
} | ||
// Now we create a lexer with the `lexer` method with which we can lex an input. | ||
let lexer = lexerdef.lexer(l); | ||
let param = Rc::new(RefCell::new(0)); | ||
// Pass the lexer to the parser and lex and parse the input. | ||
let (_opt, errs) = param_y::parse(&lexer, param.clone()); | ||
for e in errs { | ||
println!("{}", e.pp(&lexer, ¶m_y::token_epp)); | ||
} | ||
println!("Evaluated: {:?}", ¶m); | ||
} | ||
_ => break, | ||
} | ||
} | ||
} |
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,5 @@ | ||
%% | ||
(\-?)[0-9]+ "INT" | ||
\- "Decr" | ||
\+ "Incr" | ||
[\n\t\ ] ; |
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,21 @@ | ||
%token Incr Decr | ||
%parse-param val: Rc<RefCell<i64>> | ||
%% | ||
Expr -> () : "INT" Ops { | ||
*val.borrow_mut() += parse_int($lexer.span_str($1.map_err(|_| "<evaluation aborted>").unwrap().span())).unwrap() | ||
}; | ||
Ops -> (): | ||
%empty {} | ||
| Ops Incr { *val.borrow_mut() += 1; } | ||
| Ops Decr { *val.borrow_mut() -= 1; }; | ||
%% | ||
use std::{ rc::Rc, cell::RefCell, error::Error }; | ||
|
||
fn parse_int(s: &str) -> Result<i64, Box<dyn Error>> { | ||
match s.parse::<i64>() { | ||
Ok(val) => Ok(val), | ||
Err(_) => { | ||
Err(Box::from(format!("{} cannot be represented as a i64", s))) | ||
} | ||
} | ||
} |
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