Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions Cargo.lock

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

28 changes: 26 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ anyhow = { workspace = true }
serde = { workspace = true }
serde_json = "1.0.117"
copager = { path = ".", features = ["all"] }
example_lang_arithmetic = { path = "./examples/lang_arithmetic" }
example_lang_json = { path = "./examples/lang_json" }
example_lang_pl0 = { path = "./examples/lang_pl0" }
example_lang_xml = { path = "./examples/lang_xml" }

[features]
# common
Expand Down Expand Up @@ -60,12 +64,32 @@ members = [
"./crates/utils",

# Examples
"./examples/oneshot",
"./examples/prebuild",
"./examples/build_oneshot",
"./examples/build_prebuild",
"./examples/lang_arithmetic",
"./examples/lang_json",
"./examples/lang_pl0",
"./examples/lang_xml",
]
exclude = []

[workspace.dependencies]
anyhow = "1.0.82"
thiserror = "1.0.58"
serde = { version = "1.0.197", features = ["derive"] }

[[test]]
name = "test_by_arithmetic"
path = "./tests/arithmetic/test.rs"

[[test]]
name = "test_by_json"
path = "./tests/json/test.rs"

[[test]]
name = "test_by_pl0"
path = "./tests/pl0/test.rs"

[[test]]
name = "test_by_xml"
path = "./tests/xml/test.rs"
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,22 @@ Rust製パーサジェネレータ

## Examples

### One-shot

[examples/oneshot](examples/oneshot)
- [example_build_oneshot](examples/build_oneshot)
- [example_build_prebuild](examples/build_prebuild)
- [example_lang_arithmetic](examples/lang_arithmetic)
- [example_lang_json](examples/lang_json)
- [example_lang_pl0](examples/lang_pl0)
- [example_lang_xml](examples/lang_xml)

```
$ echo "10 * (20 + 30)" | cargo run -p example_oneshot
Success : (Expr (Term (Term (Num "10")) "*" (Num "(" (Expr (Expr (Term (Num "20"))) "+" (Term (Num "30"))) ")")))
$ cargo run -p example_build_oneshot
Example <one-shot>
Input: 10 * 20 + 30
Success: (Expr (Expr (Term (Term (Num "10")) "*" (Num "20"))) "+" (Term (Num "30")))
```

### Pre-build

[examples/prebuild](examples/prebuild)
## Test

```
$ echo "10 * (20 + 30)" | cargo run -p example_prebuild
Success : (Expr (Term (Term (Num "10")) "*" (Num "(" (Expr (Expr (Term (Num "20"))) "+" (Term (Num "30"))) ")")))
$ cargo test
```

## Docs

```
$ make -C docs run
```

⇒ http://localhost:1313
9 changes: 9 additions & 0 deletions examples/build_oneshot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "example_build_oneshot"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
thiserror = { workspace = true }
copager = { path = "../..", features = ["derive", "regexlex", "lr1", "sexp"] }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::stdin;
use std::io::{stdin, stdout, Write};

use copager::lex::{LexSource, RegexLexer};
use copager::parse::{ParseSource, LR1};
Expand Down Expand Up @@ -49,14 +49,18 @@ type MyParser = LR1<ExprToken, ExprRule>;
type MyProcessor = Processor<MyGrammar, MyLexer, MyParser>;

fn main() -> anyhow::Result<()> {
println!("Example <one-shot>");
print!("Input: ");
stdout().flush()?;

let mut input = String::new();
stdin().read_line(&mut input)?;

let sexp = MyProcessor::new()
.build_lexer()?
.build_parser()?
.process::<SExp<_, _>>(&input)?;
println!("Success : {}", sexp);
println!("Success: {}", sexp);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "example_prebuild"
name = "example_build_prebuild"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use std::io::stdin;
use std::io::{stdin, stdout, Write};

use copager::ir::SExp;

use grammar::MyProcessor;

#[copager::load]
fn main(processor: MyProcessor) -> anyhow::Result<()> {
println!("Example <one-shot>");
print!("Input: ");
stdout().flush()?;

let mut input = String::new();
stdin().read_line(&mut input)?;

let sexp = processor
.build_lexer()?
.build_parser_by_cache()
.process::<SExp<_, _>>(&input)?;
println!("Success : {}", sexp);
println!("Success: {}", sexp);

Ok(())
}
9 changes: 9 additions & 0 deletions examples/lang_arithmetic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "example_lang_arithmetic"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
thiserror = { workspace = true }
copager = { path = "../..", features = ["derive", "regexlex", "lr1", "sexp"] }
43 changes: 43 additions & 0 deletions examples/lang_arithmetic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use copager::lex::LexSource;
use copager::parse::ParseSource;
use copager::prelude::*;
use copager::Grammar;

#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq, LexSource)]
pub enum ArithmeticToken {
#[default]
#[token(text = r"\+")]
Plus,
#[token(text = r"-")]
Minus,
#[token(text = r"\*")]
Mul,
#[token(text = r"/")]
Div,
#[token(text = r"\(")]
BracketL,
#[token(text = r"\)")]
BracketR,
#[token(text = r"[1-9][0-9]*")]
Num,
#[token(text = r"[ \t\n]+", ignored)]
_Whitespace,
}

#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq, ParseSource)]
pub enum ArithmeticRule {
#[default]
#[rule("<expr> ::= <expr> Plus <term>")]
#[rule("<expr> ::= <expr> Minus <term>")]
#[rule("<expr> ::= <term>")]
Expr,
#[rule("<term> ::= <term> Mul <num>")]
#[rule("<term> ::= <term> Div <num>")]
#[rule("<term> ::= <num>")]
Term,
#[rule("<num> ::= BracketL <expr> BracketR")]
#[rule("<num> ::= Num")]
Num,
}

pub type Arithmetic = Grammar<ArithmeticToken, ArithmeticRule>;
29 changes: 29 additions & 0 deletions examples/lang_arithmetic/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::io::{stdin, stdout, Write};

use copager::lex::RegexLexer;
use copager::parse::LR1;
use copager::ir::SExp;
use copager::Processor;

use example_lang_arithmetic::*;

type MyLexer = RegexLexer<ArithmeticToken>;
type MyParser = LR1<ArithmeticToken, ArithmeticRule>;
type MyProcessor = Processor<Arithmetic, MyLexer, MyParser>;

fn main() -> anyhow::Result<()> {
println!("Example <arithmetic>");
print!("Input: ");
stdout().flush()?;

let mut input = String::new();
stdin().read_line(&mut input)?;

let sexp = MyProcessor::new()
.build_lexer()?
.build_parser()?
.process::<SExp<_, _>>(&input)?;
println!("Success: {}", sexp);

Ok(())
}
9 changes: 9 additions & 0 deletions examples/lang_json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "example_lang_json"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
thiserror = { workspace = true }
copager = { path = "../..", features = ["derive", "regexlex", "lr1", "sexp"] }
Loading