mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-26 16:37:30 +03:00
Add grammar check
This commit is contained in:
parent
d8fad17792
commit
6398f3cb1e
@ -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
|
||||
}
|
||||
}
|
||||
},
|
||||
|
85
wasm/.resources/basic/expected_grammar.json
Normal file
85
wasm/.resources/basic/expected_grammar.json
Normal file
@ -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
|
||||
}
|
||||
}
|
60
wasm/src/grammar.rs
Normal file
60
wasm/src/grammar.rs
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -20,5 +20,8 @@
|
||||
pub mod ast;
|
||||
pub use ast::*;
|
||||
|
||||
pub mod grammar;
|
||||
pub use grammar::*;
|
||||
|
||||
// pub mod compiler;
|
||||
// pub use compiler::*;
|
||||
|
Loading…
Reference in New Issue
Block a user