Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5cff3331 authored by Martin Geisler's avatar Martin Geisler
Browse files

pdl: move code generators into ‘backends’ module

The Rust generator is now nearly 1k lines long, so it’s time to move
it into a more deeply nested module structure. As a first step, I’ll
move it to a ‘backends’ module, and then we can expand further from
there.

Test: m pdl
Change-Id: I22550ecaefe9bbab0e9c2c020d23a9f0874a8716
parent 12da0a29
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
//! Compiler backends.

pub mod json;
pub mod rust;
+9 −0
Original line number Diff line number Diff line
//! Rust compiler backend.

use crate::ast;

/// Turn the AST into a JSON representation.
pub fn generate(file: &ast::File) -> Result<String, String> {
    serde_json::to_string_pretty(&file)
        .map_err(|err| format!("could not JSON serialize grammar: {err}"))
}
+3 −1
Original line number Diff line number Diff line
//! Rust compiler backend.

// The `format-push-string` lint was briefly enabled present in Rust
// 1.62. It is now moved the disabled "restriction" category instead.
// See https://github.com/rust-lang/rust-clippy/issues/9077 for the
@@ -636,7 +638,7 @@ fn generate_decl(
///
/// The code is not formatted, pipe it through `rustfmt` to get
/// readable source code.
pub fn generate_rust(sources: &ast::SourceDatabase, file: &ast::File) -> String {
pub fn generate(sources: &ast::SourceDatabase, file: &ast::File) -> String {
    let source = sources.get(file.file).expect("could not read source");

    let mut children = HashMap::new();
+3 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ use codespan_reporting::term::{self, termcolor};
use structopt::StructOpt;

mod ast;
mod generator;
mod backends;
mod lint;
mod parser;
#[cfg(test)]
@@ -67,10 +67,10 @@ fn main() -> std::process::ExitCode {

            match opt.output_format {
                OutputFormat::JSON => {
                    println!("{}", serde_json::to_string_pretty(&file).unwrap())
                    println!("{}", backends::json::generate(&file).unwrap())
                }
                OutputFormat::Rust => {
                    println!("{}", generator::generate_rust(&sources, &file))
                    println!("{}", backends::rust::generate(&sources, &file))
                }
            }
            std::process::ExitCode::SUCCESS