语法类别 stx 是可能出现在 Lean.Parser.Command.syntax : commandsyntax 命令主体中的说明符的语法。
字符串文字被解析为 atoms(包括 if、#eval 或 where 等关键字):
stx ::=
Parses the literal symbol.
The symbol is automatically included in the set of reserved tokens ("keywords").
Keywords cannot be used as identifiers, unless the identifier is otherwise escaped.
For example, `"fun"` reserves `fun` as a keyword; to refer an identifier named `fun` one can write `«fun»`.
Adding a `&` prefix prevents it from being reserved, for example `&"true"`.
Whitespace before or after the atom is used as a pretty printing hint.
For example, `" + "` parses `+` and pretty prints it with whitespace on both sides.
The whitespace has no effect on parsing behavior.
str
字符串中的前导空格和尾随空格不会影响解析,但会导致 Lean 在显示 proof states 中的语法和错误消息时在相应位置插入空格。
通常,在语法规则中作为原子出现的有效标识符成为保留关键字。
在字符串文字前面加上 & 符号 (&) 可抑制此行为:
stx ::= ...
| Parses a literal symbol. The `&` prefix prevents it from being included in the set of reserved tokens ("keywords").
This means that the symbol can still be recognized as an identifier by other parsers.
Some syntax categories, such as `tactic`, automatically apply `&` to the first symbol.
Whitespace before or after the atom is used as a pretty printing hint.
For example, `" + "` parses `+` and pretty prints it with whitespace on both sides.
The whitespace has no effect on parsing behavior.
(Not exposed by parser description syntax:
If the `includeIdent` argument is true, lets `ident` be reinterpreted as `atom` if it matches.)
&str
标识符指定给定位置预期的语法类别,并且可以选择提供优先级:
stx ::= ...
| ident(:prec)?
* 修饰符是 Kleene 星号,匹配前面语法的零次或多次重复。
也可以使用 many 写入。
stx ::= ...
| `p*` is shorthand for `many(p)`. It uses parser `p` 0 or more times, and produces a
`nullNode` containing the array of parsed results. This parser has arity 1.
If `p` has arity more than 1, it is auto-grouped in the items generated by the parser.
stx *
+ 修饰符匹配前述语法的一次或多次重复。
也可以使用 many1 写入。
stx ::= ...
| `p+` is shorthand for `many1(p)`. It uses parser `p` 1 or more times, and produces a
`nullNode` containing the array of parsed results. This parser has arity 1.
If `p` has arity more than 1, it is auto-grouped in the items generated by the parser.
stx +
? 修饰符使子项成为可选,并匹配前面语法的零次或一次(但不能多次)重复。
也可写为optional。
stx ::= ...
| `(p)?` is shorthand for `optional(p)`. It uses parser `p` 0 or 1 times, and produces a
`nullNode` containing the array of parsed results. This parser has arity 1.
`p` is allowed to have arity n > 1 (in which case the node will have either 0 or n children),
but if it has arity 0 then the result will be ambiguous.
Because `?` is an identifier character, `ident?` will not work as intended.
You have to write either `ident ?` or `(ident)?` for it to parse as the `?` combinator
applied to the `ident` parser.
stx ?stx ::= ...
| optional(stx)
,* 修饰符与前面带有交错逗号的语法的零次或多次重复相匹配。
也可以使用 sepBy 写入。
stx ::= ...
| `p,*` is shorthand for `sepBy(p, ",")`. It parses 0 or more occurrences of
`p` separated by `,`, that is: `empty | p | p,p | p,p,p | ...`.
It produces a `nullNode` containing a `SepArray` with the interleaved parser
results. It has arity 1, and auto-groups its component parser if needed.
stx ,*
,+ 修饰符与前面带有交错逗号的语法的一次或多次重复相匹配。
也可以使用 sepBy1 写入。
stx ::= ...
| `p,+` is shorthand for `sepBy1(p, ",")`. It parses 1 or more occurrences of
`p` separated by `,`, that is: `p | p,p | p,p,p | ...`.
It produces a `nullNode` containing a `SepArray` with the interleaved parser
results. It has arity 1, and auto-groups its component parser if needed.
stx ,+
,*,? 修饰符将前面语法的零次或多次重复与交错逗号匹配,从而允许在最终重复之后使用可选的尾随逗号。
也可以使用 sepBy 和 allowTrailingSep 修饰符来编写。
stx ::= ...
| `p,*,?` is shorthand for `sepBy(p, ",", allowTrailingSep)`.
It parses 0 or more occurrences of `p` separated by `,`, possibly including
a trailing `,`, that is: `empty | p | p, | p,p | p,p, | p,p,p | ...`.
It produces a `nullNode` containing a `SepArray` with the interleaved parser
results. It has arity 1, and auto-groups its component parser if needed.
stx ,*,?
,+,? 修饰符将前面语法的一次或多次重复与交错逗号相匹配,从而允许在最后一次重复之后使用可选的尾随逗号。
也可以使用 sepBy1 和 allowTrailingSep 修饰符来编写。
stx ::= ...
| `p,+,?` is shorthand for `sepBy1(p, ",", allowTrailingSep)`.
It parses 1 or more occurrences of `p` separated by `,`, possibly including
a trailing `,`, that is: `p | p, | p,p | p,p, | p,p,p | ...`.
It produces a `nullNode` containing a `SepArray` with the interleaved parser
results. It has arity 1, and auto-groups its component parser if needed.
stx ,+,?
<|> 运算符(可写为 orelse)与任一语法匹配。
然而,如果第一个分支消耗了任何令牌,那么它就会被提交,并且失败将不会被回溯:
stx ::= ...
| `p1 <|> p2` is shorthand for `orelse(p1, p2)`, and parses either `p1` or `p2`.
It does not backtrack, meaning that if `p1` consumes at least one token then
`p2` will not be tried. Therefore, the parsers should all differ in their first
token. The `atomic(p)` parser combinator can be used to locally backtrack a parser.
(For full backtracking, consider using extensible syntax classes instead.)
On success, if the inner parser does not generate exactly one node, it will be
automatically wrapped in a `group` node, so the result will always be arity 1.
The `<|>` combinator does not generate a node of its own, and in particular
does not tag the inner parsers to distinguish them, which can present a problem
when reconstructing the parse. A well formed `<|>` parser should use disjoint
node kinds for `p1` and `p2`.
stx <|> stxstx ::= ...
| orelse(stx, stx)
! 运算符与其参数的补集匹配。
如果它的参数失败,那么它会成功,重置解析状态。
stx ::= ...
| `!p` parses the negation of `p`. That is, it fails if `p` succeeds, and
otherwise parses nothing. It has arity 0.
! stx
语法说明符可以使用括号进行分组。
stx ::= ...
| (stx)
可以使用 many 和 many1 定义重复。
后者需要至少一个重复语法实例。
stx ::= ...
| many(stx)stx ::= ...
| many1(stx)
带有分隔符的重复可以使用 sepBy 和 sepBy1 来定义,它们分别匹配零个或多个出现以及一个或多个出现,由某种其他语法分隔。
它们分为三个品种:
-
双参数版本使用字符串文字中提供的原子来解析分隔符,并且不允许尾随分隔符。
-
三参数版本使用第三个参数来解析分隔符,使用原子进行漂亮的打印。
-
四参数版本可选择允许分隔符在序列结束时出现额外的时间。
第四个参数必须始终是关键字 allowTrailingSep。
stx ::= ...
| sepBy(stx, str)stx ::= ...
| sepBy(stx, str, stx)stx ::= ...
| sepBy(stx, str, stx, allowTrailingSep)stx ::= ...
| sepBy1(stx, str)stx ::= ...
| sepBy1(stx, str, stx)stx ::= ...
| sepBy1(stx, str, stx, allowTrailingSep)