• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/tclsh
2
3# Copyright JS Foundation and other contributors, http://js.foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17foreach file_name [getSourceFileNames] {
18    set state "control"
19    set do_marks {}
20    set prev_tok_type ""
21    set prev_ctrl ""
22    set expect_while false
23    set expect_open_brace false
24    set paren_count 0
25
26    foreach token [getTokens $file_name 1 0 -1 -1 {if do while else for leftparen rightparen semicolon leftbrace rightbrace}] {
27        set tok_val [lindex $token 0]
28        set line_num [lindex $token 1]
29        set col_num [lindex $token 2]
30        set tok_type [lindex $token 3]
31
32        if {$state == "expression"} {
33            # puts "expression $paren_count $tok_type ($line_num , $col_num)"
34            if {$tok_type == "leftparen"} {
35                incr paren_count
36            } elseif {$tok_type == "rightparen"} {
37                incr paren_count -1
38                if {$paren_count == 0} {
39                    set state "control"
40                    set expect_open_brace true
41                } elseif {$paren_count < 0 } {
42                    report $file_name $line_num "unexpected right parentheses"
43                }
44            } elseif {$tok_type != "semicolon"} {
45                report $file_name $line_num "unexpected token: $tok_type"
46            }
47        } else {
48            if {$expect_open_brace == true} {
49                if {$tok_type == "if" && $prev_tok_type == "else"} {
50                    # empty
51                } elseif {$tok_type != "leftbrace"} {
52                    report $file_name [lindex $prev_ctrl 1] "brace after \'[lindex $prev_ctrl 3]\' required"
53                }
54                set expect_open_brace false
55            }
56
57            if {$tok_type == "while" && ($expect_while == true || [lindex $prev_ctrl 3] == "do")} {
58                set expect_while false
59                set prev_ctrl ""
60            } elseif {$tok_type in {if for while}} {
61                set state "expression"
62                set prev_ctrl $token
63            } elseif {$tok_type in {do else}} {
64                set expect_open_brace true
65                set prev_ctrl $token
66            } elseif {$tok_type == "leftbrace"} {
67                if {[lindex $prev_ctrl 3] == "do"} {
68                    lappend do_marks 1
69                } else {
70                    lappend do_marks 0
71                }
72                set prev_ctrl ""
73            } elseif {$tok_type == "rightbrace"} {
74                if {[llength $do_marks] > 0} {
75                    if {[lindex $do_marks end] == 1} {
76                        set expect_while true
77                    }
78                    set do_marks [lreplace $do_marks end end]
79                } else {
80                    report $file_name $line_num "unmatched brace"
81                }
82            }
83        }
84
85        set prev_tok_type $tok_type
86    }
87}
88