add group number syntax to pest ast

This commit is contained in:
collin 2020-08-18 12:46:29 -07:00
parent d3571a8e01
commit b34b5f4a03
2 changed files with 25 additions and 3 deletions

View File

@ -234,7 +234,8 @@ value_boolean = { "true" | "false" }
value_field = ${ value_number ~ type_field }
// Declared in values/group_value.rs
value_group = ${ group_tuple ~ type_group }
value_group = ${ group_single_or_tuple ~ type_group }
group_single_or_tuple = {value_number | group_tuple}
group_tuple = !{"(" ~ group_coordinate ~ "," ~ group_coordinate ~ ")"}
// Declared in values/group_coordinate.rs

View File

@ -14,7 +14,12 @@
// 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::{ast::Rule, types::GroupType, values::GroupCoordinate, SpanDef};
use crate::{
ast::Rule,
types::GroupType,
values::{GroupCoordinate, NumberValue},
SpanDef,
};
use pest::Span;
use pest_ast::FromPest;
@ -24,7 +29,7 @@ use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::value_group))]
pub struct GroupValue<'ast> {
pub value: GroupTuple<'ast>,
pub value: GroupRepresentation<'ast>,
pub type_: GroupType,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
@ -37,6 +42,22 @@ impl<'ast> fmt::Display for GroupValue<'ast> {
}
}
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::group_single_or_tuple))]
pub enum GroupRepresentation<'ast> {
Single(NumberValue<'ast>),
Tuple(GroupTuple<'ast>),
}
impl<'ast> fmt::Display for GroupRepresentation<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GroupRepresentation::Single(number) => write!(f, "{}", number),
GroupRepresentation::Tuple(tuple) => write!(f, "{}", tuple),
}
}
}
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::group_tuple))]
pub struct GroupTuple<'ast> {