Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-ulex/BackEnds/SML/template-ml-ulex.sml

raw
1 @header@
2 = struct
3
4 datatype yystart_state =
5 @startstates@
6
7 local
8
9 structure UserDeclarations =
10 struct
11
12 @userdecls@
13
14 end
15
16 datatype yymatch
17 = yyNO_MATCH
18 | yyMATCH of ULexBuffer.stream * action * yymatch
19 withtype action = ULexBuffer.stream * yymatch -> UserDeclarations.lex_result
20
21 val yytable : ((UTF8.wchar * UTF8.wchar * int) list * int list) Vector.vector =
22 @table@
23
24 fun yystreamify' p input = ULexBuffer.mkStream (p, input)
25
26 fun yystreamifyReader' p readFn strm = let
27 val s = ref strm
28 fun iter(strm, n, accum) =
29 if n > 1024 then (String.implode (rev accum), strm)
30 else (case readFn strm
31 of NONE => (String.implode (rev accum), strm)
32 | SOME(c, strm') => iter (strm', n+1, c::accum))
33 fun input() = let
34 val (data, strm) = iter(!s, 0, [])
35 in
36 s := strm;
37 data
38 end
39 in
40 yystreamify' p input
41 end
42
43 fun yystreamifyInstream' p strm = yystreamify' p (fn ()=>TextIO.input strm)
44
45 fun innerLex
46 @args@
47 (yystrm_, yyss_, yysm) = let
48 (* current start state *)
49 val yyss = ref yyss_
50 fun YYBEGIN ss = (yyss := ss)
51 (* current input stream *)
52 val yystrm = ref yystrm_
53 fun yysetStrm strm = yystrm := strm
54 fun yygetPos() = ULexBuffer.getpos (!yystrm)
55 fun yystreamify input = yystreamify' (yygetPos()) input
56 fun yystreamifyReader readFn strm = yystreamifyReader' (yygetPos()) readFn strm
57 fun yystreamifyInstream strm = yystreamifyInstream' (yygetPos()) strm
58 (* start position of token -- can be updated via skip() *)
59 val yystartPos = ref (yygetPos())
60 (* get one char of input *)
61 fun yygetc strm = (case ULexBuffer.getu strm
62 of (SOME (0w10, s')) =>
63 (AntlrStreamPos.markNewLine yysm (ULexBuffer.getpos strm);
64 SOME (0w10, s'))
65 | x => x)
66 fun yygetList getc strm = let
67 val get1 = UTF8.getu getc
68 fun iter (strm, accum) =
69 (case get1 strm
70 of NONE => rev accum
71 | SOME (w, strm') => iter (strm', w::accum)
72 (* end case *))
73 in
74 iter (strm, [])
75 end
76 (* create yytext *)
77 fun yymksubstr(strm) = ULexBuffer.subtract (strm, !yystrm)
78 fun yymktext(strm) = Substring.string (yymksubstr strm)
79 fun yymkunicode(strm) = yygetList Substring.getc (yymksubstr strm)
80 open UserDeclarations
81 fun lex () = let
82 fun yystuck (yyNO_MATCH) = raise Fail "lexer reached a stuck state"
83 | yystuck (yyMATCH (strm, action, old)) =
84 action (strm, old)
85 val yypos = yygetPos()
86 fun yygetlineNo strm = AntlrStreamPos.lineNo yysm (ULexBuffer.getpos strm)
87 fun yygetcolNo strm = AntlrStreamPos.colNo yysm (ULexBuffer.getpos strm)
88 fun yyactsToMatches (strm, [], oldMatches) = oldMatches
89 | yyactsToMatches (strm, act::acts, oldMatches) =
90 yyMATCH (strm, act, yyactsToMatches (strm, acts, oldMatches))
91 fun yygo actTable =
92 (fn (~1, _, oldMatches) => yystuck oldMatches
93 | (curState, strm, oldMatches) => let
94 val (transitions, finals') = Vector.sub (yytable, curState)
95 val finals = List.map (fn i => Vector.sub (actTable, i)) finals'
96 fun tryfinal() =
97 yystuck (yyactsToMatches (strm, finals, oldMatches))
98 fun find (c, []) = NONE
99 | find (c, (c1, c2, s)::ts) =
100 if c1 <= c andalso c <= c2 then SOME s
101 else find (c, ts)
102 in case yygetc strm
103 of SOME(c, strm') =>
104 (case find (c, transitions)
105 of NONE => tryfinal()
106 | SOME n =>
107 yygo actTable
108 (n, strm',
109 yyactsToMatches (strm, finals, oldMatches)))
110 | NONE => tryfinal()
111 end)
112 val yylastwasnref = ref (ULexBuffer.lastWasNL (!yystrm))
113 fun continue() = let val yylastwasn = !yylastwasnref in
114 @lexer@
115
116 end
117 and skip() = (yystartPos := yygetPos();
118 yylastwasnref := ULexBuffer.lastWasNL (!yystrm);
119 continue())
120 in (continue(), (!yystartPos, yygetPos()-1), !yystrm, !yyss) end
121 in
122 lex()
123 end
124 in
125 type pos = AntlrStreamPos.pos
126 type span = AntlrStreamPos.span
127 type tok = UserDeclarations.lex_result
128
129 datatype prestrm = STRM of ULexBuffer.stream *
130 (yystart_state * tok * span * prestrm * yystart_state) option ref
131 type strm = (prestrm * yystart_state)
132
133 fun lex sm
134 @args@
135 (STRM (yystrm, memo), ss) = (case !memo
136 of NONE => let
137 val (tok, span, yystrm', ss') = innerLex
138 @pargs@
139 (yystrm, ss, sm)
140 val strm' = STRM (yystrm', ref NONE);
141 in
142 memo := SOME (ss, tok, span, strm', ss');
143 (tok, span, (strm', ss'))
144 end
145 | SOME (ss', tok, span, strm', ss'') =>
146 if ss = ss' then
147 (tok, span, (strm', ss''))
148 else (
149 memo := NONE;
150 lex sm
151 @pargs@
152 (STRM (yystrm, memo), ss))
153 (* end case *))
154
155 fun streamify input = (STRM (yystreamify' 0 input, ref NONE), INITIAL)
156 fun streamifyReader readFn strm = (STRM (yystreamifyReader' 0 readFn strm, ref NONE),
157 INITIAL)
158 fun streamifyInstream strm = (STRM (yystreamifyInstream' 0 strm, ref NONE),
159 INITIAL)
160
161 fun getPos (STRM (strm, _), _) = ULexBuffer.getpos strm
162
163 end
164 end
165