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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
parsetest
Commits
a1cadd00
Commit
a1cadd00
authored
7 years ago
by
Per
Browse files
Options
Downloads
Patches
Plain Diff
error handling
parent
c63545e5
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/ex1.rs
+10
-4
10 additions, 4 deletions
examples/ex1.rs
src/lib.rs
+50
-2
50 additions, 2 deletions
src/lib.rs
with
60 additions
and
6 deletions
examples/ex1.rs
+
10
−
4
View file @
a1cadd00
#![feature(proc_macro)]
#![feature(proc_macro)]
extern
crate
parsetest
;
extern
crate
parsetest
;
use
parsetest
::
mylit
;
use
parsetest
::
{
mylit
,
mylitp
}
;
fn
main
()
{
fn
main
()
{
println!
(
"here"
);
println!
(
"here"
);
let
v
=
mylit!
(
99
);
// let v = mylit!(99);
println!
(
"v {}"
,
v
);
// println!("v {}", v);
// let v = mylit!(102);
// println!("v {}", v);
let
v
=
mylit!
(
102
);
let
v
=
mylit
p
!
(
(
99
)
);
println!
(
"v {}"
,
v
);
println!
(
"v {}"
,
v
);
// let v = mylitp!(9o9);
// println!("v {}", v);
}
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
50
−
2
View file @
a1cadd00
...
@@ -7,13 +7,14 @@ extern crate quote;
...
@@ -7,13 +7,14 @@ extern crate quote;
extern
crate
syn
;
extern
crate
syn
;
use
proc_macro
::
TokenStream
;
use
proc_macro
::
TokenStream
;
use
syn
::
punctuated
::
Punctuated
;
//
use syn::punctuated::Punctuated;
use
syn
::
spanned
::
Spanned
;
use
syn
::
spanned
::
Spanned
;
use
syn
::
synom
::
Synom
;
use
syn
::
synom
::
Synom
;
use
syn
::
{
Expr
,
ExprArray
,
Ident
,
LitInt
}
;
use
syn
::
LitInt
;
use
quote
::
ToTokens
;
use
quote
::
ToTokens
;
use
std
::
convert
::
From
;
use
std
::
convert
::
From
;
use
std
::
error
::
Error
;
/// MyLit
/// MyLit
struct
MyLit
{
struct
MyLit
{
...
@@ -39,3 +40,50 @@ pub fn mylit(input: TokenStream) -> TokenStream {
...
@@ -39,3 +40,50 @@ pub fn mylit(input: TokenStream) -> TokenStream {
}
}
From
::
from
(
v
.val
.into_tokens
())
From
::
from
(
v
.val
.into_tokens
())
}
}
/// MyLit
struct
MyLitP
{
val
:
LitInt
,
}
impl
Synom
for
MyLitP
{
named!
(
parse
->
Self
,
do_parse!
(
val
:
call!
(
LitInt
::
parse
)
>>
(
MyLitP
{
val
})
));
}
#[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
();
}
}
}
//panic!("here {}", err),
//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