diff --git a/Makefile b/Makefile index 1c599bd..2c4166d 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ FILES= \ semgrep_output_$(VER).ts \ semgrep_output_$(VER).jsonschema \ semgrep_output_$(VER).proto \ + ast_generic_$(VER).py \ ast_generic_$(VER)_j.ml \ ast_generic_$(VER)_j.mli \ Language.ml \ diff --git a/ast_generic_v1.atd b/ast_generic_v1.atd index 1884a88..991ea75 100644 --- a/ast_generic_v1.atd +++ b/ast_generic_v1.atd @@ -966,7 +966,7 @@ type directive = [ | ImportFrom of ( tok (* 'import'/'from' for Python, 'include' for C *) * module_name - * (ident * alias nullable (* as name alias *)) list + * import_from_kind list ) | ImportAs of (tok * module_name * alias nullable) (* as name *) @@ -987,6 +987,11 @@ type directive = [ (* ... as name *) type alias = (ident * id_info) +type import_from_kind = [ + | Direct of alias + | Aliased of (ident * alias) + ] + (*****************************************************************************) (* Toplevel *) (*****************************************************************************) diff --git a/ast_generic_v1.py b/ast_generic_v1.py index e68b368..6a307e3 100644 --- a/ast_generic_v1.py +++ b/ast_generic_v1.py @@ -9,7 +9,7 @@ # Import annotations to allow forward references from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import Any, Callable, Dict, List, NoReturn, Optional, Tuple, Union import json @@ -137,6 +137,19 @@ def read_nullable(x: Any) -> Any: return read_nullable +def _atd_read_option(read_elt: Callable[[Any], Any]) \ + -> Callable[[Optional[Any]], Optional[Any]]: + def read_option(x: Any) -> Any: + if x == 'None': + return None + elif isinstance(x, List) and len(x) == 2 and x[0] == 'Some': + return read_elt(x[1]) + else: + _atd_bad_json('option', x) + raise AssertionError('impossible') # keep mypy happy + return read_option + + def _atd_write_unit(x: Any) -> None: if x is None: return x @@ -232,6 +245,16 @@ def write_nullable(x: Any) -> Any: return write_nullable +def _atd_write_option(write_elt: Callable[[Any], Any]) \ + -> Callable[[Optional[Any]], Optional[Any]]: + def write_option(x: Any) -> Any: + if x is None: + return 'None' + else: + return ['Some', write_elt(x)] + return write_option + + ############################################################################ # Public classes ############################################################################ @@ -2307,6 +2330,7 @@ def to_json() -> Any: def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) + @dataclass class LDA: """Original type: operator = [ ... | LDA | ... ]""" @@ -2323,6 +2347,7 @@ def to_json() -> Any: def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) + @dataclass class RDA: """Original type: operator = [ ... | RDA | ... ]""" @@ -2339,6 +2364,7 @@ def to_json() -> Any: def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) + @dataclass class LSA: """Original type: operator = [ ... | LSA | ... ]""" @@ -2355,6 +2381,7 @@ def to_json() -> Any: def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) + @dataclass class RSA: """Original type: operator = [ ... | RSA | ... ]""" @@ -2619,6 +2646,23 @@ def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) +@dataclass +class Cls: + """Original type: special = [ ... | Cls | ... ]""" + + @property + def kind(self) -> str: + """Name of the class representing this variant.""" + return 'Cls' + + @staticmethod + def to_json() -> Any: + return 'Cls' + + def to_json_string(self, **kw: Any) -> str: + return json.dumps(self.to_json(), **kw) + + @dataclass class Self: """Original type: special = [ ... | Self | ... ]""" @@ -2917,7 +2961,7 @@ def to_json_string(self, **kw: Any) -> str: class Special: """Original type: special = [ ... ]""" - value: Union[This, Super, Self, Parent, Eval, Typeof, Instanceof, Sizeof, Defined, ConcatString, EncodedString, InterpolatedElement, Spread, HashSplat, ForOf, Op, IncrDecr_, Require, OtherSpecial] + value: Union[This, Super, Cls, Self, Parent, Eval, Typeof, Instanceof, Sizeof, Defined, ConcatString, EncodedString, InterpolatedElement, Spread, HashSplat, ForOf, Op, IncrDecr_, Require, OtherSpecial] @property def kind(self) -> str: @@ -2931,6 +2975,8 @@ def from_json(cls, x: Any) -> 'Special': return cls(This()) if x == 'Super': return cls(Super()) + if x == 'Cls': + return cls(Cls()) if x == 'Self': return cls(Self()) if x == 'Parent': @@ -3196,27 +3242,6 @@ def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) -@dataclass -class IntNullableWrap: - """Original type: _int_nullable_wrap""" - - value: Tuple[Optional[int], Tok] - - @classmethod - def from_json(cls, x: Any) -> 'IntNullableWrap': - return cls((lambda x: (_atd_read_nullable(_atd_read_int)(x[0]), Tok.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x)) - - def to_json(self) -> Any: - return (lambda x: [_atd_write_nullable(_atd_write_int)(x[0]), (lambda x: x.to_json())(x[1])] if isinstance(x, tuple) and len(x) == 2 else _atd_bad_python('tuple of length 2', x))(self.value) - - @classmethod - def from_json_string(cls, x: str) -> 'IntNullableWrap': - return cls.from_json(json.loads(x)) - - def to_json_string(self, **kw: Any) -> str: - return json.dumps(self.to_json(), **kw) - - @dataclass class IntWrap: """Original type: _int_wrap""" @@ -3343,6 +3368,27 @@ def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) +@dataclass +class XB40703a: + """Original type: _x_b40703a""" + + value: Tuple[Optional[int], Tok] + + @classmethod + def from_json(cls, x: Any) -> 'XB40703a': + return cls((lambda x: (_atd_read_nullable(_atd_read_int)(x[0]), Tok.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x)) + + def to_json(self) -> Any: + return (lambda x: [_atd_write_nullable(_atd_write_int)(x[0]), (lambda x: x.to_json())(x[1])] if isinstance(x, tuple) and len(x) == 2 else _atd_bad_python('tuple of length 2', x))(self.value) + + @classmethod + def from_json_string(cls, x: str) -> 'XB40703a': + return cls.from_json(json.loads(x)) + + def to_json_string(self, **kw: Any) -> str: + return json.dumps(self.to_json(), **kw) + + @dataclass class Ident: """Original type: ident""" @@ -3428,7 +3474,7 @@ def to_json_string(self, **kw: Any) -> str: class Int: """Original type: literal = [ ... | Int of ... | ... ]""" - value: IntNullableWrap + value: XB40703a @property def kind(self) -> str: @@ -3640,7 +3686,7 @@ def from_json(cls, x: Any) -> 'Literal': if cons == 'Bool': return cls(Bool(BoolWrap.from_json(x[1]))) if cons == 'Int': - return cls(Int(IntNullableWrap.from_json(x[1]))) + return cls(Int(XB40703a.from_json(x[1]))) if cons == 'Float': return cls(Float(FloatNullableWrap.from_json(x[1]))) if cons == 'Char': @@ -5761,7 +5807,7 @@ def to_json_string(self, **kw: Any) -> str: class ImportFrom: """Original type: directive = [ ... | ImportFrom of ... | ... ]""" - value: Tuple[Tok, ModuleName, List[Tuple[Ident, Optional[Alias]]]] + value: Tuple[Tok, ModuleName, List[Tuple[Alias, Optional[Alias]]]] @property def kind(self) -> str: @@ -5899,7 +5945,7 @@ def from_json(cls, x: Any) -> 'Directive': if isinstance(x, List) and len(x) == 2: cons = x[0] if cons == 'ImportFrom': - return cls(ImportFrom((lambda x: (Tok.from_json(x[0]), ModuleName.from_json(x[1]), _atd_read_list((lambda x: (Ident.from_json(x[0]), _atd_read_nullable(Alias.from_json)(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x)))(x[2])) if isinstance(x, list) and len(x) == 3 else _atd_bad_json('array of length 3', x))(x[1]))) + return cls(ImportFrom((lambda x: (Tok.from_json(x[0]), ModuleName.from_json(x[1]), _atd_read_list((lambda x: (Alias.from_json(x[0]), _atd_read_nullable(Alias.from_json)(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x)))(x[2])) if isinstance(x, list) and len(x) == 3 else _atd_bad_json('array of length 3', x))(x[1]))) if cons == 'ImportAs': return cls(ImportAs((lambda x: (Tok.from_json(x[0]), ModuleName.from_json(x[1]), _atd_read_nullable(Alias.from_json)(x[2])) if isinstance(x, list) and len(x) == 3 else _atd_bad_json('array of length 3', x))(x[1]))) if cons == 'ImportAll': @@ -8117,18 +8163,18 @@ def to_json_string(self, **kw: Any) -> str: @dataclass -class PatUnderscore: - """Original type: pattern = [ ... | PatUnderscore of ... | ... ]""" +class PatWildcard: + """Original type: pattern = [ ... | PatWildcard of ... | ... ]""" value: Tok @property def kind(self) -> str: """Name of the class representing this variant.""" - return 'PatUnderscore' + return 'PatWildcard' def to_json(self) -> Any: - return ['PatUnderscore', (lambda x: x.to_json())(self.value)] + return ['PatWildcard', (lambda x: x.to_json())(self.value)] def to_json_string(self, **kw: Any) -> str: return json.dumps(self.to_json(), **kw) @@ -8246,7 +8292,7 @@ def to_json_string(self, **kw: Any) -> str: class Pattern: """Original type: pattern = [ ... ]""" - value: Union[PatLiteral, PatConstructor, PatRecord, PatId, PatTuple, PatList, PatKeyVal, PatUnderscore, PatDisj, PatTyped, PatWhen, PatAs, PatType, OtherPat] + value: Union[PatLiteral, PatConstructor, PatRecord, PatId, PatTuple, PatList, PatKeyVal, PatWildcard, PatDisj, PatTyped, PatWhen, PatAs, PatType, OtherPat] @property def kind(self) -> str: @@ -8271,8 +8317,8 @@ def from_json(cls, x: Any) -> 'Pattern': return cls(PatList(PatternListBracket.from_json(x[1]))) if cons == 'PatKeyVal': return cls(PatKeyVal((lambda x: (Pattern.from_json(x[0]), Pattern.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x[1]))) - if cons == 'PatUnderscore': - return cls(PatUnderscore(Tok.from_json(x[1]))) + if cons == 'PatWildcard': + return cls(PatWildcard(Tok.from_json(x[1]))) if cons == 'PatDisj': return cls(PatDisj((lambda x: (Pattern.from_json(x[0]), Pattern.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x[1]))) if cons == 'PatTyped': diff --git a/ast_generic_v1_j.ml b/ast_generic_v1_j.ml index 682c5d8..c4f5adc 100644 --- a/ast_generic_v1_j.ml +++ b/ast_generic_v1_j.ml @@ -4497,7 +4497,29 @@ let read_xml_kind = ( ) let xml_kind_of_string s = read_xml_kind (Yojson.Safe.init_lexer ()) (Lexing.from_string s) -let rec write__alias_nullable ob x = ( +let rec write__alias_alias_nullable_list ob x = ( + Atdgen_runtime.Oj_run.write_list ( + fun ob x -> + Buffer.add_char ob '['; + (let x, _ = x in + ( + write_alias + ) ob x + ); + Buffer.add_char ob ','; + (let _, x = x in + ( + write__alias_nullable + ) ob x + ); + Buffer.add_char ob ']'; + ) +) ob x +and string_of__alias_alias_nullable_list ?(len = 1024) x = + let ob = Buffer.create len in + write__alias_alias_nullable_list ob x; + Buffer.contents ob +and write__alias_nullable ob x = ( Atdgen_runtime.Oj_run.write_nullable ( write_alias ) @@ -4930,28 +4952,6 @@ and string_of__for_var_or_expr_list ?(len = 1024) x = let ob = Buffer.create len in write__for_var_or_expr_list ob x; Buffer.contents ob -and write__ident_alias_nullable_list ob x = ( - Atdgen_runtime.Oj_run.write_list ( - fun ob x -> - Buffer.add_char ob '['; - (let x, _ = x in - ( - write_ident - ) ob x - ); - Buffer.add_char ob ','; - (let _, x = x in - ( - write__alias_nullable - ) ob x - ); - Buffer.add_char ob ']'; - ) -) ob x -and string_of__ident_alias_nullable_list ?(len = 1024) x = - let ob = Buffer.create len in - write__ident_alias_nullable_list ob x; - Buffer.contents ob and write__ident_type_arguments_nullable_list ob x = ( Atdgen_runtime.Oj_run.write_list ( fun ob x -> @@ -5963,7 +5963,7 @@ and write_directive = ( Buffer.add_char ob ','; (let _, _, x = x in ( - write__ident_alias_nullable_list + write__alias_alias_nullable_list ) ob x ); Buffer.add_char ob ']'; @@ -8829,7 +8829,55 @@ and string_of_xml_body ?(len = 1024) x = let ob = Buffer.create len in write_xml_body ob x; Buffer.contents ob -let rec read__alias_nullable p lb = ( +let rec read__alias_alias_nullable_list p lb = ( + Atdgen_runtime.Oj_run.read_list ( + fun p lb -> + Yojson.Safe.read_space p lb; + let std_tuple = Yojson.Safe.start_any_tuple p lb in + let len = ref 0 in + let end_of_tuple = ref false in + (try + let x0 = + let x = + ( + read_alias + ) p lb + in + incr len; + Yojson.Safe.read_space p lb; + Yojson.Safe.read_tuple_sep2 p std_tuple lb; + x + in + let x1 = + let x = + ( + read__alias_nullable + ) p lb + in + incr len; + (try + Yojson.Safe.read_space p lb; + Yojson.Safe.read_tuple_sep2 p std_tuple lb; + with Yojson.End_of_tuple -> end_of_tuple := true); + x + in + if not !end_of_tuple then ( + try + while true do + Yojson.Safe.skip_json p lb; + Yojson.Safe.read_space p lb; + Yojson.Safe.read_tuple_sep2 p std_tuple lb; + done + with Yojson.End_of_tuple -> () + ); + (x0, x1) + with Yojson.End_of_tuple -> + Atdgen_runtime.Oj_run.missing_tuple_fields p !len [ 0; 1 ]); + ) +) p lb +and _alias_alias_nullable_list_of_string s = + read__alias_alias_nullable_list (Yojson.Safe.init_lexer ()) (Lexing.from_string s) +and read__alias_nullable p lb = ( fun p lb -> Yojson.Safe.read_space p lb; (if Yojson.Safe.read_null_if_possible p lb then None @@ -9621,54 +9669,6 @@ and read__for_var_or_expr_list p lb = ( ) p lb and _for_var_or_expr_list_of_string s = read__for_var_or_expr_list (Yojson.Safe.init_lexer ()) (Lexing.from_string s) -and read__ident_alias_nullable_list p lb = ( - Atdgen_runtime.Oj_run.read_list ( - fun p lb -> - Yojson.Safe.read_space p lb; - let std_tuple = Yojson.Safe.start_any_tuple p lb in - let len = ref 0 in - let end_of_tuple = ref false in - (try - let x0 = - let x = - ( - read_ident - ) p lb - in - incr len; - Yojson.Safe.read_space p lb; - Yojson.Safe.read_tuple_sep2 p std_tuple lb; - x - in - let x1 = - let x = - ( - read__alias_nullable - ) p lb - in - incr len; - (try - Yojson.Safe.read_space p lb; - Yojson.Safe.read_tuple_sep2 p std_tuple lb; - with Yojson.End_of_tuple -> end_of_tuple := true); - x - in - if not !end_of_tuple then ( - try - while true do - Yojson.Safe.skip_json p lb; - Yojson.Safe.read_space p lb; - Yojson.Safe.read_tuple_sep2 p std_tuple lb; - done - with Yojson.End_of_tuple -> () - ); - (x0, x1) - with Yojson.End_of_tuple -> - Atdgen_runtime.Oj_run.missing_tuple_fields p !len [ 0; 1 ]); - ) -) p lb -and _ident_alias_nullable_list_of_string s = - read__ident_alias_nullable_list (Yojson.Safe.init_lexer ()) (Lexing.from_string s) and read__ident_type_arguments_nullable_list p lb = ( Atdgen_runtime.Oj_run.read_list ( fun p lb -> @@ -13029,7 +13029,7 @@ and read_directive = ( let x2 = let x = ( - read__ident_alias_nullable_list + read__alias_alias_nullable_list ) p lb in incr len; @@ -13383,7 +13383,7 @@ and read_directive = ( let x2 = let x = ( - read__ident_alias_nullable_list + read__alias_alias_nullable_list ) p lb in incr len;