Skip to content

Commit ea2f671

Browse files
committed
Fixed the multiple parentheseses ((())) problem. Reduces it down to just one set now
1 parent f1ac0b6 commit ea2f671

File tree

7 files changed

+40
-47
lines changed

7 files changed

+40
-47
lines changed

lib/tcsh2bash/nodes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
require File.dirname(__FILE__) + '/nodes/tcsh'
44
require File.dirname(__FILE__) + '/nodes/assignment'
5+
require File.dirname(__FILE__) + '/nodes/boolean'
56
require File.dirname(__FILE__) + '/nodes/operators'

lib/tcsh2bash/nodes/boolean.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Tcsh2Bash
2+
module Boolean
3+
4+
def self.bracketize (input)
5+
'[ ' + input + ' ]'
6+
end
7+
8+
class ParenBoolExpr < Treetop::Runtime::SyntaxNode
9+
def to_bash
10+
paren_bool_expr.to_bash
11+
end
12+
end
13+
14+
class StringComparison < Treetop::Runtime::SyntaxNode
15+
def to_bash
16+
Tcsh2Bash::Boolean.bracketize (first.text_value + ' ' + string_oper.text_value + ' ' + second.text_value)
17+
end
18+
end
19+
20+
class NumericComparison < Treetop::Runtime::SyntaxNode
21+
def to_bash
22+
Tcsh2Bash::Boolean.bracketize (first.text_value + ' ' + operator.to_bash + ' ' + second.text_value)
23+
end
24+
end
25+
end
26+
end

lib/tcsh2bash/parsers/boolean.treetop

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,24 @@ module Tcsh2Bash
33
include Symbols
44
include Operators
55

6+
# @todo - Rule for flag matching (-r some_file.txt)
7+
# - AND and OR
8+
69
rule paren_bool_expr
7-
open_paren space* paren_bool_expr space* close_paren {
8-
def to_bash
9-
open_paren.to_bash + ' ' + paren_bool_expr.to_bash + ' ' + close_paren.to_bash
10-
end
11-
}
12-
/
13-
bool_expr
10+
open_paren space* paren_bool_expr space* close_paren <ParenBoolExpr> / bool_expr
1411
end
1512

16-
#rule bool_expr
17-
# comparison space* (and_or space* comparison)*
18-
#end
19-
20-
## @todo - Add rule for flag matching (-r some_file.txt)
21-
#rule comparison
22-
# negation? string_comparison / numeric_comparison
23-
#end
24-
2513
rule bool_expr
2614
string_comparison / numeric_comparison
2715
end
2816

2917
# @todo - Add rule for string regex match (=~ and !~)
3018
rule string_comparison
31-
first:(variable_ref / string) space+ string_oper:('==' / '!=') space+ second:(variable_ref / string) {
32-
def to_bash
33-
first.text_value + ' ' + string_oper.text_value + ' ' + second.text_value
34-
end
35-
}
19+
first:(variable_ref / string) space+ string_oper:('==' / '!=') space+ second:(variable_ref / string) <StringComparison>
3620
end
3721

3822
rule numeric_comparison
39-
first:(variable_ref / number) space* operator space* second:(variable_ref / number) {
40-
def to_bash
41-
first.text_value + ' ' + operator.to_bash + ' ' + second.text_value
42-
end
43-
}
23+
first:(variable_ref / number) space* operator space* second:(variable_ref / number) <NumericComparison>
4424
end
4525

4626
rule and_or

lib/tcsh2bash/parsers/conditional.treetop

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module Tcsh2Bash
1313
#end
1414

1515
rule if_one_line
16-
'if' space* open_paren bool_expr close_paren space* stmt:(assignment / anything) {
16+
'if' space* open_paren paren_bool_expr close_paren space* stmt:(assignment / anything) {
1717
def to_bash
18-
"if [ " + bool_expr.to_bash + " ]\nthen\n " + stmt.to_bash.strip + "\nfi"
18+
"if " + paren_bool_expr.to_bash + "\nthen\n " + stmt.to_bash + "\nfi"
1919
end
2020
}
2121
end

lib/tcsh2bash/parsers/symbols.treetop

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,11 @@ module Tcsh2Bash
2020
end
2121

2222
rule open_paren
23-
'(' {
24-
def to_bash
25-
'['
26-
end
27-
}
23+
'('
2824
end
2925

3026
rule close_paren
31-
')' {
32-
def to_bash
33-
']'
34-
end
35-
}
27+
')'
3628
end
3729

3830
rule quote

spec/parsers/boolean_parser_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module Tcsh2Bash
66
@parser = BooleanParser.new
77
end
88

9-
10-
119
describe "string comparison" do
1210
specify { convert_to_bash('("foo" == "foo")').should == '[ "foo" == "foo" ]' }
1311
specify { convert_to_bash('("foo" != "bar")').should == '[ "foo" != "bar" ]' }
@@ -24,7 +22,7 @@ module Tcsh2Bash
2422

2523
# @todo - Figure out this situation
2624
describe "multiple parentheses" do
27-
specify { convert_to_bash("(((1 == 1)))").should == "[ 1 == 1 ]" }
25+
specify { convert_to_bash('((("foo" == "bar")))').should == '[ "foo" == "bar" ]' }
2826
end
2927

3028
=begin

spec/parsers/tcsh_parser_spec.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ module Tcsh2Bash
77
end
88

99
describe 'one line' do
10-
it 'should parse this' do
11-
parse('set foo=bar').should_not be_nil
12-
end
10+
specify { parse('set foo=bar').should_not be_nil }
1311
end
1412

1513
describe 'two lines' do
16-
it 'should parse this' do
17-
parse("set foo=bar\nalias hello world").should_not be_nil
18-
end
14+
specify { parse("set foo=bar\nalias hello world").should_not be_nil }
1915
end
2016

2117
describe 'bad line' do

0 commit comments

Comments
 (0)