1(*
2    Copyright (c) 2013-2016 David C.J. Matthews
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License version 2.1 as published by the Free Software Foundation.
7    
8    This library is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    Lesser General Public License for more details.
12    
13    You should have received a copy of the GNU Lesser General Public
14    License along with this library; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16*)
17
18(*
19    Derived from the original parse-tree
20
21    Copyright (c) 2000
22        Cambridge University Technical Services Limited
23
24    Further development:
25    Copyright (c) 2000-13 David C.J. Matthews
26
27    Title:      Parse Tree Structure and Operations.
28    Author:     Dave Matthews, Cambridge University Computer Laboratory
29    Copyright   Cambridge University 1985
30
31*)
32
33signature BaseParseTreeSig =
34sig
35    type types
36    and  typeVarForm
37    and  typeConstrSet
38    and  values
39    and  infixity
40    and  structVals
41
42    type typeParsetree
43
44    type location =
45        { file: string, startLine: FixedInt.int, startPosition: FixedInt.int,
46          endLine: FixedInt.int, endPosition: FixedInt.int }
47
48    type breakPoint = bool ref
49
50    datatype parsetree = 
51        Ident               of
52      (* An identifier is just a name. In the second pass it is associated
53         with a particular declaration and the type is assigned into the
54         type field. The type of this identifier is needed to deal with
55         overloaded operators. If we have an occurence of ``='', say, the
56         type of the value will be 'a * 'a -> bool but the type of a particular
57         occurence, i.e. the type of the identifier must be int * int -> bool,
58         say, after all the unification has been done. *)
59        {
60            name: string,
61            expType: types ref,
62            value: values ref,
63            location: location,
64            possible: (unit -> string list) ref (* Used with the IDE. *)
65        }
66
67    |   Literal             of
68           (* Literal constants may be overloaded on more than one type. The
69              types are specified by installing appropriate conversion functions:
70              convInt, convReal, convChar, convString and convWord. *)
71            { converter: values, expType: types ref, literal: string, location: location }
72
73    |   Applic              of
74            (* Function application *)
75            { f: parsetree, arg: parsetree, location: location, isInfix: bool, expType: types ref }
76
77    |   Cond                of
78            (* Conditional *)
79        {
80            test: parsetree,
81            thenpt: parsetree,
82            elsept: parsetree,
83            location: location,
84            thenBreak: breakPoint option ref,
85            elseBreak: breakPoint option ref
86        } 
87
88    |   TupleTree           of { fields: parsetree list, location: location, expType: types ref }
89
90    |   ValDeclaration      of
91        {
92            dec:    valbind list,
93            explicit: {lookup: string -> typeVarForm option,
94                       apply: (string * typeVarForm -> unit) -> unit },
95            implicit: {lookup: string -> typeVarForm option,
96                       apply: (string * typeVarForm -> unit) -> unit },
97            location: location
98        }
99
100    |   FunDeclaration      of
101        {
102            dec:    fvalbind list,
103            explicit: {lookup: string -> typeVarForm option,
104                       apply: (string * typeVarForm -> unit) -> unit },
105            implicit: {lookup: string -> typeVarForm option,
106                       apply: (string * typeVarForm -> unit) -> unit },
107            location: location
108        } 
109
110    |   OpenDec             of
111            (* Open a structure.  The variables, structures and types are just needed if
112               debugging information is being generated. *)
113        {
114            decs: structureIdentForm list,
115            variables: values list ref,
116            structures: structVals list ref,
117            typeconstrs: typeConstrSet list ref,
118            location: location
119        }
120
121    |   Constraint          of
122           (* Constraint (explicit type given) *)
123           (* A constraint has a value and a type. The actual type, will, however
124              be the unification of these two and not necessarily the given type. *)
125            { value: parsetree, given: typeParsetree, location: location }
126
127    |   Layered             of
128          (* Layered pattern. Equivalent to an ordinary pattern except that the
129             variable is given the name of the object which is to be matched. *)
130            { var: parsetree, pattern: parsetree, location: location }
131
132    |   Fn                  of
133            { matches: matchtree list, location: location, expType: types ref }
134
135    |   Localdec            of (* Local dec in dec and let dec in exp. *)
136        {
137            decs: (parsetree * breakPoint option ref) list,
138            body: (parsetree * breakPoint option ref) list,
139            isLocal: bool,
140            varsInBody: values list ref, (* Variables in the in..dec part
141                                            of a local declaration. *)
142            location: location
143        } 
144
145    |   TypeDeclaration     of typebind list * location
146
147    |   AbsDatatypeDeclaration  of (* Datatype and Abstract Type declarations *)
148        {
149            isAbsType: bool,
150            typelist:  datatypebind list,
151            withtypes: typebind list,
152            declist:   (parsetree * breakPoint option ref) list,
153            location:  location,
154            equalityStatus: bool list ref
155        }
156
157    |   DatatypeReplication of
158        {
159            newType:  string,
160            oldType:  string,
161            oldLoc:   location,
162            newLoc:   location,
163            location: location
164        }
165
166    |   ExpSeq              of (parsetree * breakPoint option ref) list * location
167
168    |   Directive           of
169            (* Directives are infix, infixr and nonfix. They are processed by the
170               parser itself and only appear in the parse tree for completeness. *)
171            { tlist: string list, fix: infixity, location: location } 
172
173    |   ExDeclaration       of exbind list * location
174
175    |   Raise               of parsetree * location
176
177    |   HandleTree          of
178            (* Execute an expression and catch any exceptions. *)
179            { exp: parsetree, hrules: matchtree list, location: location, listLocation: location }
180
181    |   While               of
182            (* Ordinary while-loop *)
183            { test: parsetree, body: parsetree, location: location, breakPoint: breakPoint option ref } 
184
185    |   Case                of
186            (* Case-statement *)
187            { test: parsetree, match: matchtree list, location: location, listLocation: location, expType: types ref }
188
189    |   Andalso             of { first: parsetree, second: parsetree, location: location } 
190
191    |   Orelse              of { first: parsetree, second: parsetree, location: location }
192
193    |   Labelled            of
194        (* Labelled record & the entry in the list. "frozen" is false if it's
195           a pattern with "...". *)
196            { recList: labelRecEntry list, frozen: bool, expType: types ref, location: location }
197
198    |   Selector            of
199            { name: string, labType: types, typeof: types, location: location }
200
201    |   List                of
202            { elements: parsetree list, location: location, expType: types ref }
203    |   EmptyTree
204    |   WildCard            of location
205    |   Unit                of location
206    |   Parenthesised       of parsetree * location
207   
208    and valbind = (* Value bindings.*)
209        ValBind of (* Consists of a declaration part (pattern) and an expression. *)
210        {
211            dec: parsetree,
212            exp: parsetree,
213            line: location,
214            isRecursive: bool,
215            variables: values list ref (* list of variables declared and their poly vars *)
216        } 
217    
218   and fvalbind = (* Function binding *)
219   (* `Fun' bindings *)
220      (* A function binding is a list of clauses, each of which uses a
221         valBinding to hold the list of patterns and the corresponding function
222         body. The second pass extracts the function variable and the number of
223         patterns in each clause. It checks that they are the same in each
224         clause. *)
225       FValBind of
226         {
227           clauses:     fvalclause list, 
228           numOfPatts:  int ref,
229           functVar:    values ref,
230           argType:     types ref,
231           resultType:  types ref,
232           location:    location
233         }
234
235    and fvalclause = (* Clause within a function binding. *)
236        FValClause of
237        {
238            dec: funpattern,
239            exp: parsetree,
240            line: location,
241            breakPoint: breakPoint option ref
242        }
243        
244    and typebind = (* Non-generative type binding *)
245        TypeBind of
246         {
247           name: string,
248           typeVars: typeVarForm list,
249           decType: typeParsetree option,
250           isEqtype: bool, (* True if this was an eqtype in a signature. *)
251           tcon:     typeConstrSet ref,
252           nameLoc:  location,
253           fullLoc:  location
254         } 
255
256    and datatypebind = (* Generative type binding *)
257        DatatypeBind of
258         {
259           name:          string,
260           typeVars:      typeVarForm list,
261           constrs:       valueConstr list,
262           tcon:          typeConstrSet ref,
263           nameLoc:       location,
264           fullLoc:  location
265         }
266
267   and exbind = (* An exception declaration. It has a name and
268                   optionally a previous exception and a type. *)
269        ExBind of
270         {
271           name:         string,
272           previous:     parsetree,
273           ofType:       typeParsetree option,
274           value:        values ref,
275           nameLoc:      location,
276           fullLoc:      location
277         } 
278
279    and matchtree =
280    (* A match is a pattern and an expression. If the pattern matches then
281       the expression is evaluated in the environment of the pattern. *)
282        MatchTree of
283        {
284            vars: parsetree,
285            exp: parsetree,
286            location: location,
287            argType: types ref,
288            resType: types ref,
289            breakPoint: breakPoint option ref
290        } 
291
292    (* Name of a structure. Used only in an ``open'' declaration. *)
293    withtype structureIdentForm = 
294    {
295        name:   string,
296        value:  structVals option ref,
297        location: location
298    } 
299
300    (* An entry in a label record in an expression or a pattern. *)
301    and labelRecEntry =
302    {
303        name: string,
304        nameLoc: location,
305        valOrPat: parsetree,
306        fullLocation: location,
307        expType: types ref
308    }
309    
310    and funpattern = (* The declaration part of a fun binding. *)
311        { ident: { name: string, expType: types ref, location: location },
312          isInfix: bool, args: parsetree list, constraint: typeParsetree option }
313
314    and valueConstr =
315        {constrName: string, constrArg: typeParsetree option, idLocn: location, constrVal: values ref}
316
317    structure Sharing:
318    sig
319        type types = types
320        and  typeVarForm = typeVarForm
321        and  typeConstrSet = typeConstrSet
322        and  values = values
323        and  infixity = infixity
324        and  structVals = structVals
325        and  typeParsetree = typeParsetree
326        and  parsetree = parsetree
327        and  valbind = valbind
328        and  fvalbind = fvalbind
329        and  fvalclause = fvalclause
330        and  typebind = typebind
331        and  datatypebind = datatypebind
332        and  exbind = exbind
333        and  matchtree = matchtree
334    end
335end;
336