| 1 |
@token-struct@ |
| 2 |
|
| 3 |
@header@ |
| 4 |
= struct |
| 5 |
|
| 6 |
local |
| 7 |
structure Tok = |
| 8 |
@tokmod@ |
| 9 |
|
| 10 |
structure UserCode = |
| 11 |
struct |
| 12 |
@usrdefs@ |
| 13 |
|
| 14 |
@actions@ |
| 15 |
end (* UserCode *) |
| 16 |
|
| 17 |
structure Err = AntlrErrHandler( |
| 18 |
structure Tok = Tok |
| 19 |
structure Lex = Lex) |
| 20 |
|
| 21 |
(* replace functor with inline structure for better optimization |
| 22 |
structure EBNF = AntlrEBNF( |
| 23 |
struct |
| 24 |
type strm = Err.wstream |
| 25 |
val getSpan = Err.getSpan |
| 26 |
end) |
| 27 |
*) |
| 28 |
structure EBNF = |
| 29 |
struct |
| 30 |
fun optional (pred, parse, strm) = |
| 31 |
if pred strm |
| 32 |
then let |
| 33 |
val (y, span, strm') = parse strm |
| 34 |
in |
| 35 |
(SOME y, span, strm') |
| 36 |
end |
| 37 |
else (NONE, Err.getSpan strm, strm) |
| 38 |
|
| 39 |
fun closure (pred, parse, strm) = let |
| 40 |
fun iter (strm, (left, right), ys) = |
| 41 |
if pred strm |
| 42 |
then let |
| 43 |
val (y, (_, right'), strm') = parse strm |
| 44 |
in iter (strm', (left, right'), y::ys) |
| 45 |
end |
| 46 |
else (List.rev ys, (left, right), strm) |
| 47 |
in |
| 48 |
iter (strm, Err.getSpan strm, []) |
| 49 |
end |
| 50 |
|
| 51 |
fun posclos (pred, parse, strm) = let |
| 52 |
val (y, (left, _), strm') = parse strm |
| 53 |
val (ys, (_, right), strm'') = closure (pred, parse, strm') |
| 54 |
in |
| 55 |
(y::ys, (left, right), strm'') |
| 56 |
end |
| 57 |
end |
| 58 |
|
| 59 |
fun mk lexFn = let |
| 60 |
@ehargs@ |
| 61 |
val (eh, lex) = Err.mkErrHandler {get = getS, put = putS} |
| 62 |
fun fail() = Err.failure eh |
| 63 |
fun tryProds (strm, prods) = let |
| 64 |
fun try [] = fail() |
| 65 |
| try (prod :: prods) = |
| 66 |
(Err.whileDisabled eh (fn() => prod strm)) |
| 67 |
handle Err.ParseError => try (prods) |
| 68 |
in try prods end |
| 69 |
@matchfns@ |
| 70 |
|
| 71 |
@parser@ |
| 72 |
|
| 73 |
end |