-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage-project-template.rs
More file actions
50 lines (43 loc) · 1.21 KB
/
language-project-template.rs
File metadata and controls
50 lines (43 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::fs;
use std::process;
use clap::{Parser, Subcommand};
use language_project_template::lexer::lex;
use language_project_template::parser::parse;
#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[clap(short, long, value_parser, help = "Path to the input file")]
file_path: String,
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Lex,
Parse,
}
fn main() {
let cli = Cli::parse();
let contents = fs::read_to_string(&cli.file_path).unwrap_or_else(|err| {
eprintln!("Error reading file `{}`: {}", cli.file_path, err);
process::exit(1);
});
match &cli.command {
Commands::Lex => {
let tokens: Vec<
Result<
(usize, language_project_template::prelude::Token, usize),
(
language_project_template::prelude::ExprError,
std::ops::Range<usize>,
),
>,
> = lex(&contents);
println!("{:#?}", tokens);
}
Commands::Parse => {
let ast = parse(&contents);
println!("{ast:#?}");
}
}
}