File tree Expand file tree Collapse file tree 7 files changed +40
-47
lines changed Expand file tree Collapse file tree 7 files changed +40
-47
lines changed Original file line number Diff line number Diff line change 2
2
3
3
require File . dirname ( __FILE__ ) + '/nodes/tcsh'
4
4
require File . dirname ( __FILE__ ) + '/nodes/assignment'
5
+ require File . dirname ( __FILE__ ) + '/nodes/boolean'
5
6
require File . dirname ( __FILE__ ) + '/nodes/operators'
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -3,44 +3,24 @@ module Tcsh2Bash
3
3
include Symbols
4
4
include Operators
5
5
6
+ # @todo - Rule for flag matching (-r some_file.txt)
7
+ # - AND and OR
8
+
6
9
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
14
11
end
15
12
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
-
25
13
rule bool_expr
26
14
string_comparison / numeric_comparison
27
15
end
28
16
29
17
# @todo - Add rule for string regex match (=~ and !~)
30
18
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>
36
20
end
37
21
38
22
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>
44
24
end
45
25
46
26
rule and_or
Original file line number Diff line number Diff line change @@ -13,9 +13,9 @@ module Tcsh2Bash
13
13
#end
14
14
15
15
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) {
17
17
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"
19
19
end
20
20
}
21
21
end
Original file line number Diff line number Diff line change @@ -20,19 +20,11 @@ module Tcsh2Bash
20
20
end
21
21
22
22
rule open_paren
23
- '(' {
24
- def to_bash
25
- '['
26
- end
27
- }
23
+ '('
28
24
end
29
25
30
26
rule close_paren
31
- ')' {
32
- def to_bash
33
- ']'
34
- end
35
- }
27
+ ')'
36
28
end
37
29
38
30
rule quote
Original file line number Diff line number Diff line change @@ -6,8 +6,6 @@ module Tcsh2Bash
6
6
@parser = BooleanParser . new
7
7
end
8
8
9
-
10
-
11
9
describe "string comparison" do
12
10
specify { convert_to_bash ( '("foo" == "foo")' ) . should == '[ "foo" == "foo" ]' }
13
11
specify { convert_to_bash ( '("foo" != "bar")' ) . should == '[ "foo" != "bar" ]' }
@@ -24,7 +22,7 @@ module Tcsh2Bash
24
22
25
23
# @todo - Figure out this situation
26
24
describe "multiple parentheses" do
27
- specify { convert_to_bash ( " (((1 == 1 )))" ) . should == "[ 1 == 1 ]" }
25
+ specify { convert_to_bash ( ' ((("foo" == "bar" )))' ) . should == '[ "foo" == "bar" ]' }
28
26
end
29
27
30
28
=begin
Original file line number Diff line number Diff line change @@ -7,15 +7,11 @@ module Tcsh2Bash
7
7
end
8
8
9
9
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 }
13
11
end
14
12
15
13
describe 'two lines' do
16
- it 'should parse this' do
17
- parse ( "set foo=bar\n alias hello world" ) . should_not be_nil
18
- end
14
+ specify { parse ( "set foo=bar\n alias hello world" ) . should_not be_nil }
19
15
end
20
16
21
17
describe 'bad line' do
You can’t perform that action at this time.
0 commit comments