add definition rule to pest

This commit is contained in:
collin 2020-08-05 20:56:49 -07:00
parent 8ad4724b01
commit 4550573213
7 changed files with 91 additions and 73 deletions

View File

@ -0,0 +1,16 @@
use crate::{
ast::Rule,
circuits::Circuit,
functions::{Function, TestFunction},
};
use pest_ast::FromPest;
use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::definition))]
pub enum Definition<'ast> {
Circuit(Circuit<'ast>),
Function(Function<'ast>),
TestFunction(TestFunction<'ast>),
}

View File

@ -0,0 +1,2 @@
pub mod definition;
pub use definition::*;

View File

@ -1,11 +1,4 @@
use crate::{
ast::Rule,
circuits::Circuit,
common::EOI,
functions::{Function, TestFunction},
imports::Import,
SpanDef,
};
use crate::{ast::Rule, common::EOI, definitions::Definition, imports::Import, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -15,9 +8,7 @@ use serde::Serialize;
#[pest_ast(rule(Rule::file))]
pub struct File<'ast> {
pub imports: Vec<Import<'ast>>,
pub circuits: Vec<Circuit<'ast>>,
pub functions: Vec<Function<'ast>>,
pub tests: Vec<TestFunction<'ast>>,
pub definitions: Vec<Definition<'ast>>,
pub eoi: EOI,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]

View File

@ -5,7 +5,7 @@ use pest_ast::FromPest;
use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::function_definition))]
#[pest_ast(rule(Rule::function))]
pub struct Function<'ast> {
pub function_name: Identifier<'ast>,
pub parameters: Vec<Input<'ast>>,

View File

@ -3,8 +3,15 @@
// Declared in common/assignee.rs
assignee = { identifier ~ access_assignee* }
// Declared in common/file.rs
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test_function* ~ NEWLINE* ~ EOI }
// Declared in files/file.rs
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ definition* ~ NEWLINE* ~ EOI }
// Declared in definitions/definition.rs
definition = {
circuit
| function
| test_function
}
// Declared in common/identifier.rs
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
@ -265,7 +272,7 @@ circuit_field = { identifier ~ ":" ~ expression }
circuit_field_definition = { identifier ~ ":" ~ type_ ~ ","?}
// Declared in circuits/circuit_function.rs
circuit_function = { static_? ~ function_definition }
circuit_function = { static_? ~ function }
// Declared in circuits/circuit_member.rs
circuit_member = { circuit_function | circuit_field_definition ~ NEWLINE*}
@ -357,8 +364,11 @@ statement_return = { "return " ~ return_}
/// Functions
// Declared in functions/test_function.rs
test_function = { "test " ~ function }
// Declared in functions/function.rs
function_definition = { "function " ~ identifier ~ "(" ~ NEWLINE* ~ input_list ~ NEWLINE* ~ ")" ~ ("->" ~ (type_ | "(" ~ type_list ~ ")"))? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
function = { "function " ~ identifier ~ "(" ~ NEWLINE* ~ input_list ~ NEWLINE* ~ ")" ~ ("->" ~ (type_ | "(" ~ type_list ~ ")"))? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
// Declared in functions/input/function_input.rs
function_input = { mutable? ~ identifier ~ ":" ~ type_ }
@ -373,8 +383,6 @@ input = {
}
input_list = _{ (input ~ ("," ~ NEWLINE* ~ input)*)? }
// Declared in functions/test_function.rs
test_function = { "test " ~ function_definition }
/// Imports

View File

@ -10,6 +10,7 @@ mod ast;
pub mod access;
pub mod circuits;
pub mod common;
pub mod definitions;
pub mod expressions;
pub mod files;
pub mod functions;

View File

@ -1,77 +1,77 @@
{
"imports": [],
"circuits": [],
"functions": [
"definitions": [
{
"function_name": {
"value": "main",
"span": {
"input": "main",
"start": 9,
"end": 13
}
},
"parameters": [],
"returns": [],
"statements": [
{
"Return": {
"return_": {
"Single": {
"Binary": {
"operation": "Add",
"left": {
"Value": {
"Implicit": {
"Positive": {
"value": "1",
"span": {
"input": "1",
"start": 29,
"end": 30
"Function": {
"function_name": {
"value": "main",
"span": {
"input": "main",
"start": 9,
"end": 13
}
},
"parameters": [],
"returns": [],
"statements": [
{
"Return": {
"return_": {
"Single": {
"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
},
"right": {
"Value": {
"Implicit": {
"Positive": {
"value": "1",
"span": {
"input": "1",
"start": 33,
"end": 34
}
}
}
}
},
"span": {
"input": "1 + 1",
"start": 29,
"end": 34
}
},
"span": {
"input": "1 + 1",
"start": 29,
"end": 34
}
}
},
"span": {
"input": "return 1 + 1",
"start": 22,
"end": 34
}
},
"span": {
"input": "return 1 + 1",
"start": 22,
"end": 34
}
}
],
"span": {
"input": "function main() {\n return 1 + 1\n}\n",
"start": 0,
"end": 37
}
],
"span": {
"input": "function main() {\n return 1 + 1\n}\n",
"start": 0,
"end": 37
}
}
],
"tests": [],
"eoi": null,
"span": {
"input": "function main() {\n return 1 + 1\n}\n",