Skip to content
Snippets Groups Projects
Commit b2c59e2a authored by Jorge Aparicio's avatar Jorge Aparicio
Browse files

more typed: use Path / Ty instead of just "Tokens"

parent 2272227e
No related branches found
No related tags found
No related merge requests found
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)
}) })
} }
......
...@@ -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: Tokens, pub ty: Ty,
} }
impl App { impl App {
......
...@@ -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> {
......
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(),
},
],
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment