format string and grammar changes

This commit is contained in:
gluaxspeed 2021-07-20 14:52:35 -07:00
parent 9facd3e984
commit a9593971b9
34 changed files with 328 additions and 286 deletions

View File

@ -92,7 +92,7 @@ impl<'a> MonoidalReducerStatement<'a, BoolAnd> for ReturnPathReducer {
if_true.append(if_false.unwrap_or(BoolAnd(false)))
}
fn reduce_formatted_string(&mut self, input: &FormatString, parameters: Vec<BoolAnd>) -> BoolAnd {
fn reduce_formatted_string(&mut self, input: &ConsoleArgs, parameters: Vec<BoolAnd>) -> BoolAnd {
BoolAnd(false)
}

View File

@ -109,13 +109,29 @@ pub enum CharValue {
NonScalar(u32),
}
impl From<&leo_ast::Char> for CharValue {
fn from(other: &leo_ast::Char) -> Self {
use leo_ast::Char::*;
match other {
Scalar(value) => CharValue::Scalar(*value),
NonScalar(value) => CharValue::NonScalar(*value),
}
}
}
impl Into<leo_ast::Char> for &CharValue {
fn into(self) -> leo_ast::Char {
use leo_ast::Char::*;
match self {
CharValue::Scalar(value) => Scalar(*value),
CharValue::NonScalar(value) => NonScalar(*value),
}
}
}
impl From<leo_ast::CharValue> for CharValue {
fn from(other: leo_ast::CharValue) -> Self {
use leo_ast::Char::*;
match other.character {
Scalar(value) => CharValue::Scalar(value),
NonScalar(value) => CharValue::NonScalar(value),
}
Self::from(&other.character)
}
}

View File

@ -225,7 +225,7 @@ impl<'a, T: Monoid, R: MonoidalReducerStatement<'a, T>> MonoidalDirector<'a, T,
.reduce_conditional_statement(input, condition, if_true, if_false)
}
pub fn reduce_formatted_string(&mut self, input: &FormatString<'a>) -> T {
pub fn reduce_formatted_string(&mut self, input: &ConsoleArgs<'a>) -> T {
let parameters = input
.parameters
.iter()

View File

@ -118,7 +118,7 @@ pub trait MonoidalReducerStatement<'a, T: Monoid>: MonoidalReducerExpression<'a,
condition.append(if_true).append_option(if_false)
}
fn reduce_formatted_string(&mut self, input: &FormatString<'a>, parameters: Vec<T>) -> T {
fn reduce_formatted_string(&mut self, input: &ConsoleArgs<'a>, parameters: Vec<T>) -> T {
T::default().append_all(parameters.into_iter())
}

View File

@ -243,7 +243,7 @@ impl<'a, R: ReconstructingReducerStatement<'a>> ReconstructingDirector<'a, R> {
.reduce_conditional_statement(input, condition, if_true, if_false)
}
pub fn reduce_formatted_string(&mut self, input: FormatString<'a>) -> FormatString<'a> {
pub fn reduce_formatted_string(&mut self, input: ConsoleArgs<'a>) -> ConsoleArgs<'a> {
let parameters = input
.parameters
.iter()

View File

@ -274,12 +274,12 @@ pub trait ReconstructingReducerStatement<'a>: ReconstructingReducerExpression<'a
fn reduce_formatted_string(
&mut self,
input: FormatString<'a>,
input: ConsoleArgs<'a>,
parameters: Vec<&'a Expression<'a>>,
) -> FormatString<'a> {
FormatString {
) -> ConsoleArgs<'a> {
ConsoleArgs {
span: input.span,
parts: input.parts,
string: input.string,
parameters: parameters.into_iter().map(Cell::new).collect(),
}
}
@ -293,7 +293,7 @@ pub trait ReconstructingReducerStatement<'a>: ReconstructingReducerExpression<'a
})
}
fn reduce_console_log(&mut self, input: ConsoleStatement<'a>, argument: FormatString<'a>) -> Statement<'a> {
fn reduce_console_log(&mut self, input: ConsoleStatement<'a>, argument: ConsoleArgs<'a>) -> Statement<'a> {
assert!(!matches!(input.function, ConsoleFunction::Assert(_)));
Statement::Console(ConsoleStatement {
parent: input.parent,

View File

@ -120,7 +120,7 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
Default::default()
}
fn visit_formatted_string(&mut self, input: &FormatString<'a>) -> VisitResult {
fn visit_formatted_string(&mut self, input: &ConsoleArgs<'a>) -> VisitResult {
Default::default()
}

View File

@ -319,7 +319,7 @@ impl<'a, R: StatementVisitor<'a>> VisitorDirector<'a, R> {
}
}
pub fn visit_formatted_string(&mut self, input: &FormatString<'a>) -> ConcreteVisitResult {
pub fn visit_formatted_string(&mut self, input: &ConsoleArgs<'a>) -> ConcreteVisitResult {
match self.visitor.visit_formatted_string(input) {
VisitResult::VisitChildren => {
for parameter in input.parameters.iter() {

View File

@ -14,15 +14,15 @@
// 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 crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
use leo_ast::{ConsoleFunction as AstConsoleFunction, FormatStringPart};
use crate::{AsgConvertError, CharValue, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
use leo_ast::ConsoleFunction as AstConsoleFunction;
use std::cell::Cell;
// TODO (protryon): Refactor to not require/depend on span
#[derive(Clone)]
pub struct FormatString<'a> {
pub parts: Vec<FormatStringPart>,
pub struct ConsoleArgs<'a> {
pub string: Vec<CharValue>,
pub parameters: Vec<Cell<&'a Expression<'a>>>,
pub span: Span,
}
@ -30,9 +30,9 @@ pub struct FormatString<'a> {
#[derive(Clone)]
pub enum ConsoleFunction<'a> {
Assert(Cell<&'a Expression<'a>>),
Debug(FormatString<'a>),
Error(FormatString<'a>),
Log(FormatString<'a>),
Debug(ConsoleArgs<'a>),
Error(ConsoleArgs<'a>),
Log(ConsoleArgs<'a>),
}
#[derive(Clone)]
@ -48,41 +48,42 @@ impl<'a> Node for ConsoleStatement<'a> {
}
}
impl<'a> FromAst<'a, leo_ast::FormatString> for FormatString<'a> {
impl<'a> FromAst<'a, leo_ast::ConsoleArgs> for ConsoleArgs<'a> {
fn from_ast(
scope: &'a Scope<'a>,
value: &leo_ast::FormatString,
value: &leo_ast::ConsoleArgs,
_expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> {
let expected_param_len = value
.parts
.iter()
.filter(|x| matches!(x, FormatStringPart::Container))
.count();
if value.parameters.len() != expected_param_len {
// + 1 for formatting string as to not confuse user
return Err(AsgConvertError::unexpected_call_argument_count(
expected_param_len + 1,
value.parameters.len() + 1,
&value.span,
));
}
// let expected_param_len = value
// .parts
// .iter()
// .filter(|x| matches!(x, FormatStringPart::Container))
// .count();
// if value.parameters.len() != expected_param_len {
// + 1 for formatting string as to not confuse user
// return Err(AsgConvertError::unexpected_call_argument_count(
// expected_param_len + 1,
// value.parameters.len() + 1,
// &value.span,
// ));
// }
let mut parameters = vec![];
for parameter in value.parameters.iter() {
parameters.push(Cell::new(<&Expression<'a>>::from_ast(scope, parameter, None)?));
}
Ok(FormatString {
parts: value.parts.clone(),
Ok(ConsoleArgs {
string: value.string.iter().map(CharValue::from).collect::<Vec<_>>(),
parameters,
span: value.span.clone(),
})
}
}
impl<'a> Into<leo_ast::FormatString> for &FormatString<'a> {
fn into(self) -> leo_ast::FormatString {
leo_ast::FormatString {
parts: self.parts.clone(),
impl<'a> Into<leo_ast::ConsoleArgs> for &ConsoleArgs<'a> {
fn into(self) -> leo_ast::ConsoleArgs {
leo_ast::ConsoleArgs {
string: self.string.iter().map(|c| c.into()).collect::<Vec<_>>(),
parameters: self.parameters.iter().map(|e| e.get().into()).collect(),
span: self.span.clone(),
}
@ -102,15 +103,9 @@ impl<'a> FromAst<'a, leo_ast::ConsoleStatement> for ConsoleStatement<'a> {
AstConsoleFunction::Assert(expression) => ConsoleFunction::Assert(Cell::new(
<&Expression<'a>>::from_ast(scope, expression, Some(Type::Boolean.into()))?,
)),
AstConsoleFunction::Debug(formatted_string) => {
ConsoleFunction::Debug(FormatString::from_ast(scope, formatted_string, None)?)
}
AstConsoleFunction::Error(formatted_string) => {
ConsoleFunction::Error(FormatString::from_ast(scope, formatted_string, None)?)
}
AstConsoleFunction::Log(formatted_string) => {
ConsoleFunction::Log(FormatString::from_ast(scope, formatted_string, None)?)
}
AstConsoleFunction::Debug(args) => ConsoleFunction::Debug(ConsoleArgs::from_ast(scope, args, None)?),
AstConsoleFunction::Error(args) => ConsoleFunction::Error(ConsoleArgs::from_ast(scope, args, None)?),
AstConsoleFunction::Log(args) => ConsoleFunction::Log(ConsoleArgs::from_ast(scope, args, None)?),
},
})
}
@ -122,9 +117,9 @@ impl<'a> Into<leo_ast::ConsoleStatement> for &ConsoleStatement<'a> {
leo_ast::ConsoleStatement {
function: match &self.function {
Assert(e) => AstConsoleFunction::Assert(e.get().into()),
Debug(formatted_string) => AstConsoleFunction::Debug(formatted_string.into()),
Error(formatted_string) => AstConsoleFunction::Error(formatted_string.into()),
Log(formatted_string) => AstConsoleFunction::Log(formatted_string.into()),
Debug(args) => AstConsoleFunction::Debug(args.into()),
Error(args) => AstConsoleFunction::Error(args.into()),
Log(args) => AstConsoleFunction::Log(args.into()),
},
span: self.span.clone().unwrap_or_default(),
}

View File

@ -1,3 +0,0 @@
function main() {
console.log("{}");
}

View File

@ -1,3 +0,0 @@
function main() {
console.log("", 1u32);
}

View File

@ -27,15 +27,3 @@ fn test_log_parameter_fail_unknown() {
let program_string = include_str!("log_parameter_fail_unknown.leo");
load_asg(program_string).err().unwrap();
}
#[test]
fn test_log_parameter_fail_empty() {
let program_string = include_str!("log_parameter_fail_empty.leo");
load_asg(program_string).err().unwrap();
}
#[test]
fn test_log_parameter_fail_none() {
let program_string = include_str!("log_parameter_fail_empty.leo");
load_asg(program_string).err().unwrap();
}

View File

@ -35,6 +35,13 @@ impl ReducerError {
ReducerError::Error(FormattedError::new_from_span(message, span))
}
pub fn empty_string(span: &Span) -> Self {
let message =
"Cannot constrcut an empty string: it has the type of [char; 0] which is not possible.".to_string();
Self::new_from_span(message, span)
}
pub fn impossible_console_assert_call(span: &Span) -> Self {
let message = "Console::Assert cannot be matched here, its handled in another case.".to_string();

View File

@ -391,23 +391,23 @@ impl Canonicalizer {
ConsoleFunction::Assert(expression) => {
ConsoleFunction::Assert(self.canonicalize_expression(expression))
}
ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => {
let parameters = format
ConsoleFunction::Debug(args) | ConsoleFunction::Error(args) | ConsoleFunction::Log(args) => {
let parameters = args
.parameters
.iter()
.map(|parameter| self.canonicalize_expression(parameter))
.collect();
let formatted = FormatString {
parts: format.parts.clone(),
let console_args = ConsoleArgs {
string: args.string.clone(),
parameters,
span: format.span.clone(),
span: args.span.clone(),
};
match &console_function_call.function {
ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted),
ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted),
ConsoleFunction::Log(_) => ConsoleFunction::Log(formatted),
ConsoleFunction::Debug(_) => ConsoleFunction::Debug(console_args),
ConsoleFunction::Error(_) => ConsoleFunction::Error(console_args),
ConsoleFunction::Log(_) => ConsoleFunction::Log(console_args),
_ => unimplemented!(), // impossible
}
}
@ -493,6 +493,10 @@ impl ReconstructingReducer for Canonicalizer {
}
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, ReducerError> {
if string.is_empty() {
return Err(ReducerError::empty_string(span));
}
let mut elements = Vec::new();
let mut col_adder = 0;
for (index, character) in string.iter().enumerate() {

View File

@ -390,23 +390,23 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
) -> Result<ConsoleStatement, ReducerError> {
let function = match &console_function_call.function {
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)?),
ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => {
ConsoleFunction::Debug(args) | ConsoleFunction::Error(args) | ConsoleFunction::Log(args) => {
let mut parameters = vec![];
for parameter in format.parameters.iter() {
for parameter in args.parameters.iter() {
parameters.push(self.reduce_expression(parameter)?);
}
let formatted = FormatString {
parts: format.parts.clone(),
let formatted = ConsoleArgs {
string: args.string.clone(),
parameters,
span: format.span.clone(),
span: args.span.clone(),
};
match &console_function_call.function {
ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted),
ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted),
ConsoleFunction::Log(_) => ConsoleFunction::Log(formatted),
_ => return Err(ReducerError::impossible_console_assert_call(&format.span)),
_ => return Err(ReducerError::impossible_console_assert_call(&args.span)),
}
}
};

View File

@ -0,0 +1,96 @@
// 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 crate::{Char, Expression, Node, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
// use tendril::StrTendril;
// #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
// pub enum FormatStringPart {
// Const(#[serde(with = "crate::common::tendril_json")] StrTendril),
// Container,
// }
// impl FormatStringPart {
// pub fn from_string(string: Vec<Char>) -> Vec<Self> {
// let mut parts = Vec::new();
// let mut in_container = false;
// let mut substring = String::new();
// for (_, character) in string.iter().enumerate() {
// match character {
// Char::Scalar(scalar) => match scalar {
// '{' if !in_container => {
// parts.push(FormatStringPart::Const(substring.clone().into()));
// substring.clear();
// in_container = true;
// }
// '}' if in_container => {
// in_container = false;
// parts.push(FormatStringPart::Container);
// }
// _ if in_container => {
// in_container = false;
// }
// _ => substring.push(*scalar),
// },
// Char::NonScalar(non_scalar) => {
// substring.push_str(format!("\\u{{{:x}}}", non_scalar).as_str());
// in_container = false;
// }
// }
// }
// if !substring.is_empty() {
// parts.push(FormatStringPart::Const(substring.into()));
// }
// parts
// }
// }
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct ConsoleArgs {
pub string: Vec<Char>,
pub parameters: Vec<Expression>,
pub span: Span,
}
impl fmt::Display for ConsoleArgs {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"\"{}\", {}",
self.string.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(""),
self.parameters
.iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(",")
)
}
}
impl Node for ConsoleArgs {
fn span(&self) -> &Span {
&self.span
}
fn set_span(&mut self, span: Span) {
self.span = span;
}
}

View File

@ -14,7 +14,7 @@
// 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 crate::{Expression, FormatString, Node, Span};
use crate::{ConsoleArgs, Expression, Node, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
@ -22,9 +22,9 @@ use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum ConsoleFunction {
Assert(Expression),
Debug(FormatString),
Error(FormatString),
Log(FormatString),
Debug(ConsoleArgs),
Error(ConsoleArgs),
Log(ConsoleArgs),
}
impl fmt::Display for ConsoleFunction {

View File

@ -1,103 +0,0 @@
// 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 crate::{Char, Expression, Node, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
use tendril::StrTendril;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum FormatStringPart {
Const(#[serde(with = "crate::common::tendril_json")] StrTendril),
Container,
}
impl FormatStringPart {
pub fn from_string(string: Vec<Char>) -> Vec<Self> {
let mut parts = Vec::new();
let mut in_container = false;
let mut substring = String::new();
for (_, character) in string.iter().enumerate() {
match character {
Char::Scalar(scalar) => match scalar {
'{' if !in_container => {
parts.push(FormatStringPart::Const(substring.clone().into()));
substring.clear();
in_container = true;
}
'}' if in_container => {
in_container = false;
parts.push(FormatStringPart::Container);
}
_ if in_container => {
in_container = false;
}
_ => substring.push(*scalar),
},
Char::NonScalar(non_scalar) => {
substring.push_str(format!("\\u{{{:x}}}", non_scalar).as_str());
in_container = false;
}
}
}
if !substring.is_empty() {
parts.push(FormatStringPart::Const(substring.into()));
}
parts
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct FormatString {
pub parts: Vec<FormatStringPart>,
pub parameters: Vec<Expression>,
pub span: Span,
}
impl fmt::Display for FormatString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"\"{}\", {}",
self.parts
.iter()
.map(|x| match x {
FormatStringPart::Const(x) => x.to_string(),
FormatStringPart::Container => "{}".to_string(),
})
.collect::<Vec<_>>()
.join(""),
self.parameters
.iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(",")
)
}
}
impl Node for FormatString {
fn span(&self) -> &Span {
&self.span
}
fn set_span(&mut self, span: Span) {
self.span = span;
}
}

View File

@ -17,11 +17,8 @@
pub mod console_function;
pub use console_function::*;
pub mod console_args;
pub use console_args::*;
pub mod console_statement;
pub use console_statement::*;
pub mod formatted_container;
pub use formatted_container::*;
pub mod formatted_string;
pub use formatted_string::*;

View File

@ -17,8 +17,7 @@
//! Evaluates a formatted string in a compiled Leo program.
use crate::{errors::ConsoleError, program::ConstrainedProgram, GroupType};
use leo_asg::FormatString;
use leo_ast::FormatStringPart;
use leo_asg::{CharValue, ConsoleArgs};
use snarkvm_fields::PrimeField;
use snarkvm_r1cs::ConstraintSystem;
@ -26,35 +25,65 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
pub fn format<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
formatted: &FormatString<'a>,
args: &ConsoleArgs<'a>,
) -> Result<String, ConsoleError> {
// Check that containers and parameters match
let container_count = formatted
.parts
.iter()
.filter(|x| matches!(x, FormatStringPart::Container))
.count();
if container_count != formatted.parameters.len() {
return Err(ConsoleError::length(
container_count,
formatted.parameters.len(),
&formatted.span,
));
}
let mut executed_containers = Vec::with_capacity(formatted.parameters.len());
for parameter in formatted.parameters.iter() {
executed_containers.push(self.enforce_expression(cs, parameter.get())?.to_string());
}
let mut out = vec![];
let mut parameters = executed_containers.iter();
for part in formatted.parts.iter() {
match part {
FormatStringPart::Const(c) => out.push(c.to_string()),
FormatStringPart::Container => out.push(parameters.next().unwrap().to_string()),
let mut out = Vec::new();
let mut in_container = false;
let mut substring = String::new();
let mut arg_index = 0;
let mut escape_right_bracket = false;
for (index, character) in args.string.iter().enumerate() {
match character {
_ if escape_right_bracket => {
escape_right_bracket = false;
continue;
}
CharValue::Scalar(scalar) => match scalar {
'{' if !in_container => {
out.push(substring.clone());
substring.clear();
in_container = true;
}
'{' if in_container => {
substring.push('{');
in_container = false;
}
'}' if in_container => {
in_container = false;
let parameter = match args.parameters.get(arg_index) {
Some(index) => index,
None => return Err(ConsoleError::length(arg_index + 1, args.parameters.len(), &args.span)),
};
out.push(self.enforce_expression(cs, parameter.get())?.to_string());
arg_index += 1;
}
'}' if !in_container => {
if let Some(CharValue::Scalar(next)) = args.string.get(index + 1) {
if *next == '}' {
substring.push('}');
escape_right_bracket = true;
} else {
return Err(ConsoleError::expected_escaped_right_brace(&args.span));
}
}
}
_ if in_container => {
return Err(ConsoleError::expected_left_or_right_brace(&args.span));
}
_ => substring.push(*scalar),
},
CharValue::NonScalar(non_scalar) => {
substring.push_str(format!("\\u{{{:x}}}", non_scalar).as_str());
in_container = false;
}
}
}
out.push(substring);
// Check that containers and parameters match
if arg_index != args.parameters.len() {
return Err(ConsoleError::length(arg_index, args.parameters.len(), &args.span));
}
Ok(out.join(""))
}

View File

@ -33,6 +33,18 @@ impl ConsoleError {
ConsoleError::Error(FormattedError::new_from_span(message, span))
}
pub fn expected_left_or_right_brace(span: &Span) -> Self {
let message = "Formatter given a {. Expected a { or } after".to_string();
Self::new_from_span(message, span)
}
pub fn expected_escaped_right_brace(span: &Span) -> Self {
let message = "Formatter given a }. Expected a container {} or }}".to_string();
Self::new_from_span(message, span)
}
pub fn length(containers: usize, parameters: usize, span: &Span) -> Self {
let message = format!(
"Formatter given {} containers and found {} parameters",

View File

@ -76,12 +76,12 @@ use leo_ast::{
CircuitStaticFunctionAccessExpression,
CombinerError,
ConditionalStatement as AstConditionalStatement,
ConsoleArgs as AstConsoleArgs,
ConsoleFunction as AstConsoleFunction,
ConsoleStatement as AstConsoleStatement,
DefinitionStatement as AstDefinitionStatement,
Expression as AstExpression,
ExpressionStatement as AstExpressionStatement,
FormatString,
Function as AstFunction,
GroupTuple,
GroupValue as AstGroupValue,
@ -597,25 +597,27 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
(AstConsoleFunction::Assert(ast_expression), AsgConsoleFunction::Assert(asg_expression)) => {
AstConsoleFunction::Assert(self.reduce_expression(&ast_expression, asg_expression.get())?)
}
(AstConsoleFunction::Debug(ast_format), AsgConsoleFunction::Debug(asg_format))
| (AstConsoleFunction::Error(ast_format), AsgConsoleFunction::Error(asg_format))
| (AstConsoleFunction::Log(ast_format), AsgConsoleFunction::Log(asg_format)) => {
(AstConsoleFunction::Debug(ast_console_args), AsgConsoleFunction::Debug(asg_format))
| (AstConsoleFunction::Error(ast_console_args), AsgConsoleFunction::Error(asg_format))
| (AstConsoleFunction::Log(ast_console_args), AsgConsoleFunction::Log(asg_format)) => {
let mut parameters = vec![];
for (ast_parameter, asg_parameter) in ast_format.parameters.iter().zip(asg_format.parameters.iter()) {
for (ast_parameter, asg_parameter) in
ast_console_args.parameters.iter().zip(asg_format.parameters.iter())
{
parameters.push(self.reduce_expression(&ast_parameter, asg_parameter.get())?);
}
let formatted = FormatString {
parts: ast_format.parts.clone(),
let args = AstConsoleArgs {
string: ast_console_args.string.clone(),
parameters,
span: ast_format.span.clone(),
span: ast_console_args.span.clone(),
};
match &ast.function {
AstConsoleFunction::Debug(_) => AstConsoleFunction::Debug(formatted),
AstConsoleFunction::Error(_) => AstConsoleFunction::Error(formatted),
AstConsoleFunction::Log(_) => AstConsoleFunction::Log(formatted),
_ => return Err(ReducerError::impossible_console_assert_call(&ast_format.span)),
AstConsoleFunction::Debug(_) => AstConsoleFunction::Debug(args),
AstConsoleFunction::Error(_) => AstConsoleFunction::Error(args),
AstConsoleFunction::Log(_) => AstConsoleFunction::Log(args),
_ => return Err(ReducerError::impossible_console_assert_call(&ast_console_args.span)),
}
}
_ => ast.function.clone(),

Binary file not shown.

View File

@ -559,7 +559,7 @@ unicode-character-escape = %s"\u{" 1*6hexadecimal-digit "}"
; because string literals denote character arrays,
; and arrays must not be empty.
string-literal = double-quote 1*string-literal-element double-quote
string-literal = double-quote *string-literal-element double-quote
string-literal-element = not-double-quote-or-backslash
/ simple-character-escape

5
parser/README.md Normal file
View File

@ -0,0 +1,5 @@
# leo-parser
[![Crates.io](https://img.shields.io/crates/v/leo-parser.svg?color=neon)](https://crates.io/crates/leo-parser)
[![Authors](https://img.shields.io/badge/authors-Aleo-orange.svg)](../AUTHORS)
[![License](https://img.shields.io/badge/License-GPLv3-blue.svg)](./LICENSE.md)

View File

@ -232,9 +232,9 @@ impl ParserContext {
}
///
/// Returns a [`FormatString`] AST node if the next tokens represent a formatted string.
/// Returns a [`ConsoleArgs`] AST node if the next tokens represent a formatted string.
///
pub fn parse_formatted_string(&mut self) -> SyntaxResult<FormatString> {
pub fn parse_console_args(&mut self) -> SyntaxResult<ConsoleArgs> {
let start_span;
let string = match self.expect_any()? {
SpannedToken {
@ -247,7 +247,7 @@ impl ParserContext {
SpannedToken { token, span } => return Err(SyntaxError::unexpected_str(&token, "formatted string", &span)),
};
let parts = FormatStringPart::from_string(string);
// let parts = FormatStringPart::from_string(string);
let mut parameters = Vec::new();
while self.eat(Token::Comma).is_some() {
@ -255,8 +255,8 @@ impl ParserContext {
parameters.push(param);
}
Ok(FormatString {
parts,
Ok(ConsoleArgs {
string,
span: &start_span + parameters.last().map(|x| x.span()).unwrap_or(&start_span),
parameters,
})
@ -275,9 +275,9 @@ impl ParserContext {
let expr = self.parse_expression()?;
ConsoleFunction::Assert(expr)
}
"debug" => ConsoleFunction::Debug(self.parse_formatted_string()?),
"error" => ConsoleFunction::Error(self.parse_formatted_string()?),
"log" => ConsoleFunction::Log(self.parse_formatted_string()?),
"debug" => ConsoleFunction::Debug(self.parse_console_args()?),
"error" => ConsoleFunction::Error(self.parse_console_args()?),
"log" => ConsoleFunction::Log(self.parse_console_args()?),
x => {
return Err(SyntaxError::unexpected_ident(
&x,

View File

@ -280,7 +280,7 @@ impl Token {
}
}
if i == input.len() || i == 1 || !end {
if i == input.len() || !end {
return (0, None);
}

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- " --> compiler-test:4:17\n |\n 4 | console.log(\"{}\");\n | ^^^^\n |\n = function call expected 2 arguments, got 1"
- " --> compiler-test:4:17\n |\n 4 | console.log(\"{}\");\n | ^^^^\n |\n = Formatter given 1 containers and found 0 parameters"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- " --> compiler-test:4:17\n |\n 4 | console.log(\"\", 1u32);\n | ^\n |\n = unexpected token: '\"'"
- " --> compiler-test:4:17\n |\n 4 | console.log(\"\", 1u32);\n | ^^^^^^^^\n |\n = Formatter given 0 containers and found 1 parameters"

View File

@ -2,6 +2,7 @@
namespace: Token
expectation: Pass
outputs:
- "'\"\"' @ 1:1-3"
- "'\"string\"' @ 1:1-9"
- "'\"another { } string\"' @ 1:1-21"
- "'\"{ ] [ ; a\"' @ 1:1-12"

View File

@ -2,7 +2,6 @@
namespace: Token
expectation: Fail
outputs:
- " --> test:1:1\n |\n 1 | \"\"\n | ^\n |\n = unexpected token: '\"'"
- " --> test:1:1\n |\n 1 | \"Hello world!\n | ^\n |\n = unexpected token: '\"'"
- " --> test:1:1\n |\n 1 | \"\\\"\n | ^\n |\n = unexpected token: '\"'"
- " --> test:1:1\n |\n 1 | \"\\l\"\n | ^\n |\n = unexpected token: '\"'"

View File

@ -16,9 +16,9 @@ outputs:
- Console:
function:
Error:
parts:
- Const: ""
- Container
string:
- Scalar: 123
- Scalar: 125
parameters:
- Identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":21,\\\"col_stop\\\":22,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.error(\\\\\\\"{}\\\\\\\", x);\\\"}\"}"
span:
@ -38,11 +38,11 @@ outputs:
- Console:
function:
Error:
parts:
- Const: ""
- Container
- Const: ""
- Container
string:
- Scalar: 123
- Scalar: 125
- Scalar: 123
- Scalar: 125
parameters:
- Identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":23,\\\"col_stop\\\":24,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.error(\\\\\\\"{}{}\\\\\\\", x, y);\\\"}\"}"
- Identifier: "{\"name\":\"y\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":26,\\\"col_stop\\\":27,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.error(\\\\\\\"{}{}\\\\\\\", x, y);\\\"}\"}"
@ -63,8 +63,8 @@ outputs:
- Console:
function:
Error:
parts:
- Const: x
string:
- Scalar: 120
parameters: []
span:
line_start: 1
@ -83,9 +83,9 @@ outputs:
- Console:
function:
Debug:
parts:
- Const: ""
- Container
string:
- Scalar: 123
- Scalar: 125
parameters:
- Identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":21,\\\"col_stop\\\":22,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.debug(\\\\\\\"{}\\\\\\\", x);\\\"}\"}"
span:
@ -105,11 +105,11 @@ outputs:
- Console:
function:
Debug:
parts:
- Const: ""
- Container
- Const: ""
- Container
string:
- Scalar: 123
- Scalar: 125
- Scalar: 123
- Scalar: 125
parameters:
- Identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":23,\\\"col_stop\\\":24,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.debug(\\\\\\\"{}{}\\\\\\\", x, y);\\\"}\"}"
- Identifier: "{\"name\":\"y\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":26,\\\"col_stop\\\":27,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.debug(\\\\\\\"{}{}\\\\\\\", x, y);\\\"}\"}"
@ -130,8 +130,8 @@ outputs:
- Console:
function:
Debug:
parts:
- Const: x
string:
- Scalar: 120
parameters: []
span:
line_start: 1
@ -150,9 +150,9 @@ outputs:
- Console:
function:
Log:
parts:
- Const: ""
- Container
string:
- Scalar: 123
- Scalar: 125
parameters:
- Identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":19,\\\"col_stop\\\":20,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.log(\\\\\\\"{}\\\\\\\", x);\\\"}\"}"
span:
@ -172,11 +172,11 @@ outputs:
- Console:
function:
Log:
parts:
- Const: ""
- Container
- Const: ""
- Container
string:
- Scalar: 123
- Scalar: 125
- Scalar: 123
- Scalar: 125
parameters:
- Identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":21,\\\"col_stop\\\":22,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.log(\\\\\\\"{}{}\\\\\\\", x, y);\\\"}\"}"
- Identifier: "{\"name\":\"y\",\"span\":\"{\\\"line_start\\\":1,\\\"line_stop\\\":1,\\\"col_start\\\":24,\\\"col_stop\\\":25,\\\"path\\\":\\\"test\\\",\\\"content\\\":\\\"console.log(\\\\\\\"{}{}\\\\\\\", x, y);\\\"}\"}"
@ -197,8 +197,8 @@ outputs:
- Console:
function:
Log:
parts:
- Const: x
string:
- Scalar: 120
parameters: []
span:
line_start: 1

View File

@ -3,6 +3,8 @@ namespace: Token
expectation: Pass
*/
""
"string"
"another { } string"

View File

@ -3,8 +3,6 @@ namespace: Token
expectation: Fail
*/
""
"Hello world!
"\"