| 1 |
@header@ |
| 2 |
= struct |
| 3 |
|
| 4 |
structure yyInput : sig |
| 5 |
|
| 6 |
type stream |
| 7 |
val mkStream : (int -> string) -> stream |
| 8 |
val fromStream : TextIO.StreamIO.instream -> stream |
| 9 |
val getc : stream -> (Char.char * stream) option |
| 10 |
val getpos : stream -> int |
| 11 |
val getlineNo : stream -> int |
| 12 |
val subtract : stream * stream -> string |
| 13 |
val eof : stream -> bool |
| 14 |
val lastWasNL : stream -> bool |
| 15 |
|
| 16 |
end = struct |
| 17 |
|
| 18 |
structure TIO = TextIO |
| 19 |
structure TSIO = TIO.StreamIO |
| 20 |
structure TPIO = TextPrimIO |
| 21 |
|
| 22 |
datatype stream = Stream of { |
| 23 |
strm : TSIO.instream, |
| 24 |
id : int, (* track which streams originated |
| 25 |
* from the same stream *) |
| 26 |
pos : int, |
| 27 |
lineNo : int, |
| 28 |
lastWasNL : bool |
| 29 |
} |
| 30 |
|
| 31 |
local |
| 32 |
val next = ref 0 |
| 33 |
in |
| 34 |
fun nextId() = !next before (next := !next + 1) |
| 35 |
end |
| 36 |
|
| 37 |
val initPos = 2 (* ml-lex bug compatibility *) |
| 38 |
|
| 39 |
fun mkStream inputN = let |
| 40 |
val strm = TSIO.mkInstream |
| 41 |
(TPIO.RD { |
| 42 |
name = "lexgen", |
| 43 |
chunkSize = 4096, |
| 44 |
readVec = SOME inputN, |
| 45 |
readArr = NONE, |
| 46 |
readVecNB = NONE, |
| 47 |
readArrNB = NONE, |
| 48 |
block = NONE, |
| 49 |
canInput = NONE, |
| 50 |
avail = (fn () => NONE), |
| 51 |
getPos = NONE, |
| 52 |
setPos = NONE, |
| 53 |
endPos = NONE, |
| 54 |
verifyPos = NONE, |
| 55 |
close = (fn () => ()), |
| 56 |
ioDesc = NONE |
| 57 |
}, "") |
| 58 |
in |
| 59 |
Stream {strm = strm, id = nextId(), pos = initPos, lineNo = 1, |
| 60 |
lastWasNL = true} |
| 61 |
end |
| 62 |
|
| 63 |
fun fromStream strm = Stream { |
| 64 |
strm = strm, id = nextId(), pos = initPos, lineNo = 1, lastWasNL = true |
| 65 |
} |
| 66 |
|
| 67 |
fun getc (Stream {strm, pos, id, lineNo, ...}) = (case TSIO.input1 strm |
| 68 |
of NONE => NONE |
| 69 |
| SOME (c, strm') => |
| 70 |
SOME (c, Stream { |
| 71 |
strm = strm', |
| 72 |
pos = pos+1, |
| 73 |
id = id, |
| 74 |
lineNo = lineNo + |
| 75 |
(if c = #"\n" then 1 else 0), |
| 76 |
lastWasNL = (c = #"\n") |
| 77 |
}) |
| 78 |
(* end case*)) |
| 79 |
|
| 80 |
fun getpos (Stream {pos, ...}) = pos |
| 81 |
|
| 82 |
fun getlineNo (Stream {lineNo, ...}) = lineNo |
| 83 |
|
| 84 |
fun subtract (new, old) = let |
| 85 |
val Stream {strm = strm, pos = oldPos, id = oldId, ...} = old |
| 86 |
val Stream {pos = newPos, id = newId, ...} = new |
| 87 |
val (diff, _) = if newId = oldId andalso newPos >= oldPos |
| 88 |
then TSIO.inputN (strm, newPos - oldPos) |
| 89 |
else raise Fail |
| 90 |
"BUG: yyInput: attempted to subtract incompatible streams" |
| 91 |
in |
| 92 |
diff |
| 93 |
end |
| 94 |
|
| 95 |
fun eof s = not (isSome (getc s)) |
| 96 |
|
| 97 |
fun lastWasNL (Stream {lastWasNL, ...}) = lastWasNL |
| 98 |
|
| 99 |
end |
| 100 |
|
| 101 |
datatype yystart_state = |
| 102 |
@startstates@ |
| 103 |
|
| 104 |
structure UserDeclarations = |
| 105 |
struct |
| 106 |
|
| 107 |
@userdecls@ |
| 108 |
|
| 109 |
|
| 110 |
end |
| 111 |
|
| 112 |
datatype yymatch |
| 113 |
= yyNO_MATCH |
| 114 |
| yyMATCH of yyInput.stream * action * yymatch |
| 115 |
withtype action = yyInput.stream * yymatch -> UserDeclarations.lexresult |
| 116 |
|
| 117 |
local |
| 118 |
|
| 119 |
val yytable = |
| 120 |
@table@ |
| 121 |
|
| 122 |
fun mk yyins = let |
| 123 |
(* current start state *) |
| 124 |
val yyss = ref INITIAL |
| 125 |
fun YYBEGIN ss = (yyss := ss) |
| 126 |
(* current input stream *) |
| 127 |
val yystrm = ref yyins |
| 128 |
(* get one char of input *) |
| 129 |
val yygetc = yyInput.getc |
| 130 |
(* create yytext *) |
| 131 |
fun yymktext(strm) = yyInput.subtract (strm, !yystrm) |
| 132 |
open UserDeclarations |
| 133 |
fun lex |
| 134 |
@args@ |
| 135 |
= let |
| 136 |
fun continue() = let |
| 137 |
val yylastwasn = yyInput.lastWasNL (!yystrm) |
| 138 |
fun yystuck (yyNO_MATCH) = raise Fail "stuck state" |
| 139 |
| yystuck (yyMATCH (strm, action, old)) = |
| 140 |
action (strm, old) |
| 141 |
val yypos = yyInput.getpos (!yystrm) |
| 142 |
val yygetlineNo = yyInput.getlineNo |
| 143 |
fun yyactsToMatches (strm, [], oldMatches) = oldMatches |
| 144 |
| yyactsToMatches (strm, act::acts, oldMatches) = |
| 145 |
yyMATCH (strm, act, yyactsToMatches (strm, acts, oldMatches)) |
| 146 |
fun yygo actTable = |
| 147 |
(fn (~1, _, oldMatches) => yystuck oldMatches |
| 148 |
| (curState, strm, oldMatches) => let |
| 149 |
val (transitions, finals') = Vector.sub (yytable, curState) |
| 150 |
val finals = List.map (fn i => Vector.sub (actTable, i)) finals' |
| 151 |
fun tryfinal() = |
| 152 |
yystuck (yyactsToMatches (strm, finals, oldMatches)) |
| 153 |
fun find (c, []) = NONE |
| 154 |
| find (c, (c1, c2, s)::ts) = |
| 155 |
if c1 <= c andalso c <= c2 then SOME s |
| 156 |
else find (c, ts) |
| 157 |
in case yygetc strm |
| 158 |
of SOME(c, strm') => |
| 159 |
(case find (c, transitions) |
| 160 |
of NONE => tryfinal() |
| 161 |
| SOME n => |
| 162 |
yygo actTable |
| 163 |
(n, strm', |
| 164 |
yyactsToMatches (strm, finals, oldMatches))) |
| 165 |
| NONE => tryfinal() |
| 166 |
end) |
| 167 |
in |
| 168 |
@lexer@ |
| 169 |
|
| 170 |
end |
| 171 |
in |
| 172 |
continue() |
| 173 |
handle IO.Io{cause, ...} => raise cause |
| 174 |
end |
| 175 |
in |
| 176 |
lex |
| 177 |
end |
| 178 |
in |
| 179 |
fun makeLexer yyinputN = mk (yyInput.mkStream yyinputN) |
| 180 |
end |
| 181 |
|
| 182 |
end |