1425bb815Sopenharmony_ci#!/usr/bin/tclsh 2425bb815Sopenharmony_ci 3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation 4425bb815Sopenharmony_ci# 5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License. 7425bb815Sopenharmony_ci# You may obtain a copy of the License at 8425bb815Sopenharmony_ci# 9425bb815Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 10425bb815Sopenharmony_ci# 11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS 13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and 15425bb815Sopenharmony_ci# limitations under the License. 16425bb815Sopenharmony_ci 17425bb815Sopenharmony_ciforeach file_name [getSourceFileNames] { 18425bb815Sopenharmony_ci set state "control" 19425bb815Sopenharmony_ci set do_marks {} 20425bb815Sopenharmony_ci set prev_tok_type "" 21425bb815Sopenharmony_ci set prev_ctrl "" 22425bb815Sopenharmony_ci set expect_while false 23425bb815Sopenharmony_ci set expect_open_brace false 24425bb815Sopenharmony_ci set paren_count 0 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_ci foreach token [getTokens $file_name 1 0 -1 -1 {if do while else for leftparen rightparen semicolon leftbrace rightbrace}] { 27425bb815Sopenharmony_ci set tok_val [lindex $token 0] 28425bb815Sopenharmony_ci set line_num [lindex $token 1] 29425bb815Sopenharmony_ci set col_num [lindex $token 2] 30425bb815Sopenharmony_ci set tok_type [lindex $token 3] 31425bb815Sopenharmony_ci 32425bb815Sopenharmony_ci if {$state == "expression"} { 33425bb815Sopenharmony_ci # puts "expression $paren_count $tok_type ($line_num , $col_num)" 34425bb815Sopenharmony_ci if {$tok_type == "leftparen"} { 35425bb815Sopenharmony_ci incr paren_count 36425bb815Sopenharmony_ci } elseif {$tok_type == "rightparen"} { 37425bb815Sopenharmony_ci incr paren_count -1 38425bb815Sopenharmony_ci if {$paren_count == 0} { 39425bb815Sopenharmony_ci set state "control" 40425bb815Sopenharmony_ci set expect_open_brace true 41425bb815Sopenharmony_ci } elseif {$paren_count < 0 } { 42425bb815Sopenharmony_ci report $file_name $line_num "unexpected right parentheses" 43425bb815Sopenharmony_ci } 44425bb815Sopenharmony_ci } elseif {$tok_type != "semicolon"} { 45425bb815Sopenharmony_ci report $file_name $line_num "unexpected token: $tok_type" 46425bb815Sopenharmony_ci } 47425bb815Sopenharmony_ci } else { 48425bb815Sopenharmony_ci if {$expect_open_brace == true} { 49425bb815Sopenharmony_ci if {$tok_type == "if" && $prev_tok_type == "else"} { 50425bb815Sopenharmony_ci # empty 51425bb815Sopenharmony_ci } elseif {$tok_type != "leftbrace"} { 52425bb815Sopenharmony_ci report $file_name [lindex $prev_ctrl 1] "brace after \'[lindex $prev_ctrl 3]\' required" 53425bb815Sopenharmony_ci } 54425bb815Sopenharmony_ci set expect_open_brace false 55425bb815Sopenharmony_ci } 56425bb815Sopenharmony_ci 57425bb815Sopenharmony_ci if {$tok_type == "while" && ($expect_while == true || [lindex $prev_ctrl 3] == "do")} { 58425bb815Sopenharmony_ci set expect_while false 59425bb815Sopenharmony_ci set prev_ctrl "" 60425bb815Sopenharmony_ci } elseif {$tok_type in {if for while}} { 61425bb815Sopenharmony_ci set state "expression" 62425bb815Sopenharmony_ci set prev_ctrl $token 63425bb815Sopenharmony_ci } elseif {$tok_type in {do else}} { 64425bb815Sopenharmony_ci set expect_open_brace true 65425bb815Sopenharmony_ci set prev_ctrl $token 66425bb815Sopenharmony_ci } elseif {$tok_type == "leftbrace"} { 67425bb815Sopenharmony_ci if {[lindex $prev_ctrl 3] == "do"} { 68425bb815Sopenharmony_ci lappend do_marks 1 69425bb815Sopenharmony_ci } else { 70425bb815Sopenharmony_ci lappend do_marks 0 71425bb815Sopenharmony_ci } 72425bb815Sopenharmony_ci set prev_ctrl "" 73425bb815Sopenharmony_ci } elseif {$tok_type == "rightbrace"} { 74425bb815Sopenharmony_ci if {[llength $do_marks] > 0} { 75425bb815Sopenharmony_ci if {[lindex $do_marks end] == 1} { 76425bb815Sopenharmony_ci set expect_while true 77425bb815Sopenharmony_ci } 78425bb815Sopenharmony_ci set do_marks [lreplace $do_marks end end] 79425bb815Sopenharmony_ci } else { 80425bb815Sopenharmony_ci report $file_name $line_num "unmatched brace" 81425bb815Sopenharmony_ci } 82425bb815Sopenharmony_ci } 83425bb815Sopenharmony_ci } 84425bb815Sopenharmony_ci 85425bb815Sopenharmony_ci set prev_tok_type $tok_type 86425bb815Sopenharmony_ci } 87425bb815Sopenharmony_ci} 88