Do not identify同定する、特定する
struct構造、構造体
literals by searching for :
. Instead define定義する
a sub- category of expressions式
which excludes struct構造、構造体
literals and re-define for
, if
, and other expressions式
which takeとる
an expression式
followed下記の、次に続く、追従する
by a block (or non-terminal非終端記号
which can be replaced by a block) to takeとる
this sub-category, instead of all expressions.式
Parsing構文解析する
by looking ahead is fragile - it could easily be broken if we allow許可する、可能にする
:
to appear現れる
elsewhere in types (e.g., type ascription) or if we change struct構造、構造体
literals to not require the :
(e.g., if we allow許可する、可能にする
empty空の
structs構造、構造体
to be written with braces,括弧
or if we allow許可する、可能にする
struct構造、構造体
literals to unify field names to local variable変数、ストレージ
names, as has been suggested in the past and which we currently do for struct構造、構造体
literal patterns). We should also be able to give better error messages today if users make these mistakes. More worryingly, we might come up with some language言語
feature in the future which is not predictable now and which breaks with the current system.
Hopefully, it is pretty rareまれ
to use struct構造、構造体
literals in these positions, so there should not be much fallout. Any problems can be easily fixed by assigning代入する
the struct構造、構造体
literal into a variable.変数、ストレージ
However, this is a backwards incompatible change, so it should block 1.0.
Here is a simplified version of a subset部分集合
of Rust's abstract syntax:文法
e ::= x
| e `.` f
| name `{` (x `:` e)+ `}`
| block
| `for` e `in` e block
| `if` e block (`else` block)?
| `|` pattern* `|` e
| ...
block ::= `{` (e;)* e? `}`
Parsing構文解析する
this grammar文法
is ambiguous since x
cannot be distinguished from name
, so e block
in the for expression式
is ambiguous with the struct構造、構造体
literal expression.式
We currently solve this by using lookahead to find a :
tokenトークン
in the struct構造、構造体
literal.
I propose the following adjustment:
e ::= e'
| name `{` (x `:` e)+ `}`
| `|` pattern* `|` e
| ...
e' ::= x
| e `.` f
| block
| `for` e `in` e' block
| `if` e' block (`else` block)?
| `|` pattern* `|` e'
| ...
block ::= `{` (e;)* e? `}`
e'
is just e without struct構造、構造体
literal expressions.式
We use e'
instead of e
wherever e
is followed下記の、次に続く、追従する
directly直接
by block or any other non-terminal非終端記号
which may have block as its first terminal (after any possible expansions).
For any expressions式
where a sub-expression is the final lexical字句的
element要素
(closures in the subset部分集合
above, but also unary単項の
and binary2進数
operations), we require two versions of the meta-expression - the normal one in e
and a version with e'
for the final element要素
in e'
.
Implementation実装
would be simpler, we just add a flag to parser::restriction
called RESTRICT_BLOCK
or something, which puts us into a mode which reflects e'
. We would drop in to this mode when parsing構文解析する
e'
position expressions式
and drop out of it for all but the last sub-expression of an expression.式
It makes the formal grammar文法
and parsing構文解析する
a little more complicated (although it is simpler in terms項、用語
of needing less lookahead and avoiding避ける、回避する
a special case).
Don't do this.
Allow許可する、可能にする
all expressions式
but greedily parse構文解析する
non-terminals非終端記号
in these positions, e.g., for N {} {}
would be parsed構文解析する
as for (N {}) {}
. This seems worse because I believe it will be much rarer to have structs構造、構造体
in these positions than to have an identifier識別子
in the first position, followed下記の、次に続く、追従する
by two blocks (i.e., parse構文解析する
as (for N {}) {}
).
Do we need to expose this distinction anywhereどこでも
outside of the parser? E.g., macros?