Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D7050E
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
D7050E
Commits
ae504bc3
Commit
ae504bc3
authored
5 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
Now stores the parse error from the std lib.
parent
c9df980e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.vscode/tasks.json
+12
-0
12 additions, 0 deletions
.vscode/tasks.json
examples/main_span_expr_custom_err.rs
+14
-22
14 additions, 22 deletions
examples/main_span_expr_custom_err.rs
with
26 additions
and
22 deletions
.vscode/tasks.json
+
12
−
0
View file @
ae504bc3
...
...
@@ -14,6 +14,18 @@
"kind"
:
"build"
,
"isDefault"
:
true
}
},
{
"type"
:
"shell"
,
"label"
:
"cargo check --example main_span_expr_custom_err"
,
"command"
:
"cargo check --example main_span_expr_custom_err"
,
"problemMatcher"
:
[
"$rustc"
],
"group"
:
{
"kind"
:
"build"
,
"isDefault"
:
true
}
}
]
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
examples/main_span_expr_custom_err.rs
+
14
−
22
View file @
ae504bc3
...
...
@@ -5,9 +5,9 @@ use nom::{
bytes
::
complete
::
tag
,
character
::
complete
::{
digit1
,
multispace0
},
combinator
::
map
,
error
,
sequence
::{
preceded
,
tuple
},
Err
,
error
,
};
use
nom_locate
::
LocatedSpan
;
...
...
@@ -19,19 +19,19 @@ pub struct Error<'a>(Span<'a>, Option<Span<'a>>, ErrorKind);
type
IResult
<
'a
,
I
,
O
,
E
=
Error
<
'a
>>
=
Result
<
(
I
,
O
),
Err
<
E
>>
;
impl
<
'a
>
error
::
ParseError
<
Span
<
'a
>>
for
Error
<
'a
>
{
fn
from_error_kind
(
input
:
Span
<
'a
>
,
kind
:
error
::
ErrorKind
)
->
Self
{
Error
(
input
,
None
,
ErrorKind
::
Nom
(
kind
))
}
fn
from_error_kind
(
input
:
Span
<
'a
>
,
kind
:
error
::
ErrorKind
)
->
Self
{
Error
(
input
,
None
,
ErrorKind
::
Nom
(
kind
))
}
fn
append
(
_
:
Span
<
'a
>
,
_
:
error
::
ErrorKind
,
other
:
Self
)
->
Self
{
other
}
fn
append
(
_
:
Span
<
'a
>
,
_
:
error
::
ErrorKind
,
other
:
Self
)
->
Self
{
other
}
}
#[derive(Debug)]
enum
ErrorKind
{
Parse
,
Nom
(
error
::
ErrorKind
)
Parse
IntError
(
std
::
num
::
ParseIntError
)
,
Nom
(
error
::
ErrorKind
)
,
}
#[derive(Debug,
PartialEq)]
...
...
@@ -59,17 +59,9 @@ type SpanExpr<'a> = (Span<'a>, Expr<'a>);
pub
fn
parse_i32
<
'a
>
(
i
:
Span
<
'a
>
)
->
IResult
<
Span
<
'a
>
,
SpanExpr
>
{
let
(
i
,
digits
)
=
digit1
(
i
)
?
;
if
let
Ok
(
int
)
=
digits
.fragment
.parse
()
{
Ok
((
i
,
(
digits
,
Expr
::
Num
(
int
)),
))
}
else
{
Err
(
Err
::
Failure
(
Error
(
i
,
Some
(
digits
),
ErrorKind
::
Parse
,
)))
match
digits
.fragment
.parse
()
{
Ok
(
int
)
=>
Ok
((
i
,
(
digits
,
Expr
::
Num
(
int
)))),
Err
(
e
)
=>
Err
(
Err
::
Failure
(
Error
(
i
,
Some
(
digits
),
ErrorKind
::
ParseIntError
(
e
)))),
}
}
...
...
@@ -111,7 +103,7 @@ fn dump_expr(se: &SpanExpr) -> String {
fn
main
()
{
let
i
=
"
\n
1+2+10000-
\n
3"
;
// uncomment below for an error example
//
let i = "\n 1+200000000000000000+a10000- \n3";
let
i
=
"
\n
1+200000000000000000+a10000-
\n
3"
;
let
pe
=
parse_expr_ms
(
Span
::
new
(
i
));
println!
(
"pe: {:?}
\n
"
,
pe
);
match
pe
{
...
...
@@ -124,7 +116,7 @@ fn main() {
);
println!
(
"raw e: {:?}
\n
"
,
&
e
);
println!
(
"pretty e: {}
\n
"
,
dump_expr
(
&
(
s
,
e
)));
}
,
}
Err
(
Err
::
Failure
(
Error
(
_
,
Some
(
s
),
err
)))
=>
{
println!
(
"{:?} error at:
\n\t
line: {:?}
\n\t
column: {:?}
\n\t
Value: {:?}
\n
"
,
...
...
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