Skip to content

Commit

Permalink
lib: inline pest grammars into .rs files
Browse files Browse the repository at this point in the history
When building Jujutsu crates as a dependency in a Buck project, the usage of
`#[grammar]` requires the `.pest` files in the code to be in the `$PWD` relative
to where the Rust compiler is invoked on the crate; which isn't identical to how
it is done by Cargo, causing a build failure ("file not found").

Rather than special case a Buck build path into the codebase for this (only used
by downstream consumers in other projects), inlining the grammar definition into
a new Rust file makes this go away.

Signed-off-by: Austin Seipp <[email protected]>
  • Loading branch information
thoughtpolice committed Aug 25, 2024
1 parent 45bd7aa commit bf432af
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
6 changes: 1 addition & 5 deletions lib/src/fileset_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ use pest::pratt_parser::Assoc;
use pest::pratt_parser::Op;
use pest::pratt_parser::PrattParser;
use pest::Parser;
use pest_derive::Parser;
use thiserror::Error;

use crate::dsl_util;
use crate::dsl_util::InvalidArguments;
use crate::dsl_util::StringLiteralParser;

#[derive(Parser)]
#[grammar = "fileset.pest"]
struct FilesetParser;
use crate::grammars::fileset::*;

const STRING_LITERAL_PARSER: StringLiteralParser<Rule> = StringLiteralParser {
content_rule: Rule::string_content,
Expand Down
8 changes: 8 additions & 0 deletions lib/src/fileset.pest → lib/src/grammars/fileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(missing_docs)]

use pest_derive::Parser;

#[derive(Parser)]
#[grammar_inline = r###"
whitespace = _{ " " | "\t" | "\r" | "\n" | "\x0c" }
// XID_CONTINUE: https://www.unicode.org/reports/tr31/#Default_Identifier_Syntax
Expand Down Expand Up @@ -86,3 +92,5 @@ program_or_bare_string = _{
| bare_string_pattern ~ EOI
| bare_string ~ EOI )
}
"###]
pub struct FilesetParser;
18 changes: 18 additions & 0 deletions lib/src/grammars/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2021-2024 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Grammars for parsing Jujutsu formats.
pub mod fileset;
pub mod revset;
16 changes: 12 additions & 4 deletions lib/src/revset.pest → lib/src/grammars/revset.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// Copyright 2021 The Jujutsu Authors
//
// Copyright 2021-2024 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(missing_docs)]

use pest_derive::Parser;

#[derive(Parser)]
#[grammar_inline = r###"
whitespace = _{ " " | "\t" | "\r" | "\n" | "\x0c" }
identifier_part = @{ (ASCII_ALPHANUMERIC | "_" | "/")+ }
Expand Down Expand Up @@ -116,3 +122,5 @@ function_alias_declaration = {
alias_declaration = _{
SOI ~ (function_alias_declaration | identifier) ~ EOI
}
"###]
pub struct RevsetParser;
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod git;
pub mod git_backend;
pub mod gitignore;
pub mod gpg_signing;
pub mod grammars;
pub mod graph;
pub mod hex_util;
pub mod id_prefix;
Expand Down
6 changes: 1 addition & 5 deletions lib/src/revset_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use pest::pratt_parser::Assoc;
use pest::pratt_parser::Op;
use pest::pratt_parser::PrattParser;
use pest::Parser;
use pest_derive::Parser;
use thiserror::Error;

use crate::dsl_util;
Expand All @@ -44,10 +43,7 @@ use crate::dsl_util::FoldableExpression;
use crate::dsl_util::InvalidArguments;
use crate::dsl_util::KeywordArgument;
use crate::dsl_util::StringLiteralParser;

#[derive(Parser)]
#[grammar = "revset.pest"]
struct RevsetParser;
use crate::grammars::revset::*;

const STRING_LITERAL_PARSER: StringLiteralParser<Rule> = StringLiteralParser {
content_rule: Rule::string_content,
Expand Down

0 comments on commit bf432af

Please sign in to comment.