From 6398f3cb1e07dd6753420e0083176f7256a11d8b Mon Sep 17 00:00:00 2001 From: howardwu Date: Fri, 5 Feb 2021 13:00:45 -0800 Subject: [PATCH] Add grammar check --- wasm/.resources/basic/expected_ast.json | 18 ++--- wasm/.resources/basic/expected_grammar.json | 85 +++++++++++++++++++++ wasm/src/grammar.rs | 60 +++++++++++++++ wasm/src/lib.rs | 3 + 4 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 wasm/.resources/basic/expected_grammar.json create mode 100644 wasm/src/grammar.rs diff --git a/wasm/.resources/basic/expected_ast.json b/wasm/.resources/basic/expected_ast.json index 5703033471..dec3baf947 100644 --- a/wasm/.resources/basic/expected_ast.json +++ b/wasm/.resources/basic/expected_ast.json @@ -8,7 +8,7 @@ "identifier": "{\"name\":\"main\",\"span\":\"{\\\"text\\\":\\\" function main() {\\\",\\\"line\\\":1,\\\"start\\\":10,\\\"end\\\":14}\"}", "input": [], "output": null, - "block" : { + "block": { "statements": [ { "Return": { @@ -27,7 +27,6 @@ ] } }, - "op": "Add", "right": { "Value": { "Implicit": [ @@ -41,6 +40,7 @@ ] } }, + "op": "Add", "span": { "text": " return 1 + 1", "line": 2, @@ -49,7 +49,7 @@ } } }, - "span" : { + "span": { "text": " return 1 + 1", "line": 2, "start": 5, @@ -58,18 +58,18 @@ } } ], - "span" : { - "text": " return 1 + 1", - "line": 2, - "start": 5, - "end": 17 + "span": { + "text": " function main() {", + "line": 1, + "start": 17, + "end": 2 } }, "span": { "text": " function main() {", "line": 1, "start": 1, - "end": 1 + "end": 2 } } }, diff --git a/wasm/.resources/basic/expected_grammar.json b/wasm/.resources/basic/expected_grammar.json new file mode 100644 index 0000000000..6c0e4c9404 --- /dev/null +++ b/wasm/.resources/basic/expected_grammar.json @@ -0,0 +1,85 @@ +{ + "definitions": [ + { + "Function": { + "identifier": { + "value": "main", + "span": { + "input": "main", + "start": 9, + "end": 13 + } + }, + "parameters": [], + "returns": null, + "block": { + "statements": [ + { + "Return": { + "expression": { + "Binary": { + "operation": "Add", + "left": { + "Value": { + "Implicit": { + "Positive": { + "value": "1", + "span": { + "input": "1", + "start": 29, + "end": 30 + } + } + } + } + }, + "right": { + "Value": { + "Implicit": { + "Positive": { + "value": "1", + "span": { + "input": "1", + "start": 33, + "end": 34 + } + } + } + } + }, + "span": { + "input": "1 + 1", + "start": 29, + "end": 34 + } + } + }, + "span": { + "input": "return 1 + 1", + "start": 22, + "end": 34 + } + } + } + ], + "span": { + "input": "{\n return 1 + 1\n}", + "start": 16, + "end": 36 + } + }, + "span": { + "input": "function main() {\n return 1 + 1\n}", + "start": 0, + "end": 36 + } + } + } + ], + "eoi": null, + "span": { + "input": "function main() {\n return 1 + 1\n}\n", + "start": 0, + "end": 37 + } +} \ No newline at end of file diff --git a/wasm/src/grammar.rs b/wasm/src/grammar.rs new file mode 100644 index 0000000000..d181661446 --- /dev/null +++ b/wasm/src/grammar.rs @@ -0,0 +1,60 @@ +// Copyright (C) 2019-2021 Aleo Systems Inc. +// This file is part of the Leo library. + +// The Leo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Leo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Leo library. If not, see . + +use leo_ast::Ast as LeoAst; +use leo_grammar::Grammar as LeoGrammar; + +use std::path::Path; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct Grammar(pub(crate) String); + +#[wasm_bindgen] +impl Grammar { + #[wasm_bindgen(constructor)] + pub fn new(filepath: &str, program_string: &str) -> Self { + let grammar = LeoGrammar::new(&Path::new(filepath), &program_string).unwrap(); + Self(grammar.to_json_string().unwrap()) + } + + #[wasm_bindgen] + pub fn to_string(&self) -> String { + self.0.clone() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use wasm_bindgen_test::*; + + #[wasm_bindgen_test] + fn grammar_test() { + let expected = include_str!("../.resources/basic/expected_grammar.json"); + + let filepath = "../.resources/basic/main.leo"; + let program_string = include_str!("../.resources/basic/main.leo"); + + let candidate = Grammar::new(filepath, program_string).to_string(); + + let expected = JsValue::from_str(expected); + let candidate = JsValue::from_serde(&candidate).unwrap(); + + assert_eq!(expected, candidate); + } +} diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index d457955975..624e19f64c 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -20,5 +20,8 @@ pub mod ast; pub use ast::*; +pub mod grammar; +pub use grammar::*; + // pub mod compiler; // pub use compiler::*;