Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
parsetest
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
parsetest
Commits
fd104dee
Commit
fd104dee
authored
7 years ago
by
Per
Browse files
Options
Downloads
Patches
Plain Diff
wip
parent
f33a77e2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
README.md
+129
-0
129 additions, 0 deletions
README.md
epanded_ex1.rs
+169
-0
169 additions, 0 deletions
epanded_ex1.rs
examples/ex1.rs
+9
-14
9 additions, 14 deletions
examples/ex1.rs
examples/ex2.rs
+32
-0
32 additions, 0 deletions
examples/ex2.rs
src/lib.rs
+81
-47
81 additions, 47 deletions
src/lib.rs
with
420 additions
and
61 deletions
README.md
0 → 100644
+
129
−
0
View file @
fd104dee
# Writing parsers using `syn` and `nom` parser combinators
## Parsing a LitInt and rejecting invalid range
As a simple example let us start out with
`mylit!(LitInt)`
, where
`LitInt`
should be an intereger literal
`x`
in the range
`10 <= x < 100`
.
Consider the following program (
`ex1.rs`
):
```
rust
#![feature(proc_macro)]
extern
crate
parsetest
;
use
parsetest
::
mylit
;
fn
main
()
{
// should pass
let
_v
=
mylit!
(
99
);
// should be rejected
let
_v
=
mylit!
(
102
);
// should be rejected
let
_v
=
mylit!
(
9
o9
);
// should be rejected
let
_v
=
mylit!
((
99
));
}
```
The expected outcome for
`mylit!`
is:
```
shell
>
Executing task: cargo run
--example
ex1 <
Compiling parsetest v0.1.0
(
file:///home/pln/course/parsetest
)
error: expected literal 10 <
=
x < 100, got 102
--
>
examples/ex1.rs:11:21
|
11 |
let
_v
=
mylit!
(
102
)
;
| ^^^
error: expected literal 10 <
=
x < 100, got 9
--
>
examples/ex1.rs:14:21
|
14 |
let
_v
=
mylit!
(
9o9
)
;
| ^^^
error: invalid suffix
`
o9
`
for
numeric literal
--
>
examples/ex1.rs:14:21
|
14 |
let
_v
=
mylit!
(
9o9
)
;
| ^^^
|
=
help
: the suffix must be one of the integral types
(
`
u32
`
,
`
isize
`
, etc
)
error: proc macro panicked
--
>
examples/ex1.rs:17:14
|
17 |
let
_v
=
mylit!
((
99
))
;
| ^^^^^^^^^^^^
|
=
help
: message: called
`
Result::unwrap
()
`
on an
`
Err
`
value: ParseError
(
None
)
error: Could not compile
`
parsetest
`
.
```
The procedural macro is defined in the
`src/lib.rs`
as follows.
```
rust
#![feature(proc_macro)]
extern
crate
proc_macro
;
#[macro_use]
extern
crate
quote
;
#[macro_use]
extern
crate
syn
;
use
proc_macro
::
TokenStream
;
use
syn
::
spanned
::
Spanned
;
use
syn
::
synom
::
Synom
;
use
syn
::
LitInt
;
use
quote
::
ToTokens
;
use
std
::
convert
::
From
;
/// MyLit
struct
MyLit
{
val
:
LitInt
,
}
impl
Synom
for
MyLit
{
named!
(
parse
->
Self
,
do_parse!
(
val
:
syn!
(
LitInt
)
>>
(
MyLit
{
val
})
));
}
#[proc_macro]
pub
fn
mylit
(
input
:
TokenStream
)
->
TokenStream
{
let
v
:
MyLit
=
syn
::
parse
(
input
)
.unwrap
();
let
value
:
u32
=
v
.val
.value
()
as
u32
;
if
!
(
10
<=
value
&&
value
<
100
)
{
v
.val
.span
()
.unstable
()
.error
(
format!
(
"expected literal 10 <= x < 100, got {}"
,
value
,))
.emit
();
}
From
::
from
(
v
.val
.into_tokens
())
}
```
So
`MyLit`
here is a structure for holding the parsed token.
We implement
`Synom`
for
`MyLit`
, to exactly parse a
`LitInt`
.
The
`named!`
macro turns the argument (a parser combinator) into a function, in this case
a function with the identifier
`parser`
(mathing the
`Synom`
trait
`parser`
).
```
rust
pub
trait
Synom
:
Sized
{
fn
parse
(
input
:
Cursor
)
->
PResult
<
Self
>
;
fn
description
()
->
Option
<&
'static
str
>
{
...
}
}
```
In order for
`syn::parse`
to be able to parse a
`TokenStream`
for
This diff is collapsed.
Click to expand it.
epanded_ex1.rs
0 → 100644
+
169
−
0
View file @
fd104dee
#![feature(prelude_import)]
#![no_std]
#![feature(proc_macro)]
#[prelude_import]
use
std
::
prelude
::
v1
::
*
;
#[macro_use]
extern
crate
std
as
std
;
extern
crate
proc_macro
;
#[macro_use]
extern
crate
quote
;
#[macro_use]
extern
crate
syn
;
use
proc_macro
::
TokenStream
;
use
syn
::
spanned
::
Spanned
;
use
syn
::
synom
::
Synom
;
use
syn
::
LitInt
;
use
quote
::
ToTokens
;
use
std
::
convert
::
From
;
use
std
::
error
::
Error
;
/// MyLit
struct
MyLit
{
val
:
LitInt
,
}
impl
Synom
for
MyLit
{
// /// MyLitP
// struct MyLitP {
// val: LitInt,
// }
// impl Synom for MyLitP {
// named!(parse -> Self, do_parse!(
// val: parens!(call!(LitInt::parse)) >> (MyLitP { val: val.1 })
// ));
// }
// #[proc_macro]
// pub fn mylitp(input: TokenStream) -> TokenStream {
// match syn::parse::<MyLitP>(input) {
// Ok(v) => {
// let value: u32 = v.val.value() as u32;
// if !(10 <= value && value < 100) {
// v.val
// .span()
// .unstable()
// .error(format!(
// "expected literal 10 <= x < 100, got {}",
// value,
// ))
// .emit();
// }
// From::from(v.val.into_tokens())
// }
// Err(err) => {
// let desc = err.description();
// let tokens = quote! {
// compile_error!(#desc)
// };
// return tokens.into();
// }
// }
// }
// // /// MyLits
// // struct MyLits {
// // val: LitInt,
// // valp: LitInt,
// // }
// // impl Synom for MyLits {
// // named!(parse -> Self, do_parse!(
// // val: syn!
// // valp: parens!(call!(LitInt::parse)) >> (MyLitP { val: val.1 })
// // ));
// // }
// // #[proc_macro]
// // pub fn mylitp(input: TokenStream) -> TokenStream {
// // match syn::parse::<MyLitP>(input) {
// // Ok(v) => {
// // let value: u32 = v.val.value() as u32;
// // if !(10 <= value && value < 100) {
// // v.val
// // .span()
// // .unstable()
// // .error(format!(
// // "expected literal 10 <= x < 100, got {}",
// // value,
// // ))
// // .emit();
// // }
// // From::from(v.val.into_tokens())
// // }
// // Err(err) => {
// // //let desc = format!("could not parse {:?}", err);
// // let desc = err.description();
// // // println!(
// // // "here -----------------------------------------desc {:?}",
// // // desc
// // // );
// // let tokens = quote! {
// // compile_error!(#desc)
// // };
// // return tokens.into();
// // }
// // }
// // }
fn
parse
(
i
:
::
buffer
::
Cursor
)
->
::
synom
::
PResult
<
Self
>
{
match
<
LitInt
as
::
synom
::
Synom
>
::
parse
(
i
)
{
::
std
::
result
::
Result
::
Err
(
err
)
=>
::
std
::
result
::
Result
::
Err
(
err
),
::
std
::
result
::
Result
::
Ok
((
o
,
i
))
=>
{
let
val
=
o
;
::
std
::
result
::
Result
::
Ok
(((
MyLit
{
val
}),
i
))
}
}
}
}
#[proc_macro]
pub
fn
mylit
(
input
:
TokenStream
)
->
TokenStream
{
let
v
:
MyLit
=
syn
::
parse
(
input
)
.unwrap
();
let
value
:
u32
=
v
.val
.value
()
as
u32
;
if
!
(
10
<=
value
&&
value
<
100
)
{
v
.val
.span
()
.unstable
()
.error
(::
fmt
::
format
(::
std
::
fmt
::
Arguments
::
new_v1_formatted
(
&
[
"expected literal 10 <= x < 100, got "
],
&
match
(
&
value
,)
{
(
__arg0
,)
=>
[
::
std
::
fmt
::
ArgumentV1
::
new
(
__arg0
,
::
std
::
fmt
::
Display
::
fmt
,
),
],
},
&
[
::
std
::
fmt
::
rt
::
v1
::
Argument
{
position
:
::
std
::
fmt
::
rt
::
v1
::
Position
::
At
(
0usize
),
format
:
::
std
::
fmt
::
rt
::
v1
::
FormatSpec
{
fill
:
' '
,
align
:
::
std
::
fmt
::
rt
::
v1
::
Alignment
::
Unknown
,
flags
:
0u32
,
precision
:
::
std
::
fmt
::
rt
::
v1
::
Count
::
Implied
,
width
:
::
std
::
fmt
::
rt
::
v1
::
Count
::
Implied
,
},
},
],
)))
.emit
();
}
From
::
from
(
v
.val
.into_tokens
())
}
pub
mod
registrar
{
extern
crate
proc_macro
;
#[rustc_derive_registrar]
pub
fn
_registrar
(
_registrar
:
&
mut
proc_macro
::
__internal
::
Registry
)
->
()
{
proc_macro
::
__internal
::
Registry
::
register_bang_proc_macro
(
_registrar
,
"mylit"
,
::
mylit
,
)
}
}
This diff is collapsed.
Click to expand it.
examples/ex1.rs
+
9
−
14
View file @
fd104dee
#![feature(proc_macro)]
#![feature(proc_macro)]
extern
crate
parsetest
;
extern
crate
parsetest
;
use
parsetest
::
{
mylit
,
mylitp
}
;
use
parsetest
::
mylit
;
fn
main
()
{
fn
main
()
{
println!
(
"here"
);
// should pass
let
_v
=
mylit!
(
99
);
//
let v = mylit!(99);
//
should be rejected
// println!("v {}", v
);
let
_v
=
mylit!
(
102
);
//
let v = mylit!(102);
//
should be rejected
// println!("v {}", v
);
let
_v
=
mylit!
(
9
o9
);
let
v
=
mylitp!
((
99
));
// should be rejected
println!
(
"v {}"
,
v
);
let
_v
=
mylit!
((
99
));
let
v
=
mylitp!
(
99
);
println!
(
"v {}"
,
v
);
let
v
=
mylitp!
((
9
o9
));
println!
(
"v {}"
,
v
);
}
}
This diff is collapsed.
Click to expand it.
examples/ex2.rs
0 → 100644
+
32
−
0
View file @
fd104dee
#![feature(proc_macro)]
extern
crate
parsetest
;
use
parsetest
::{
mylit
,
mylitp
};
fn
main
()
{
println!
(
"here"
);
// should pass
let
v
=
mylit!
(
99
);
println!
(
"v {}"
,
v
);
// be rejected
let
v
=
mylit!
(
102
);
println!
(
"v {}"
,
v
);
// should be rejected
let
v
=
mylitp!
((
99
));
println!
(
"v {}"
,
v
);
// should pass
let
v
=
mylitp!
((
99
));
println!
(
"v {}"
,
v
);
// should be rejected
let
v
=
mylitp!
(
99
);
println!
(
"v {}"
,
v
);
// should be rejected
let
v
=
mylitp!
((
9
o9
));
println!
(
"v {}"
,
v
);
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
81
−
47
View file @
fd104dee
...
@@ -7,7 +7,6 @@ extern crate quote;
...
@@ -7,7 +7,6 @@ extern crate quote;
extern
crate
syn
;
extern
crate
syn
;
use
proc_macro
::
TokenStream
;
use
proc_macro
::
TokenStream
;
// use syn::punctuated::Punctuated;
use
syn
::
spanned
::
Spanned
;
use
syn
::
spanned
::
Spanned
;
use
syn
::
synom
::
Synom
;
use
syn
::
synom
::
Synom
;
use
syn
::
LitInt
;
use
syn
::
LitInt
;
...
@@ -23,7 +22,7 @@ struct MyLit {
...
@@ -23,7 +22,7 @@ struct MyLit {
impl
Synom
for
MyLit
{
impl
Synom
for
MyLit
{
named!
(
parse
->
Self
,
do_parse!
(
named!
(
parse
->
Self
,
do_parse!
(
val
:
call
!
(
LitInt
::
parse
)
>>
(
MyLit
{
val
})
val
:
syn
!
(
LitInt
)
>>
(
MyLit
{
val
})
));
));
}
}
...
@@ -41,52 +40,87 @@ pub fn mylit(input: TokenStream) -> TokenStream {
...
@@ -41,52 +40,87 @@ pub fn mylit(input: TokenStream) -> TokenStream {
From
::
from
(
v
.val
.into_tokens
())
From
::
from
(
v
.val
.into_tokens
())
}
}
/// MyLit
//
/// MyLit
P
struct
MyLitP
{
//
struct MyLitP {
val
:
LitInt
,
//
val: LitInt,
}
//
}
impl
Synom
for
MyLitP
{
//
impl Synom for MyLitP {
// named!(parse -> Self, do_parse!(
// named!(parse -> Self, do_parse!(
//
val: call!(LitInt::parse) >> (MyLitP { val })
//
val:
parens!(
call!(LitInt::parse)
)
>> (MyLitP { val
: val.1
})
// ));
// ));
named!
(
parse
->
Self
,
do_parse!
(
// }
val
:
parens!
(
call!
(
LitInt
::
parse
))
>>
(
MyLitP
{
val
:
val
.1
})
));
}
#[proc_macro]
// #[proc_macro]
pub
fn
mylitp
(
input
:
TokenStream
)
->
TokenStream
{
// pub fn mylitp(input: TokenStream) -> TokenStream {
match
syn
::
parse
::
<
MyLitP
>
(
input
)
{
// match syn::parse::<MyLitP>(input) {
Ok
(
v
)
=>
{
// Ok(v) => {
let
value
:
u32
=
v
.val
.value
()
as
u32
;
// let value: u32 = v.val.value() as u32;
if
!
(
10
<=
value
&&
value
<
100
)
{
// if !(10 <= value && value < 100) {
v
.val
// v.val
.span
()
// .span()
.unstable
()
// .unstable()
.error
(
format!
(
// .error(format!(
"expected literal 10 <= x < 100, got {}"
,
// "expected literal 10 <= x < 100, got {}",
value
,
// value,
))
// ))
.emit
();
// .emit();
}
// }
From
::
from
(
v
.val
.into_tokens
())
// From::from(v.val.into_tokens())
}
// }
Err
(
err
)
=>
{
// Err(err) => {
//let desc = format!("could not parse {:?}", err);
// let desc = err.description();
let
desc
=
err
.description
();
// let tokens = quote! {
// println!(
// compile_error!(#desc)
// "here -----------------------------------------desc {:?}",
// };
// desc
// return tokens.into();
// );
// }
let
tokens
=
quote!
{
// }
compile_error!
(
#
desc
)
// }
};
return
tokens
.into
();
// // /// MyLits
}
// // struct MyLits {
}
// // val: LitInt,
}
// // valp: LitInt,
// // }
// // impl Synom for MyLits {
// // named!(parse -> Self, do_parse!(
// // val: syn!
// // valp: parens!(call!(LitInt::parse)) >> (MyLitP { val: val.1 })
// // ));
// // }
// // #[proc_macro]
// // pub fn mylitp(input: TokenStream) -> TokenStream {
// // match syn::parse::<MyLitP>(input) {
// // Ok(v) => {
// // let value: u32 = v.val.value() as u32;
// // if !(10 <= value && value < 100) {
// // v.val
// // .span()
// // .unstable()
// // .error(format!(
// // "expected literal 10 <= x < 100, got {}",
// // value,
// // ))
// // .emit();
// // }
// // From::from(v.val.into_tokens())
// // }
// // Err(err) => {
// // //let desc = format!("could not parse {:?}", err);
// // let desc = err.description();
// // // println!(
// // // "here -----------------------------------------desc {:?}",
// // // desc
// // // );
// // let tokens = quote! {
// // compile_error!(#desc)
// // };
//panic!("here {}", err),
// // return tokens.into();
//let desc = syn::synom::Synom::description(err);
// // }
// // }
// // }
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment