Skip to content

Commit

Permalink
PrefixLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
density215 committed Dec 7, 2023
1 parent d0591bb commit e229c29
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 7 deletions.
32 changes: 27 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use nom::{
combinator::map,
IResult,
};
use nom::{AsChar, Finish};
use nom::{AsChar, Finish, Parser};

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71.0, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71.0, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71.0)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71.0)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71.0, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71.0, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly, --no-default-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly, --all-features)

unused import: `Parser`

Check warning on line 29 in src/ast.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly, --all-features)

unused import: `Parser`
use serde::{Serialize, Serializer};
use smallvec::SmallVec;

Expand Down Expand Up @@ -1893,7 +1893,7 @@ impl IpAddressLiteral {
terminated(digit1, char('.')),
terminated(digit1, char('.')),
take_while1(|ch: char| ch.is_dec_digit()),
not(char(':')),
not(char(':'))
))),
recognize(tuple((
terminated(hex_digit0, char(':')),
Expand All @@ -1904,7 +1904,7 @@ impl IpAddressLiteral {
opt(terminated(hex_digit0, char(':'))),
opt(terminated(hex_digit0, char(':'))),
hex_digit0,
not(char('.')),
not(char('.'))
))),
)),
)(input)?;
Expand All @@ -1921,6 +1921,25 @@ impl From<&'_ IpAddressLiteral> for ShortString {

//------------ PrefixLiteral -------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PrefixLiteral(pub String);

impl PrefixLiteral {
pub fn parse(input: &str) -> IResult<&str, Self, VerboseError<&str>> {
let (input, prefix_str) = context(
"prefix literal",
recognize(
tuple((
IpAddressLiteral::parse,
PrefixLengthLiteral::parse
)
))
)(input)?;

Ok((input, Self(prefix_str.to_string())))
}
}

//------------ PrefixLengthLiteral -------------------------------------------

/// A prefix length literal is a sequence of digits preceded by a '/'.
Expand Down Expand Up @@ -2267,12 +2286,13 @@ impl LargeCommunityLiteral {
tuple((
opt(alt((tag("AS"), tag("0x")))),
take_while1(|ch: char| ch.is_hex_digit()),
tag(":"),
char(':'),
opt(tag("0x")),
take_while1(|ch: char| ch.is_hex_digit()),
tag(":"),
char(':'),
opt(tag("0x")),
take_while1(|ch: char| ch.is_hex_digit()),
not(char(':'))
)),
)(input)?;

Expand Down Expand Up @@ -2531,6 +2551,7 @@ impl std::fmt::Display for AcceptReject {
#[derive(Clone, Debug)]
pub enum ValueExpr {
StringLiteral(StringLiteral),
PrefixLiteral(PrefixLiteral),
PrefixLengthLiteral(PrefixLengthLiteral),
AsnLiteral(AsnLiteral),
IpAddressLiteral(IpAddressLiteral),
Expand Down Expand Up @@ -2567,6 +2588,7 @@ impl ValueExpr {
StandardCommunityLiteral::parse,
ValueExpr::StandardCommunityLiteral,
),
map(PrefixLiteral::parse, ValueExpr::PrefixLiteral),
map(IpAddressLiteral::parse, ValueExpr::IpAddressLiteral),
map(AsnLiteral::parse, ValueExpr::AsnLiteral),
map(HexLiteral::parse, ValueExpr::HexLiteral),
Expand Down
10 changes: 10 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::types::builtin::BuiltinTypeValue;
use crate::types::builtin::HexLiteral;
use crate::types::builtin::IntegerLiteral;
use crate::types::builtin::IpAddress;
use crate::types::builtin::Prefix;
use crate::types::builtin::PrefixLength;
use crate::types::builtin::StringLiteral;
use crate::types::enum_types::GlobalEnumTypeDef;
Expand Down Expand Up @@ -1708,6 +1709,15 @@ impl ast::ValueExpr {
Token::Constant(None),
))
}
ast::ValueExpr::PrefixLiteral(pfx_lit) => {
Ok(symbols::Symbol::new_with_value(
"prefix_lit".into(),
symbols::SymbolKind::Constant,
Prefix::try_from(pfx_lit)?.into(),
vec![],
Token::Constant(None)
))
}
ast::ValueExpr::IpAddressLiteral(ip_address_lit) => {
Ok(symbols::Symbol::new_with_value(
"ip_address_lit".into(),
Expand Down
11 changes: 10 additions & 1 deletion src/types/builtin/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use routecore::bgp::communities::{
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};

use crate::ast::IpAddressLiteral;
use crate::ast::{IpAddressLiteral, PrefixLiteral};
use crate::attr_change_set::VectorValue;
use crate::compiler::compile::CompileError;
use crate::first_into_vm_err;
Expand Down Expand Up @@ -1389,6 +1389,15 @@ impl From<Prefix> for TypeValue {
}
}

impl TryFrom<&'_ PrefixLiteral> for Prefix {
type Error = CompileError;

fn try_from(value: &PrefixLiteral) -> Result<Self, Self::Error> {
Ok(Prefix(<routecore::addr::Prefix as std::str::FromStr>::from_str(value.0.as_str())
.map_err(|e| CompileError::from(format!("Cannot parse '{:?}' as an IP Address: {}", value, e)))?))
}
}

impl From<routecore::addr::Prefix> for Prefix {
fn from(val: routecore::addr::Prefix) -> Self {
Prefix(val)
Expand Down
3 changes: 3 additions & 0 deletions src/types/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ impl TryFrom<ValueExpr> for ElementTypeValue {
ValueExpr::IntegerLiteral(i_lit) => {
Ok(ElementTypeValue::Primitive(i_lit.into()))
}
ValueExpr::PrefixLiteral(pfx_lit) => {
Ok(ElementTypeValue::Primitive((&pfx_lit).try_into()?))
}
ValueExpr::IpAddressLiteral(ip_lit) => {
Ok(ElementTypeValue::Primitive((&ip_lit).try_into()?))
}
Expand Down
10 changes: 10 additions & 0 deletions src/types/typevalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,16 @@ impl TryFrom<&'_ crate::ast::IpAddressLiteral> for TypeValue {
}
}

impl TryFrom<&'_ crate::ast::PrefixLiteral> for TypeValue {
type Error = CompileError;

fn try_from(value: &crate::ast::PrefixLiteral) -> Result<Self, Self::Error> {
Ok(TypeValue::Builtin(BuiltinTypeValue::Prefix(
value.try_into()?
)))
}
}

impl From<crate::ast::PrefixLengthLiteral> for TypeValue {
fn from(value: crate::ast::PrefixLengthLiteral) -> Self {
TypeValue::Builtin(BuiltinTypeValue::PrefixLength(PrefixLength(
Expand Down
Loading

0 comments on commit e229c29

Please sign in to comment.