Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-ulex/BackEnds/SML/sml-output-support.sml

raw
1 (* sml-output-support.sml
2 *
3 * COPYRIGHT (c) 2005
4 * John Reppy (http://www.cs.uchicago.edu/~jhr)
5 * Aaron Turon ([email protected])
6 * All rights reserved.
7 *
8 * Some supporting code shared between different methods of
9 * SML output
10 *)
11
12 structure SMLOutputSupport =
13 struct
14
15 structure RE = RegExp
16 structure Sym = RE.Sym
17 structure SIS = RegExp.SymSet
18 structure LO = LexOutputSpec
19
20 datatype ml_exp = datatype ML.ml_exp
21 datatype ml_pat = datatype ML.ml_pat
22
23 fun ML_Sym s = ML_Raw [ML.Tok (RE.symToString s)]
24
25 val inp = "inp"
26 val inpVar = ML_Var inp
27
28 fun idOf (LO.State {id, ...}) = id
29 fun nameOf' i = "yyQ" ^ (Int.toString i)
30 fun nameOf s = nameOf' (idOf s)
31 fun actName i = "yyAction" ^ (Int.toString i)
32
33 (* simple heuristic to avoid computing unused values *)
34 local
35 val has = String.isSubstring
36 in
37 val hasyytext = has "yytext"
38 val hasyysubstr = has "yysubstr"
39 val hasyyunicode= has "yyunicode"
40 val hasREJECT = has "REJECT"
41 val hasyylineno = has "yylineno"
42 val hasyycolno = has "yycolno"
43 end
44
45 (* map over the intervals of a symbol set *)
46 fun mapInt f syms =
47 SIS.foldlInt (fn (i, ls) => (f i)::ls) [] syms
48
49 (* generate code for an action *)
50 fun mkAction (i, action, k) = let
51 val updStrm = ML_RefPut (ML_Var "yystrm", ML_Var "strm")
52 val act = ML_Raw [ML.Tok action]
53 val seq = ML_Seq [updStrm, act]
54 val lets = if hasyysubstr action andalso not (!Options.lexCompat)
55 then ML_Let
56 ("yysubstr",
57 ML_App("yymksubstr", [ML_Var "strm"]),
58 seq)
59 else seq
60 val lett = if hasyytext action
61 then ML_Let
62 ("yytext",
63 ML_App("yymktext", [ML_Var "strm"]),
64 lets)
65 else lets
66 val letu = if hasyyunicode action
67 then ML_Let
68 ("yyunicode",
69 ML_App("yymkunicode", [ML_Var "strm"]),
70 lett)
71 else lett
72 val letl = if hasyylineno action
73 then ML_Let
74 ("yylineno",
75 ML_App("ref",
76 [ML_App ("yygetlineNo",
77 [ML_RefGet (ML_Var "yystrm")])]),
78 letu)
79 else letu
80 val letc = if hasyycolno action
81 then ML_Let
82 ("yycolno",
83 ML_App("ref",
84 [ML_App ("yygetcolNo",
85 [ML_RefGet (ML_Var "yystrm")])]),
86 letl)
87 else letl
88 val letr = if hasREJECT action
89 then ML_Let
90 ("oldStrm", ML_RefGet (ML_Var "yystrm"),
91 ML_Fun
92 ("REJECT", [],
93 ML_Seq
94 [ML_RefPut (ML_Var "yystrm",
95 ML_Var "oldStrm"),
96 ML_App("yystuck", [ML_Var "lastMatch"])],
97 letc))
98 else letc
99 in
100 ML_NewGroup (ML_Fun (actName i, ["strm", "lastMatch : yymatch"], letr, k))
101 end
102
103 (* output start state datatype *)
104 fun startStatesHook spec strm = let
105 val LO.Spec {startStates, ...} = spec
106 val machNames = #1 (ListPair.unzip startStates)
107 in
108 TextIO.output (strm, String.concatWith " | " machNames)
109 end
110
111 (* output user declarations *)
112 fun userDeclsHook spec strm = let
113 val LO.Spec {decls, ...} = spec
114 in
115 TextIO.output (strm, decls)
116 end
117
118 (* output "header" -- the structure/functor definition of the lexer *)
119 fun headerHook spec strm = let
120 val LO.Spec {header, ...} = spec
121 in
122 TextIO.output (strm, header)
123 end
124
125 (* generate args parameter for the lexer *)
126 fun argsHook spec strm = let
127 val LO.Spec {arg, ...} = spec
128 val arg' = case (String.size arg, !Options.lexCompat)
129 of (0, true) => "(yyarg as ())"
130 | (_, true) => "(yyarg as " ^ arg ^ ") ()"
131 | (0, false) => ""
132 | (_, false) => "(yyarg as " ^ arg ^ ")"
133 in
134 TextIO.output (strm, arg')
135 end
136
137 (* ml-ulex mode only *)
138 fun pargsHook spec strm = let
139 val LO.Spec {arg, ...} = spec
140 in
141 TextIO.output (strm,
142 if String.size arg > 0 then "yyarg" else "")
143 end
144
145 fun mkEOF (eofRules, innerExp) =
146 if !Options.lexCompat
147 then
148 ML_If (ML_App("yyInput.eof", [ML_RefGet (ML_Var "yystrm")]),
149 ML_App("UserDeclarations.eof", [ML_Var "yyarg"]),
150 innerExp)
151 else
152 ML_If (
153 ML_App("ULexBuffer.eof", [ML_RefGet (ML_Var "yystrm")]),
154 ML_Let("yycolno", ML_App("ref", [ML_App ("yygetcolNo", [ML_RefGet (ML_Var "yystrm")])]),
155 ML_Let("yylineno", ML_App("ref", [ML_App ("yygetlineNo", [ML_RefGet (ML_Var "yystrm")])]),
156 ML_Case(ML_RefGet (ML_Var "yyss"),
157 map (fn ("_", act) => (ML_Wild, ML_Raw [ML.Tok ("(" ^ act ^ ")")])
158 | (ss, act) => (ML_ConPat (ss, []), ML_Raw [ML.Tok ("(" ^ act ^ ")")]))
159 eofRules
160 ))),
161 innerExp)
162
163 (* include the templates, which are defined in a compiler-specific way *)
164 open Templates
165
166 end