Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-ulex/FrontEnds/ml-lex/ml-lex.yacc

raw
1 structure S = LexSpec
2
3 structure RE = RegExp
4 structure SIS = RE.SymSet
5
6 val symTable : RE.re AtomMap.map ref = ref AtomMap.empty
7
8 val wildcard = SIS.complement (SIS.singleton 0w10) (* everything but \n *)
9 fun charToSym c = Word.fromInt (Char.ord c)
10 fun strToSym s = charToSym (String.sub (s, 0))
11
12 fun mkRule (ss, (false, re), act) = ((ss, re), act)
13 | mkRule (ss, (true, re), act) = ((ss, re),
14 "if not yylastwasn then REJECT() else (" ^ act ^")")
15
16 %%
17
18 %name MLLex
19
20 %term
21 EOF
22 | DECLS of string
23 | LT (* < *)
24 | GT (* > *)
25 | LP (* ( *)
26 | RP (* ) *)
27 | LB (* [ *)
28 | RB (* ] *)
29 | RBD (* -] *)
30 | LCB (* { *)
31 | RCB (* } *)
32 | QMARK
33 | STAR
34 | PLUS
35 | BAR
36 | CARAT
37 | DOLLAR
38 | SLASH
39 | DASH
40 | HIGH_CHAR (* "\h"; represents the range \128-\255 when not in %full mode *)
41 | CHAR of string
42 | UNICHAR of UTF8.wchar
43 | DOT
44 | EQ
45 | REPS of int
46 | ID of string
47 | ARROW
48 | ACT of string
49 | SEMI
50 | LEXMARK
51 | COMMA
52 | STATES
53 | LEXSTATE of string
54 | COUNT
55 | REJECTTOK
56 | FULL
57 | UNICODE
58 | STRUCT
59 | HEADER
60 | ARG
61 | POSARG
62
63 %nonterm
64 Start of S.spec
65 | Decls of string
66 | Defs of S.config
67 | StartStates of AtomSet.set
68 | Rules of S.rule list
69 | Rule of S.rule
70 | RuleStates of AtomSet.set
71 | LineBreakExp of (bool * RE.re)
72 | OrExp of RE.re
73 | CatExp of RE.re
74 | Exp of RE.re
75 | InExp of RE.re
76 | CharClass of SIS.set
77 | CharClass' of SIS.set
78 | CharRng of SIS.set
79 | AChar of UTF8.wchar
80 | NonCarat of UTF8.wchar
81
82 %left BAR
83 %nonassoc QMARK
84 %left PLUS
85 %nonassoc STAR
86
87 %pos int
88 %eop EOF
89 %noshift EOF
90 %start Start
91 %verbose
92
93 %%
94
95 Start
96 : Decls LEXMARK Defs LEXMARK Rules
97 (S.Spec {decls = Decls,
98 conf = Defs,
99 rules = Rules,
100 eofRules = []})
101
102 Decls
103 : DECLS
104 (DECLS)
105 | (* empty *)
106 ("")
107
108 Defs
109 : (* empty *)
110 (S.mkConfig())
111 | Defs STATES StartStates SEMI
112 (S.updStartStates (Defs, StartStates))
113 | Defs HEADER ACT
114 (S.updHeader (Defs,
115 String.substring (ACT, 1, String.size ACT - 2)))
116 | Defs STRUCT ID
117 (S.updStructName (Defs, ID))
118 | Defs ARG ACT
119 (S.updArg (Defs, ACT))
120 | Defs UNICODE
121 (S.updClamp (Defs, S.NO_CLAMP))
122 | Defs FULL
123 (S.updClamp (Defs, S.CLAMP255))
124 | Defs COUNT
125 (Defs)
126 | Defs REJECTTOK
127 (Defs)
128 | Defs ID EQ OrExp SEMI
129 (symTable := AtomMap.insert
130 (!symTable, Atom.atom ID, OrExp);
131 Defs)
132
133 StartStates
134 : LEXSTATE
135 (AtomSet.singleton (Atom.atom LEXSTATE))
136 | LEXSTATE StartStates
137 (AtomSet.add (StartStates, Atom.atom LEXSTATE))
138
139 Rules
140 : (* empty *)
141 ([])
142 | Rule Rules
143 (Rule :: Rules)
144
145 Rule
146 : LineBreakExp ARROW ACT
147 (mkRule (NONE, LineBreakExp, ACT))
148 | LT RuleStates GT LineBreakExp ARROW ACT
149 (mkRule (SOME RuleStates, LineBreakExp, ACT))
150
151 RuleStates
152 : LEXSTATE
153 (AtomSet.singleton (Atom.atom LEXSTATE))
154 | RuleStates COMMA LEXSTATE
155 (AtomSet.add (RuleStates, Atom.atom LEXSTATE))
156
157 LineBreakExp
158 : CARAT OrExp
159 (true, OrExp)
160 | OrExp
161 (false, OrExp)
162
163 OrExp
164 : OrExp BAR CatExp
165 (RE.mkOr (OrExp, CatExp))
166 | CatExp
167 (CatExp)
168
169 CatExp
170 : CatExp Exp
171 (RE.mkConcat (CatExp, Exp))
172 | Exp
173 (Exp)
174
175 Exp
176 : Exp QMARK
177 (RE.mkOpt Exp)
178 | Exp STAR
179 (RE.mkClosure Exp)
180 | Exp PLUS
181 (RE.mkAtLeast (Exp, 1))
182 | Exp REPS RCB
183 (RE.mkRep (Exp, REPS, REPS))
184 | Exp REPS COMMA REPS RCB
185 (RE.mkRep (Exp, REPS1, REPS2))
186 | InExp
187 (InExp)
188
189 InExp
190 : HIGH_CHAR
191 (RE.mkSymSet (SIS.interval (0w128, 0w255)))
192 | CHAR
193 (RE.mkSymSet (SIS.singleton (strToSym CHAR)))
194 | UNICHAR
195 (RE.mkSymSet (SIS.singleton UNICHAR))
196 | DOT
197 (RE.mkSymSet wildcard)
198 | ID RCB
199 (case AtomMap.find (!symTable, Atom.atom ID)
200 of SOME re => re
201 | NONE => raise Fail ("'" ^ ID ^ "' not defined"))
202 | LP OrExp RP
203 (OrExp)
204 | LB CARAT CharClass
205 (RE.mkSymSet (SIS.complement CharClass))
206 | LB CharClass
207 (RE.mkSymSet CharClass)
208
209 CharClass
210 : CharClass' RB
211 (CharClass')
212 | DASH CharClass' RB
213 (SIS.add (CharClass', charToSym #"-"))
214 | RBD
215 (SIS.singleton (charToSym #"-"))
216 | CharClass' RBD
217 (SIS.add (CharClass', charToSym #"-"))
218
219 CharClass'
220 : NonCarat
221 (SIS.singleton NonCarat)
222 | NonCarat DASH AChar
223 (SIS.interval (NonCarat, AChar))
224 | CharClass' CharRng
225 (SIS.union (CharRng, CharClass'))
226
227 CharRng
228 : AChar DASH AChar
229 (SIS.interval (AChar1, AChar2))
230 | AChar
231 (SIS.singleton AChar)
232
233 AChar
234 : CARAT
235 (charToSym #"^")
236 | NonCarat
237 (NonCarat)
238
239 NonCarat
240 : CHAR
241 (strToSym CHAR)
242 | UNICHAR
243 (UNICHAR)