-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support bitfield constants in Spicy for parsing.
One can now define bitfield "constants" for parsing by providing integer expressions with fields: type Foo = unit { x: bitfield(8) { a: 0..3 = 2; b: 4..7; c: 7 = 1; }; This will first parse the bitfield as usual and then enforce that the two bit ranges that are coming with expressions (i.e., `a` and `c`) indeed containing the expected values. If they don't, that's a parse error. We also support using such bitfield constants for look-ahead parsing: type Foo = unit { x: uint8[]; y: bitfield(8) { a: 0..3 = 4; b: 4..7; }; }; This will parse uint8s until a value is discovered that has its bits set as defined by the bitfield constant. (We use the term "constant" loosely here: only the bits with values are actually enforced to be constant, all others are parsed as usual.) Closes #1467.
- Loading branch information
Showing
19 changed files
with
240 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Automatically generated; edit in Sphinx source code, not here. | ||
module Test; | ||
|
||
public type Foo = unit { | ||
x: int8; | ||
: skip bytes &size=5; | ||
y: int8; | ||
on %done { print self; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
[error] <...>/ctor-fail.spicy:10:5: value is outside of bitfield element's range | ||
[error] <...>/ctor-fail.spicy:11:5: value is outside of bitfield element's range | ||
[error] <...>/ctor-fail.spicy:13:5: cannot coerce expression '-1' of type 'int<64>' to type 'uint<8>' | ||
[error] <...>/ctor-fail.spicy:14:5: cannot coerce expression '"42"' of type 'string' to type 'uint<8>' | ||
[error] spicyc: aborting after errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
[$x=[1, 2, 3], $y=(4, 8), $z=5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
(2, 8, 1) | ||
[error] terminating with uncaught exception of type spicy::rt::ParseError: unexpected value for bitfield element 'a' (<...>/ctor.spicy:10:6-14:4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
[error] <...>/width-fail.spicy:9:8-12:6: upper limit is beyond the width of the bitfield | ||
[error] <...>/width-fail.spicy:9:8-12:6: lower limit needs to be lower than upper limit | ||
[error] <...>/width-fail.spicy:10:9: upper limit is beyond the width of the bitfield | ||
[error] <...>/width-fail.spicy:11:9: lower limit needs to be lower than upper limit | ||
[error] spicyc: aborting after errors |
14 changes: 8 additions & 6 deletions
14
tests/Baseline/spicy.types.unit.synchronize-literals/output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
[$a=b"Axy", $b=6, $c=b"Cxy", $d=b"Dxy"] | ||
[$a=(not set), $b=6, $c=b"Cxy", $d=b"Dxy"] | ||
[$a=b"Axy", $b=(not set), $c=b"Cxy", $d=b"Dxy"] | ||
[$a=b"Axy", $b=6, $c=(not set), $d=b"Dxy"] | ||
[$a=b"Axy", $b=6, $c=(not set), $d=b"Dxy"] | ||
[error] terminating with uncaught exception of type spicy::rt::ParseError: unexpected token to consume (<...>/synchronize-literals.spicy:23:8) | ||
[$a=b"Axy", $b=6, $c=b"Cxy", $d=b"Dxy", $e=(1)] | ||
[$a=(not set), $b=6, $c=b"Cxy", $d=b"Dxy", $e=(1)] | ||
[$a=b"Axy", $b=(not set), $c=b"Cxy", $d=b"Dxy", $e=(1)] | ||
[$a=b"Axy", $b=6, $c=(not set), $d=b"Dxy", $e=(1)] | ||
[$a=b"Axy", $b=6, $c=(not set), $d=b"Dxy", $e=(1)] | ||
[$a=b"Axy", $b=6, $c=b"Cxy", $d=(not set), $e=(1)] | ||
[error] terminating with uncaught exception of type spicy::rt::ParseError: failed to synchronize: expecting 'Cxy' (<...>/synchronize-literals.spicy:24:8) | ||
[error] terminating with uncaught exception of type spicy::rt::ParseError: failed to synchronize: failed to match regular expression (<...>/synchronize-literals.spicy:25:8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# @TEST-EXEC-FAIL: spicyc -j %INPUT >>output 2>&1 | ||
# @TEST-EXEC: btest-diff output | ||
# | ||
# @TEST-DOC: Check that we catch invalid bitfield constants. | ||
|
||
module Test; | ||
|
||
public type Foo = unit { | ||
x: bitfield(8) { | ||
a: 1..3 = 255; # error | ||
b: 1..3 = 8; # error | ||
c: 1..3 = 7; # ok | ||
d: 4..7 = -1; | ||
e: 4..7 = "42"; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# @TEST-EXEC: printf '\001\002\003\204\005' | spicy-driver %INPUT >>output | ||
# @TEST-EXEC: btest-diff output | ||
# | ||
# @TEST-DOC: Check look-ahead vector parsing with bitfield constant as terminator. | ||
|
||
module Test; | ||
|
||
public type Foo = unit { | ||
x: uint8[]; | ||
y: bitfield(8) { | ||
a: 0..3 = 4; | ||
b: 4..7; | ||
}; | ||
z: uint8; | ||
|
||
on %done { print self; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# @TEST-EXEC: ${SCRIPTS}/printf '\x82' | spicy-driver %INPUT >>output 2>&1 | ||
# @TEST-EXEC-FAIL: ${SCRIPTS}/printf '\200' | spicy-driver %INPUT >>output 2>&1 | ||
# @TEST-EXEC: btest-diff output | ||
# | ||
# @TEST-DOC: Check bitfield constant parsing. | ||
|
||
module Test; | ||
|
||
public type Foo = unit { | ||
x: bitfield(8) { | ||
a: 0..3 = 2; | ||
b: 4..7; | ||
c: 7 = 1; | ||
}; | ||
|
||
on %done { print self.x; } | ||
}; |
Oops, something went wrong.