Skip to content

Commit

Permalink
Add line comments to chirp parser
Browse files Browse the repository at this point in the history
It is now possible to add comments to a chirp file using the

    // until end of line

syntax

Fixes #63
  • Loading branch information
nicopap committed Aug 22, 2023
1 parent 5b836f4 commit 719cc6a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
9 changes: 7 additions & 2 deletions chirp/src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@ impl<'w, 's, 'a, 'l, 'll, 'r, D: ParseDsl> Interpreter<'w, 's, 'a, 'l, 'll, 'r,
pub fn statements(&self, input: &mut &BStr) -> PResult<(), ()> {
use winnow::{
ascii,
combinator::{alt, delimited, opt, preceded, separated0, separated_pair, success},
combinator::{
alt, delimited, opt, preceded, repeat, separated0, separated_pair, success,
},
token::{one_of, take_till1},
};
// Note: we use `void` to reduce the size of input/output types. It's
// a major source of performance problems in winnow.
let line_comment = || preceded(b"//", take_till1(b'\n'));
let repeat = repeat::<_, _, (), _, _>;
let spc_trail = || repeat(.., (line_comment(), ascii::multispace0));
let (spc, spc1, opt) = (
|| ascii::multispace0.void(),
|| (ascii::multispace0, spc_trail()).void(),
|| ascii::multispace1.void(),
|| opt(b' ').void(),
);
Expand Down
9 changes: 5 additions & 4 deletions chirp/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,15 @@ pub mod quick {
impl<'a> Iterator for ArgIter<'a> {
type Item = Result<&'a str, ArgError>;
fn next(&mut self) -> Option<Self::Item> {
if self.input.is_empty() {
return None;
}
self.count += 1;
#[cold]
#[allow(clippy::missing_const_for_fn)] // false positive
fn err<T>(_: T) -> ArgError {
ArgError::ArgParse
}
if self.input.is_empty() {
return None;
}
self.count += 1;
if self.count - 1 == 0 {
let text = balanced_text.parse_next(&mut self.input).map_err(err);
// SAFETY: `ArgIter.input` is always valid utf8 because of the
Expand Down
22 changes: 16 additions & 6 deletions examples/parse_dsl_macro/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ fn main() {

let mut world1 = World::new();
let chirp = r#"
row("first row", rules(px(10), pct(11))) {
spawn(rules(pct(20), px(21)), "first child", empty_px 30);
spawn(empty_px 31, "2");
// Some comments
row(
"first row", // demonstrating
rules(px(10), pct(11))
) { // that it is possible
spawn(rules(pct(20), px(21)), "first child", empty_px 30); // to
spawn(empty_px 31, "2"); // add comments
}
// To a chirp file
column("second element", rules(px(40), pct(41))) {
spawn(rules(pct(50), px(51)), empty_px 60, "child3");
spawn(empty_px 61, "so called \"fourth\" child");
Expand All @@ -190,10 +195,15 @@ fn main() {
let mut state = SystemState::<Commands>::new(&mut world2);
let mut cmds = state.get_mut(&mut world2);
dsl! { <LayoutDsl> cmds,
row("first row", rules(px(10), pct(11))) {
spawn(rules(pct(20), px(21)), "first child", empty_px 30);
spawn(empty_px 31, "2");
// Some comments
row(
"first row", // demonstrating
rules(px(10), pct(11))
) { // that it is possible
spawn(rules(pct(20), px(21)), "first child", empty_px 30); // to
spawn(empty_px 31, "2"); // add comments
}
// To a chirp file
column("second element", rules(px(40), pct(41))) {
spawn(rules(pct(50), px(51)), empty_px 60, "child3");
spawn(empty_px 61, "so called \"fourth\" child");
Expand Down

0 comments on commit 719cc6a

Please sign in to comment.