Skip to content

Commit

Permalink
add option keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
yoav-lavi committed Feb 22, 2022
1 parent 6636dc5 commit d7690b6
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ OPTIONS:
- `char` - matches a single character, equivalent to regex `.`
- `some` - used with `of` to express 1 or more of a pattern, equivalent to regex `+`
- `over` - used with `of` to express more than an amount of a pattern, equivalent to regex `{6,}` (assuming `over 5 of ...`)
- `option` - used with `of` to express 0 or 1 of a pattern, equivalent to regex `?`

## Symbols

Expand Down Expand Up @@ -211,6 +212,7 @@ The Melody file extension is `.mdy`
| `over 5 of "A";` | `A{6,}` ||
| WASM binding | ||
| Rust crate | ||
| `option of` | `?` ||
| enforce group close | | 🐣 |
| tests | | 🐣 |
| `not <space>;` | `\S` ||
Expand All @@ -227,7 +229,6 @@ The Melody file extension is `.mdy`
| `not "A";` | `[^A]` ||
| `flags: global, multiline, ...` | `/.../gm...` ||
| `/* comment */` | ||
| `maybe of` | `?` ||
| `maybe some of` | `*` ||
| `either of ..., ...` | `\|` ||
| `any of "a", "b", "c"` | `[abc]` ||
Expand Down
4 changes: 2 additions & 2 deletions crates/melody_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "A CLI wrapping the Melody language compiler"
homepage = "https://github.com/yoav-lavi/melody"
repository = "https://github.com/yoav-lavi/melody"
readme = "README.md"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
rust-version = "1.58.0"
license = "MIT"
Expand All @@ -13,7 +13,7 @@ keywords = ["melody", "melodylang"]
[dependencies]
clap = { version = "3.0.7", features = ["derive"] }
colored = "2.0.0"
melody_compiler = { version = "0.1.1", path = "../melody_compiler" }
melody_compiler = { version = "0.2.0", path = "../melody_compiler" }

[[bin]]
name = "melody"
Expand Down
2 changes: 1 addition & 1 deletion crates/melody_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "The Melody language compiler"
homepage = "https://github.com/yoav-lavi/melody"
repository = "https://github.com/yoav-lavi/melody"
readme = "README.md"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
rust-version = "1.58.0"
license = "MIT"
Expand Down
25 changes: 25 additions & 0 deletions crates/melody_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ enum Token {
#[regex("some of")]
SomeExpression,

#[regex("option of")]
OptionExpression,

#[regex(r#""(\\"|[^"\n])*""#, raw)]
RawDouble(String),

Expand Down Expand Up @@ -270,6 +273,10 @@ pub fn compiler(source: &str) -> Result<String, ParseError> {
quantifier = Some(String::from("+"));
None
}
Token::OptionExpression => {
quantifier = Some(String::from("?"));
None
}

// direct replacements
Token::LineStart => handle_quantifier(String::from("^"), quantifier.clone(), false),
Expand Down Expand Up @@ -495,3 +502,21 @@ fn some_test() {
.unwrap();
assert_eq!(multiple_output, "/(?:ABC)+/");
}

#[test]
fn option_test() {
let single_output = compiler(
r#"
option of char;
"#,
)
.unwrap();
assert_eq!(single_output, "/.?/");
let multiple_output = compiler(
r#"
option of "ABC";
"#,
)
.unwrap();
assert_eq!(multiple_output, "/(?:ABC)?/");
}
4 changes: 2 additions & 2 deletions crates/melody_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "melody_wasm"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
rust-version = "1.58.0"
description = "WASM bindings for the Melody language compiler"
Expand All @@ -13,4 +13,4 @@ crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
melody_compiler = { version = "0.1.1", path = "../melody_compiler" }
melody_compiler = { version = "0.2.0", path = "../melody_compiler" }
1 change: 1 addition & 0 deletions docs/docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sidebar_position: 6
- `char` - matches a single character, equivalent to regex `.`
- `some` - used with `of` to express 1 or more of a pattern, equivalent to regex `+`
- `over` - used with `of` to express more than an amount of a pattern, equivalent to regex `{6,}` (assuming `over 5 of ...`)
- `option` - used with `of` to express 0 or 1 of a pattern, equivalent to regex `?`

## Symbols

Expand Down
2 changes: 1 addition & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "melody",
"displayName": "Melody",
"description": "Melody syntax highlighting",
"version": "0.0.3",
"version": "0.2.0",
"publisher": "yoavlavi",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions extensions/vscode/syntaxes/melody.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"patterns": [
{
"name": "keyword.other.melody",
"match": "\\b(of|capture|to|of|some|match|over)\\b"
"match": "\\b(of|capture|to|of|some|match|over|option)\\b"
}
]
},
Expand All @@ -35,7 +35,7 @@
},
{
"name": "constant.character.melody",
"match": "\\b(A|Z|a|z)\\b"
"match": "\\b[A-Za-z]\\b"
},
{
"name": "constant.character.melody",
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "playground",
"private": true,
"version": "0.0.0",
"version": "0.2.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand Down
4 changes: 2 additions & 2 deletions playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ languages.register({ id: MELODY_LANGUAGE_ID });
languages.setMonarchTokensProvider(MELODY_LANGUAGE_ID, {
tokenizer: {
root: [
[/(of|capture|to|of|some|match|over)/, 'keyword'],
[/(of|capture|to|of|some|match|over|option)/, 'keyword'],
[/\d/, 'digit'],
[/"(\\"|[^"\n])*"/, 'string'],
[/'(\\'|[^'\n])*'/, 'string'],
Expand All @@ -47,7 +47,7 @@ languages.setMonarchTokensProvider(MELODY_LANGUAGE_ID, {
'character',
],
[/(start|end|char)/, 'character'],
[/(A|Z|a|z)/, 'character'],
[/[A-Za-z]/, 'character'],
[/\/\/.*/, 'comment'],
],
},
Expand Down
Binary file modified playground/src/wasm/melody_wasm_bg.wasm
Binary file not shown.

0 comments on commit d7690b6

Please sign in to comment.