Skip to content
Draft
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
19 changes: 19 additions & 0 deletions t/example-nondet-parsing.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!perl -T
use 5.006;

use strict;
use warnings;

use Test::More;
use Language::Bel::Test;

plan tests => 3;

my $output = output_of_eval_file("t/nondet-parsing.bel");

my @lines = split(/\n/, $output);

is scalar(@lines), 2, "anticipated number of lines of output";
is $lines[0], "(seq \\a seq \\a \\a) ", "first line";
is $lines[1], "(seq seq \\a \\a \\a) ", "second line";

39 changes: 39 additions & 0 deletions t/nondet-parsing.bel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;; Example based on https://users.monash.edu/~lloyd/tildeFP/1988BCJ/

;; (type parser (cont input trace) -> (listof trace))
;; (type cont (input trace) -> (listof trace))

;; (ann fin cont)
(def fin (input trace)
(if (no input)
(list trace)
nil))

;; (ann letter (char) -> parser)
(def letter (ch)
(fn (cont input trace)
(if (no input) nil
(id ch (car input)) (cont (cdr input) (snoc trace ch))
nil)))

;; (ann seq (parser parser) -> parser)
(def seq (p1 p2)
(fn (cont input trace)
(p1 (fn (input2 trace2)
(p2 cont input2 trace2))
input
(snoc trace 'seq))))

;; (ann either (parser parser) -> parser)
(def either (p1 p2)
(fn (cont input trace)
(append (p1 cont input trace)
(p2 cont input trace))))

(set a (letter \a)
aORaa (either a (seq a a))
S (seq aORaa aORaa))

(each match (S fin "aaa" nil)
(prn match))