Skip to content

Commit cacecb7

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Store load arg trailing comma in AST
Summary: {F1082857697} Used in the following diff D48941531. Reviewed By: cjhopman Differential Revision: D48941532 fbshipit-source-id: a2b4ae4bd84c8769b60503e0f01d6f2f59e0df84
1 parent 65a2124 commit cacecb7

File tree

8 files changed

+53
-14
lines changed

8 files changed

+53
-14
lines changed

starlark/src/typing/fill_types_for_lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'a, 'v> GlobalTypesBuilder<'a, 'v> {
291291
}
292292

293293
fn load(&mut self, load: &LoadP<CstPayload>) -> Result<(), InternalError> {
294-
for LoadArgP { local, their } in &load.args {
294+
for LoadArgP { local, their, .. } in &load.args {
295295
let ty = load.payload.get(their).cloned().unwrap_or_else(Ty::any);
296296
self.assign_ident_value(local, GlobalValue::ty(ty))?;
297297
}

starlark_lsp/src/definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ impl LspModule {
573573
path: load.module.node.to_owned(),
574574
})
575575
} else {
576-
load.args.iter().find_map(|LoadArgP { local, their }| {
576+
load.args.iter().find_map(|LoadArgP { local, their, .. }| {
577577
if local.span.contains(pos) || their.span.contains(pos) {
578578
Some(IdentifierDefinition::LoadedLocation {
579579
source: codemap.resolve_span(their.span),

starlark_lsp/src/server.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,9 @@ impl<T: LspContext> Backend<T> {
957957
let load_span = ast.ast.codemap.resolve_span(*load_span);
958958
let mut load_args: Vec<(&str, &str)> = previously_loaded_symbols
959959
.iter()
960-
.map(|LoadArgP { local, their }| (local.ident.as_str(), their.node.as_str()))
960+
.map(|LoadArgP { local, their, .. }| {
961+
(local.ident.as_str(), their.node.as_str())
962+
})
961963
.collect();
962964
load_args.push((symbol, symbol));
963965
load_args.sort_by(|(_, a), (_, b)| a.cmp(b));

starlark_syntax/src/syntax/ast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ impl AstPayload for AstNoPayload {
5252
type TypeExprPayload = ();
5353
}
5454

55+
/// `,` token.
56+
#[derive(Copy, Clone, Dupe, Debug)]
57+
pub struct Comma;
58+
5559
pub type Expr = ExprP<AstNoPayload>;
5660
pub type TypeExpr = TypeExprP<AstNoPayload>;
5761
pub type AssignTarget = AssignTargetP<AstNoPayload>;
@@ -246,6 +250,8 @@ pub struct LoadArgP<P: AstPayload> {
246250
pub local: AstAssignIdentP<P>,
247251
/// `"y" in `x="y"`.
248252
pub their: AstString,
253+
/// Trailing comma.
254+
pub comma: Option<Spanned<Comma>>,
249255
}
250256

251257
impl<P: AstPayload> LoadArgP<P> {

starlark_syntax/src/syntax/grammar.lalrpop

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,15 @@ AssignStmt_: Stmt = <lhs:TestList> <ty:Type> <op:AssignOp> <rhs:TestList>
191191
ExprStmt: AstStmt = ASTS<ExprStmt_>;
192192
ExprStmt_: Stmt = <Test> => Stmt::Expression(<>);
193193

194+
Comma: Spanned<Comma> = <@L> "," <@R> => Comma.ast(<>);
195+
194196
LoadStmt: AstStmt = ASTS<LoadStmt_>;
195-
LoadStmt_: Stmt = "load" "(" <module:string> <args:("," <LoadStmtSyms>)*> ","? ")"
196-
=> grammar_util::check_load(<>, state);
197+
LoadStmt_: Stmt = {
198+
"load" "(" <module:string> ")"
199+
=> grammar_util::check_load_0(<>, state),
200+
"load" "(" <module:string> Comma <args:(<LoadStmtSyms> <Comma>)*> <last:(<LoadStmtSyms>)?> ")"
201+
=> grammar_util::check_load(<>, state)
202+
};
197203

198204
LoadStmtBindingName: AstString = <identifier> "=";
199205

starlark_syntax/src/syntax/grammar_util.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::syntax::ast::AstParameter;
4040
use crate::syntax::ast::AstStmt;
4141
use crate::syntax::ast::AstString;
4242
use crate::syntax::ast::AstTypeExpr;
43+
use crate::syntax::ast::Comma;
4344
use crate::syntax::ast::DefP;
4445
use crate::syntax::ast::Expr;
4546
use crate::syntax::ast::ExprP;
@@ -189,20 +190,42 @@ pub fn check_def(
189190
})
190191
}
191192

193+
pub(crate) fn check_load_0(module: AstString, parser_state: &mut ParserState) -> Stmt {
194+
parser_state.errors.push(EvalException::new(
195+
GrammarUtilError::LoadRequiresAtLeastTwoArguments.into(),
196+
module.span,
197+
parser_state.codemap,
198+
));
199+
Stmt::Load(LoadP {
200+
module,
201+
args: Vec::new(),
202+
payload: (),
203+
})
204+
}
205+
192206
pub(crate) fn check_load(
193207
module: AstString,
194-
args: Vec<(AstAssignIdent, AstString)>,
208+
args: Vec<((AstAssignIdent, AstString), Spanned<Comma>)>,
209+
last: Option<(AstAssignIdent, AstString)>,
195210
parser_state: &mut ParserState,
196211
) -> Stmt {
197-
if args.is_empty() {
198-
parser_state.errors.push(EvalException::new(
199-
GrammarUtilError::LoadRequiresAtLeastTwoArguments.into(),
200-
module.span,
201-
parser_state.codemap,
202-
));
212+
if args.is_empty() && last.is_none() {
213+
return check_load_0(module, parser_state);
203214
}
204215

205-
let args = args.into_map(|(local, their)| LoadArgP { local, their });
216+
let args = args
217+
.into_iter()
218+
.map(|((local, their), comma)| LoadArgP {
219+
local,
220+
their,
221+
comma: Some(comma),
222+
})
223+
.chain(last.map(|(local, their)| LoadArgP {
224+
local,
225+
their,
226+
comma: None,
227+
}))
228+
.collect();
206229

207230
Stmt::Load(LoadP {
208231
module,

starlark_syntax/src/syntax/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl AstModule {
183183
symbols: load
184184
.args
185185
.iter()
186-
.map(|LoadArgP { local, their }| {
186+
.map(|LoadArgP { local, their, .. }| {
187187
(local.node.ident.as_str(), their.node.as_str())
188188
})
189189
.collect(),

starlark_syntax/src/syntax/payload_map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ impl<A: AstPayload> LoadArgP<A> {
5454
let LoadArgP {
5555
local,
5656
their: remote,
57+
comma,
5758
} = self;
5859
LoadArgP {
5960
local: local.into_map_payload(f),
6061
their: remote,
62+
comma,
6163
}
6264
}
6365
}

0 commit comments

Comments
 (0)