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
Aron Widforss
D7050E
Commits
cfb43069
Verified
Commit
cfb43069
authored
5 years ago
by
Aron Widforss
Browse files
Options
Downloads
Patches
Plain Diff
Adds custom Error type to main_span_expr
parent
bca462aa
Branches
master
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#158
failed
5 years ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/main_span_expr.rs
+60
-18
60 additions, 18 deletions
examples/main_span_expr.rs
with
60 additions
and
18 deletions
examples/main_span_expr.rs
+
60
−
18
View file @
cfb43069
...
...
@@ -6,13 +6,34 @@ use nom::{
character
::
complete
::{
digit1
,
multispace0
},
combinator
::
map
,
sequence
::{
preceded
,
tuple
},
IResult
,
Err
,
error
,
};
use
nom_locate
::
LocatedSpan
;
type
Span
<
'a
>
=
LocatedSpan
<&
'a
str
>
;
#[derive(Debug)]
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
append
(
_
:
Span
<
'a
>
,
_
:
error
::
ErrorKind
,
other
:
Self
)
->
Self
{
other
}
}
#[derive(Debug)]
enum
ErrorKind
{
Parse
,
Nom
(
error
::
ErrorKind
)
}
#[derive(Debug,
PartialEq)]
pub
enum
Op
{
Add
,
...
...
@@ -36,13 +57,20 @@ pub enum Expr<'a> {
type
SpanExpr
<
'a
>
=
(
Span
<
'a
>
,
Expr
<
'a
>
);
pub
fn
parse_i32
(
i
:
Span
)
->
IResult
<
Span
,
SpanExpr
>
{
map
(
digit1
,
|
digit_str
:
Span
|
{
(
digit_str
,
Expr
::
Num
(
digit_str
.fragment.parse
::
<
i32
>
()
.unwrap
()),
)
})(
i
)
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
,
)))
}
}
fn
parse_expr
(
i
:
Span
)
->
IResult
<
Span
,
SpanExpr
>
{
...
...
@@ -81,16 +109,30 @@ fn dump_expr(se: &SpanExpr) -> String {
}
fn
main
()
{
let
(
_
,
(
s
,
e
))
=
parse_expr_ms
(
Span
::
new
(
"
\n
1+2 -
\n
3"
))
.unwrap
();
println!
(
"span for the whole,expression: {:?},
\n
line: {:?},
\n
column: {:?}"
,
s
,
s
.line
,
s
.get_column
()
);
println!
(
"raw e: {:?}"
,
&
e
);
println!
(
"pretty e: {}"
,
dump_expr
(
&
(
s
,
e
)));
let
i
=
"
\n
1+2+10000-
\n
3"
;
match
parse_expr_ms
(
Span
::
new
(
i
))
{
Ok
((
_
,
(
s
,
e
)))
=>
{
println!
(
"span for expression: {:?},
\n
line: {:?},
\n
column: {:?}"
,
s
,
s
.line
,
s
.get_column
()
);
println!
(
"raw e: {:?}"
,
&
e
);
println!
(
"pretty e: {}"
,
dump_expr
(
&
(
s
,
e
)));
},
Err
(
Err
::
Failure
(
Error
(
_
,
Some
(
s
),
err
)))
=>
{
println!
(
"{:?} error at:
\n\t
Line: {:?}
\n\t
Offset: {:?}
\n\t
Value: {:?}"
,
err
,
s
.line
,
s
.get_column
(),
s
.fragment
,
);
println!
(
"raw s: {:?}"
,
&
s
);
}
Err
(
err
)
=>
Err
(
err
)
.unwrap
(),
}
}
// In this example, we have a `parse_expr_ms` is the "top" level parser.
...
...
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