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

changed to Cro

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