impl console functions in pest ast

This commit is contained in:
collin 2020-08-16 13:22:18 -07:00
parent a704eddb51
commit 34d09df92e
21 changed files with 176 additions and 215 deletions

View File

@ -0,0 +1,21 @@
use crate::{ast::Rule, expressions::Expression, SpanDef};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::console_assert))]
pub struct ConsoleAssert<'ast> {
pub expression: Expression<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for ConsoleAssert<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "assert({})", self.expression)
}
}

View File

@ -1,4 +1,4 @@
use crate::{ast::Rule, SpanDef};
use crate::{ast::Rule, console::FormattedString, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -6,15 +6,16 @@ use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::error))]
pub struct Error<'ast> {
#[pest_ast(rule(Rule::console_debug))]
pub struct ConsoleDebug<'ast> {
pub string: FormattedString<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Error<'ast> {
impl<'ast> fmt::Display for ConsoleDebug<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error")
write!(f, "debug({})", self.string)
}
}

View File

@ -1,4 +1,4 @@
use crate::{ast::Rule, SpanDef};
use crate::{ast::Rule, console::FormattedString, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -6,15 +6,16 @@ use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::print))]
pub struct Print<'ast> {
#[pest_ast(rule(Rule::console_error))]
pub struct ConsoleError<'ast> {
pub string: FormattedString<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Print<'ast> {
impl<'ast> fmt::Display for ConsoleError<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "print")
write!(f, "error({})", self.string)
}
}

View File

@ -0,0 +1,28 @@
use crate::{
ast::Rule,
console::{ConsoleAssert, ConsoleDebug, ConsoleError, ConsolePrint},
};
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::console_function))]
pub enum ConsoleFunction<'ast> {
Assert(ConsoleAssert<'ast>),
Debug(ConsoleDebug<'ast>),
Error(ConsoleError<'ast>),
Print(ConsolePrint<'ast>),
}
impl<'ast> fmt::Display for ConsoleFunction<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ConsoleFunction::Assert(assert) => write!(f, "{}", assert),
ConsoleFunction::Debug(debug) => write!(f, "{}", debug),
ConsoleFunction::Error(error) => write!(f, "{}", error),
ConsoleFunction::Print(print) => write!(f, "{}", print),
}
}
}

View File

@ -0,0 +1,28 @@
use crate::{
ast::Rule,
common::LineEnd,
console::{ConsoleFunction, ConsoleKeyword},
SpanDef,
};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::console_function_call))]
pub struct ConsoleFunctionCall<'ast> {
pub keyword: ConsoleKeyword<'ast>,
pub function: ConsoleFunction<'ast>,
pub line_end: LineEnd,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for ConsoleFunctionCall<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "console.{};", self.function)
}
}

View File

@ -3,18 +3,11 @@ use crate::{ast::Rule, SpanDef};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::debug))]
pub struct Debug<'ast> {
#[pest_ast(rule(Rule::console_keyword))]
pub struct ConsoleKeyword<'ast> {
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Debug<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "debug")
}
}

View File

@ -0,0 +1,21 @@
use crate::{ast::Rule, console::FormattedString, SpanDef};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::console_print))]
pub struct ConsolePrint<'ast> {
pub string: FormattedString<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for ConsolePrint<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "print({})", self.string)
}
}

View File

@ -1,6 +1,6 @@
use crate::{
ast::{span_into_string, Rule},
macros::{FormattedContainer, FormattedParameter},
console::{FormattedContainer, FormattedParameter},
SpanDef,
};

29
ast/src/console/mod.rs Normal file
View File

@ -0,0 +1,29 @@
pub mod console_assert;
pub use console_assert::*;
pub mod console_debug;
pub use console_debug::*;
pub mod console_error;
pub use console_error::*;
pub mod console_function;
pub use console_function::*;
pub mod console_function_call;
pub use console_function_call::*;
pub mod console_keyword;
pub use console_keyword::*;
pub mod console_print;
pub use console_print::*;
pub mod formatted_container;
pub use formatted_container::*;
pub mod formatted_parameter;
pub use formatted_parameter::*;
pub mod formatted_string;
pub use formatted_string::*;

View File

