Created
September 2, 2018 15:14
-
-
Save Slabity/ff826704b8f8aafc9fda3d54076676af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "nix-parser" | |
| version = "0.1.0" | |
| authors = ["Tyler Slabinski <[email protected]>"] | |
| [dependencies.rnix] | |
| git = "https://gitlab.com/jD91mZM2/rnix.git" | |
| [dependencies] | |
| arenatree = "0.1.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate rnix; | |
| extern crate arenatree; | |
| use rnix::parser::AST; | |
| use rnix::parser::ASTNode; | |
| use rnix::parser::ASTKind; | |
| use rnix::parser::Arena; | |
| use rnix::parser::NodeId; | |
| fn get_siblings(arena: &mut Arena, node: &ASTNode) -> Option<Vec<ASTNode>> { | |
| match node.node.sibling { | |
| Some(id) => { | |
| let sibling = arena.take(id); | |
| match get_siblings(arena, &sibling) { | |
| Some(mut siblings) => { | |
| siblings.insert(0, sibling); | |
| Some(siblings) | |
| }, | |
| None => { | |
| Some(vec![sibling]) | |
| } | |
| } | |
| }, | |
| None => None | |
| } | |
| } | |
| fn get_children(arena: &mut Arena, node: &ASTNode) -> Option<Vec<ASTNode>> { | |
| match node.node.child { | |
| Some(id) => { | |
| let child = arena.take(id); | |
| match get_siblings(arena, &child) { | |
| Some(mut siblings) => { | |
| siblings.insert(0, child); | |
| Some(siblings) | |
| }, | |
| None => { | |
| Some(vec![child]) | |
| } | |
| } | |
| }, | |
| None => None | |
| } | |
| } | |
| fn eval_letin(arena: &mut Arena, node: &ASTNode) { | |
| let children = get_children(arena, node).unwrap(); | |
| let siblings = get_siblings(arena, node); | |
| println!("{:#?}", children); | |
| println!("{:#?}", siblings); | |
| assert_eq!(children.len(), 4); | |
| } | |
| fn eval_ast_node(arena: &mut Arena, node: NodeId) { | |
| let root_node = arena.take(node); | |
| match root_node.kind { | |
| ASTKind::LetIn => eval_letin(arena, &root_node), | |
| _ => println!("Unknown node {:?}", root_node.kind) | |
| } | |
| } | |
| fn eval_ast(ast: AST) { | |
| let mut arena = ast.arena; | |
| let root_id = ast.root; | |
| eval_ast_node(&mut arena, root_id) | |
| } | |
| fn parse_file(path: &str) -> AST { | |
| let content = std::fs::read_to_string(path).unwrap(); | |
| rnix::parse(&content).unwrap() | |
| } | |
| fn main() { | |
| let path = "/nix/var/nix/profiles/per-user/root/channels/nixos/default.nix"; | |
| let ast = parse_file(path); | |
| eval_ast(ast); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment