Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
999f6926
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,发现更多精彩内容 >>
提交
999f6926
编写于
5月 07, 2013
作者:
J
John Clements
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
getting rid of interner_key! macro
上级
fc4f304e
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
60 addition
and
125 deletion
+60
-125
src/librustdoc/extract.rs
src/librustdoc/extract.rs
+6
-12
src/libsyntax/ast.rs
src/libsyntax/ast.rs
+6
-18
src/libsyntax/ext/expand.rs
src/libsyntax/ext/expand.rs
+1
-1
src/libsyntax/parse/lexer.rs
src/libsyntax/parse/lexer.rs
+1
-1
src/libsyntax/parse/mod.rs
src/libsyntax/parse/mod.rs
+22
-21
src/libsyntax/parse/token.rs
src/libsyntax/parse/token.rs
+19
-10
src/libsyntax/syntax.rc
src/libsyntax/syntax.rc
+0
-3
src/libsyntax/util/interner.rs
src/libsyntax/util/interner.rs
+2
-14
src/libsyntax/util/parser_testing.rs
src/libsyntax/util/parser_testing.rs
+3
-45
未找到文件。
src/librustdoc/extract.rs
浏览文件 @
999f6926
...
...
@@ -17,24 +17,18 @@
use
core
::
local_data
::
local_data_get
;
use
syntax
::
ast
;
use
syntax
;
/* can't import macros yet, so this is copied from token.rs. See its comment
* there. */
macro_rules!
interner_key
(
()
=>
(
cast
::
transmute
::
<
(
uint
,
uint
),
&
fn
(
v
:
@@
syntax
::
parse
::
token
::
ident_interner
)
>
((
-
3
as
uint
,
0u
)))
)
use
syntax
::
parse
::
token
::{
ident_interner
};
use
syntax
::
parse
::
token
;
// Hack; rather than thread an interner through everywhere, rely on
// thread-local data
pub
fn
to_str
(
id
:
ast
::
ident
)
->
~
str
{
let
intr
=
unsafe
{
local_data_get
(
interner_key!
())
};
return
copy
*
(
*
intr
.get
())
.get
(
id
);
let
intr
=
token
::
get_ident_interner
();
return
copy
*
(
*
intr
)
.get
(
id
);
}
pub
fn
interner
()
->
@
syntax
::
parse
::
token
::
ident_interner
{
return
*
(
unsafe
{
local_data_get
(
interner_key!
())
})
.get
();
pub
fn
interner
()
->
@
ident_interner
{
return
token
::
get_ident_interner
();
}
pub
fn
from_srv
(
...
...
src/libsyntax/ast.rs
浏览文件 @
999f6926
...
...
@@ -13,9 +13,10 @@
use
codemap
::{
span
,
spanned
};
use
abi
::
AbiSet
;
use
opt_vec
::
OptVec
;
use
parse
::
token
::
get_ident_interner
;
use
core
::
cast
;
use
core
::
option
::{
None
,
Option
,
Some
};
use
core
::
option
::{
Option
};
use
core
::
to_bytes
;
use
core
::
to_bytes
::
IterBytes
;
use
core
::
to_str
::
ToStr
;
...
...
@@ -67,7 +68,7 @@ pub enum SyntaxContext_ {
// in the "from" slot. In essence, they're all
// pointers to a single "rename" event node.
Rename
(
ident
,
Name
,
SyntaxContext
),
IllegalCtxt
()
IllegalCtxt
()
}
// a name represents an identifier
...
...
@@ -78,27 +79,14 @@ pub enum SyntaxContext_ {
impl
<
S
:
Encoder
>
Encodable
<
S
>
for
ident
{
fn
encode
(
&
self
,
s
:
&
mut
S
)
{
unsafe
{
let
intr
=
match
local_data
::
local_data_get
(
interner_key!
())
{
None
=>
fail
!
(
"encode: TLS interner not set up"
),
Some
(
intr
)
=>
intr
};
s
.emit_str
(
*
(
*
intr
)
.get
(
*
self
));
}
let
intr
=
get_ident_interner
();
s
.emit_str
(
*
(
*
intr
)
.get
(
*
self
));
}
}
impl
<
D
:
Decoder
>
Decodable
<
D
>
for
ident
{
fn
decode
(
d
:
&
mut
D
)
->
ident
{
let
intr
=
match
unsafe
{
local_data
::
local_data_get
(
interner_key!
())
}
{
None
=>
fail
!
(
"decode: TLS interner not set up"
),
Some
(
intr
)
=>
intr
};
let
intr
=
get_ident_interner
();
(
*
intr
)
.intern
(
d
.read_str
())
}
}
...
...
src/libsyntax/ext/expand.rs
浏览文件 @
999f6926
...
...
@@ -806,7 +806,7 @@ fn renaming () {
};
let
table
=
@
mut
new_sctable
();
let
a_name
=
100
;
// enforced by testing_interner
let
a2_name
=
sess
.interner
.gensym
(
@~
"a2"
)
.repr
;
let
a2_name
=
sess
.interner
.gensym
(
"a2"
)
.repr
;
let
renamer
=
new_ident_renamer
(
ast
::
ident
{
repr
:
a_name
,
ctxt
:
empty_ctxt
},
a2_name
,
table
);
let
renamed_ast
=
fun_to_ident_folder
(
renamer
)
.fold_item
(
item_ast
)
.get
();
...
...
src/libsyntax/parse/lexer.rs
浏览文件 @
999f6926
...
...
@@ -786,7 +786,7 @@ struct Env {
fn
setup
(
teststr
:
~
str
)
->
Env
{
let
cm
=
CodeMap
::
new
();
let
fm
=
cm
.new_filemap
(
~
"zebra.rs"
,
@
teststr
);
let
ident_interner
=
token
::
mk_ident_interner
();
// interner::mk
();
let
ident_interner
=
token
::
get_ident_interner
();
let
span_handler
=
diagnostic
::
mk_span_handler
(
diagnostic
::
mk_handler
(
None
),
@
cm
);
Env
{
...
...
src/libsyntax/parse/mod.rs
浏览文件 @
999f6926
...
...
@@ -19,7 +19,7 @@
use
parse
::
attr
::
parser_attr
;
use
parse
::
lexer
::
reader
;
use
parse
::
parser
::
Parser
;
use
parse
::
token
::{
ident_interner
,
mk
_ident_interner
};
use
parse
::
token
::{
ident_interner
,
get
_ident_interner
};
use
core
::
io
;
use
core
::
option
::{
None
,
Option
,
Some
};
...
...
@@ -59,7 +59,7 @@ pub fn new_parse_sess(demitter: Option<Emitter>) -> @mut ParseSess {
cm
:
cm
,
next_id
:
1
,
span_diagnostic
:
mk_span_handler
(
mk_handler
(
demitter
),
cm
),
interner
:
mk
_ident_interner
(),
interner
:
get
_ident_interner
(),
}
}
...
...
@@ -70,7 +70,7 @@ pub fn new_parse_sess_special_handler(sh: @span_handler,
cm
:
cm
,
next_id
:
1
,
span_diagnostic
:
sh
,
interner
:
mk
_ident_interner
(),
interner
:
get
_ident_interner
(),
}
}
...
...
@@ -352,8 +352,8 @@ mod test {
use
opt_vec
;
use
ast
;
use
abi
;
use
ast_util
::
new_ident
;
use
parse
::
parser
::
Parser
;
use
parse
::
token
::
intern
;
use
util
::
parser_testing
::{
string_to_tts_and_sess
,
string_to_parser
};
use
util
::
parser_testing
::{
string_to_expr
,
string_to_item
};
use
util
::
parser_testing
::{
string_to_stmt
};
...
...
@@ -378,8 +378,8 @@ fn sp (a: uint, b: uint) -> span {
}
// convert a vector of uints to a vector of ast::idents
fn
ints_to_idents
(
ids
:
~
[
uint
])
->
~
[
ast
::
ident
]
{
ids
.map
(|
u
|
new_ident
(
*
u
))
fn
ints_to_idents
(
ids
:
~
[
~
str
])
->
~
[
ast
::
ident
]
{
ids
.map
(|
u
|
intern
(
*
u
))
}
#[test]
fn
path_exprs_1
()
{
...
...
@@ -388,7 +388,7 @@ fn ints_to_idents(ids: ~[uint]) -> ~[ast::ident] {
callee_id
:
2
,
node
:
ast
::
expr_path
(
@
ast
::
Path
{
span
:
sp
(
0
,
1
),
global
:
false
,
idents
:
~
[
new_ident
(
100
)],
idents
:
~
[
intern
(
"a"
)],
rp
:
None
,
types
:
~
[]}),
span
:
sp
(
0
,
1
)})
...
...
@@ -398,11 +398,12 @@ fn ints_to_idents(ids: ~[uint]) -> ~[ast::ident] {
assert_eq!
(
string_to_expr
(
@~
"::a::b"
),
@
ast
::
expr
{
id
:
1
,
callee_id
:
2
,
node
:
ast
::
expr_path
(
@
ast
::
Path
{
span
:
sp
(
0
,
6
),
global
:
true
,
idents
:
ints_to_idents
(
~
[
100
,
101
]),
rp
:
None
,
types
:
~
[]}),
node
:
ast
::
expr_path
(
@
ast
::
Path
{
span
:
sp
(
0
,
6
),
global
:
true
,
idents
:
ints_to_idents
(
~
[
~
"a"
,
~
"b"
]),
rp
:
None
,
types
:
~
[]}),
span
:
sp
(
0
,
6
)})
}
...
...
@@ -451,7 +452,7 @@ fn ints_to_idents(ids: ~[uint]) -> ~[ast::ident] {
node
:
ast
::
expr_path
(
@
ast
::
Path
{
span
:
sp
(
7
,
8
),
global
:
false
,
idents
:
~
[
new_ident
(
103
)],
idents
:
~
[
intern
(
"d"
)],
rp
:
None
,
types
:
~
[]
}),
...
...
@@ -469,7 +470,7 @@ fn ints_to_idents(ids: ~[uint]) -> ~[ast::ident] {
@
ast
::
Path
{
span
:
sp
(
0
,
1
),
global
:
false
,
idents
:
~
[
new_ident
(
101
)],
idents
:
~
[
intern
(
"b"
)],
rp
:
None
,
types
:
~
[]}),
span
:
sp
(
0
,
1
)},
...
...
@@ -490,7 +491,7 @@ fn parser_done(p: Parser){
@
ast
::
Path
{
span
:
sp
(
0
,
1
),
global
:
false
,
idents
:
~
[
new_ident
(
101
)],
idents
:
~
[
intern
(
"b"
)],
rp
:
None
,
types
:
~
[]},
None
// no idea
...
...
@@ -509,7 +510,7 @@ fn parser_done(p: Parser){
span
:
sp
(
4
,
4
),
// this is bizarre...
// check this in the original parser?
global
:
false
,
idents
:
~
[
new_ident
(
105
)],
idents
:
~
[
intern
(
"int"
)],
rp
:
None
,
types
:
~
[]},
2
),
...
...
@@ -519,7 +520,7 @@ fn parser_done(p: Parser){
@
ast
::
Path
{
span
:
sp
(
0
,
1
),
global
:
false
,
idents
:
~
[
new_ident
(
101
)],
idents
:
~
[
intern
(
"b"
)],
rp
:
None
,
types
:
~
[]},
None
// no idea
...
...
@@ -535,7 +536,7 @@ fn parser_done(p: Parser){
// assignment order of the node_ids.
assert_eq!
(
string_to_item
(
@~
"fn a (b : int) { b; }"
),
Some
(
@
ast
::
item
{
ident
:
new_ident
(
100
),
@
ast
::
item
{
ident
:
intern
(
"a"
),
attrs
:
~
[],
id
:
10
,
// fixme
node
:
ast
::
item_fn
(
ast
::
fn_decl
{
...
...
@@ -545,7 +546,7 @@ fn parser_done(p: Parser){
node
:
ast
::
ty_path
(
@
ast
::
Path
{
span
:
sp
(
10
,
13
),
global
:
false
,
idents
:
~
[
new_ident
(
106
)],
idents
:
~
[
intern
(
"int"
)],
rp
:
None
,
types
:
~
[]},
2
),
...
...
@@ -556,7 +557,7 @@ fn parser_done(p: Parser){
@
ast
::
Path
{
span
:
sp
(
6
,
7
),
global
:
false
,
idents
:
~
[
new_ident
(
101
)],
idents
:
~
[
intern
(
"b"
)],
rp
:
None
,
types
:
~
[]},
None
// no idea
...
...
@@ -587,7 +588,7 @@ fn parser_done(p: Parser){
@
ast
::
Path
{
span
:
sp
(
17
,
18
),
global
:
false
,
idents
:
~
[
new_ident
(
101
)],
idents
:
~
[
intern
(
"b"
)],
rp
:
None
,
types
:
~
[]}),
span
:
sp
(
17
,
18
)},
...
...
src/libsyntax/parse/token.rs
浏览文件 @
999f6926
...
...
@@ -411,8 +411,7 @@ fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q)
}
// return a fresh interner, preloaded with special identifiers.
// EFFECT: stores this interner in TLS
pub
fn
mk_fresh_ident_interner
()
->
@
ident_interner
{
fn
mk_fresh_ident_interner
()
->
@
ident_interner
{
// the indices here must correspond to the numbers in
// special_idents.
let
init_vec
=
~
[
...
...
@@ -453,23 +452,27 @@ pub fn mk_fresh_ident_interner() -> @ident_interner {
"Self"
,
// 34
];
let
rv
=
@
ident_interner
{
@
ident_interner
{
interner
:
interner
::
StrInterner
::
prefill
(
init_vec
)
};
unsafe
{
local_data
::
local_data_set
(
interner_key!
(),
@
rv
);
}
rv
}
// if an interner exists in TLS, return it. Otherwise, prepare a
// fresh one.
pub
fn
mk
_ident_interner
()
->
@
ident_interner
{
pub
fn
get
_ident_interner
()
->
@
ident_interner
{
unsafe
{
match
local_data
::
local_data_get
(
interner_key!
())
{
let
key
=
(
cast
::
transmute
::
<
(
uint
,
uint
),
&
fn
(
v
:
@@
::
parse
::
token
::
ident_interner
)
>
(
(
-
3
as
uint
,
0u
)));
match
local_data
::
local_data_get
(
key
)
{
Some
(
interner
)
=>
*
interner
,
None
=>
{
mk_fresh_ident_interner
()
let
interner
=
mk_fresh_ident_interner
();
unsafe
{
local_data
::
local_data_set
(
key
,
@
interner
);
}
interner
}
}
}
...
...
@@ -481,6 +484,12 @@ pub fn mk_fake_ident_interner() -> @ident_interner {
@
ident_interner
{
interner
:
interner
::
StrInterner
::
new
()
}
}
// maps a string to its interned representation
pub
fn
intern
(
str
:
&
str
)
->
ast
::
ident
{
let
interner
=
get_ident_interner
();
interner
.intern
(
str
)
}
/**
* All the valid words that have meaning in the Rust language.
*
...
...
src/libsyntax/syntax.rc
浏览文件 @
999f6926
...
...
@@ -25,9 +25,6 @@
extern mod std;
// allow the interner_key macro
// to escape this module:
#[macro_escape]
pub mod util {
pub mod interner;
#[cfg(test)]
...
...
src/libsyntax/util/interner.rs
浏览文件 @
999f6926
...
...
@@ -12,9 +12,6 @@
// allows bidirectional lookup; i.e. given a value, one can easily find the
// type, and vice versa.
// allow the interner_key macro to escape this module:
#[macro_escape]
;
use
core
::
cmp
::
Equiv
;
use
core
::
hashmap
::
HashMap
;
use
syntax
::
parse
::
token
::
StringRef
;
...
...
@@ -78,6 +75,8 @@ fn find_equiv<Q:Hash + IterBytes + Equiv<T>>(&self, val: &Q)
}
}
// A StrInterner differs from Interner<String> in that it accepts
// borrowed pointers rather than @ ones, resulting in less allocation.
pub
struct
StrInterner
{
priv
map
:
@
mut
HashMap
<@~
str
,
uint
>
,
priv
vect
:
@
mut
~
[
@~
str
],
...
...
@@ -133,17 +132,6 @@ fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q)
}
}
/* Key for thread-local data for sneaking interner information to the
* encoder/decoder. It sounds like a hack because it is one.
* Bonus ultra-hack: functions as keys don't work across crates,
* so we have to use a unique number. See taskgroup_key! in task.rs
* for another case of this. */
macro_rules!
interner_key
(
()
=>
(
cast
::
transmute
::
<
(
uint
,
uint
),
&
fn
(
v
:
@@
::
parse
::
token
::
ident_interner
)
>
(
(
-
3
as
uint
,
0u
)))
)
#[cfg(test)]
mod
tests
{
use
super
::
*
;
...
...
src/libsyntax/util/parser_testing.rs
浏览文件 @
999f6926
...
...
@@ -9,64 +9,22 @@
// except according to those terms.
use
core
::
option
::{
Option
,
None
};
use
core
::
int
;
use
core
::
num
::
NumCast
;
use
codemap
::
CodeMap
;
use
ast
;
use
parse
::
parser
::
Parser
;
use
parse
::
token
::{
ident_interner
,
mk_fresh_ident_interner
};
use
diagnostic
::{
mk_handler
,
mk_span_handler
};
use
parse
::{
new_parse_sess
};
use
syntax
::
parse
::{
ParseSess
,
string_to_filemap
,
filemap_to_tts
};
use
syntax
::
parse
::{
new_parser_from_source_str
};
// add known names to interner for testing
fn
mk_testing_interner
()
->
@
ident_interner
{
let
i
=
mk_fresh_ident_interner
();
// baby hack; in order to put the identifiers
// 'a' and 'b' at known locations, we're going
// to fill up the interner to length 100. If
// the # of preloaded items on the interner
// ever gets larger than 100, we'll have to
// adjust this number (say, to 200) and
// change the numbers in the identifier
// test cases below.
assert
!
(
i
.len
()
<
100
);
for
int
::
range
(
0
,
100
-
((
i
.len
())
.to_int
()))
|
_
dc
|
{
i
.gensym
(
~
"dontcare"
);
}
i
.intern
(
"a"
);
i
.intern
(
"b"
);
i
.intern
(
"c"
);
i
.intern
(
"d"
);
i
.intern
(
"return"
);
assert
!
(
i
.get
(
ast
::
ident
{
repr
:
101
,
ctxt
:
0
})
==
@~
"b"
);
i
}
// make a parse_sess that's closed over a
// testing interner (where a -> 100, b -> 101)
fn
mk_testing_parse_sess
()
->
@
mut
ParseSess
{
let
interner
=
mk_testing_interner
();
let
cm
=
@
CodeMap
::
new
();
@
mut
ParseSess
{
cm
:
cm
,
next_id
:
1
,
span_diagnostic
:
mk_span_handler
(
mk_handler
(
None
),
cm
),
interner
:
interner
,
}
}
// map a string to tts, using a made-up filename: return both the token_trees
// and the ParseSess
pub
fn
string_to_tts_and_sess
(
source_str
:
@~
str
)
->
(
~
[
ast
::
token_tree
],
@
mut
ParseSess
)
{
let
ps
=
mk_testing_parse_sess
(
);
let
ps
=
new_parse_sess
(
None
);
(
filemap_to_tts
(
ps
,
string_to_filemap
(
ps
,
source_str
,
~
"bogofile"
)),
ps
)
}
pub
fn
string_to_parser_and_sess
(
source_str
:
@~
str
)
->
(
Parser
,
@
mut
ParseSess
)
{
let
ps
=
mk_testing_parse_sess
(
);
let
ps
=
new_parse_sess
(
None
);
(
new_parser_from_source_str
(
ps
,
~
[],
~
"bogofile"
,
source_str
),
ps
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录