Skip to content

Commit

Permalink
Fix conflicting identifiers and introduce custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalthaj committed Sep 19, 2024
1 parent 43120a7 commit 748068a
Show file tree
Hide file tree
Showing 22 changed files with 543 additions and 315 deletions.
7 changes: 6 additions & 1 deletion golem-rib/src/parser/binary_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
// limitations under the License.

use crate::expr::Expr;
use crate::parser::errors::RibParseError;
use crate::InferredType;
use combine::error::StreamError;
use combine::parser::char::{spaces, string};
use combine::{attempt, choice, Parser};
use combine::{attempt, choice, ParseError, Parser};

pub fn binary<Input>(
left_expr: impl Parser<Input, Output = Expr>,
right_expr: impl Parser<Input, Output = Expr>,
) -> impl Parser<Input, Output = Expr>
where
Input: combine::Stream<Token = char>,
RibParseError: Into<
<Input::Error as ParseError<Input::Token, Input::Range, Input::Position>>::StreamError,
>,
{
spaces().with(
(
Expand Down
7 changes: 6 additions & 1 deletion golem-rib/src/parser/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
// limitations under the License.

use crate::expr::Expr;
use crate::parser::errors::RibParseError;
use combine::error::StreamError;
use combine::parser::char::spaces;
use combine::parser::char::string;
use combine::{attempt, Parser};
use combine::{attempt, ParseError, Parser};

pub fn boolean_literal<Input>() -> impl Parser<Input, Output = Expr>
where
Input: combine::Stream<Token = char>,
RibParseError: Into<
<Input::Error as ParseError<Input::Token, Input::Range, Input::Position>>::StreamError,
>,
{
attempt(string("true"))
.map(|_| Expr::boolean(true))
Expand Down
19 changes: 15 additions & 4 deletions golem-rib/src/parser/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use combine::error::Commit;
use combine::error::{Commit, StreamError};
use combine::parser::char::{alpha_num, string};
use combine::parser::char::{char, spaces};
use combine::parser::repeat::take_until;

use combine::{any, attempt, between, choice, many1, optional, parser, token, Parser};
use combine::{any, attempt, between, choice, many1, optional, parser, token, ParseError, Parser};

use crate::expr::Expr;
use crate::function_name::{
ParsedFunctionName, ParsedFunctionReference, ParsedFunctionSite, SemVer,
};
use crate::parser::errors::RibParseError;
use crate::parser::rib_expr::rib_expr;
use combine::sep_by;
use poem_openapi::__private::poem::EndpointExt;

// A call can be a function or constructing an anonymous variant at the type of writing Rib which user expects to work at runtime
pub fn call<Input>() -> impl Parser<Input, Output = Expr>
where
Input: combine::Stream<Token = char>,
RibParseError: Into<
<Input::Error as ParseError<Input::Token, Input::Range, Input::Position>>::StreamError,
>,
{
(
function_name().skip(spaces()),
Expand All @@ -50,6 +55,9 @@ where
pub fn function_name<Input>() -> impl Parser<Input, Output = ParsedFunctionName>
where
Input: combine::Stream<Token = char>,
RibParseError: Into<
<Input::Error as ParseError<Input::Token, Input::Range, Input::Position>>::StreamError,
>,
{
let identifier = || many1(alpha_num().or(token('-'))).map(|string: String| string);
let namespace = many1(identifier()).message("namespace");
Expand Down Expand Up @@ -97,9 +105,12 @@ where

let version = attempt(token('@'))
.with(take_until(attempt(string(".{"))))
.map(|v: String| {
.and_then(|v: String| {
let stripped = v.strip_suffix('.').unwrap_or(&v);
semver::Version::parse(stripped).expect("Failed to parse version")
match semver::Version::parse(stripped) {
Ok(version) => Ok(version),
Err(_) => Err(RibParseError::Message("Invalid version".to_string()).into()),
}
})
.message("version");

Expand Down
6 changes: 5 additions & 1 deletion golem-rib/src/parser/cond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
// limitations under the License.

use crate::expr::Expr;
use crate::parser::errors::RibParseError;
use crate::parser::rib_expr::rib_expr;
use combine::parser::char::{spaces, string};
use combine::{attempt, Parser};
use combine::{attempt, ParseError, Parser};

pub fn conditional<Input>() -> impl Parser<Input, Output = Expr>
where
Input: combine::Stream<Token = char>,
RibParseError: Into<
<Input::Error as ParseError<Input::Token, Input::Range, Input::Position>>::StreamError,
>,
{
// Use attempt only for the initial "if" to resolve ambiguity with identifiers
attempt(string("if").skip(spaces())).with(
Expand Down
Loading

0 comments on commit 748068a

Please sign in to comment.