Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit RibParseError, avoid expects in parsers and handle keywords properly #950

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions golem-rib/src/parser/binary_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use combine::parser::char::{spaces, string};
use combine::{attempt, choice, ParseError, Parser};

use crate::expr::Expr;
use crate::parser::errors::RibParseError;
use crate::InferredType;
use combine::parser::char::{spaces, string};
use combine::{attempt, choice, 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 Expand Up @@ -55,10 +60,12 @@ where

#[cfg(test)]
mod test {
use super::*;
use crate::parser::rib_expr::rib_expr;
use combine::EasyParser;

use crate::parser::rib_expr::rib_expr;

use super::*;

#[test]
fn test_greater_than() {
let input = "foo > bar";
Expand Down
15 changes: 11 additions & 4 deletions golem-rib/src/parser/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

use crate::expr::Expr;
use crate::parser::errors::RibParseError;

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 All @@ -30,10 +35,12 @@ where

#[cfg(test)]
mod tests {
use super::*;
use crate::parser::rib_expr::rib_expr;
use combine::EasyParser;

use crate::parser::rib_expr::rib_expr;

use super::*;

#[test]
fn test_boolean_true() {
let input = "true";
Expand Down
21 changes: 14 additions & 7 deletions golem-rib/src/parser/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ use combine::error::Commit;
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::sep_by;
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;

// 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 +53,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 +103,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 Expand Up @@ -196,14 +205,12 @@ where

#[cfg(test)]
mod function_call_tests {

use combine::EasyParser;

use crate::expr::Expr;
use crate::function_name::{
ParsedFunctionName, ParsedFunctionReference, ParsedFunctionSite, SemVer,
};

use crate::parser::rib_expr::rib_expr;

#[test]
Expand Down
19 changes: 15 additions & 4 deletions golem-rib/src/parser/cond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use combine::parser::char::{alpha_num, char, spaces, string};
use combine::{attempt, not_followed_by, ParseError, Parser};

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};

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(
attempt(
string("if")
.skip(not_followed_by(alpha_num().or(char('-')).or(char('_'))))
.skip(spaces()),
)
.with(
(
rib_expr().skip(spaces()),
string("then").skip(spaces()),
Expand All @@ -36,9 +46,10 @@ where

#[cfg(test)]
mod tests {
use super::*;
use combine::EasyParser;

use super::*;

#[test]
fn test_conditional() {
let input = "if foo then bar else baz";
Expand Down
Loading