Skip to content
Snippets Groups Projects
Commit a76f6d17 authored by Per Lindgren's avatar Per Lindgren
Browse files

changed to Cro

parent 5f201c68
Branches svd2rust0.12
No related tags found
No related merge requests found
...@@ -175,10 +175,7 @@ fn tasks(tasks: Option<::Tasks>) -> Result<Tasks> { ...@@ -175,10 +175,7 @@ fn tasks(tasks: Option<::Tasks>) -> Result<Tasks> {
enabled: task.enabled, enabled: task.enabled,
path: task.path, path: task.path,
priority: task.priority, priority: task.priority,
resources: ::check::resources( resources: ::check::resources("resources", task.resources)?,
"resources",
task.resources,
)?,
}, },
)) ))
})().chain_err(|| format!("checking task `{}`", name_)) })().chain_err(|| format!("checking task `{}`", name_))
...@@ -191,9 +188,7 @@ fn tasks(tasks: Option<::Tasks>) -> Result<Tasks> { ...@@ -191,9 +188,7 @@ fn tasks(tasks: Option<::Tasks>) -> Result<Tasks> {
/// `crc! { .. }` /// `crc! { .. }`
#[derive(Debug)] #[derive(Debug)]
pub struct Crc { pub struct Cro {
/// `crcs: Crcs`
pub crcs: Option<Crcs>,
/// `crcs: Crcs` /// `crcs: Crcs`
pub ips: ::Ips, pub ips: ::Ips,
/// `crcs: Crcs` /// `crcs: Crcs`
...@@ -201,11 +196,10 @@ pub struct Crc { ...@@ -201,11 +196,10 @@ pub struct Crc {
} }
/// Checks the syntax of the parsed `app!` macro /// Checks the syntax of the parsed `app!` macro
pub fn crc(crc: ::Crc) -> Result<Crc> { pub fn crc(cro: ::Cro) -> Result<Cro> {
Ok(Crc { Ok(Cro {
crcs: crc.crcs, ips: ::check::ips(cro.ips).chain_err(|| "checking `ips`")?,
ips: ::check::ips(crc.ips).chain_err(|| "checking `ips`")?, ops: ::check::ops(cro.ops).chain_err(|| "checking `ops`")?,
ops: ::check::ops(crc.ops).chain_err(|| "checking `ops`")?,
}) })
} }
......
...@@ -120,20 +120,18 @@ pub type Ips = HashMap<Ident, (Ty, Path)>; ...@@ -120,20 +120,18 @@ pub type Ips = HashMap<Ident, (Ty, Path)>;
/// `$($Ident: $Ty = $Expr < $Path,)*` /// `$($Ident: $Ty = $Expr < $Path,)*`
pub type Ops = HashMap<Ident, (Ty, Expr, Path)>; pub type Ops = HashMap<Ident, (Ty, Expr, Path)>;
/// `crc! { .. }` /// `cro! { .. }`
#[derive(Debug)] #[derive(Debug)]
pub struct Crc { pub struct Cro {
/// Inner Crcs (can be None)
pub crcs: Option<Crcs>,
/// Input ports (should not be None, given we have interrupts at top level) /// Input ports (should not be None, given we have interrupts at top level)
pub ips: Option<Ips>, pub ips: Option<Ips>,
/// Output ports (should not be None, given we treat peripherals as Crcs) /// Output ports (should not be None, given we treat peripherals as Crcs)
pub ops: Option<Ops>, pub ops: Option<Ops>,
} }
impl Crc { impl Cro {
/// Parses the contents of the `sys! { .. }` macro /// Parses the contents of the `sys! { .. }` macro
pub fn parse(input: &str) -> Result<Self> { pub fn parse(input: &str) -> Result<Self> {
parse::crc(input) parse::cro(input)
} }
} }
...@@ -2,13 +2,11 @@ use std::collections::{HashMap, HashSet}; ...@@ -2,13 +2,11 @@ use std::collections::{HashMap, HashSet};
use std::iter::Peekable; use std::iter::Peekable;
use std::slice::Iter; use std::slice::Iter;
use syn::{self, DelimToken, Ident, IntTy, Lit, Path, Token, BinOpToken, use syn::{self, DelimToken, Ident, IntTy, Lit, Path, Token, BinOpToken, TokenTree, Ty};
TokenTree, Ty};
use error::*; use error::*;
use {Crc, Crcs, Ips, Ops, App, Idle, Init, Resources, Static, Statics, Task, use {Cro, Ips, Ops, App, Idle, Init, Resources, Static, Statics, Task, Tasks, Expr};
Tasks, Expr};
/// Parses the contents of `app! { $App }` /// Parses the contents of `app! { $App }`
pub fn app(input: &str) -> Result<App> { pub fn app(input: &str) -> Result<App> {
...@@ -25,8 +23,7 @@ pub fn app(input: &str) -> Result<App> { ...@@ -25,8 +23,7 @@ pub fn app(input: &str) -> Result<App> {
"device" => { "device" => {
ensure!(device.is_none(), "duplicated `device` field"); ensure!(device.is_none(), "duplicated `device` field");
device = device = Some(::parse::path(tts).chain_err(|| "parsing `device`")?);
Some(::parse::path(tts).chain_err(|| "parsing `device`")?);
} }
"idle" => { "idle" => {
ensure!(idle.is_none(), "duplicated `idle` field"); ensure!(idle.is_none(), "duplicated `idle` field");
...@@ -41,15 +38,12 @@ pub fn app(input: &str) -> Result<App> { ...@@ -41,15 +38,12 @@ pub fn app(input: &str) -> Result<App> {
"resources" => { "resources" => {
ensure!(resources.is_none(), "duplicated `resources` field"); ensure!(resources.is_none(), "duplicated `resources` field");
resources = Some(::parse::statics(tts).chain_err( resources = Some(::parse::statics(tts).chain_err(|| "parsing `resources`")?);
|| "parsing `resources`",
)?);
} }
"tasks" => { "tasks" => {
ensure!(tasks.is_none(), "duplicated `tasks` field"); ensure!(tasks.is_none(), "duplicated `tasks` field");
tasks = tasks = Some(::parse::tasks(tts).chain_err(|| "parsing `tasks`")?);
Some(::parse::tasks(tts).chain_err(|| "parsing `tasks`")?);
} }
_ => bail!("unknown field: `{}`", key), _ => bail!("unknown field: `{}`", key),
} }
...@@ -77,11 +71,7 @@ fn bool(tt: Option<&TokenTree>) -> Result<bool> { ...@@ -77,11 +71,7 @@ fn bool(tt: Option<&TokenTree>) -> Result<bool> {
} }
/// Parses a delimited token tree /// Parses a delimited token tree
fn delimited<R, F>( fn delimited<R, F>(tts: &mut Peekable<Iter<TokenTree>>, delimiter: DelimToken, f: F) -> Result<R>
tts: &mut Peekable<Iter<TokenTree>>,
delimiter: DelimToken,
f: F,
) -> Result<R>
where where
F: FnOnce(&[TokenTree]) -> Result<R>, F: FnOnce(&[TokenTree]) -> Result<R>,
{ {
...@@ -147,14 +137,9 @@ fn idle(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Idle> { ...@@ -147,14 +137,9 @@ fn idle(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Idle> {
path = Some(::parse::path(tts)?); path = Some(::parse::path(tts)?);
} }
"resources" => { "resources" => {
ensure!( ensure!(resources.is_none(), "duplicated `resources` field");
resources.is_none(),
"duplicated `resources` field"
);
resources = Some(::parse::resources(tts).chain_err( resources = Some(::parse::resources(tts).chain_err(|| "parsing `resources`")?);
|| "parsing `resources`",
)?);
} }
_ => bail!("unknown field: `{}`", key), _ => bail!("unknown field: `{}`", key),
} }
...@@ -203,10 +188,7 @@ fn resources(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Resources> { ...@@ -203,10 +188,7 @@ fn resources(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Resources> {
let mut tts = tts.iter().peekable(); let mut tts = tts.iter().peekable();
while let Some(tt) = tts.next() { while let Some(tt) = tts.next() {
if let &TokenTree::Token(Token::Ident(ref ident)) = tt { if let &TokenTree::Token(Token::Ident(ref ident)) = tt {
ensure!( ensure!(!idents.contains(ident), "ident {} listed more than once");
!idents.contains(ident),
"ident {} listed more than once"
);
idents.insert(ident.clone()); idents.insert(ident.clone());
...@@ -280,16 +262,14 @@ fn statics(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Statics> { ...@@ -280,16 +262,14 @@ fn statics(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Statics> {
let mut tts = tts.iter(); let mut tts = tts.iter();
while let Some(tt) = tts.next() { while let Some(tt) = tts.next() {
match tt { match tt {
&TokenTree::Token(Token::Ident(ref id)) &TokenTree::Token(Token::Ident(ref id)) if id.as_ref() == "static" => {}
if id.as_ref() == "static" => {}
_ => { _ => {
bail!("expected keyword `static`, found {:?}", tt); bail!("expected keyword `static`, found {:?}", tt);
} }
} }
let tt = tts.next(); let tt = tts.next();
let ident = let ident = if let Some(&TokenTree::Token(Token::Ident(ref id))) = tt {
if let Some(&TokenTree::Token(Token::Ident(ref id))) = tt {
id id
} else { } else {
bail!("expected Ident, found {:?}", tt); bail!("expected Ident, found {:?}", tt);
...@@ -354,33 +334,22 @@ fn task(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Task> { ...@@ -354,33 +334,22 @@ fn task(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Task> {
"enabled" => { "enabled" => {
ensure!(enabled.is_none(), "duplicated `enabled` field"); ensure!(enabled.is_none(), "duplicated `enabled` field");
enabled = Some(::parse::bool(tts.next()).chain_err( enabled = Some(::parse::bool(tts.next()).chain_err(|| "parsing `enabled`")?);
|| "parsing `enabled`",
)?);
} }
"path" => { "path" => {
ensure!(path.is_none(), "duplicated `path` field"); ensure!(path.is_none(), "duplicated `path` field");
path = Some( path = Some(::parse::path(tts).chain_err(|| "parsing `path`")?);
::parse::path(tts).chain_err(|| "parsing `path`")?,
);
} }
"priority" => { "priority" => {
ensure!(priority.is_none(), "duplicated `priority` field"); ensure!(priority.is_none(), "duplicated `priority` field");
priority = Some(::parse::u8(tts.next()).chain_err( priority = Some(::parse::u8(tts.next()).chain_err(|| "parsing `priority`")?);
|| "parsing `priority`",
)?);
} }
"resources" => { "resources" => {
ensure!( ensure!(resources.is_none(), "duplicated `resources` field");
resources.is_none(),
"duplicated `resources` field"
);
resources = Some(::parse::resources(tts).chain_err( resources = Some(::parse::resources(tts).chain_err(|| "parsing `resources`")?);
|| "parsing `resources`",
)?);
} }
_ => bail!("unknown field: `{}`", key), _ => bail!("unknown field: `{}`", key),
} }
...@@ -426,12 +395,7 @@ fn tasks(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Tasks> { ...@@ -426,12 +395,7 @@ fn tasks(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Tasks> {
/// Parses an integer with type `u8` /// Parses an integer with type `u8`
fn u8(tt: Option<&TokenTree>) -> Result<u8> { fn u8(tt: Option<&TokenTree>) -> Result<u8> {
if let Some( if let Some(&TokenTree::Token(Token::Literal(Lit::Int(priority, IntTy::Unsuffixed)))) = tt {
&TokenTree::Token(
Token::Literal(Lit::Int(priority, IntTy::Unsuffixed)),
),
) = tt
{
ensure!(priority < 256, "{} is out of the `u8` range", priority); ensure!(priority < 256, "{} is out of the `u8` range", priority);
Ok(priority as u8) Ok(priority as u8)
...@@ -441,20 +405,15 @@ fn u8(tt: Option<&TokenTree>) -> Result<u8> { ...@@ -441,20 +405,15 @@ fn u8(tt: Option<&TokenTree>) -> Result<u8> {
} }
/// Parses the contents of `crc! { $Crc }` /// Parses the contents of `crc! { $Crc }`
pub fn crc(input: &str) -> Result<Crc> { pub fn cro(input: &str) -> Result<Cro> {
let tts = syn::parse_token_trees(input)?; let tts = syn::parse_token_trees(input)?;
let mut crcs = None;
let mut ips = None; let mut ips = None;
let mut ops = None; let mut ops = None;
fields(&tts, |key, tts| { fields(&tts, |key, tts| {
match key.as_ref() { match key.as_ref() {
"crcs" => {
ensure!(crcs.is_none(), "duplicated `crcs` field");
crcs = Some(::parse::crcs(tts).chain_err(|| "parsing `crcs`")?);
}
"ips" => { "ips" => {
ensure!(ips.is_none(), "duplicated `ips` field"); ensure!(ips.is_none(), "duplicated `ips` field");
...@@ -474,11 +433,11 @@ pub fn crc(input: &str) -> Result<Crc> { ...@@ -474,11 +433,11 @@ pub fn crc(input: &str) -> Result<Crc> {
Ok(()) Ok(())
})?; })?;
Ok(Crc { crcs, ips, ops }) Ok(Cro { ips, ops })
} }
/*
/// Parses `$($Ident: $Path,)*` /// Parses `$($Ident: $Path,)*`
fn crcs(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Crcs> { fn crcs(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Crcs> {
::parse::delimited(tts, DelimToken::Brace, |tts| { ::parse::delimited(tts, DelimToken::Brace, |tts| {
...@@ -511,6 +470,7 @@ fn crcs(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Crcs> { ...@@ -511,6 +470,7 @@ fn crcs(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Crcs> {
}) })
} }
*/
/// Parses `$($Ident: $Ty > $Path,)*` /// Parses `$($Ident: $Ty > $Path,)*`
fn ips(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ips> { fn ips(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ips> {
...@@ -536,8 +496,7 @@ fn ips(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ips> { ...@@ -536,8 +496,7 @@ fn ips(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ips> {
bail!("expected Colon, found {:?}", tt); bail!("expected Colon, found {:?}", tt);
} }
let ty = let ty = ::parse::ty(&TokenTree::Token(Token::Gt), &mut tts)
::parse::ty(&TokenTree::Token(Token::Gt), &mut tts)
.chain_err(|| format!("parsing `type` for {:?}", ident))?; .chain_err(|| format!("parsing `type` for {:?}", ident))?;
let path = ::parse::path(&mut tts).chain_err(|| "parsing `path`")?; let path = ::parse::path(&mut tts).chain_err(|| "parsing `path`")?;
...@@ -574,13 +533,11 @@ fn ops(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ops> { ...@@ -574,13 +533,11 @@ fn ops(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ops> {
bail!("expected Colon, found {:?}", tt); bail!("expected Colon, found {:?}", tt);
} }
let ty = let ty = ::parse::ty(&TokenTree::Token(Token::Eq), &mut tts)
::parse::ty(&TokenTree::Token(Token::Eq), &mut tts)
.chain_err(|| format!("parsing `type` for {:?}", ident))?; .chain_err(|| format!("parsing `type` for {:?}", ident))?;
let expr = let expr = ::parse::expr(&TokenTree::Token(Token::Lt), &mut tts)
::parse::expr(&TokenTree::Token(Token::Lt), &mut tts)
.chain_err(|| format!("parsing `type` for {:?}", ident))?; .chain_err(|| format!("parsing `type` for {:?}", ident))?;
...@@ -616,10 +573,7 @@ fn ty(token: &TokenTree, tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ty> { ...@@ -616,10 +573,7 @@ fn ty(token: &TokenTree, tts: &mut Peekable<Iter<TokenTree>>) -> Result<Ty> {
} }
/// Parses `$Expr ` /// Parses `$Expr `
fn expr( fn expr(token: &TokenTree, tts: &mut Peekable<Iter<TokenTree>>) -> Result<::Expr> {
token: &TokenTree,
tts: &mut Peekable<Iter<TokenTree>>,
) -> Result<::Expr> {
let mut fragments = vec![]; let mut fragments = vec![];
loop { loop {
if let Some(tt) = tts.next() { if let Some(tt) = tts.next() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment