Skip to content

Commit ba9241c

Browse files
authored
Merge pull request #62 from returntocorp/mj-ast-nullables
Use ATD's nullable type instead of option
2 parents deffcb8 + dbeaed6 commit ba9241c

11 files changed

+10790
-1077
lines changed

Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ VER=v1
66

77
# Those files are in lowercase because atdpy/atdts seems to
88
# generate lowercase files, even though the input is capitalized
9-
# TODO can't yet generate ast_generic_v0.py because of the use
10-
# of parametric types in it (need monomorphise in atdpy or remove them)
119
FILES= \
1210
semgrep_output_$(VER)_j.ml \
1311
semgrep_output_$(VER)_j.mli \
@@ -17,26 +15,34 @@ FILES= \
1715
ast_generic_$(VER)_j.ml \
1816
ast_generic_$(VER)_j.mli \
1917

20-
2118
.PHONY: build
2219
build: $(FILES)
2320

21+
# need atdpy >= 2.11.0 to support parametrized types
2422
%.py: %.atd
2523
atdpy $<
2624

2725
%_j.ml %_j.mli: %.atd
2826
atdgen -j -j-std $<
2927

30-
# need atdts > 2.10.0 (dev version 6a7399a works)
28+
# need atdts >= 2.11.0
3129
%.ts: %.atd
3230
atdts $<
3331

3432
# need atdcat >= 2.6.0
3533
semgrep_output_$(VER).jsonschema: semgrep_output_$(VER).atd
3634
atdcat -jsonschema cli_output $< > $@
3735

36+
.PHONY: clean
3837
clean:
3938
rm -f $(FILES)
4039

40+
.PHONY: setup
4141
setup:
4242
opam install --deps-only .
43+
44+
# The tests require semgrep-core, among other things.
45+
#
46+
.PHONY: test
47+
test:
48+
$(MAKE) -C tests

ast_generic_v1.atd

+58-44
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ type name = [
115115
]
116116

117117
type qualified_info = {
118-
name_last : (ident * type_arguments option);
119-
name_middle : qualifier option;
118+
name_last : (ident * type_arguments nullable);
119+
?name_middle : qualifier option;
120120
(* ::, Ruby, C++, also '`' abuse for PolyVariant in OCaml *)
121-
name_top : tok option;
121+
?name_top : tok option;
122122
name_info : id_info;
123123
}
124124

125125
type qualifier = [
126126
(* Java/C++/Rust *)
127-
| QDots of (ident * type_arguments option) list
127+
| QDots of (ident * type_arguments nullable) list
128128
(* Ruby/Lua *)
129129
| QExpr of (expr * tok)
130130
]
@@ -135,9 +135,9 @@ type qualifier = [
135135

136136
(* EXPERIMENTAL *)
137137
type id_info = {
138-
id_resolved: resolved_name option; (* variable tagger (naming) *)
139-
id_type: type_ option; (* type checker (typing) *)
140-
id_svalue: svalue option; (* constant propagation *)
138+
?id_resolved: resolved_name option; (* variable tagger (naming) *)
139+
?id_type: type_ option; (* type checker (typing) *)
140+
?id_svalue: svalue option; (* constant propagation *)
141141
}
142142

143143
(*****************************************************************************)
@@ -195,8 +195,12 @@ type expr = [
195195
(* in Js ArrayAccess is also abused to perform DotAccess (..., FDynamic) *)
196196
| ArrayAccess of (expr * expr bracket)
197197

198-
| SliceAccess of (expr *
199-
(expr option (* lower *) * expr option (* upper *) * expr option (* step *)) bracket)
198+
| SliceAccess of (
199+
expr
200+
* (expr nullable (* lower *)
201+
* expr nullable (* upper *)
202+
* expr nullable (* step *)) bracket
203+
)
200204

201205
(* very special value *)
202206
| Lambda of function_definition
@@ -206,7 +210,7 @@ type expr = [
206210
(* a.k.a ternary expression, or regular if in OCaml *)
207211
| Conditional of (expr * expr * expr)
208212

209-
| Yield of (tok * expr option * bool) (* 'from' for Python *)
213+
| Yield of (tok * expr nullable * bool) (* 'from' for Python *)
210214
| Await of (tok * expr)
211215

212216
| Cast of (type_ * expr)
@@ -232,9 +236,10 @@ type literal = [
232236
(* the numbers are an option because OCaml numbers (e.g., 63bits int)
233237
* may not be able to represent all numbers.
234238
*)
235-
| Int of int option wrap_ | Float of float option wrap_
236-
| Char of string wrap_ | String of string wrap_
237-
| Regexp of (string wrap_ bracket (* // *) * string wrap_ option (* modifiers *))
239+
| Int of int nullable wrap_ | Float of float nullable wrap_
240+
| Char of string wrap_ | String of string wrap_
241+
| Regexp of (string wrap_ bracket (* // *)
242+
* string wrap_ nullable (* modifiers *))
238243
| Atom of (tok * string wrap_) (* Ruby *)
239244
| Unit of tok (* a.k.a Void *) | Null of tok | Undefined of tok (* JS *)
240245
| Imag of string wrap_ (* Go, Python *) | Ratio of string wrap_ (* Ruby *)
@@ -397,7 +402,7 @@ type xml_attr_value = expr
397402
type xml_body = [
398403
| XmlText of string wrap_
399404
(* this can be None when people abuse {} to put comments in it *)
400-
| XmlExpr of expr option bracket
405+
| XmlExpr of expr nullable bracket
401406
| XmlXml of xml
402407
]
403408

@@ -426,27 +431,30 @@ type stmt = [
426431

427432
| Block of stmt list bracket (* can be fake {} in Python where use layout *)
428433

429-
| If of (tok (* 'if' or 'elif' *) * condition * stmt * stmt option)
430-
| While of (tok * condition * stmt)
434+
| If of (tok (* 'if' or 'elif' *) * condition * stmt * stmt nullable)
435+
| While of (tok * condition * stmt)
431436

432-
| Return of (tok * expr option * sc)
437+
| Return of (tok * expr nullable * sc)
433438

434439
| DoWhile of (tok * stmt * expr)
435440
| For of (tok (* 'for', 'foreach'*) * for_header * stmt)
436441

437442
(* The expr can be None for Go and Ruby. *)
438-
| Switch of (tok (* 'switch' or also 'select' in Go *) * condition option *
439-
case_and_body list)
443+
| Switch of (tok (* 'switch' or also 'select' in Go *)
444+
* condition nullable
445+
* case_and_body list)
440446

441447
| Continue of (tok * label_ident * sc)
442-
| Break of (tok * label_ident * sc)
448+
| Break of (tok * label_ident * sc)
443449

444450
| Label of (label * stmt)
445451
| Goto of (tok * label)
446452

447453
| Throw of (tok (* 'raise' in OCaml, 'throw' in Java/PHP *) * expr * sc)
448-
| Try of (tok * stmt * catch list * finally option)
449-
| WithUsingResource of (tok (* 'with' in Python, 'using' in C# *) * stmt list * stmt)
454+
| Try of (tok * stmt * catch list * finally nullable)
455+
| WithUsingResource of (tok (* 'with' in Python, 'using' in C# *)
456+
* stmt list
457+
* stmt)
450458
| Assert of (tok * arguments * sc)
451459

452460
| DefStmt of definition
@@ -494,8 +502,8 @@ type label_ident = [
494502

495503
type for_header = [
496504
| ForClassic of (for_var_or_expr list (* init *) *
497-
expr option (* cond *) *
498-
expr option) (* next *)
505+
expr nullable (* cond *) *
506+
expr nullable) (* next *)
499507
| ForEach of for_each
500508
(* Scala *)
501509
| MultiForEach of multi_for_each list
@@ -568,7 +576,7 @@ type type_ = [
568576
| TyFun of (parameter list * type_ (* return type *))
569577

570578
(* a special case of TApply, also a special case of TPointer *)
571-
| TyArray of ((* const_expr *) expr option bracket * type_)
579+
| TyArray of ((* const_expr *) expr nullable bracket * type_)
572580
| TyTuple of type_ list bracket
573581

574582
| TyVar of ident (* type variable in polymorphic types (not a typedef) *)
@@ -589,7 +597,8 @@ type type_ = [
589597
* Most record types are defined via a TypeDef and are then referenced
590598
* via a TyName. Here we have flexible record types (a.k.a. rows in OCaml).
591599
*)
592-
| TyRecordAnon of (class_kind wrap_ (* 'struct/shape', fake in other *)* field list bracket)
600+
| TyRecordAnon of (class_kind wrap_ (* 'struct/shape', fake in other *)
601+
* field list bracket)
593602

594603
(* For languages such as Python which abuse expr to represent types.*)
595604
| TyExpr of expr
@@ -601,8 +610,10 @@ type type_arguments = type_argument list bracket
601610
type type_argument = [
602611
| TA of type_
603612
(* Java only *)
604-
| TAWildcard of (tok (* '?' *) *
605-
(bool wrap_ (* extends|super, true=super *) * type_) option)
613+
| TAWildcard of (
614+
tok (* '?' *)
615+
* (bool wrap_ (* extends|super, true=super *) * type_) nullable
616+
)
606617
(* C++/Rust (Rust restrict expr to literals and ConstBlock) *)
607618
| TAExpr of expr
608619
| OtherTypeArg of (todo_kind * any list)
@@ -728,9 +739,9 @@ type type_parameter_classic = {
728739
(* upper type bounds (must-be-a-subtype-of) *)
729740
tp_bounds : type_ list;
730741
(* for Rust/C++. Similar to parameter_classic, but with type here. *)
731-
tp_default : type_ option;
742+
tp_default : type_ nullable;
732743
(* declaration-site variance (Kotlin/Hack/Scala) *)
733-
tp_variance : variance wrap_ option;
744+
tp_variance : variance wrap_ nullable;
734745
}
735746

736747
(* TODO bracket *)
@@ -751,7 +762,7 @@ type variance = [
751762
type function_definition = {
752763
fkind: function_kind wrap_;
753764
fparams: parameters;
754-
frettype: type_ option; (* return type *)
765+
frettype: type_ nullable; (* return type *)
755766
fbody: function_body;
756767
}
757768

@@ -780,9 +791,9 @@ type parameter = [
780791

781792

782793
type parameter_classic = {
783-
pname: ident option;
784-
ptype: type_ option;
785-
pdefault: expr option;
794+
pname: ident nullable;
795+
ptype: type_ nullable;
796+
pdefault: expr nullable;
786797

787798
pattrs: attribute list;
788799

@@ -809,8 +820,8 @@ type function_body = [
809820
*)
810821

811822
type variable_definition = {
812-
vinit: expr option;
813-
vtype: type_ option;
823+
?vinit: expr option;
824+
?vtype: type_ option;
814825
}
815826

816827
(* ------------------------------------------------------------------------- *)
@@ -847,7 +858,7 @@ type or_type_element = [
847858
(* OCaml *)
848859
| OrConstructor of (ident * type_ list)
849860
(* C *)
850-
| OrEnum of (ident * expr option)
861+
| OrEnum of (ident * expr nullable)
851862
(* Java? *)
852863
| OrUnion of (ident * type_)
853864

@@ -898,16 +909,16 @@ type class_kind = [
898909
* can be defined in the class header via cparams and then this class
899910
* header can call its parent constructor using those cparams).
900911
*)
901-
type class_parent = (type_ * arguments option)
912+
type class_parent = (type_ * arguments nullable)
902913

903914
(* ------------------------------------------------------------------------- *)
904915
(* Enum entry *)
905916
(* ------------------------------------------------------------------------- *)
906917
(* for EnumClass, complex enums-as-classes in Java/Kotlin/Scala? *)
907918
type enum_entry_definition = {
908919
(* the enum identifier is in the corresponding entity *)
909-
ee_args : arguments option;
910-
ee_body : field list bracket option;
920+
ee_args : arguments nullable;
921+
ee_body : field list bracket nullable;
911922
}
912923

913924
(* ------------------------------------------------------------------------- *)
@@ -920,7 +931,7 @@ type module_definition = {
920931

921932
type module_definition_kind = [
922933
| ModuleAlias of dotted_ident
923-
| ModuleStruct of (dotted_ident option * item list)
934+
| ModuleStruct of (dotted_ident nullable * item list)
924935

925936
| OtherModule of (todo_kind * any list)
926937
]
@@ -940,10 +951,13 @@ type macro_definition = {
940951
(*****************************************************************************)
941952

942953
type directive = [
943-
| ImportFrom of (tok (* 'import'/'from' for Python, 'include' for C *) *
944-
module_name * (ident * alias option (* as name alias *)) list)
954+
| ImportFrom of (
955+
tok (* 'import'/'from' for Python, 'include' for C *)
956+
* module_name
957+
* (ident * alias nullable (* as name alias *)) list
958+
)
945959

946-
| ImportAs of (tok * module_name * alias option) (* as name *)
960+
| ImportAs of (tok * module_name * alias nullable) (* as name *)
947961
| ImportAll of (tok * module_name * tok) (* '.' in Go, '*' in Java/Python *)
948962

949963
(* packages are different from modules in that multiple files can reuse

0 commit comments

Comments
 (0)