Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CDDL grammar correction for RFC8610 #61

Merged
merged 43 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
37643c2
feat: example files
apskhem Jan 12, 2024
217c16f
refactor: rename test files
apskhem Jan 15, 2024
40bda27
refactor: rename from rfc9615 to rfc9165
apskhem Jan 15, 2024
45dfa8f
feat: error display for testing multiple files
apskhem Jan 15, 2024
19aa070
feat: sort file reading
apskhem Jan 15, 2024
b94d969
fix: whitespace problem for ',', braces, and //
apskhem Jan 16, 2024
b0607d3
feat: group elements initial unit test
apskhem Jan 16, 2024
4267afd
fix: comments
apskhem Jan 16, 2024
10884a6
fix: grammar correction for comments
apskhem Jan 16, 2024
f2c9e22
fix: grammar correcttion according to abnf
apskhem Jan 17, 2024
5dd9c20
fix: remove genericarg from typename and groupname
apskhem Jan 17, 2024
1c0bf32
fix: unit tests
apskhem Jan 17, 2024
1e7d1ce
feat: initial group_elements test cases
apskhem Jan 17, 2024
8e1cc71
fix: put atomic test back
apskhem Jan 17, 2024
7c9fa06
revert: deleted grammar and tests
apskhem Jan 18, 2024
f0cf1e6
fix: correct test cases
apskhem Jan 19, 2024
3c2bbd3
feat: type decl tests
apskhem Jan 22, 2024
0682361
feat: type decl tests
apskhem Jan 22, 2024
a2d738c
feat: setup type decl test
apskhem Jan 22, 2024
a3410f6
feat: type1 test cases
apskhem Jan 22, 2024
68e61b4
feat: composition testing
apskhem Jan 22, 2024
0c5942a
feat: rules test cases
apskhem Jan 22, 2024
0754589
feat: rules test cases
apskhem Jan 23, 2024
58ec3ac
feat: all examples from rfc8610
apskhem Jan 23, 2024
e72029e
feat: add rule level tests
apskhem Jan 24, 2024
4cc1736
refactor: use general passes and fails function in unit test
apskhem Jan 24, 2024
e1bf0ba
fix: error msg for cddl
apskhem Jan 24, 2024
d391c81
fix: cddl test file name reading
apskhem Jan 24, 2024
6da42d3
fix: cspell
apskhem Jan 24, 2024
dd7f8a4
chore: lintfix
apskhem Jan 24, 2024
07eed72
refactor: cddl filter fn
apskhem Jan 24, 2024
edf532e
refactor: reset
apskhem Jan 24, 2024
4a77203
fix: pub(crate) for unit tests
apskhem Jan 25, 2024
321aaaa
chore: fmtfix
apskhem Jan 25, 2024
3689b59
refactor: don't dry util functions
apskhem Jan 25, 2024
0dca0dc
fix: disable lint warning
apskhem Jan 25, 2024
dd11923
chore: fmtfix
apskhem Jan 25, 2024
05f863b
refactor: change common path
apskhem Jan 25, 2024
29bef0e
feat: common
apskhem Jan 25, 2024
188cb15
refactor: remove tmp vars
apskhem Jan 25, 2024
5140506
feat: common consts
apskhem Jan 26, 2024
353fa21
fix: add tmp allow dead code
apskhem Jan 26, 2024
ac3de3a
fix: cspell
apskhem Jan 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// Test Expressions ONLY TO Be USED by Unit Tests.
// Extends `cddl.pest` with rules needed to properly check sub-rules.

// cspell: words intfloat hexfloat
// cspell: words intfloat hexfloat groupname assignt
// cspell: words assigng genericparm genericarg rangeop ctlop
// cspell: words grpchoice grpent memberkey bareword optcom

/// Test Expression for the `rule` Rule.
rule_TEST = ${ SOI ~ rule ~ EOI }
Expand Down
4 changes: 2 additions & 2 deletions hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! CDDL Grammar adapted from RFC8610 Appendix B
//! https://www.rfc-editor.org/rfc/rfc8610#appendix-B

// cspell: words assignt groupname grpent genericparm assigng
// cspell: words assignt groupname grpent genericparm assigng optcom
// cspell: words genericarg rangeop ctlop grpchoice memberkey bareword hexfloat intfloat
// cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable
// cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable
apskhem marked this conversation as resolved.
Show resolved Hide resolved

cddl = ${
SOI
Expand Down
6 changes: 2 additions & 4 deletions hermes/crates/cbork/cddl-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ pub struct CDDLError(CDDLErrorType);
/// let result = parse_cddl(&mut input, &Extension::CDDLParser);
/// assert!(result.is_ok());
/// ```
pub fn parse_cddl<'a>(
input: &'a mut String, extension: &Extension,
) -> Result<AST<'a>, CDDLError> {
pub fn parse_cddl<'a>(input: &'a mut String, extension: &Extension) -> Result<AST<'a>, CDDLError> {
input.push_str("\n\n");
input.push_str(POSTLUDE);

Expand All @@ -138,7 +136,7 @@ pub fn parse_cddl<'a>(
},
};

result.map_err( CDDLError::from)
result.map_err(CDDLError::from)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// cspell: words hexpair rstuvw abcdefghijklmnopqrstuvwyz rstuvw
// cspell: words hexpair rstuvw abcdefghijklmnopqrstuvwyz rstuvw Xhhb Bhcm

