Removed dep on API
[ninja.git] / src / op5 / ninja_sdk / parsegen / html / LalrHTMLVisualizationGenerator.php
blob0d4dfcece6757541e0b5f95224d2db2c550c913b
1 <?php
3 class LalrHTMLVisualizationGenerator {
4 private $name;
5 private $fsm;
6 private $grammar;
7 private $fp;
8 private $filename;
9 private $dir = 'html';
11 public function __construct( $parser_name, $fsm, $grammar ) {
12 $this->name = $parser_name;
13 $this->filename = $parser_name . "Visualization";
14 $this->grammar = $grammar;
15 $this->fsm = $fsm;
18 $this->goto_map = array();
19 foreach( $this->fsm->get_statetable() as $state_id => $map ) {
20 foreach( $map as $symbol => $action_arr ) {
21 list( $action, $target ) = explode(':',$action_arr,2);
22 if( $action == 'goto' ) {
23 if( !isset( $this->goto_map[$symbol] ) )
24 $this->goto_map[$symbol] = array();
25 $this->goto_map[$symbol][$state_id] = $target;
31 public function generate() {
32 if( !is_dir( $this->dir ) && !mkdir( $this->dir, 0755, true ) )
33 throw new GeneratorException( "Could not create dir $class_dir" );
35 $this->fp = fopen( $this->dir . DIRECTORY_SEPARATOR . $this->filename.'.html', 'w' );
37 ob_start( array( $this, 'write_block'), 1024 );
38 $this->build_html();
39 ob_end_flush();
41 fclose( $this->fp );
44 public function write_block( $block ) {
45 fwrite( $this->fp, $block );
48 private function build_html() {
50 <!DOCTYPE html>
51 <html>
52 <head>
53 <title>Visualization of parser <?php echo htmlentities($this->name);?></title>
54 <style type="text/css">
55 td, th {
56 margin: 0;
59 .bordered {
60 border-right: 1px solid #bbbbbb;
63 td, th {
64 vertical-align: top;
65 text-align: left;
67 .hard_top {
68 border-top: 3px solid black;
71 .inner_table {
72 padding: 0px;
75 .inner_table table {
76 width: 100%;
77 margin: 0;
78 padding: 0;
81 .inner_table td, .inner_table th {
82 border: 0;
85 td.bar, th.bar {
86 background-color: #dddddd;
89 td.target {
90 text-decoration: underline;
93 td.mark {
94 background-color: #dddddd;
97 </style>
98 </head>
99 <body>
100 <h1>Visualization of parser <?php echo htmlentities($this->name);?></h1>
101 <table>
102 <tr><td style="width: 50%;">
103 <h2>Lexical analysis</h2>
104 <table>
105 <?php foreach( $this->grammar->get_tokens() as $token => $match ): ?>
106 <tr>
107 <th><?php echo htmlentities( $token ); ?></th>
108 <td><?php echo htmlentities($match); ?></td>
109 </tr>
110 <?php endforeach; ?>
111 </table>
112 </td><td style="width: 50%;">
113 <h2>Grammar</h2>
114 <table>
115 <?php foreach( $this->grammar->get_rules() as $item ):?>
116 <tr>
117 <th><?php echo htmlentities($item->get_name());?></th>
118 <td class="target"><?php echo htmlentities($item->generates());?></td>
119 <td>=</td>
120 <?php foreach( $item->get_symbols() as $i=>$sym ): ?>
121 <td><?php echo $sym; ?></td>
122 <?php endforeach; ?>
123 </tr>
124 <?php endforeach; ?>
125 </table>
126 </td></tr>
127 </table>
129 <h2>LR Parser table</h2>
130 <table class="visible" cellspacing="0">
131 <tr>
132 <th colspan="2">State</th>
133 <th class="bar"></th>
134 <th>Error handler</th>
135 <th class="bar"></th>
136 <?php foreach( $this->grammar->terminals() as $sym ): if($sym[0]=='_') continue;?>
137 <th><?php echo htmlentities($sym); ?></th>
138 <?php endforeach; ?>
139 <th class="bar"></th>
140 <?php foreach( $this->grammar->non_terminals() as $sym ): ?>
141 <th><?php echo htmlentities($sym); ?></th>
142 <?php endforeach; ?>
143 </tr>
145 <?php foreach( $this->fsm->get_statetable() as $state_id => $map ):?>
146 <tr>
147 <th class="hard_top bordered"><?php echo htmlentities($state_id); ?></th>
148 <td class="inner_table hard_top bordered">
149 <table>
150 <?php foreach( $this->fsm->get_state($state_id)->closure() as $item ):?>
151 <tr>
152 <th><?php echo htmlentities($item->get_name());?></th>
153 <td class="target"><?php echo htmlentities($item->generates());?></td>
154 <td>=</td>
155 <?php foreach( $item->get_symbols() as $i=>$sym ): ?>
156 <td<?php if( $item->get_ptr() == $i ) echo ' class="mark"';?>><?php echo $sym; ?></td>
157 <?php endforeach; ?>
158 <?php if( $item->complete() ):?><td class="mark">&nbsp;</td><?php endif;?>
159 </tr>
160 <?php endforeach; ?>
161 </table>
162 </td>
163 <td class="bar hard_top bordered"></td>
164 <td class="hard_top bordered"><?php echo $this->fsm->get_default_error_handler($state_id); ?></td>
165 <td class="bar hard_top bordered"></td>
166 <?php foreach( $this->grammar->terminals() as $sym ): if($sym[0]=='_') continue; ?>
167 <td class="hard_top bordered"><?php
168 if( isset( $map[$sym] ) ) {
169 list($action, $target) = explode(':',$map[$sym],2);
170 print $action.'<br/>'.$target;
172 ?></td>
173 <?php endforeach; ?>
174 <td class="bar hard_top bordered"></td>
175 <?php foreach( $this->grammar->non_terminals() as $sym ): ?>
176 <td class="hard_top bordered"><?php
177 if( isset( $map[$sym] ) ) {
178 list($action, $target) = explode(':',$map[$sym],2);
179 print $action.'<br/>'.$target;
181 ?></td>
182 <?php endforeach; ?>
183 </tr>
184 <?php endforeach; ?>
185 </table>
186 </body>
187 </html>
188 <?php