3 PGE::Hs - Match and display PGE rules as Haskell expressions
7 (You need to run C<make PGE-Hs.pbc> in F<compilers/pge> first.)
10 load_bytecode "PGE.pbc"
11 load_bytecode "PGE/Hs.pir"
12 $P0 = find_global "PGE::Hs", "match"
13 $S0 = $P0("Hello", "(...)*$")
14 print $S0 # PGE_Match 2 5 [PGE_Array [PGE_Match 2 5 [] []]] []
19 The Haskell-side data structure is defined thus:
22 = PGE_Match Int Int [MatchPGE] [(String, MatchPGE)]
23 | PGE_Array [MatchPGE]
26 deriving (Show, Eq, Ord, Read)
28 This F<PGE-Hs.pbc> is built separately (not by default). The reason is
29 because it's intended to be bundled with Pugs, so as to make Pugs usable
30 with vanilla Parrot from 0.2.0 on, using either an external F<parrot>
31 executable, or a linked F<libparrot>.
33 In external F<parrot> mode, Parrot's include path looks into the
34 F<.pbc> files inside the library tree first, then look into the current
35 directory, F<.>. Hence this file includes, rather than loads, the
36 F<PGE.pbc> library, because if Pugs is shipped with its own copy
37 of F<PGE.pbc>, Parrot would ignore that file and prefer to load
38 the one in the Parrot tree instead.
40 Granted, it is possible to pass in Pugs's own library path into an
41 environment variable (maybe C<PARROT_LIBS>?), but as this was not in
42 the 0.3.0 release, I (audreyt) decided to take the easy route. :-)
46 This is an initial sketch. The dump format may change, and the
47 whole thing may be taken out or refactored away at any moment.
51 .namespace [ "PGE::Hs" ]
53 .const string PGE_FAIL = "PGE_Fail"
54 .const string PGE_SUB_POS = "@!list"
55 .const string PGE_SUB_NAMED = "%!hash"
59 load_bytecode "Data/Escape.pir"
60 $P0 = get_class 'PGE::Match'
66 .param pmc adverbs :slurpy :named
67 .local pmc p6rule_compile, rulesub
69 p6rule_compile = compreg 'PGE::Perl6Regex'
72 # adverbs['grammar'] = 'PGE::Grammar'
73 # adverbs['name'] = name
74 rulesub = p6rule_compile(pattern, adverbs :named :flat)
76 $I0 = exists adverbs["grammar"]
78 $P0 = get_class "PGE::Grammar"
80 addmethod $P0, name, rulesub
89 .param pmc adverbs :slurpy :named
90 .local string out, tmps
93 .local pmc p6rule_compile
96 p6rule_compile = compreg 'PGE::Perl6Regex'
100 rulesub = p6rule_compile(pattern, adverbs :named :flat)
101 match = rulesub(x, 'grammar' => 'PGE::Grammar')
104 unless match goto match_fail
105 tmps = match."dump_hs"()
114 get_results "0, 0", match, out
124 .local string ret, tmp
159 .namespace [ "PGE::Match" ]
161 .sub "dump_hs" :method
165 .local int tmpi, cond
166 .local string tmps, key
167 .local pmc capt, iter, subelm, elm, escape
170 escape = find_global "Data::Escape", "String"
184 capt = getattribute self, PGE_SUB_POS
185 if_null capt, subrules
190 unless spi < spc goto subrules
193 cond = defined capt[spi]
194 unless cond goto subpats_fail
208 iter = new 'Iterator', capt
213 cond = defined capt[key]
214 unless cond goto subrules_fail
232 $I0 = does elm, "array"
233 if $I0 goto dumper_array
234 $I0 = can elm, "dump_hs"
235 unless $I0 goto dumper_string
236 tmps = elm."dump_hs"()
241 out .= 'PGE_String "'
255 unless ari < arc goto dumper_done
256 goto dumper_array_body
258 unless ari < arc goto dumper_done
262 tmps = subelm."dump_hs"()
265 goto dumper_array_loop
276 # vim: expandtab shiftwidth=4 ft=pir: