00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 package require textutil
00014
00015 namespace ::page::gen::peg::canon {}
00016
00017
00018
00019
00020 ret ::page::gen::peg::canon (type t , type chan) {
00021
00022 # Generate data for inherited attributes
00023 # used during synthesis.
00024 canon::Setup $t
00025
00026 # Synthesize all text fragments we need.
00027 canon::Synth $t
00028
00029 # And write the grammar text.
00030 puts $chan [$t get root TEXT]
00031 return
00032 }
00033
00034
00035
00036
00037 ret ::page::gen::peg::canon::Setup (type t) {
00038 # Phase 1: Top-down, inherited attributes:
00039 #
00040 # - Max length of nonterminal symbols defined by the grammar.
00041 #
00042 # - Indentation put on all rules to get enough space for
00043 # definition attributes.
00044
00045 set max -1
00046 array set modes {}
00047
00048 foreach {sym def} [$t get root definitions] {
00049 set l [string length $sym]
00050 if {$l > $max} {set max $l}
00051
00052 set mode [string index [$t get $def mode] 0]
00053 set modes($mode) .
00054 }
00055 set modeset [join [lsort [array names modes]] ""]
00056 set mlen [AttrFieldLength $modeset]
00057 set heading [expr {$max + $mlen + 4}]
00058 # The constant 4 is for ' <- ', see
00059 # SynthNode/Nonterminal
00060
00061 # Save the computed information for access by the definitions and
00062 # other operators.
00063
00064 $t set root SYM_FIELDLEN $max
00065 $t set root ATT_FIELDLEN $mlen
00066 $t set root ATT_BASE $modeset
00067 $t set root HEADLEN $heading
00068 return
00069 }
00070
00071 ret ::page::gen::peg::canon::Synth (type t) {
00072 # Phase 2: Bottom-up, synthesized attributes
00073 #
00074 # - Text block per node, length and height.
00075
00076 $t walk root -order post -type dfs n {
00077 SynthNode $t $n
00078 }
00079 return
00080 }
00081
00082 ret ::page::gen::peg::canon::SynthNode (type t , type n) {
00083 if {$n eq "root"} {
00084 set code Root
00085 } elseif {[$t keyexists $n symbol]} {
00086 set code Nonterminal
00087 } elseif {[$t keyexists $n op]} {
00088 set code [$t get $n op]
00089 } else {
00090 return -code error "PANIC. Bad node $n, cannot classify"
00091 }
00092
00093 #puts stderr "SynthNode/$code $t $n"
00094
00095 SynthNode/$code $t $n
00096
00097 #SHOW [$t get $n TEXT] 1 0
00098 #catch {puts stderr "\t.[$t get $n W]x[$t get $n H]"}
00099 return
00100 }
00101
00102 ret ::page::gen::peg::canon::SynthNode/Root (type t , type n) {
00103 # Root is the grammar itself.
00104
00105 # Get the data we need from our children, which are start
00106 # expression and nonterminal definitions.
00107
00108 set gname [$t get root name]
00109 set gstart [$t get root start]
00110 if {$gstart ne ""} {
00111 set stext [$t get $gstart TEXT]
00112 } else {
00113 puts stderr "No start expression."
00114 set stext ""
00115 }
00116 set rules {}
00117 foreach {sym def} [$t get root definitions] {
00118 lappend rules [list $sym [$t get $def TEXT]]
00119 }
00120
00121 # Combine them into a text for the whole grammar.
00122
00123 set intro "PEG $gname \("
00124 set ispace [::textutil::blank [string length $intro]]
00125
00126 set out ""
00127 append out "# -*- text -*-" \n
00128 append out "## Parsing Expression Grammar '$gname'." \n
00129 append out "## Layouted by the PG backend 'PEGwriter'." \n
00130 append out \n
00131 append out $intro[::textutil::indent $stext $ispace 1]\)
00132 append out \n
00133 append out \n
00134
00135 foreach e [lsort -dict -index 0 $rules] {
00136 foreach {sym text} $e break
00137 append out $text \n
00138 append out \n
00139 }
00140
00141 append out "END\;" \n
00142
00143 $t set root TEXT $out
00144 return
00145 }
00146
00147 ret ::page::gen::peg::canon::SynthNode/Nonterminal (type t , type n) {
00148 # This is the root of a definition. We now
00149 # have to combine the text block for the
00150 # expression with nonterminal and attribute
00151 # data.
00152
00153 variable ms
00154
00155 set abase [$t get root ATT_BASE]
00156 set sfl [$t get root SYM_FIELDLEN]
00157 set mode [$t get $n mode]
00158 set sym [$t get $n symbol]
00159 set etext [$t get [lindex [$t children $n] 0] TEXT]
00160
00161 set out ""
00162 append out $ms($abase,$mode)
00163 append out $sym
00164 append out [::textutil::blank [expr {$sfl - [string length $sym]}]]
00165 append out " <- "
00166
00167 set ispace [::textutil::blank [string length $out]]
00168
00169 append out [::textutil::indent $etext $ispace 1]
00170 append out " ;"
00171
00172 $t set $n TEXT $out
00173 return
00174 }
00175
00176 ret ::page::gen::peg::canon::SynthNode/t (type t , type n) {
00177 # Terminal node. Primitive layout.
00178 # Put the char into single or double quotes.
00179
00180 set ch [$t get $n char]
00181 if {$ch eq "'"} {set q "\""} else {set q '}
00182
00183 set text $q$ch$q
00184
00185 SetBlock $t $n $text
00186 return
00187 }
00188
00189 ret ::page::gen::peg::canon::SynthNode/n (type t , type n) {
00190 # Nonterminal node. Primitive layout. Text is the name of smybol
00191 # itself.
00192
00193 SetBlock $t $n [$t get $n sym]
00194 return
00195 }
00196
00197 ret ::page::gen::peg::canon::SynthNode/.. (type t , type n) {
00198 # Range is [x-y]
00199 set b [$t get $n begin]
00200 set e [$t get $n end]
00201 SetBlock $t $n "\[${b}-${e}\]"
00202 return
00203 }
00204
00205 ret ::page::gen::peg::canon::SynthNode/alnum (type t , type n) {SetBlock $t $n <alnum>}
00206 ret ::page::gen::peg::canon::SynthNode/alpha (type t , type n) {SetBlock $t $n <alpha>}
00207 ret ::page::gen::peg::canon::SynthNode/dot (type t , type n) {SetBlock $t $n .}
00208 ret ::page::gen::peg::canon::SynthNode/epsilon (type t , type n) {SetBlock $t $n ""}
00209
00210 ret ::page::gen::peg::canon::SynthNode/? (type t , type n) {SynthSuffix $t $n ?}
00211 ret ::page::gen::peg::canon::SynthNode
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 namespace ::page::gen::peg::canon {
00441 variable llen 80
00442 variable ms ; array ms = {
00443 dlmv,discard {void: }
00444 dlmv,leaf {leaf: }
00445 dlmv,match {match: }
00446 dlmv,value { }
00447 dlmv,* 7
00448
00449 dlm,discard {void: } dlv,discard {void: }
00450 dlm,leaf {leaf: } dlv,leaf {leaf: }
00451 dlm,match {match: } dlv,value { }
00452 dlm,* 7 dlv,* 6
00453
00454 dmv,discard {void: } lmv,leaf {leaf: }
00455 dmv,match {match: } lmv,match {match: }
00456 dmv,value { } lmv,value { }
00457 dmv,* 7 lmv,* 7
00458
00459 dl,discard {void: } dm,discard {void: }
00460 dl,leaf {leaf: } dm,match {match: }
00461 dl,* 6 dm,* 7
00462
00463 lm,leaf {leaf: } dv,discard {void: }
00464 lm,match {match: } dv,value { }
00465 lm,* 7 dv,* 6
00466
00467 lv,leaf {leaf: } mv,match {match: }
00468 lv,value { } mv,value { }
00469 lv,* 6 mv,* 7
00470
00471 d,discard {void: } d,* 6
00472 l,leaf {leaf: } l,* 6
00473 m,match {match: } m,* 7
00474 v,value {} v,* 0
00475 }
00476 }
00477
00478
00479
00480
00481 package provide page::gen::peg::canon 0.1
00482