starlark_syntax/dialect.rs
1/*
2 * Copyright 2018 The Starlark in Rust Authors.
3 * Copyright (c) Facebook, Inc. and its affiliates.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use dupe::Dupe;
19
20/// How to handle type annotations in Starlark.
21///
22/// If you are enabling types, you will often want to use
23/// `LibraryExtension::Typing` when constructing a `Globals` environment.
24#[derive(Debug, Clone, Copy, Dupe, Eq, PartialEq, Hash)]
25pub enum DialectTypes {
26 /// Prohibit types at parse time.
27 Disable,
28 /// Allow types at parse time, but ignore types at runtime.
29 ParseOnly,
30 /// Check types at runtime.
31 Enable,
32}
33
34/// Starlark language features to enable, e.g. [`Standard`](Dialect::Standard) to follow the Starlark standard.
35#[derive(Debug, Clone, Eq, PartialEq, Hash)]
36pub struct Dialect {
37 /// Are `def` statements permitted.
38 /// Enabled by default.
39 pub enable_def: bool,
40 /// Are `lambda` expressions permitted.
41 /// Enabled by default.
42 pub enable_lambda: bool,
43 /// Are `load` statements permitted.
44 /// Enabled by default.
45 pub enable_load: bool,
46 /// Are `*` keyword-only arguments allowed as per [PEP 3102](https://www.python.org/dev/peps/pep-3102/).
47 /// Disabled by default.
48 pub enable_keyword_only_arguments: bool,
49 /// Are `/` for positional-only arguments allowed.
50 pub enable_positional_only_arguments: bool,
51 /// Are expressions allowed in type positions as per [PEP 484](https://www.python.org/dev/peps/pep-0484/).
52 /// Disabled by default.
53 pub enable_types: DialectTypes,
54 /// Do `load()` statements reexport their definition.
55 /// Enabled by default,
56 /// but may change in future definitions of the standard.
57 pub enable_load_reexport: bool,
58 /// Are `for`, `if` and other statements allowed at the top level.
59 /// Disabled by default.
60 pub enable_top_level_stmt: bool,
61 /// Are `f"{expression}"` strings supported? Only works where `expression` is an atomic
62 /// identifier.
63 /// Disabled by default.
64 ///
65 /// [Starlark spec proposal](https://github.com/bazelbuild/starlark/issues/91).
66 pub enable_f_strings: bool,
67 /// Like `#[non_exhaustive]`, but allows struct expression.
68 ///
69 /// [Explanation](https://github.com/rust-lang/rust-clippy/issues/6559).
70 pub _non_exhaustive: (),
71}
72
73impl Default for Dialect {
74 fn default() -> Dialect {
75 Dialect::Standard
76 }
77}
78
79// These are morally enumerations, so give them enumeration-like names
80// even though they are actually global constants
81#[allow(non_upper_case_globals)]
82impl Dialect {
83 /// Follow the [Starlark language standard](https://github.com/bazelbuild/starlark/blob/master/spec.md) as much as possible.
84 ///
85 /// This is also returned by [`Dialect::default()`](Dialect::default).
86 pub const Standard: Self = Self {
87 enable_def: true,
88 enable_lambda: true,
89 enable_load: true,
90 enable_keyword_only_arguments: false,
91 enable_positional_only_arguments: false,
92 enable_types: DialectTypes::Disable,
93 enable_load_reexport: true, // But they plan to change it
94 enable_top_level_stmt: false,
95 enable_f_strings: false,
96 _non_exhaustive: (),
97 };
98
99 /// This option is deprecated. Extend `Standard` instead.
100 #[doc(hidden)]
101 pub const Extended: Self = Self {
102 enable_def: true,
103 enable_lambda: true,
104 enable_load: true,
105 enable_keyword_only_arguments: true,
106 enable_positional_only_arguments: false,
107 enable_types: DialectTypes::Enable,
108 enable_load_reexport: true,
109 enable_top_level_stmt: true,
110 enable_f_strings: false,
111 _non_exhaustive: (),
112 };
113
114 /// Only for starlark-rust self tests.
115 #[doc(hidden)]
116 pub const AllOptionsInternal: Self = Self {
117 enable_def: true,
118 enable_lambda: true,
119 enable_load: true,
120 enable_keyword_only_arguments: true,
121 enable_positional_only_arguments: true,
122 enable_types: DialectTypes::Enable,
123 enable_load_reexport: true,
124 enable_top_level_stmt: true,
125 enable_f_strings: true,
126 _non_exhaustive: (),
127 };
128}