Skip to content

Commit 2356d6b

Browse files
author
Andrew Weiss
committed
initial commit
0 parents  commit 2356d6b

10 files changed

+1165
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

.rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tab_spaces = 2

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "cddl"
3+
version = "0.1.0"
4+
authors = ["Andrew Weiss <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
whoami = "0.4.1"

src/ast.rs

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
use super::token::Token;
2+
use std::fmt;
3+
use std::string::ToString;
4+
5+
pub trait Node {
6+
fn token_literal(&self) -> Option<String>;
7+
}
8+
9+
#[derive(Default, Debug)]
10+
pub struct CDDL {
11+
pub rules: Vec<Rule>,
12+
}
13+
14+
impl Node for CDDL {
15+
fn token_literal(&self) -> Option<String> {
16+
if self.rules.len() > 0 {
17+
return self.rules[0].token_literal();
18+
}
19+
20+
None
21+
}
22+
}
23+
24+
#[derive(Debug)]
25+
pub struct Identifier(pub Token);
26+
27+
impl Node for Identifier {
28+
fn token_literal(&self) -> Option<String> {
29+
Some(format!("{:?}", self.0))
30+
}
31+
}
32+
33+
impl From<String> for Identifier {
34+
fn from(s: String) -> Self {
35+
Identifier(Token::IDENT(s))
36+
}
37+
}
38+
39+
impl ToString for Identifier {
40+
fn to_string(&self) -> String {
41+
format!("{}", self.0.to_string())
42+
}
43+
}
44+
45+
#[derive(Debug)]
46+
pub enum Rule {
47+
Type(TypeRule),
48+
Group(GroupRule),
49+
}
50+
51+
impl Node for Rule {
52+
fn token_literal(&self) -> Option<String> {
53+
match self {
54+
Rule::Type(tr) => tr.token_literal(),
55+
Rule::Group(gr) => gr.token_literal(),
56+
}
57+
}
58+
}
59+
60+
#[derive(Debug)]
61+
pub struct TypeRule {
62+
pub name: Identifier,
63+
pub generic_param: Option<GenericParm>,
64+
pub is_type_choice_alternate: bool,
65+
pub value: Type,
66+
}
67+
68+
impl TypeRule {
69+
pub fn token_literal(&self) -> Option<String> {
70+
self.name.token_literal()
71+
}
72+
}
73+
74+
#[derive(Debug)]
75+
pub struct GroupRule {
76+
pub name: Identifier,
77+
pub generic_para: Option<GenericParm>,
78+
pub is_group_choice_alternate: bool,
79+
pub entry: GroupEntry,
80+
}
81+
82+
impl Node for GroupRule {
83+
fn token_literal(&self) -> Option<String> {
84+
Some("".into())
85+
}
86+
}
87+
88+
#[derive(Default, Debug)]
89+
pub struct GenericParm(pub Vec<Identifier>);
90+
91+
#[derive(Debug)]
92+
pub struct GenericArg(pub Vec<Type1>);
93+
94+
#[derive(Debug)]
95+
pub struct Type(pub Vec<Type1>);
96+
97+
#[derive(Debug)]
98+
pub struct Type1 {
99+
pub type2: Type2,
100+
pub operator: Option<(RangeCtlOp, Type2)>,
101+
}
102+
103+
#[derive(Debug)]
104+
pub enum RangeCtlOp {
105+
RangeOp(bool),
106+
CtlOp(String),
107+
}
108+
109+
#[derive(Debug)]
110+
pub enum Type2 {
111+
Value(Identifier),
112+
Typename((Identifier, Option<GenericArg>)),
113+
Group(Type),
114+
Map(Group),
115+
Array(Group),
116+
Unwrap((Identifier, Option<GenericArg>)),
117+
ChoiceFromInlineGroup(Group),
118+
ChoiceFromGroup((Identifier, Option<GenericArg>)),
119+
TaggedData(String),
120+
TaggedDataMajorType(String),
121+
Any,
122+
}
123+
124+
impl<'a> fmt::Display for Type2 {
125+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126+
match self {
127+
Type2::Typename((tn, _)) => write!(f, "{}", tn.0),
128+
_ => write!(f, ""),
129+
}
130+
}
131+
}
132+
133+
#[derive(Debug)]
134+
pub struct Group(Vec<GroupChoice>);
135+
136+
#[derive(Debug)]
137+
pub struct GroupChoice(Vec<GroupEntry>);
138+
139+
#[derive(Debug)]
140+
pub enum GroupEntry {
141+
MemberKey(MemberKeyEntry),
142+
Groupname(GroupnameEntry),
143+
InlineGroup((Option<Occur>, Group)),
144+
}
145+
146+
#[derive(Debug)]
147+
pub struct MemberKeyEntry {
148+
pub occur: Option<Occur>,
149+
pub member_key: Option<MemberKey>,
150+
pub entry_type: Type,
151+
}
152+
153+
#[derive(Debug)]
154+
pub struct GroupnameEntry {
155+
pub occur: Option<Occur>,
156+
pub name: Identifier,
157+
pub generic_arg: Option<GenericArg>,
158+
}
159+
160+
#[derive(Debug)]
161+
pub enum MemberKey {
162+
// if true, cut is present
163+
Type1((Type1, bool)),
164+
Bareword(Identifier),
165+
Value(String)
166+
}
167+
168+
#[derive(Debug)]
169+
pub enum Occur {
170+
Exact((usize, usize)),
171+
OneOrMore,
172+
Optional,
173+
}

src/bin/repl.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use cddl::repl;
2+
use std::{error::Error, io};
3+
use whoami;
4+
5+
fn main() -> Result<(), Box<Error>> {
6+
let username = whoami::username();
7+
8+
println!("Hello {}! This is the CDDL language!", username);
9+
10+
println!("Feel free to type in commands");
11+
12+
let input = io::stdin();
13+
let output = io::stdout();
14+
repl::start(input.lock(), output.lock())?;
15+
Ok(())
16+
}

0 commit comments

Comments
 (0)