## match.pkg
## John Reppy (http://www.cs.uchicago.edu/~jhr)
## Aaron Turon (adrassi@gmail.com)
## All rights reserved.
# Compiled by:
#
src/app/future-lex/src/lexgen.lib# A simple match "backend" that runs the produced state machine directly
# on stdin. Treats end of line as end of input. Note that a match only
# occurs if the machine is in an accepting state after consuming the
# complete input; in particular, the input is meant to represent a single
# token, and the machine does not restart until the end of input.
### "Human reason has this peculiar fate
### that in one species of its knowledge
### it is burdened by questions which,
### as prescribed by the very nature
### of reason itself, it is not able to ignore,
### but which, as transcending all its powers,
### it is also not able to answer."
###
### -- Emmanuel Kant, Critique of Pure Reason
stipulate
package fil = file__premicrothread; # file__premicrothread is from
src/lib/std/src/posix/file--premicrothread.pkgherein
package match: (weak) Output { # Output is from
src/app/future-lex/src/backends/output.api #
package sis= regular_expression::symbol_set; # regular_expression is from
src/app/future-lex/src/regular-expression.pkg package lo= lex_output_spec; # lex_output_spec is from
src/app/future-lex/src/backends/lex-output-spec.pkg fun match (lo::STATE { id, label, final, next, ... }, [])
=>
final;
match (lo::STATE { id, label, final, next, ... }, symbol ! r)
=>
goto *next
where
fun goto []
=>
[];
goto ((syms, s) ! r')
=>
sis::member (syms, symbol) ?? match (s, r)
:: goto r';
end;
end;
end;
fun match_loop states
=
case (fil::read_line fil::stdin)
#
NULL => ();
THE "\n" => ();
THE s
=>
{ chars = list::reverse (list::tail (list::reverse (string::explode s)));
syms = list::map (one_word_unt::from_int o char::to_int) chars;
(list::head states)
->
q0 as lo::STATE { label, ... };
case (match (q0, syms))
#
[] => print "-- No match --\n";
i ! _ => { print "-- Match: ";
print (regular_expression::to_string (vector::get (label, i)));
print " --\n";
};
esac;
match_loop states; # Continue I/O loop
};
esac;
fun output (lo::SPEC { dfa, ... }, _)
=
match_loop dfa;
};
end;
## COPYRIGHT (c) 2005
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.