Skip to content

Commit

Permalink
Merge pull request #22 from JakeStanger/fix/escape-input
Browse files Browse the repository at this point in the history
fix: unable to escape input in string interpolation
  • Loading branch information
JakeStanger authored Sep 1, 2023
2 parents d848156 + f49f68b commit 169e019
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ small enough you can learn it in minutes. It was born out of the following frust
## What Corn Is Not

Corn is not a full programming language and does not try to be.
There are no dynamic variables, there is no interpolation and there are no statement blocks.
There are no dynamic variables, there are no operations and there are no statement blocks.

Likewise, Corn is not a data exchange format.
Unlike JSON or YAML or TOML, you cannot serialize code back into Corn.
Expand Down Expand Up @@ -436,6 +436,8 @@ Evaluates to:
}
```

To use a literal dollar, you can escape with `\$`. For example, `\$subject`.

#### Merging

Somtimes you want to re-use an object or array to compose a larger object/array.
Expand Down
1 change: 1 addition & 0 deletions assets/inputs/string_interpolation.corn
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ let {
$subject = "world"
} in {
foo = "$greeting, $subject"
bar = "\$escaped"
}
1 change: 1 addition & 0 deletions assets/outputs/json/string_interpolation.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"bar": "$escaped",
"foo": "hello, world"
}
1 change: 1 addition & 0 deletions assets/outputs/toml/string_interpolation.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bar = "$escaped"
foo = "hello, world"

1 change: 1 addition & 0 deletions assets/outputs/yaml/string_interpolation.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bar: $escaped
foo: hello, world

4 changes: 2 additions & 2 deletions libcorn/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ string_val = ${ (input | char)* }

char = {
!("\"" | "\\") ~ ANY
| "\\" ~ ("\"" | "\\" | "n" | "r" | "t")
| "\\" ~ ("\"" | "\\" | "n" | "r" | "t" | "$")
| "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}

Expand All @@ -73,7 +73,7 @@ float = @{
~ (^"e" ~ ("+" | "-")? ~ ASCII_DIGIT+)?
}

input = ${ "$" ~ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")+ }
input = ${ !"\\" ~ "$" ~ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")+ }

assignment = { input ~ "=" ~ value }

Expand Down
1 change: 1 addition & 0 deletions libcorn/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl<'a> CornParser<'a> {
'r' => '\r',
't' => '\t',
'"' => '\"',
'$' => '$',
'\\' => '\\',
_ => unreachable!(),
};
Expand Down

0 comments on commit 169e019

Please sign in to comment.