Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
社会瑞弟呀
brakeman
提交
0cc78c56
B
brakeman
项目概览
社会瑞弟呀
/
brakeman
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
brakeman
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0cc78c56
编写于
12月 06, 2012
作者:
J
Justin Collins
浏览文件
操作
浏览文件
下载
差异文件
Add methods to Sexp to avoid Sexp creation
上级
dcf13205
294d4bed
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
101 addition
and
0 deletion
+101
-0
lib/ruby_parser/bm_sexp.rb
lib/ruby_parser/bm_sexp.rb
+75
-0
test/tests/test_sexp.rb
test/tests/test_sexp.rb
+26
-0
未找到文件。
lib/ruby_parser/bm_sexp.rb
浏览文件 @
0cc78c56
...
...
@@ -254,6 +254,35 @@ class Sexp
end
end
def
each_arg
replace
=
false
expect
:call
,
:attrasgn
,
:super
,
:zsuper
range
=
nil
case
self
.
node_type
when
:call
,
:attrasgn
if
self
[
3
]
range
=
(
3
...
self
.
length
)
end
when
:super
,
:zsuper
if
self
[
1
]
range
=
(
1
...
self
.
length
)
end
end
if
range
range
.
each
do
|
i
|
res
=
yield
self
[
i
]
self
[
i
]
=
res
if
replace
end
end
self
end
def
each_arg!
&
block
self
.
each_arg
true
,
&
block
end
#Returns first argument of a method call.
def
first_arg
expect
:call
,
:attrasgn
...
...
@@ -278,6 +307,26 @@ class Sexp
self
[
4
]
=
exp
end
def
third_arg
expect
:call
,
:attrasgn
self
[
5
]
end
def
third_arg
=
exp
expect
:call
,
:attrasgn
self
[
5
]
=
exp
end
def
last_arg
expect
:call
,
:attrasgn
if
self
[
3
]
self
[
-
1
]
else
nil
end
end
#Returns condition of an if expression:
#
# s(:if,
...
...
@@ -462,6 +511,32 @@ class Sexp
self
.
body
.
unshift
:rlist
end
def
each_body
replace
=
false
expect
:defn
,
:defs
,
:methdef
,
:selfdef
,
:class
,
:module
range
=
case
self
.
node_type
when
:defn
,
:methdef
,
:class
(
3
...
self
.
length
)
when
:defs
,
:selfdef
(
4
...
self
.
length
)
when
:module
(
2
...
self
.
length
)
end
if
range
range
.
each
do
|
i
|
res
=
yield
self
[
i
]
self
[
i
]
=
res
if
replace
end
end
self
end
def
each_body!
&
block
each_body
true
,
&
block
end
def
render_type
expect
:render
self
[
1
]
...
...
test/tests/test_sexp.rb
浏览文件 @
0cc78c56
...
...
@@ -18,6 +18,7 @@ class SexpTests < Test::Unit::TestCase
assert_equal
s
(
:arglist
),
exp
.
arglist
assert_nil
exp
.
first_arg
assert_nil
exp
.
second_arg
assert_nil
exp
.
last_arg
end
def
test_method_call_with_args
...
...
@@ -29,6 +30,7 @@ class SexpTests < Test::Unit::TestCase
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
),
s
(
:lit
,
3
)),
exp
.
arglist
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
assert_equal
s
(
:lit
,
3
),
exp
.
last_arg
end
def
test_method_call_no_target
...
...
@@ -40,6 +42,7 @@ class SexpTests < Test::Unit::TestCase
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
),
s
(
:lit
,
3
)),
exp
.
arglist
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
assert_equal
s
(
:lit
,
3
),
exp
.
last_arg
end
def
test_method_call_set_target
...
...
@@ -55,6 +58,7 @@ class SexpTests < Test::Unit::TestCase
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
assert_equal
s
(
:lit
,
2
),
exp
.
last_arg
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
)),
exp
.
arglist
assert_equal
s
(
s
(
:lit
,
1
),
s
(
:lit
,
2
)),
exp
.
args
end
...
...
@@ -69,6 +73,7 @@ class SexpTests < Test::Unit::TestCase
assert_equal
s
(
s
(
:lit
,
1
),
s
(
:lit
,
2
)),
exp
.
args
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
assert_equal
s
(
:lit
,
2
),
exp
.
last_arg
end
def
test_method_call_with_block
...
...
@@ -277,4 +282,25 @@ class SexpTests < Test::Unit::TestCase
assert_equal
s
(
:iasgn
,
:@x
,
s
(
:lit
,
1
)),
exp
.
iasgn
(
true
)
assert_equal
nil
,
exp
.
iasgn
#Was deleted
end
def
test_each_arg
exp
=
parse
"blah 1, 2, 3"
args
=
[]
exp
.
each_arg
do
|
a
|
args
<<
a
.
value
end
assert_equal
[
1
,
2
,
3
],
args
end
def
test_each_arg!
exp
=
parse
"blah 1, 2"
exp
.
each_arg!
do
|
a
|
s
(
:lit
,
a
.
value
+
1
)
end
assert_equal
s
(
:lit
,
2
),
exp
.
first_arg
assert_equal
s
(
:lit
,
3
),
exp
.
second_arg
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录