Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
8dca72be
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8dca72be
编写于
2月 18, 2017
作者:
J
Jeffrey Seyfried
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize `syntax::tokenstream::Cursor`.
上级
0143774c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
63 addition
and
60 deletion
+63
-60
src/libproc_macro_plugin/qquote.rs
src/libproc_macro_plugin/qquote.rs
+4
-3
src/libsyntax/tokenstream.rs
src/libsyntax/tokenstream.rs
+59
-57
未找到文件。
src/libproc_macro_plugin/qquote.rs
浏览文件 @
8dca72be
...
...
@@ -17,6 +17,7 @@
use
syntax
::
tokenstream
::{
self
,
Delimited
,
TokenTree
,
TokenStream
};
use
syntax_pos
::
DUMMY_SP
;
use
std
::
iter
;
use
std
::
rc
::
Rc
;
pub
fn
qquote
<
'cx
>
(
stream
:
TokenStream
)
->
TokenStream
{
...
...
@@ -75,14 +76,14 @@ fn quote(&self) -> TokenStream {
return
quote!
(::
syntax
::
tokenstream
::
TokenStream
::
empty
());
}
struct
Quote
(
tokenstream
::
Cursor
);
struct
Quote
(
iter
::
Peekable
<
tokenstream
::
Cursor
>
);
impl
Iterator
for
Quote
{
type
Item
=
TokenStream
;
fn
next
(
&
mut
self
)
->
Option
<
TokenStream
>
{
let
is_unquote
=
match
self
.0
.peek
()
{
Some
(
TokenTree
::
Token
(
_
,
Token
::
Ident
(
ident
)))
if
ident
.name
==
"unquote"
=>
{
Some
(
&
TokenTree
::
Token
(
_
,
Token
::
Ident
(
ident
)))
if
ident
.name
==
"unquote"
=>
{
self
.0
.next
();
true
}
...
...
@@ -96,7 +97,7 @@ fn next(&mut self) -> Option<TokenStream> {
}
}
let
quoted
=
Quote
(
self
.trees
())
.collect
::
<
TokenStream
>
();
let
quoted
=
Quote
(
self
.trees
()
.peekable
()
)
.collect
::
<
TokenStream
>
();
quote!
([(
unquote
quoted
)]
.iter
()
.cloned
()
.collect
::
<
::
syntax
::
tokenstream
::
TokenStream
>
())
}
}
...
...
src/libsyntax/tokenstream.rs
浏览文件 @
8dca72be
...
...
@@ -299,7 +299,7 @@ fn from(tt: TokenTree) -> TokenStream {
impl
<
T
:
Into
<
TokenStream
>>
iter
::
FromIterator
<
T
>
for
TokenStream
{
fn
from_iter
<
I
:
IntoIterator
<
Item
=
T
>>
(
iter
:
I
)
->
Self
{
TokenStream
::
concat
(
iter
.into_iter
()
.map
(
Into
::
into
))
TokenStream
::
concat
(
iter
.into_iter
()
.map
(
Into
::
into
)
.collect
::
<
Vec
<
_
>>
()
)
}
}
...
...
@@ -323,19 +323,16 @@ pub fn is_empty(&self) -> bool {
}
}
pub
fn
concat
<
I
:
IntoIterator
<
Item
=
TokenStream
>>
(
streams
:
I
)
->
TokenStream
{
let
mut
streams
=
streams
.into_iter
()
.filter
(|
stream
|
!
stream
.is_empty
());
let
first_stream
=
match
streams
.next
()
{
Some
(
stream
)
=>
stream
,
None
=>
return
TokenStream
::
empty
(),
};
let
second_stream
=
match
streams
.next
()
{
Some
(
stream
)
=>
stream
,
None
=>
return
first_stream
,
};
let
mut
vec
=
vec!
[
first_stream
,
second_stream
];
vec
.extend
(
streams
);
TokenStream
{
kind
:
TokenStreamKind
::
Stream
(
RcSlice
::
new
(
vec
))
}
pub
fn
concat
(
mut
streams
:
Vec
<
TokenStream
>
)
->
TokenStream
{
match
streams
.len
()
{
0
=>
TokenStream
::
empty
(),
1
=>
TokenStream
::
from
(
streams
.pop
()
.unwrap
()),
_
=>
TokenStream
::
concat_rc_slice
(
RcSlice
::
new
(
streams
)),
}
}
fn
concat_rc_slice
(
streams
:
RcSlice
<
TokenStream
>
)
->
TokenStream
{
TokenStream
{
kind
:
TokenStreamKind
::
Stream
(
streams
)
}
}
pub
fn
trees
(
&
self
)
->
Cursor
{
...
...
@@ -357,62 +354,67 @@ pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
}
}
pub
struct
Cursor
{
current_frame
:
CursorFrame
,
stack
:
Vec
<
CursorFrame
>
,
pub
struct
Cursor
(
CursorKind
);
enum
CursorKind
{
Empty
,
Tree
(
TokenTree
,
bool
/* consumed? */
),
Stream
(
StreamCursor
),
}
struct
StreamCursor
{
stream
:
RcSlice
<
TokenStream
>
,
index
:
usize
,
stack
:
Vec
<
(
RcSlice
<
TokenStream
>
,
usize
)
>
,
}
impl
Iterator
for
Cursor
{
type
Item
=
TokenTree
;
fn
next
(
&
mut
self
)
->
Option
<
TokenTree
>
{
let
tree
=
self
.peek
();
self
.current_frame
=
self
.stack
.pop
()
.unwrap_or
(
CursorFrame
::
Empty
);
tree
}
}
enum
CursorFrame
{
Empty
,
Tree
(
TokenTree
),
Stream
(
RcSlice
<
TokenStream
>
,
usize
),
}
let
cursor
=
match
self
.0
{
CursorKind
::
Stream
(
ref
mut
cursor
)
=>
cursor
,
CursorKind
::
Tree
(
ref
tree
,
ref
mut
consumed
@
false
)
=>
{
*
consumed
=
true
;
return
Some
(
tree
.clone
());
}
_
=>
return
None
,
};
impl
CursorFrame
{
fn
new
(
stream
:
TokenStream
)
->
Self
{
match
stream
.kind
{
TokenStreamKind
::
Empty
=>
CursorFrame
::
Empty
,
TokenStreamKind
::
Tree
(
tree
)
=>
CursorFrame
::
Tree
(
tree
),
TokenStreamKind
::
Stream
(
stream
)
=>
CursorFrame
::
Stream
(
stream
,
0
),
loop
{
if
cursor
.index
<
cursor
.stream
.len
()
{
match
cursor
.stream
[
cursor
.index
]
.kind
.clone
()
{
TokenStreamKind
::
Tree
(
tree
)
=>
{
cursor
.index
+=
1
;
return
Some
(
tree
);
}
TokenStreamKind
::
Stream
(
stream
)
=>
{
cursor
.stack
.push
((
mem
::
replace
(
&
mut
cursor
.stream
,
stream
),
mem
::
replace
(
&
mut
cursor
.index
,
0
)
+
1
));
}
TokenStreamKind
::
Empty
=>
{
cursor
.index
+=
1
;
}
}
}
else
if
let
Some
((
stream
,
index
))
=
cursor
.stack
.pop
()
{
cursor
.stream
=
stream
;
cursor
.index
=
index
;
}
else
{
return
None
;
}
}
}
}
impl
Cursor
{
fn
new
(
stream
:
TokenStream
)
->
Self
{
Cursor
{
current_frame
:
CursorFrame
::
new
(
stream
),
stack
:
Vec
::
new
(),
}
}
pub
fn
peek
(
&
mut
self
)
->
Option
<
TokenTree
>
{
while
let
CursorFrame
::
Stream
(
stream
,
index
)
=
mem
::
replace
(
&
mut
self
.current_frame
,
CursorFrame
::
Empty
)
{
self
.current_frame
=
if
index
==
stream
.len
()
{
self
.stack
.pop
()
.unwrap_or
(
CursorFrame
::
Empty
)
}
else
{
let
frame
=
CursorFrame
::
new
(
stream
[
index
]
.clone
());
self
.stack
.push
(
CursorFrame
::
Stream
(
stream
,
index
+
1
));
frame
};
}
match
self
.current_frame
{
CursorFrame
::
Empty
=>
None
,
CursorFrame
::
Tree
(
ref
tree
)
=>
Some
(
tree
.clone
()),
CursorFrame
::
Stream
(
..
)
=>
unreachable!
(),
}
Cursor
(
match
stream
.kind
{
TokenStreamKind
::
Empty
=>
CursorKind
::
Empty
,
TokenStreamKind
::
Tree
(
tree
)
=>
CursorKind
::
Tree
(
tree
,
false
),
TokenStreamKind
::
Stream
(
stream
)
=>
{
CursorKind
::
Stream
(
StreamCursor
{
stream
:
stream
,
index
:
0
,
stack
:
Vec
::
new
()
})
}
})
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录