Skip to content

Commit

Permalink
named buckets must be snake_case
Browse files Browse the repository at this point in the history
  • Loading branch information
loomstyla committed Apr 21, 2023
1 parent 38b5231 commit 48fc740
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
60 changes: 56 additions & 4 deletions palabritas-parser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ mod test {
}
}

#[test]
fn parse_snake_case() {
//snake_case = { ASCII_ALPHA_LOWER ~ (ASCII_ALPHA_LOWER | "_" | ASCII_DIGIT)* }
assert_parse(Rule::snake_case, &make_random_snake_case());
}

#[test]
fn parse_identifier() {
//identifier = { (ASCII_ALPHA | "_" ) ~ (ASCII_ALPHANUMERIC | "_")* }
Expand Down Expand Up @@ -269,16 +275,33 @@ mod test {

#[test]
fn parse_named_bucket() {
//named_bucket = { indentation* ~ "[" ~ " "* ~ text ~ "]" ~ " "* ~ command* }
let text = make_random_string();
//named_bucket = { indentation* ~ "[" ~ " "* ~ probability? ~ snake_case ~ " "* ~ "]" ~ " "* ~ command* }
let command = "\nreq ".to_string() + &(make_random_condition());
let integer = rand::thread_rng().gen_range(i8::MIN..i8::MAX).to_string();
let probability = "(".to_string() + &integer + ")";

assert_parse(
Rule::named_bucket,
&(make_random_indentation() + "[" + &text + "]"),
&(make_random_indentation() + "[" + &make_random_snake_case() + "]"),
);
assert_parse(
Rule::named_bucket,
&(make_random_indentation() + "[" + &text + "]" + &command),
&(make_random_indentation() + "[" + &make_random_snake_case() + "]" + &command),
);

assert_parse(
Rule::named_bucket,
&(make_random_indentation() + "[" + &probability + &make_random_snake_case() + "]"),
);

assert_parse(
Rule::named_bucket,
&(make_random_indentation()
+ "["
+ &probability
+ &make_random_snake_case()
+ "]"
+ &command),
);
}

Expand Down Expand Up @@ -313,6 +336,35 @@ mod test {
make_random_identifier() + " " + &make_random_identifier()
}

fn make_random_snake_case() -> String {
let alphanumeric_size = rand::thread_rng().gen_range(1..20);
let underscore_size = rand::thread_rng().gen_range(1..5);

//Making alphanumeric string
let snake_case: Vec<u8> = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(alphanumeric_size)
.collect();

let mut snake_case = std::str::from_utf8(&snake_case).unwrap().to_string();

//Adding underscores
for _ in 0..underscore_size {
snake_case += "_";
}
shuffle_string(&mut snake_case);

//Making sure starting character is not a number

This comment has been minimized.

Copy link
@frantufro

frantufro Apr 21, 2023

Contributor

why?

This comment has been minimized.

Copy link
@loomstyla

loomstyla Apr 21, 2023

Author Contributor

why the starting character should not be a number?
I was using rust as base to define some of the parsing logic and it doesn't support identifiers starting with numbers, but we don't actually need it so maybe it's best to remove that limitation.

This comment has been minimized.

Copy link
@loomstyla

loomstyla Apr 21, 2023

Author Contributor

Although we have to make sure that it has at least an alphabetic (or underscore) character so it can be different than an integer.

let mut starting_char = rand::thread_rng().sample(&Alphanumeric) as char;
while starting_char.is_numeric() {
starting_char = rand::thread_rng().sample(&Alphanumeric) as char;
}

snake_case = starting_char.to_string() + &snake_case;

snake_case.to_lowercase()
}

fn make_random_identifier() -> String {
let alphanumeric_size = rand::thread_rng().gen_range(1..20);
let underscore_size = rand::thread_rng().gen_range(1..5);
Expand Down
3 changes: 2 additions & 1 deletion palabritas-parser/src/palabritas.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ indentation = { " " | "\t" }
string = { char+ }
comparison_operator = { "!=" | "=" | "<=" | ">=" | "<" | ">" | "!" }

snake_case = { ASCII_ALPHA_LOWER ~ (ASCII_ALPHA_LOWER | "_" | ASCII_DIGIT)* }
identifier = { (ASCII_ALPHA | "_" ) ~ (ASCII_ALPHANUMERIC | "_")* }
value = { identifier | float | percentage | integer}
condition = { identifier ~ " "* ~ (comparison_operator ~ " "*)? ~ value }
Expand All @@ -26,6 +27,6 @@ text = { probability? ~ string }

option = { indentation* ~ "*" ~ " "* ~ text ~ command* }
text_shown = { indentation* ~ text ~ command* }
named_bucket = { indentation* ~ "[" ~ " "* ~ text ~ "]" ~ " "* ~ command* }
named_bucket = { indentation* ~ "[" ~ " "* ~ probability? ~ snake_case ~ " "* ~ "]" ~ " "* ~ command* }

file = { SOI ~ (( named_bucket | text_shown | option | indentation+ | knot | stitch)? ~ NEWLINE)* ~ (named_bucket | text_shown | option | indentation+ | knot | stitch)? ~ EOI }

0 comments on commit 48fc740

Please sign in to comment.