@ -330,18 +330,13 @@ statement = {
(statement_return
| statement_conditional
| statement_for
| statement_macro
| console_function_call
| statement_definition
| statement_assign
| statement_expression
) ~ NEWLINE*
}
// Declared in statements/macro_statement.rs
statement_macro = {
assert_eq
| formatted_macro
}
// Declared in statements/assign_statement.rs
statement_assign = { assignee ~ operation_assign ~ expression ~ LINE_END }
@ -416,15 +411,35 @@ import_symbol = { identifier ~ ("as " ~ identifier)? }
COMMENT = _{ ("/*" ~ (!"*/" ~ ANY)* ~ "*/") | ("//" ~ (!NEWLINE ~ ANY)*) }
WHITESPACE = _{ " " | "\t" ~ (NEWLINE)* } // pest implicit whitespace keyword
/// Macros
/// Console Functions
// Declared in macros/assert_eq.rs
assert_eq = {"assert_eq!" ~ "(" ~ NEWLINE* ~ expression ~ "," ~ NEWLINE* ~ expression ~ NEWLINE* ~ ")" ~ LINE_END}
// Declared in console/console_function_call.rs
console_function_call = ${console_keyword ~ "." ~ console_function ~ LINE_END}
// Declared in macros/formatted_macro.rs
formatted_macro = { macro_name ~ macro_symbol ~ "(" ~ formatted_string? ~ ")" ~ LINE_END}
// Declared in console/console_keyword.rs
console_keyword = {"console"}
// Declared in macros/formatted_string.rs
// Declared in console/console_function.rs
console_function = {
console_assert
| console_debug
| console_error
| console_print
}
// Declared in console/console_assert.rs
console_assert = !{"assert(" ~ expression ~ ")"}
// Declared in console/console_debug.rs
console_debug = !{"debug(" ~ formatted_string? ~ ")"}
// Declared in console/console_error.rs
console_error = !{"error(" ~ formatted_string? ~ ")"}
// Declared in console/console_print.rs
console_print = !{"print(" ~ formatted_string? ~ ")"}
// Declared in console/formatted_string.rs
formatted_string = {
"\""
~ (!"\"" ~ (formatted_container | ANY))*
@ -432,28 +447,8 @@ formatted_string = {
~ ("," ~ formatted_parameter)*
}
// Declared in macros/formatted_container.rs
// Declared in console/formatted_container.rs
formatted_container = { "{" ~ "}"}
// Declared in macros/formatted_parameter.rs
formatted_parameter = { expression }
// Declared in macros/macro_symbol.rs
macro_symbol = {"!"}
// Declared in macros/macro_name.rs
macro_name = {
debug
| error
| print
}
// Declared in macros/print.rs
print = {"print"}
// Declared in macros/debug.rs
debug = {"debug"}
// Declared in macros/error.rs
error = {"error"}
// Declared in console/formatted_parameter.rs
formatted_parameter = { expression }

View File

@ -10,12 +10,12 @@ mod ast;
pub mod access;
pub mod circuits;
pub mod common;
pub mod console;
pub mod definitions;
pub mod expressions;
pub mod files;
pub mod functions;
pub mod imports;
pub mod macros;
pub mod operations;
pub mod statements;
pub mod types;

View File

@ -1,23 +0,0 @@
use crate::{ast::Rule, common::LineEnd, expressions::Expression, SpanDef};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::assert_eq))]
pub struct AssertEq<'ast> {
pub left: Expression<'ast>,
pub right: Expression<'ast>,
pub line_end: LineEnd,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for AssertEq<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "assert_eq({}, {});", self.left, self.right)
}
}

View File

@ -1,36 +0,0 @@
use crate::{
ast::Rule,
common::LineEnd,
macros::{FormattedString, MacroName, MacroSymbol},
SpanDef,
};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::formatted_macro))]
pub struct FormattedMacro<'ast> {
pub name: MacroName<'ast>,
pub symbol: MacroSymbol,
pub string: Option<FormattedString<'ast>>,
pub line_end: LineEnd,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for FormattedMacro<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}{}({}){}",
self.name,
self.symbol,
self.string.as_ref().map(|s| s.to_string()).unwrap_or("".to_string()),
self.line_end
)
}
}

View File

@ -1,26 +0,0 @@
use crate::{
ast::Rule,
macros::{Debug, Error, Print},
};
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::macro_name))]
pub enum MacroName<'ast> {
Debug(Debug<'ast>),
Error(Error<'ast>),
Print(Print<'ast>),
}
impl<'ast> fmt::Display for MacroName<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MacroName::Debug(ref debug) => write!(f, "{}", debug),
MacroName::Error(ref error) => write!(f, "{}", error),
MacroName::Print(ref print_line) => write!(f, "{}", print_line),
}
}
}

View File

@ -1,15 +0,0 @@
use crate::ast::Rule;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::macro_symbol))]
pub struct MacroSymbol {}
impl fmt::Display for MacroSymbol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "!")
}
}

View File

@ -1,29 +0,0 @@
pub mod assert_eq;
pub use assert_eq::*;
pub mod debug;
pub use debug::*;
pub mod error;
pub use error::*;
pub mod formatted_container;
pub use formatted_container::*;
pub mod formatted_parameter;
pub use formatted_parameter::*;
pub mod formatted_string;
pub use formatted_string::*;
pub mod formatted_macro;
pub use formatted_macro::*;
pub mod macro_name;
pub use macro_name::*;
pub mod macro_symbol;
pub use macro_symbol::*;
pub mod print;
pub use print::*;

View File

@ -1,24 +0,0 @@
use crate::{
ast::Rule,
macros::{AssertEq, FormattedMacro},
};
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::statement_macro))]
pub enum MacroStatement<'ast> {
AssertEq(AssertEq<'ast>),
Formatted(FormattedMacro<'ast>),
}
impl<'ast> fmt::Display for MacroStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MacroStatement::AssertEq(ref assert) => write!(f, "{}", assert),
MacroStatement::Formatted(ref formatted) => write!(f, "{}", formatted),
}
}
}

View File

@ -1,6 +1,3 @@
pub mod macro_statement;
pub use macro_statement::*;
pub mod assign_statement;
pub use assign_statement::*;

View File

@ -1,4 +1,4 @@
use crate::{ast::Rule, statements::*};
use crate::{ast::Rule, console::ConsoleFunctionCall, statements::*};
use pest_ast::FromPest;
use serde::Serialize;
@ -12,7 +12,7 @@ pub enum Statement<'ast> {
Assign(AssignStatement<'ast>),
Conditional(ConditionalStatement<'ast>),
Iteration(ForStatement<'ast>),
Assert(MacroStatement<'ast>),
Console(ConsoleFunctionCall<'ast>),
Expression(ExpressionStatement<'ast>),
}
@ -24,7 +24,7 @@ impl<'ast> fmt::Display for Statement<'ast> {
Statement::Assign(ref statement) => write!(f, "{}", statement),
Statement::Conditional(ref statement) => write!(f, "{}", statement),
Statement::Iteration(ref statement) => write!(f, "{}", statement),
Statement::Assert(ref statement) => write!(f, "{}", statement),
Statement::Console(ref statement) => write!(f, "{}", statement),
Statement::Expression(ref statement) => write!(f, "{}", statement.expression),
}
}