Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
1ce6cfbe
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,发现更多精彩内容 >>
提交
1ce6cfbe
编写于
3月 07, 2011
作者:
M
Marijn Haverbeke
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use a precedence table for operator parsing
上级
f017a9e7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
131 deletion
+65
-131
src/comp/front/parser.rs
src/comp/front/parser.rs
+65
-131
未找到文件。
src/comp/front/parser.rs
浏览文件 @
1ce6cfbe
...
...
@@ -35,6 +35,7 @@
fn
get_session
()
->
session
.session
;
fn
get_span
()
->
common
.span
;
fn
next_def_id
()
->
ast
.def_id
;
fn
get_prec_table
()
->
vec
[
op_spec
];
};
impure
fn
new_parser
(
session
.session
sess
,
...
...
@@ -50,7 +51,8 @@
mutable
ast
.def_num
def
,
mutable
restriction
res
,
ast
.crate_num
crate
,
lexer
.reader
rdr
)
lexer
.reader
rdr
,
vec
[
op_spec
]
precs
)
{
fn
peek
()
->
token
.token
{
ret
tok
;
...
...
@@ -100,6 +102,9 @@ fn get_env() -> eval.env {
ret
env
;
}
fn
get_prec_table
()
->
vec
[
op_spec
]
{
ret
precs
;
}
}
auto
ftype
=
SOURCE_FILE
;
if
(
_
str
.ends_with
(
path
,
".rc"
))
{
...
...
@@ -109,7 +114,8 @@ fn get_env() -> eval.env {
auto
rdr
=
lexer
.new_reader
(
srdr
,
path
);
auto
npos
=
rdr
.get_curr_pos
();
ret
stdio_parser
(
sess
,
env
,
ftype
,
lexer
.next_token
(
rdr
),
npos
,
npos
,
0
,
UNRESTRICTED
,
crate
,
rdr
);
npos
,
npos
,
0
,
UNRESTRICTED
,
crate
,
rdr
,
prec_table
());
}
impure
fn
unexpected
(
parser
p
,
token
.token
t
)
{
...
...
@@ -975,144 +981,72 @@ fn is_ident(token.token t) -> bool {
ret
@
spanned
(
lo
,
hi
,
ex
);
}
impure
fn
parse_binops
(
parser
p
,
(
impure
fn
(
parser
)
->
@
ast
.expr
)
sub
,
vec
[
tup
(
token
.binop
,
ast
.binop
)]
ops
)
type
op_spec
=
rec
(
token
.token
tok
,
ast
.binop
op
,
int
prec
);
fn
prec_table
()
->
vec
[
op_spec
]
{
ret
vec
(
rec
(
tok
=
token
.BINOP
(
token
.STAR
),
op
=
ast
.mul
,
prec
=
11
),
rec
(
tok
=
token
.BINOP
(
token
.SLASH
),
op
=
ast
.div
,
prec
=
11
),
rec
(
tok
=
token
.BINOP
(
token
.PERCENT
),
op
=
ast
.rem
,
prec
=
11
),
rec
(
tok
=
token
.BINOP
(
token
.PLUS
),
op
=
ast
.add
,
prec
=
10
),
rec
(
tok
=
token
.BINOP
(
token
.MINUS
),
op
=
ast
.sub
,
prec
=
10
),
rec
(
tok
=
token
.BINOP
(
token
.LSL
),
op
=
ast
.lsl
,
prec
=
9
),
rec
(
tok
=
token
.BINOP
(
token
.LSR
),
op
=
ast
.lsr
,
prec
=
9
),
rec
(
tok
=
token
.BINOP
(
token
.ASR
),
op
=
ast
.asr
,
prec
=
9
),
rec
(
tok
=
token
.BINOP
(
token
.AND
),
op
=
ast
.bitand
,
prec
=
8
),
rec
(
tok
=
token
.BINOP
(
token
.CARET
),
op
=
ast
.bitxor
,
prec
=
6
),
rec
(
tok
=
token
.BINOP
(
token
.OR
),
op
=
ast
.bitor
,
prec
=
6
),
// ast.mul is a bogus placeholder here, AS is special
// cased in parse_more_binops
rec
(
tok
=
token
.AS
,
op
=
ast
.mul
,
prec
=
5
),
rec
(
tok
=
token
.LT
,
op
=
ast
.lt
,
prec
=
4
),
rec
(
tok
=
token
.LE
,
op
=
ast
.le
,
prec
=
4
),
rec
(
tok
=
token
.GE
,
op
=
ast
.ge
,
prec
=
4
),
rec
(
tok
=
token
.GT
,
op
=
ast
.gt
,
prec
=
4
),
rec
(
tok
=
token
.EQEQ
,
op
=
ast
.eq
,
prec
=
3
),
rec
(
tok
=
token
.NE
,
op
=
ast
.ne
,
prec
=
3
),
rec
(
tok
=
token
.ANDAND
,
op
=
ast
.and
,
prec
=
2
),
rec
(
tok
=
token
.OROR
,
op
=
ast
.or
,
prec
=
1
));
}
impure
fn
parse_binops
(
parser
p
)
->
@
ast
.expr
{
ret
parse_more_binops
(
p
,
parse_prefix_expr
(
p
),
0
);
}
impure
fn
parse_more_binops
(
parser
p
,
@
ast
.expr
lhs
,
int
min_prec
)
->
@
ast
.expr
{
auto
lo
=
p
.get_span
();
auto
hi
=
lo
;
auto
e
=
sub
(
p
);
auto
more
=
true
;
while
(
more
)
{
more
=
false
;
for
(
tup
(
token
.binop
,
ast
.binop
)
pair
in
ops
)
{
alt
(
p
.peek
())
{
case
(
token
.BINOP
(
?
op
))
{
if
(
pair
._0
==
op
)
{
p
.bump
();
auto
rhs
=
sub
(
p
);
hi
=
rhs
.span
;
auto
exp
=
ast
.expr_binary
(
pair
._1
,
e
,
rhs
,
ast
.ann_none
);
e
=
@
spanned
(
lo
,
hi
,
exp
);
more
=
true
;
}
// Magic nonsense to work around rustboot bug
fn
op_eq
(
token
.token
a
,
token
.token
b
)
->
bool
{
if
(
a
==
b
)
{
ret
true
;}
else
{
ret
false
;}
}
auto
peeked
=
p
.peek
();
for
(
op_spec
cur
in
p
.get_prec_table
())
{
if
(
cur
.prec
>
min_prec
&&
op_eq
(
cur
.tok
,
peeked
))
{
p
.bump
();
alt
(
cur
.tok
)
{
case
(
token
.AS
)
{
auto
rhs
=
parse_ty
(
p
);
auto
_
as
=
ast
.expr_cast
(
lhs
,
rhs
,
ast
.ann_none
);
auto
span
=
@
spanned
(
lhs
.span
,
rhs
.span
,
_
as
);
ret
parse_more_binops
(
p
,
span
,
min_prec
);
}
case
(
_
)
{
auto
rhs
=
parse_more_binops
(
p
,
parse_prefix_expr
(
p
),
cur
.prec
);
auto
bin
=
ast
.expr_binary
(
cur
.op
,
lhs
,
rhs
,
ast
.ann_none
);
auto
span
=
@
spanned
(
lhs
.span
,
rhs
.span
,
bin
);
ret
parse_more_binops
(
p
,
span
,
min_prec
);
}
case
(
_
)
{
/* fall through */
}
}
}
}
ret
e
;
}
impure
fn
parse_binary_exprs
(
parser
p
,
(
impure
fn
(
parser
)
->
@
ast
.expr
)
sub
,
vec
[
tup
(
token
.token
,
ast
.binop
)]
ops
)
->
@
ast
.expr
{
auto
lo
=
p
.get_span
();
auto
hi
=
lo
;
auto
e
=
sub
(
p
);
auto
more
=
true
;
while
(
more
)
{
more
=
false
;
for
(
tup
(
token
.token
,
ast
.binop
)
pair
in
ops
)
{
if
(
pair
._0
==
p
.peek
())
{
p
.bump
();
auto
rhs
=
sub
(
p
);
hi
=
rhs
.span
;
auto
exp
=
ast
.expr_binary
(
pair
._1
,
e
,
rhs
,
ast
.ann_none
);
e
=
@
spanned
(
lo
,
hi
,
exp
);
more
=
true
;
}
}
}
ret
e
;
}
impure
fn
parse_factor_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_prefix_expr
;
ret
parse_binops
(
p
,
sub
,
vec
(
tup
(
token
.STAR
,
ast
.mul
),
tup
(
token
.SLASH
,
ast
.div
),
tup
(
token
.PERCENT
,
ast
.rem
)));
}
impure
fn
parse_term_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_factor_expr
;
ret
parse_binops
(
p
,
sub
,
vec
(
tup
(
token
.PLUS
,
ast
.add
),
tup
(
token
.MINUS
,
ast
.sub
)));
}
impure
fn
parse_shift_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_term_expr
;
ret
parse_binops
(
p
,
sub
,
vec
(
tup
(
token
.LSL
,
ast
.lsl
),
tup
(
token
.LSR
,
ast
.lsr
),
tup
(
token
.ASR
,
ast
.asr
)));
}
impure
fn
parse_bitand_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_shift_expr
;
ret
parse_binops
(
p
,
sub
,
vec
(
tup
(
token
.AND
,
ast
.bitand
)));
}
impure
fn
parse_bitxor_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_bitand_expr
;
ret
parse_binops
(
p
,
sub
,
vec
(
tup
(
token
.CARET
,
ast
.bitxor
)));
}
impure
fn
parse_bitor_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_bitxor_expr
;
ret
parse_binops
(
p
,
sub
,
vec
(
tup
(
token
.OR
,
ast
.bitor
)));
}
impure
fn
parse_cast_expr
(
parser
p
)
->
@
ast
.expr
{
auto
lo
=
p
.get_span
();
auto
e
=
parse_bitor_expr
(
p
);
auto
hi
=
e
.span
;
while
(
true
)
{
alt
(
p
.peek
())
{
case
(
token
.AS
)
{
p
.bump
();
auto
t
=
parse_ty
(
p
);
hi
=
t
.span
;
e
=
@
spanned
(
lo
,
hi
,
ast
.expr_cast
(
e
,
t
,
ast
.ann_none
));
}
case
(
_
)
{
ret
e
;
}
}
}
ret
e
;
}
impure
fn
parse_relational_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_cast_expr
;
ret
parse_binary_exprs
(
p
,
sub
,
vec
(
tup
(
token
.LT
,
ast
.lt
),
tup
(
token
.LE
,
ast
.le
),
tup
(
token
.GE
,
ast
.ge
),
tup
(
token
.GT
,
ast
.gt
)));
}
impure
fn
parse_equality_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_relational_expr
;
ret
parse_binary_exprs
(
p
,
sub
,
vec
(
tup
(
token
.EQEQ
,
ast
.eq
),
tup
(
token
.NE
,
ast
.ne
)));
}
impure
fn
parse_and_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_equality_expr
;
ret
parse_binary_exprs
(
p
,
sub
,
vec
(
tup
(
token
.ANDAND
,
ast
.and
)));
}
impure
fn
parse_or_expr
(
parser
p
)
->
@
ast
.expr
{
auto
sub
=
parse_and_expr
;
ret
parse_binary_exprs
(
p
,
sub
,
vec
(
tup
(
token
.OROR
,
ast
.or
)));
ret
lhs
;
}
impure
fn
parse_assign_expr
(
parser
p
)
->
@
ast
.expr
{
auto
lo
=
p
.get_span
();
auto
lhs
=
parse_
or_expr
(
p
);
auto
lhs
=
parse_
binops
(
p
);
alt
(
p
.peek
())
{
case
(
token
.EQ
)
{
p
.bump
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录