use cddl_parser::{
self,
Expand Down
28 changes: 20 additions & 8 deletions hermes/crates/cbork/cddl-parser/tests/cddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ fn parse_cddl_files() {

file_paths.sort();

let valid_file_paths = file_paths
.iter()
.filter(|p| matches!(p.file_name().and_then(OsStr::to_str).map(|p| p.starts_with("valid")), Some(true)));
let invalid_file_paths = file_paths
.iter()
.filter(|p| matches!(p.file_name().and_then(OsStr::to_str).map(|p| p.starts_with("invalid")), Some(true)));
let valid_file_paths = file_paths.iter().filter(|p| {
matches!(
p.file_name()
.and_then(OsStr::to_str)
.map(|p| p.starts_with("valid")),
Some(true)
)
});
let invalid_file_paths = file_paths.iter().filter(|p| {
matches!(
p.file_name()
.and_then(OsStr::to_str)
.map(|p| p.starts_with("invalid")),
Some(true)
)
});

// test for valid files
let mut err_messages = vec![];
for file_path in valid_file_paths {
let mut content = fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path));
let mut content =
fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path));

if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) {
apskhem marked this conversation as resolved.
Show resolved Hide resolved
err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1));
Expand All @@ -32,7 +43,8 @@ fn parse_cddl_files() {

// test for invalid files
for file_path in invalid_file_paths {
let mut content = fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path));
let mut content =
fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path));

let result = parse_cddl(&mut content, &Extension::RFC8610Parser);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
; cspell: words tstr

sample = {
tstr => tstr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; cspell: words tstr

sample = ; comment here {
tstr => tstr
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.4. Arrays

; cspell: words tstr

unlimited-people = [* person]
one-or-two-people = [1*2 person]
at-least-two-people = [2* person]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.8.2. Control Operator .bits

; cspell: words tcpflagbytes rwxbits

tcpflagbytes = bstr .bits flags
flags = &(
fin: 8,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.8.3. Control Operator .regexp

; cspell: words tstr

nai = tstr .regexp "[A-Za-z0-9]+@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)+"

; "N1@CH57HF.4Znqe0.dYJRN.igjf" should pass this regexp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.4. Cuts in Maps

; cspell: words tstr

extensible-map-example = {
? "optional-key" => int,
* tstr => any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.4. Cuts in Maps

; cspell: words tstr

extensible-map-example = {
? "optional-key" ^ => int,
* tstr => any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.4. Cuts in Maps

; cspell: words tstr

extensible-map-example = {
? "optional-key": int,
* tstr => any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.4. Cuts in Maps

; cspell: words tstr

extensible-map-example = {
? optional-key: int,
* tstr => any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 2.2.2.2. Turning a Group into a Choice

; cspell: words basecolors

terminal-color = &basecolors
basecolors = (
black: 0, red: 1, green: 2, yellow: 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 2.2.3. Representation Types

; cspell: words cbor tstr

my_breakfast = #6.55799(breakfast) ; cbor-any is too general!
breakfast = cereal / porridge
cereal = #6.998(tstr)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; cspell: words reputons reputon

reputation-object = {
application: text
reputons: [* reputon]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; cspell: words reputons reputon

reputation-object = {
reputation-context,
reputon-list
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; Figure 1: Using a Group Directly in a Map

; cspell: words tstr

person = {
age: int,
name: tstr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; Figure 2: A Basic Group

; cspell: words tstr

pii = (
age: int,
name: tstr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; Figure 3: Using a Group by Name

; cspell: words tstr

person = {
pii
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; Figure 4: Using a Parenthesized Group in a Map

; cspell: words tstr

person = {(
age: int,
name: tstr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; Figure 5: Maps with Copy/Paste

; cspell: words tstr

person = {
age: int,
name: tstr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; Figure 6: Using a Group for Factorization

; cspell: words tstr

person = {
identity,
employer: tstr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 2.2.2. Choices

; cspell: words tstr

attire = "bow tie" / "necktie" / "Internet attire"
protocol = 6 / 17

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 2.2.2. Choices

; cspell: words tstr

attire /= "swimwear"

delivery //= (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; cspell: words tstr bareword

; This is a comment
person = { g }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.9. Socket/Plug

; cspell: words tstr personaldata shoesize

PersonalData = {
? displayName: tstr,
NameComponents,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.1. Structs

; cspell: words tstr

Geography = [
city : tstr,
gpsCoordinates : GpsCoordinates,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.1. Structs

; cspell: words tstr

Geography = [
city : tstr,
gpsCoordinates : GpsCoordinates,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.5.2. Tables

; cspell: words tstr tostring mynumber

square-roots = {* x => y}
x = int
y = float
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 3.6. Tags

; cspell: words biguint buuid tstr

biguint = #6.2(bstr)

buuid = #6.37(bstr)
Expand Down
3 changes: 3 additions & 0 deletions hermes/crates/cbork/cddl-parser/tests/group_elements.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// cspell: words OPTCOM MEMBERKEY bareword tstr GRPENT GRPCHOICE
// cspell: words optcom memberkey grpent grpchoice

use cddl_parser::{
self,
cddl_test::{CDDLTestParser, Parser, Rule},
Expand Down
3 changes: 3 additions & 0 deletions hermes/crates/cbork/cddl-parser/tests/rules.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// cspell: words GENERICARG bigfloat ASSIGNG GROUPNAME tstr genericarg GENERICARG
// cspell: words assigng assignt ASSIGNT GENERICPARM genericparm

use cddl_parser::{
self,
cddl_test::{CDDLTestParser, Parser, Rule},
Expand Down
3 changes: 3 additions & 0 deletions hermes/crates/cbork/cddl-parser/tests/type_declarations.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// cspell: words CTLOP aname groupsocket typesocket RANGEOP tstr ctlop
// cspell: words rangeop RANGEOP

use cddl_parser::{
self,
cddl_test::{CDDLTestParser, Parser, Rule},
Expand Down