File: regenerate_parsers.tcl

package info (click to toggle)
tcllib 1.18-dfsg-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 64,304 kB
  • ctags: 28,857
  • sloc: tcl: 174,135; ansic: 14,215; sh: 2,643; xml: 1,766; yacc: 1,148; pascal: 583; makefile: 106; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (86 lines) | stat: -rw-r--r-- 2,610 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env tclsh
# -*- tcl -*-

package require Tcl 8.5

set     self      [file normalize [info script]]
set     selfdir   [file dirname $self]
set     module    [file dirname $selfdir]
lappend auto_path [file dirname $module]

package require pt::pgen 1.0.3
package require pt::util
package require fileutil
package require try

set specification     [file join $module tests/data/ok/peg_peg/3_peg_itself]
set new_parser_tcl    [file join $module pt_parse_peg_tcl.tcl-NEW]
set new_parser_critcl [file join $module pt_parse_peg_c.tcl-NEW]
set me                $tcl_platform(user)
set name              PEG
set class             pt::parse::peg

# Note: Chicken'n'Egg here. The pt_pgen parser generator needs a PEG
#       parser to read the grammar definition from which to generate
#       a PEG parser.

# This problem was initially solved by using the grammar interpreter
# package pt::peg::interp together with a definition of the PEG
# grammar kept in the container package pt::peg::container::peg. That
# definition was created through manual conversion of the PE grammar.

# And we avoid getting back into the problem by writing the generated
# parser into a different file instead of overwriting the
# definition/package just used by the parser generator.

# The user has to, well, is asked to, review the results before
# replacing the working system with the newly-made code. And in case
# the user still ran into the problem, just go to the implementation
# of package pt::peg::from::peg and switch there from use of
# pt::parse::peg to the interpreter/container combination. The code
# for bootstrapping is still present, just commented out.

puts "Reading spec..."
set spec [fileutil::cat $specification]

set version 1.0.1

puts "Generating $version ..."

try {
    # Generate snit-based Tcl parser for the PEG grammar.
    puts ...Tcl
    set tcl [pt::pgen \
		 peg  $spec \
		 snit \
		   -name    $name \
		   -user    $me \
		   -file    [file tail $specification] \
		   -class   ${class}_tcl \
		   -package ${class}_tcl \
		   -version $version \
		]

    # Generate critcl-based C parser for the PEG grammar.
    puts ...Critcl
    set ctcl [pt::pgen \
		  peg  $spec \
		  critcl \
		    -name    $name \
		    -user    $me \
		    -file    [file tail $specification] \
		    -class   $class \
		    -package [string map {:: _} $class]_c \
		    -version $version \
		 ]
} trap {PT RDE SYNTAX} {e o} {
    puts [pt::util error2readable $e $spec]
    exit 1
}

puts "Saving..."
fileutil::writeFile $new_parser_tcl    $tcl
fileutil::writeFile $new_parser_critcl $ctcl

puts OK
exit 0