Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rtfm-syntax
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
rtfm-syntax
Commits
b2c59e2a
Commit
b2c59e2a
authored
7 years ago
by
Jorge Aparicio
Browse files
Options
Downloads
Patches
Plain Diff
more typed: use Path / Ty instead of just "Tokens"
parent
2272227e
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/check.rs
+13
-12
13 additions, 12 deletions
src/check.rs
src/lib.rs
+6
-5
6 additions, 5 deletions
src/lib.rs
src/parse.rs
+4
-12
4 additions, 12 deletions
src/parse.rs
src/util.rs
+14
-0
14 additions, 0 deletions
src/util.rs
with
37 additions
and
29 deletions
src/check.rs
+
13
−
12
View file @
b2c59e2a
use
std
::
collections
::
HashMap
;
use
std
::
collections
::
HashMap
;
use
quote
::
Tokens
;
use
syn
::{
Ident
,
Path
};
use
syn
::
Ident
;
use
error
::
*
;
use
error
::
*
;
use
{
Idents
,
Statics
};
use
{
util
,
Idents
,
Statics
};
pub
type
Tasks
=
HashMap
<
Ident
,
Task
>
;
pub
type
Tasks
=
HashMap
<
Ident
,
Task
>
;
pub
struct
App
{
pub
struct
App
{
pub
device
:
Tokens
,
pub
device
:
Path
,
pub
idle
:
Idle
,
pub
idle
:
Idle
,
pub
init
:
Init
,
pub
init
:
Init
,
pub
resources
:
Statics
,
pub
resources
:
Statics
,
...
@@ -18,12 +17,12 @@ pub struct App {
...
@@ -18,12 +17,12 @@ pub struct App {
pub
struct
Idle
{
pub
struct
Idle
{
pub
locals
:
Statics
,
pub
locals
:
Statics
,
pub
path
:
Tokens
,
pub
path
:
Path
,
pub
resources
:
Idents
,
pub
resources
:
Idents
,
}
}
pub
struct
Init
{
pub
struct
Init
{
pub
path
:
Tokens
,
pub
path
:
Path
,
}
}
pub
struct
Task
{
pub
struct
Task
{
...
@@ -74,7 +73,7 @@ fn idle(idle: Option<::Idle>) -> Result<Idle> {
...
@@ -74,7 +73,7 @@ fn idle(idle: Option<::Idle>) -> Result<Idle> {
}
else
{
}
else
{
Idle
{
Idle
{
locals
:
Statics
::
new
(),
locals
:
Statics
::
new
(),
path
:
quote!
(
idle
),
path
:
util
::
mk_path
(
"
idle
"
),
resources
:
Idents
::
new
(),
resources
:
Idents
::
new
(),
}
}
})
})
...
@@ -91,21 +90,23 @@ fn init(init: Option<::Init>) -> Result<Init> {
...
@@ -91,21 +90,23 @@ fn init(init: Option<::Init>) -> Result<Init> {
bail!
(
"empty `init` field. It should be removed."
);
bail!
(
"empty `init` field. It should be removed."
);
}
}
}
else
{
}
else
{
Init
{
path
:
quote!
(
init
)
}
Init
{
path
:
util
::
mk_path
(
"init"
),
}
})
})
}
}
fn
path
(
default
:
&
str
,
path
:
Option
<
Tokens
>
)
->
Result
<
Tokens
>
{
fn
path
(
default
:
&
str
,
path
:
Option
<
Path
>
)
->
Result
<
Path
>
{
Ok
(
if
let
Some
(
path
)
=
path
{
Ok
(
if
let
Some
(
path
)
=
path
{
ensure!
(
ensure!
(
path
.as_str
()
!=
default
,
path
.segments
.len
()
==
1
&&
path
.segments
[
0
]
.ident
.as_ref
()
!=
default
,
"this is the default value. It should be omitted."
"this is the default value. It should be omitted."
);
);
path
path
}
else
{
}
else
{
let
default
=
Ident
::
new
(
default
);
util
::
mk_path
(
default
)
quote!
(
#
default
)
})
})
}
}
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
6
−
5
View file @
b2c59e2a
...
@@ -10,11 +10,12 @@ pub mod check;
...
@@ -10,11 +10,12 @@ pub mod check;
pub
mod
error
;
pub
mod
error
;
mod
parse
;
mod
parse
;
mod
util
;
use
std
::
collections
::{
HashMap
,
HashSet
};
use
std
::
collections
::{
HashMap
,
HashSet
};
use
quote
::
Tokens
;
use
quote
::
Tokens
;
use
syn
::
Ident
;
use
syn
::
{
Ident
,
Path
,
Ty
}
;
use
error
::
*
;
use
error
::
*
;
...
@@ -26,7 +27,7 @@ pub type Tasks = HashMap<Ident, Task>;
...
@@ -26,7 +27,7 @@ pub type Tasks = HashMap<Ident, Task>;
#[derive(Debug)]
#[derive(Debug)]
pub
struct
App
{
pub
struct
App
{
pub
device
:
Tokens
,
pub
device
:
Path
,
pub
idle
:
Option
<
Idle
>
,
pub
idle
:
Option
<
Idle
>
,
pub
init
:
Option
<
Init
>
,
pub
init
:
Option
<
Init
>
,
pub
resources
:
Option
<
Statics
>
,
pub
resources
:
Option
<
Statics
>
,
...
@@ -36,14 +37,14 @@ pub struct App {
...
@@ -36,14 +37,14 @@ pub struct App {
/// `init`
/// `init`
#[derive(Debug)]
#[derive(Debug)]
pub
struct
Init
{
pub
struct
Init
{
pub
path
:
Option
<
Tokens
>
,
pub
path
:
Option
<
Path
>
,
}
}
/// `idle`
/// `idle`
#[derive(Debug)]
#[derive(Debug)]
pub
struct
Idle
{
pub
struct
Idle
{
pub
locals
:
Option
<
Statics
>
,
pub
locals
:
Option
<
Statics
>
,
pub
path
:
Option
<
Tokens
>
,
pub
path
:
Option
<
Path
>
,
pub
resources
:
Option
<
Idents
>
,
pub
resources
:
Option
<
Idents
>
,
}
}
...
@@ -58,7 +59,7 @@ pub struct Task {
...
@@ -58,7 +59,7 @@ pub struct Task {
#[derive(Debug)]
#[derive(Debug)]
pub
struct
Static
{
pub
struct
Static
{
pub
expr
:
Tokens
,
pub
expr
:
Tokens
,
pub
ty
:
T
okens
,
pub
ty
:
T
y
,
}
}
impl
App
{
impl
App
{
...
...
This diff is collapsed.
Click to expand it.
src/parse.rs
+
4
−
12
View file @
b2c59e2a
...
@@ -2,8 +2,7 @@ use std::collections::{HashMap, HashSet};
...
@@ -2,8 +2,7 @@ use std::collections::{HashMap, HashSet};
use
std
::
iter
::
Peekable
;
use
std
::
iter
::
Peekable
;
use
std
::
slice
::
Iter
;
use
std
::
slice
::
Iter
;
use
quote
::
Tokens
;
use
syn
::{
self
,
DelimToken
,
Ident
,
IntTy
,
Lit
,
Path
,
Token
,
TokenTree
};
use
syn
::{
self
,
DelimToken
,
Ident
,
IntTy
,
Lit
,
Token
,
TokenTree
};
use
error
::
*
;
use
error
::
*
;
...
@@ -234,12 +233,6 @@ fn static_(tts: &mut Iter<TokenTree>) -> Result<Static> {
...
@@ -234,12 +233,6 @@ fn static_(tts: &mut Iter<TokenTree>) -> Result<Static> {
if
let
Some
(
tt
)
=
tts
.next
()
{
if
let
Some
(
tt
)
=
tts
.next
()
{
if
tt
==
&
TokenTree
::
Token
(
Token
::
Eq
)
{
if
tt
==
&
TokenTree
::
Token
(
Token
::
Eq
)
{
break
;
break
;
}
else
if
tt
==
&
TokenTree
::
Token
(
Token
::
Semi
)
{
fragments
.push
(
tt
);
bail!
(
"expected a type, found Semicolon: `{}`"
,
quote!
(
#
(
#
fragments
)
*
)
);
}
else
{
}
else
{
fragments
.push
(
tt
);
fragments
.push
(
tt
);
}
}
...
@@ -248,8 +241,7 @@ fn static_(tts: &mut Iter<TokenTree>) -> Result<Static> {
...
@@ -248,8 +241,7 @@ fn static_(tts: &mut Iter<TokenTree>) -> Result<Static> {
}
}
}
}
ensure!
(
!
fragments
.is_empty
(),
"type is missing"
);
let
ty
=
syn
::
parse_type
(
&
format!
(
"{}"
,
quote!
(
#
(
#
fragments
)
*
)))
?
;
let
ty
=
quote!
(
#
(
#
fragments
)
*
);
let
mut
fragments
=
vec!
[];
let
mut
fragments
=
vec!
[];
loop
{
loop
{
...
@@ -306,7 +298,7 @@ fn statics(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Statics> {
...
@@ -306,7 +298,7 @@ fn statics(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Statics> {
})
})
}
}
fn
path
(
tts
:
&
mut
Peekable
<
Iter
<
TokenTree
>>
)
->
Result
<
Tokens
>
{
fn
path
(
tts
:
&
mut
Peekable
<
Iter
<
TokenTree
>>
)
->
Result
<
Path
>
{
let
mut
fragments
=
vec!
[];
let
mut
fragments
=
vec!
[];
loop
{
loop
{
...
@@ -323,7 +315,7 @@ fn path(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Tokens> {
...
@@ -323,7 +315,7 @@ fn path(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Tokens> {
tts
.next
();
tts
.next
();
}
}
Ok
(
quote!
(
#
(
#
fragments
)
*
))
Ok
(
syn
::
parse_path
(
&
format!
(
"{}"
,
quote!
(
#
(
#
fragments
)
*
))
)
?
)
}
}
fn
task
(
tts
:
&
mut
Peekable
<
Iter
<
TokenTree
>>
)
->
Result
<
Task
>
{
fn
task
(
tts
:
&
mut
Peekable
<
Iter
<
TokenTree
>>
)
->
Result
<
Task
>
{
...
...
This diff is collapsed.
Click to expand it.
src/util.rs
0 → 100644
+
14
−
0
View file @
b2c59e2a
use
syn
::{
Ident
,
Path
,
PathParameters
,
PathSegment
};
/// Creates a path with contents `#ident`
pub
fn
mk_path
(
ident
:
&
str
)
->
Path
{
Path
{
global
:
false
,
segments
:
vec!
[
PathSegment
{
ident
:
Ident
::
new
(
ident
),
parameters
:
PathParameters
::
none
(),
},
],
}
}
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