Permalink
Browse files

Added expect combinator.

  • Loading branch information...
1 parent 9536799 commit 7517378d77a0f1b2e3a2932cd658b14d96cdf920 @wbhart committed Aug 2, 2012
Showing with 36 additions and 0 deletions.
  1. +28 −0 parser.c
  2. +8 −0 parser.h
View
28 parser.c
@@ -71,6 +71,34 @@ combinator_t * match(char * str)
return comb;
}
+ast_t * expect_fn(input_t * in, void * args)
+{
+ expect_args * eargs = (expect_args *) args;
+ ast_t * ast;
+
+ combinator_t * comb = eargs->comb;
+
+ if (ast = parse(in, comb))
+ return ast;
+ else
+ exception(eargs->msg);
+
+ return NULL;
+}
+
+combinator_t * expect(combinator_t * c, char * msg)
+{
+ expect_args * args = GC_MALLOC(sizeof(expect_args));
+ args->msg = msg;
+ args->comb = c;
+
+ combinator_t * comb = new_combinator();
+ comb->fn = expect_fn;
+ comb->args = (void *) args;
+
+ return comb;
+}
+
ast_t * exact_fn(input_t * in, void * args)
{
char * str = ((match_args *) args)->str;
View
8 parser.h
@@ -47,6 +47,12 @@ typedef struct
char * str;
} match_args;
+typedef struct
+{
+ combinator_t * comb;
+ char * msg;
+} expect_args;
+
typedef struct seq_list
{
combinator_t * comb;
@@ -108,6 +114,8 @@ combinator_t * digit();
combinator_t * anything();
+combinator_t * expect(combinator_t * comb, char * msg);
+
combinator_t * seq(combinator_t * ret, tag_t typ, combinator_t * c1, ...);
combinator_t * multi(combinator_t * ret, tag_t typ, combinator_t * c1, ...);

0 comments on commit 7517378

Please sign in to comment.