3. Quick Start Guide 快速入门指南
For getting started with LALRPOP, it's probably best if you read the tutorial, which will introduce you to the syntax of LALRPOP files and so forth.
GPT: 要开始使用 LALRPOP,最好的方法是阅读教程,它会介绍 LALRPOP 文件的语法以及其他相关内容。
MS: 要开始使用 LALRPOP,最好阅读本教程,该教程将向您介绍 LALRPOP 文件的语法等。
But if you've done this before, or you're just the impatient sort, here is a quick 'cheat sheet' for setting up your project. First, add the following lines to your Cargo.toml:
GPT: 如果你之前做过类似的工作,或者只是比较急的话,这里有一个快速的“备忘单”,帮助你快速设置项目。首先,向你的
Cargo.toml文件中添加以下内容:
MS: 但是,如果您以前做过这件事,或者您只是不耐烦的人,这里有一个用于设置您的项目的快速“备忘单”。首先,将以下行添加到您的 Cargo.toml 中:
# The generated code depends on lalrpop-util.
[dependencies]
lalrpop-util = "0.22.0"
# Add a build-time dependency on the lalrpop library:
[build-dependencies]
lalrpop = "0.22.0"
# If you are supplying your own external lexer you can disable default features so that the
# built-in lexer feature is not included
# lalrpop = { version = "0.22.0", default-features = false }
Next create a build.rs file that looks like:
fn main() {
lalrpop::process_src().unwrap();
}
(If you already have a build.rs file, you should be able to just call process_src in addition to whatever else that file is doing.)
gpt: (如果你已经有一个
build.rs文件,你只需要在这个文件中调用process_src,同时保持文件中其他的处理逻辑不变。)
ms: (如果您已经有一个 build.rs 文件,则除了该文件正在执行的任何其他操作外,您应该能够只调用 process_src.)
In this case, process_src simply uses the default settings, which takes files in src/ ending with the .lalrpop extension, and generates corresponding Rust source files with the same name in OUT_DIR. If you want to configure how LALRPOP executes, see the advanced setup section.
gpt: 在这种情况下,
process_src只是使用默认设置,它会处理src/目录下以.lalrpop扩展名结尾的文件,并在OUT_DIR中生成具有相同名称的 Rust 源文件。如果你想配置 LALRPOP 的执行方式,可以参考高级设置部分。
ms: 在这种情况下,process_src 只使用默认设置,它采用 src/ 中以 .lalrpop 扩展名结尾的文件,并在 OUT_DIR 中生成相应的同名 Rust 源文件。如果要配置 LALRPOP 的执行方式,请参阅高级设置部分。
Some projects, for example those which build multiple crates in the same workspace, will not have a top level source directory. For example, your project may have a build.rs at the top level with foo/src and bar/src containing source files for crates foo and bar, respectively. In this situation, you could either call process_root() from the top level build.rs, which searches all files in the current directory, not just in ./src, or you could modify crate level build.rs files at your discretion.
gpt: 某些项目,例如那些在同一个工作区中构建多个 crate 的项目,可能没有顶层的源代码目录。例如,你的项目可能在顶层有一个
build.rs,而foo/src和bar/src分别包含 cratefoo和bar的源代码文件。在这种情况下,你可以选择从顶层的build.rs调用process_root(),该函数会搜索当前目录下的所有文件,而不仅仅是./src目录,或者你也可以根据需要修改各个 crate 的build.rs文件。
ms: 某些项目,例如那些在同一个工作区中构建多个 crate 的项目,将没有顶级源目录。例如,您的项目可能在顶层有一个 build.rs,其中 foo/src 和 bar/src 分别包含 crate foo 和 bar 的源文件。在这种情况下,您可以从顶级 build.rs 调用 process_root(),这将搜索当前目录中的所有文件,而不仅仅是 ./src 中,或者您可以自行决定修改 crate 级别的 build.rs 文件。
The lalrpop_mod! macro generates a wrapper module in your crate so that you can use the generated parser from your code. For example, if the source grammar is located in grammar.lalrpop, adding the following line to lib.rs will create a corresponding grammar submodule (note that you can also add this line to a foo.rs module definition instead, which will then create a submodule foo::grammar):
gpt:
lalrpop_mod!宏会在你的 crate 中生成一个包装模块,使你能够在代码中使用生成的解析器。例如,如果源文法文件位于grammar.lalrpop,在lib.rs中添加以下代码行将会创建一个相应的grammar子模块(请注意,你也可以将这行代码添加到foo.rs模块定义中,这样会创建一个foo::grammar子模块):
ms: lalrpop_mod!macro 会在 crate 中生成一个包装器模块,以便您可以使用从代码中生成的解析器。例如,如果源语法位于 grammar.lalrpop 中,则将以下行添加到 lib.rs 将创建相应的语法子模块(请注意,您也可以将此行添加到 foo.rs 模块定义中,然后将创建一个子模块 foo::grammar):
lalrpop_mod!(grammar);
Running manually 手动运行
If you prefer, you can also run the lalrpop crate as an executable. Simply run cargo install lalrpop and then you will get a lalrpop binary you can execute, like so:
gpt: 如果你更喜欢,你也可以将
lalrpopcrate 作为可执行文件来运行。只需运行以下命令来安装lalrpop:cargo install lalrpop, 安装完成后,你将获得一个可以执行的lalrpop二进制文件,像这样运行:
ms: 如果你愿意,你也可以将 lalrpop crate 作为可执行文件运行。只需运行 cargo install lalrpop,然后你就会得到一个可以执行的 lalrpop 二进制文件,就像这样:
lalrpop file.lalrpop
This will generate file.rs for you. Note that it only executes if file.lalrpop is newer than file.rs; if you'd prefer to execute unconditionally, pass -f (also try --help for other options).
gpt: 这将为你生成
file.rs文件。请注意,只有在file.lalrpop文件比file.rs更新时,才会执行该命令;如果你希望每次都强制执行,可以使用-f参数(你也可以尝试--help查看其他选项)。
ms: 这将为您生成 file.rs。请注意,它仅在 file.lalrpop 比 file.rs 新时执行;如果你更喜欢无条件执行,请传递 -f (也可以尝试 --help 其他选项)。
浙公网安备 33010602011771号