Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
1f0621d0
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,发现更多精彩内容 >>
提交
1f0621d0
编写于
9月 21, 2010
作者:
G
Graydon Hoare
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Begin teaching rustc to parse literals, atoms, stmts, blocks, items, modules, crates.
上级
8beb118d
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
182 addition
and
38 deletion
+182
-38
src/comp/driver/rustc.rs
src/comp/driver/rustc.rs
+6
-16
src/comp/fe/ast.rs
src/comp/fe/ast.rs
+7
-2
src/comp/fe/lexer.rs
src/comp/fe/lexer.rs
+1
-6
src/comp/fe/parser.rs
src/comp/fe/parser.rs
+161
-14
src/comp/fe/token.rs
src/comp/fe/token.rs
+1
-0
src/comp/util/common.rs
src/comp/util/common.rs
+6
-0
未找到文件。
src/comp/driver/rustc.rs
浏览文件 @
1f0621d0
...
...
@@ -27,22 +27,12 @@ fn main(vec[str] args) {
auto
i
=
0
;
auto
sess
=
session
.session
();
for
(
str
filename
in
args
)
{
if
(
i
>
0
)
{
auto
p
=
parser
.new_parser
(
sess
,
filename
);
log
"opened file: "
+
filename
;
auto
tok
=
p
.peek
();
while
(
true
)
{
alt
(
tok
)
{
case
(
token
.EOF
())
{
ret
;
}
case
(
_
)
{
log
token
.to_str
(
tok
);
p
.bump
();
tok
=
p
.peek
();
}
}
}
}
i
+=
1
;
if
(
i
>
0
)
{
auto
p
=
parser
.new_parser
(
sess
,
filename
);
log
"opened file: "
+
filename
;
auto
crate
=
parser
.parse_crate
(
p
);
}
i
+=
1
;
}
// Test LLVM module-writing. Nothing interesting yet.
...
...
src/comp/fe/ast.rs
浏览文件 @
1f0621d0
...
...
@@ -5,8 +5,7 @@
type
ident
=
str
;
type
crate
=
rec
(
str
filename
,
_
mod
module
);
type
crate
=
rec
(
_
mod
module
);
type
block
=
vec
[
@
stmt
];
...
...
@@ -14,6 +13,7 @@
stmt_block
(
block
);
stmt_decl
(
@
decl
);
stmt_ret
(
option
[
@
lval
]);
stmt_log
(
@
atom
);
}
...
...
@@ -36,6 +36,7 @@
tag
lit
{
lit_char
(
char
);
lit_int
(
int
);
lit_uint
(
uint
);
lit_nil
;
lit_bool
(
bool
);
}
...
...
@@ -44,7 +45,11 @@
ty_nil
;
ty_bool
;
ty_int
;
ty_uint
;
ty_machine
(
util
.common.ty_mach
);
ty_char
;
ty_str
;
ty_box
(
@
ty
);
}
tag
mode
{
...
...
src/comp/fe/lexer.rs
浏览文件 @
1f0621d0
...
...
@@ -3,12 +3,7 @@
import
std
.map
;
import
std
.map.hashmap
;
import
util
.common
;
fn
new_str_hash
[
V
]()
->
map
.hashmap
[
str
,
V
]
{
let
map
.hashfn
[
str
]
hasher
=
_
str
.hash
;
let
map
.eqfn
[
str
]
eqer
=
_
str
.eq
;
ret
map
.mk_hashmap
[
str
,
V
](
hasher
,
eqer
);
}
import
util
.common.new_str_hash
;
state
type
reader
=
state
obj
{
fn
is_eof
()
->
bool
;
...
...
src/comp/fe/parser.rs
浏览文件 @
1f0621d0
import
std
._io
;
import
driver
.session
;
import
util
.common
;
import
util
.common.new_str_hash
;
// FIXME: import std.util.option and use it here.
// import std.util.option;
tag
option
[
T
]
{
none
;
some
(
T
);
}
state
type
parser
=
state
obj
{
...
...
@@ -19,6 +29,7 @@
lexer
.reader
rdr
)
{
state
fn
peek
()
->
token
.token
{
log
token
.to_str
(
tok
);
ret
tok
;
}
...
...
@@ -56,14 +67,14 @@ fn get_span() -> common.span {
let
str
s
=
"expecting "
;
s
+=
token
.to_str
(
t
);
s
+=
", found "
;
s
+=
token
.to_str
(
t
);
s
+=
token
.to_str
(
p
.peek
()
);
p
.err
(
s
);
}
}
state
fn
parse_ident
(
parser
p
)
->
ast
.ident
{
alt
(
p
.peek
())
{
case
(
token
.IDENT
(
?
i
))
{
ret
i
;
}
case
(
token
.IDENT
(
?
i
))
{
p
.bump
();
ret
i
;
}
case
(
_
)
{
p
.err
(
"expecting ident"
);
fail
;
...
...
@@ -71,25 +82,161 @@ fn get_span() -> common.span {
}
}
state
fn
parse_item
(
parser
p
)
->
tup
(
ast
.ident
,
ast
.item
)
{
state
fn
parse_ty
(
parser
p
)
->
ast
.ty
{
alt
(
p
.peek
())
{
case
(
token
.INT
)
{
p
.bump
();
ret
ast
.ty_int
;
}
case
(
token
.UINT
)
{
p
.bump
();
ret
ast
.ty_int
;
}
case
(
token
.STR
)
{
p
.bump
();
ret
ast
.ty_str
;
}
case
(
token
.CHAR
)
{
p
.bump
();
ret
ast
.ty_char
;
}
case
(
token
.MACH
(
?
tm
))
{
p
.bump
();
ret
ast
.ty_machine
(
tm
);
}
}
p
.err
(
"expecting type"
);
fail
;
}
state
fn
parse_slot
(
parser
p
)
->
ast
.slot
{
let
ast
.mode
m
=
ast
.val
;
if
(
p
.peek
()
==
token
.BINOP
(
token
.AND
))
{
m
=
ast
.alias
;
p
.bump
();
}
let
ast
.ty
t
=
parse_ty
(
p
);
ret
rec
(
ty
=
t
,
mode
=
m
);
}
state
fn
parse_seq
[
T
](
token
.token
bra
,
token
.token
ket
,
option
[
token
.token
]
sep
,
(
state
fn
(
parser
)
->
T
)
f
,
parser
p
)
->
vec
[
T
]
{
let
bool
first
=
true
;
expect
(
p
,
bra
);
let
vec
[
T
]
v
=
vec
();
while
(
p
.peek
()
!=
ket
)
{
alt
(
sep
)
{
case
(
some
[
token
.token
](
?
t
))
{
if
(
first
)
{
first
=
false
;
}
else
{
expect
(
p
,
t
);
}
}
case
(
_
)
{
}
}
// FIXME: v += f(p) doesn't work at the moment.
let
T
t
=
f
(
p
);
v
+=
vec
(
t
);
}
expect
(
p
,
ket
);
ret
v
;
}
state
fn
parse_lit
(
parser
p
)
->
ast
.lit
{
alt
(
p
.peek
())
{
case
(
token
.FN
())
{
case
(
token
.LIT_INT
(
?
i
))
{
p
.bump
();
ret
ast
.lit_int
(
i
);
}
case
(
token
.LIT_UINT
(
?
u
))
{
p
.bump
();
auto
id
=
parse_ident
(
p
);
expect
(
p
,
token
.LPAREN
);
let
vec
[
rec
(
ast
.slot
slot
,
ast
.ident
ident
)]
inputs
=
vec
();
let
vec
[
@
ast
.stmt
]
body
=
vec
();
auto
output
=
rec
(
ty
=
ast
.ty_nil
,
mode
=
ast
.val
);
let
ast
._fn
f
=
rec
(
inputs
=
inputs
,
output
=
output
,
body
=
body
);
ret
tup
(
id
,
ast
.item_fn
(
@
f
)
);
ret
ast
.lit_uint
(
u
);
}
case
(
token
.LIT_CHAR
(
?
c
))
{
p
.bump
();
ret
ast
.lit_char
(
c
);
}
case
(
token
.LIT_BOOL
(
?
b
))
{
p
.bump
(
);
ret
ast
.lit_bool
(
b
);
}
}
p
.err
(
"expect
ing item
"
);
p
.err
(
"expect
ed literal
"
);
fail
;
}
state
fn
parse_atom
(
parser
p
)
->
ast
.atom
{
ret
ast
.atom_lit
(
@
parse_lit
(
p
));
}
state
fn
parse_stmt
(
parser
p
)
->
@
ast
.stmt
{
alt
(
p
.peek
())
{
case
(
token
.LOG
)
{
p
.bump
();
auto
a
=
@
parse_atom
(
p
);
expect
(
p
,
token
.SEMI
);
ret
@
ast
.stmt_log
(
a
);
}
}
p
.err
(
"expected statement"
);
fail
;
}
state
fn
parse_block
(
parser
p
)
->
ast
.block
{
auto
f
=
parse_stmt
;
// FIXME: passing parse_stmt as an lval doesn't work at the moment.
ret
parse_seq
[
@
ast
.stmt
](
token
.LBRACE
,
token
.RBRACE
,
none
[
token
.token
],
f
,
p
);
}
state
fn
parse_slot_ident_pair
(
parser
p
)
->
rec
(
ast
.slot
slot
,
ast
.ident
ident
)
{
auto
s
=
parse_slot
(
p
);
auto
i
=
parse_ident
(
p
);
ret
rec
(
slot
=
s
,
ident
=
i
);
}
state
fn
parse_fn
(
parser
p
)
->
tup
(
ast
.ident
,
ast
.item
)
{
expect
(
p
,
token
.FN
);
auto
id
=
parse_ident
(
p
);
auto
pf
=
parse_slot_ident_pair
;
auto
inputs
=
// FIXME: passing parse_slot_ident_pair as an lval doesn't work at the
// moment.
parse_seq
[
rec
(
ast
.slot
slot
,
ast
.ident
ident
)]
(
token
.LPAREN
,
token
.RPAREN
,
some
(
token
.COMMA
),
pf
,
p
);
auto
output
;
if
(
p
.peek
()
==
token
.RARROW
)
{
p
.bump
();
output
=
rec
(
ty
=
parse_ty
(
p
),
mode
=
ast
.val
);
}
else
{
output
=
rec
(
ty
=
ast
.ty_nil
,
mode
=
ast
.val
);
}
auto
body
=
parse_block
(
p
);
let
ast
._fn
f
=
rec
(
inputs
=
inputs
,
output
=
output
,
body
=
body
);
ret
tup
(
id
,
ast
.item_fn
(
@
f
));
}
state
fn
parse_item
(
parser
p
)
->
tup
(
ast
.ident
,
ast
.item
)
{
alt
(
p
.peek
())
{
case
(
token
.FN
)
{
ret
parse_fn
(
p
);
}
}
p
.err
(
"expectied item"
);
fail
;
}
state
fn
parse_crate
(
parser
p
)
->
ast
.crate
{
let
ast
._mod
m
=
new_str_hash
[
ast
.item
]();
while
(
p
.peek
()
!=
token
.EOF
)
{
auto
i
=
parse_item
(
p
);
m
.insert
(
i
._0
,
i
._1
);
}
ret
rec
(
module
=
m
);
}
//
// Local Variables:
// mode: rust
...
...
src/comp/fe/token.rs
浏览文件 @
1f0621d0
import
util
.common.ty_mach
;
import
util
.common.ty_mach_to_str
;
import
util
.common.new_str_hash
;
import
std
._int
;
import
std
._uint
;
...
...
src/comp/util/common.rs
浏览文件 @
1f0621d0
...
...
@@ -35,6 +35,12 @@ fn ty_mach_to_str(ty_mach tm) -> str {
}
}
fn
new_str_hash
[
V
]()
->
std
.map.hashmap
[
str
,
V
]
{
let
std
.map.hashfn
[
str
]
hasher
=
std
._str.hash
;
let
std
.map.eqfn
[
str
]
eqer
=
std
._str.eq
;
ret
std
.map.mk_hashmap
[
str
,
V
](
hasher
,
eqer
);
}
//
// Local Variables:
// mode: rust
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录