detgen.str 3.92 KB
Newer Older
1
2
3
4
/*
 * detgen.str: this file is a part of SDF-DetGen from the Transformers
 *             project.
 * Copyright (C) 2003, 2004  EPITA Research and Development Laboratory.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
20
21
 */
/**
22
23
24
25
26
 * This program generate the corresponding deterministic grammar module
 * extension AST of a definition grammar AST.
 *
 * Author: Valentin David <valentin@lrde.epita.fr>
 */
27
module detgen
28
imports lib DetBoxedSdf sdf-2-pp-lib det-lib
29
30
31
32
33
34
35
36
37

signature
  constructors
	Import : String -> Option

strategies

        import-option =
                ArgOption("-m" + "--import"
38
                , where(<set-config> ("-m",<id>)); !Import(<id>)
39
40
41
42
43
44
45
46
47
48
49
                , !"-m n|--import n  Generated module will import n"
                )

rules

	/**
	 * :: String -> String
	 *
	 * Transform a production to a the coresponding deterministic
	 * productions.
	 */
50

51
52
53
	ToDetProds : prod(a,b,c) -> [prod(a', b, c''),
				prod(lit(btag),layout,attrs(reject)),
				prod(lit(etag),layout,attrs(reject))]
54
55
	where	  <attrs(one(dettag(id)))> c
                ; <get-tags> c => (btag, etag)
56
		; <concat> [[lit(btag)], a, [lit(etag)]] => a'
57
		; <attrs(filter(not(dettag(id))))> c => c'
58
59
60
61
62
		// Try to get the current pp-entry and add the tags.
		; ((<prod(id,id,at-pp(?pl))> prod(a, b, c')
		   ; !pl
		   ; rec x([Pathed(None,id)|id]; Hd <+ \ [_|l] -> l \; x)
		   ; Pathed(id, \ x -> [H([SOpt(HS,"0")],l)]
63
64
				where <concat> [[FBOX(KW,
				                      S(<double-quote> btag))],
65
						x,
66
67
68
						[FBOX(KW,
						      S(<double-quote> etag))]]
					      => l \)
69
70
71
		  )
		  <+
		// else generate the pp-entry.
72
		  (  <generate-pp-entries> prod(a, b, c')
73
		   ; Hd
74
		   ; alltd(FBOX(KW, S(double-quote)))
75
		   ; \ PP-Entry(_,b) -> Pathed(None,[H([SOpt(HS,"0")],b')])
76
			where <concat> [[FBOX(KW,S(<double-quote> btag))],
77
					b,
78
79
					[FBOX(KW,S(<double-quote> etag))]]
					=> b' \
80
81
82
		  ))
		; ?pp
		; <attrs(\ x -> [BoxedAttr(pplist(pp))|x] \)> c' => c''
83
	
84
85
86
	ToDetProds : prod(a,b,c) -> [prod(a', b, c''),
				prod(lit(btag),layout,attrs(reject)),
				prod(lit(etag),layout,attrs(reject))]
87
88
89
90
	where	  <attrs(one(dettag2(id,string-to-int; ?i)))> c
		; <get-tags> c => (btag, etag)
		; <explode-rule> prod(a, b, c) => (begin, elt, end)
		; <concat> [begin, [lit(btag)], elt, [lit(etag)], end] => a'
91
		; <attrs(filter(not(dettag2(id, id))))> c => c'
92
		; ((<at-pp(?pl)> c'
93
94
95
96
		   ; !pl
		   ; rec x([Pathed(None,id)|id]; Hd <+ \ [_|l] -> l \; x)
		  )
		  <+
97
		  (  <generate-pp-entries> prod(a, b, c')
98
99
100
101
		   ; Hd
		   ; \ PP-Entry(_,b) -> Pathed(None, b) \
		  ))
		; Pathed(id, oncetd(Arg(where(try(string-to-int); ?i))
102
103
104
			; !([FBOX(KW,S(<double-quote> btag))],
			    [<id>],
			    [FBOX(KW,S(<double-quote> etag))])
105
106
107
			; conc
			; !(H([SOpt(HS,"0")],<id>))))
		; ?pp
108
		; <attrs(filter(not(BoxedAttr(id))))> c'
109
110
111
112
113
114
115
116
		; attrs(\ x -> [BoxedAttr(pplist(pp))|x] \) => c''

strategies

	/**
	 * Get all the production to generate corresponding deterministic
	 * production.
	 */
117
118
	det-gen-prods =
		  collect(prod(id,id,attrs(at-dettag(id) <+ at-dettag(id,id))))
119
		; map(ToDetProds)
120
		; concat
121
		; !'module("Det",[],['exports(context-free-syntax(<id>))])
122

123

124
125
	detgen = det-iowrap((id, det-gen-prods)
			  ; try((option-defined(Import(?imp)), id)
126
127
			    ; (id,'module(?name; <concat-strings> [name, imp]
		               ,!['imports([imp])],id))), import-option)