summaryrefslogtreecommitdiff
path: root/tools/pdl/src/main.rs
blob: ff5c585143accb2e60ebbf94c94f158c204e2723 (plain)
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
//! PDL parser and linter.

use codespan_reporting::term::{self, termcolor};
use structopt::StructOpt;

mod ast;
mod lint;
mod parser;

use crate::lint::Lintable;

#[derive(Debug, StructOpt)]
#[structopt(name = "pdl-parser", about = "Packet Description Language parser tool.")]
struct Opt {
    /// Print tool version and exit.
    #[structopt(short, long = "--version")]
    version: bool,

    /// Input file.
    #[structopt(name = "FILE")]
    input_file: String,
}

fn main() {
    let opt = Opt::from_args();

    if opt.version {
        println!("Packet Description Language parser version 1.0");
        return;
    }

    let mut sources = ast::SourceDatabase::new();
    match parser::parse_file(&mut sources, opt.input_file) {
        Ok(grammar) => {
            let _ = grammar.lint().print(&sources, termcolor::ColorChoice::Always);
            println!("{}", serde_json::to_string_pretty(&grammar).unwrap())
        }
        Err(err) => {
            let writer = termcolor::StandardStream::stderr(termcolor::ColorChoice::Always);
            let config = term::Config::default();
            _ = term::emit(&mut writer.lock(), &config, &sources, &err);
        }
    }
}