Skip to content

Commit d47687b

Browse files
committed
feat: add interface for program ast output
1 parent cd6e9d2 commit d47687b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

parser/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt;
22
use core::fmt::Result;
33
use std::fmt::Formatter;
44
use lexer::token::{Token, TokenKind, Span};
5-
use serde::{Deserialize, Serialize, Deserializer, Serializer};
5+
use serde::{Deserialize, Serialize, Serializer};
66
use serde::ser::SerializeStruct;
77

88
// still wait for https://github.com/serde-rs/serde/issues/1402

parser/ast_tree_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod tests {
88
Ok(node) => {
99
serde_json::to_string_pretty(&node).unwrap()
1010
}
11-
Err(e) => format!("parse error: {}", e[0])
11+
Err(e) => panic!("parse error: {}", e[0])
1212
};
1313
assert_snapshot!(name, ast, input);
1414
}

parser/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,17 @@ pub fn parse(input: &str) -> Result<Node, ParseErrors> {
471471
Ok(Node::Program(program))
472472
}
473473

474+
pub fn parse_ast_json_string(input: &str) -> Result<String, ParseErrors> {
475+
let ast = match parse(input) {
476+
Ok(node) => {
477+
serde_json::to_string_pretty(&node).unwrap()
478+
}
479+
Err(e) => return Err(e)
480+
};
481+
482+
return Ok(ast);
483+
}
484+
474485
#[cfg(test)]
475486
mod tests {
476487
use super::*;

0 commit comments

Comments
 (0)