22
33import java .util .ArrayList ;
44import java .util .Collections ;
5- import java .util .LinkedHashMap ;
65import java .util .List ;
7- import java .util .Map ;
86import java .util .regex .Matcher ;
97import java .util .regex .Pattern ;
108
163161 */
164162public class TypeScriptASTConverter {
165163 private String source ;
166- private final JsonObject nodeFlags ;
167- private final JsonObject syntaxKinds ;
168- private final Map <Integer , String > nodeFlagMap = new LinkedHashMap <>();
169- private final Map <Integer , String > syntaxKindMap = new LinkedHashMap <>();
164+ private final TypeScriptParserMetadata metadata ;
170165 private int [] lineStarts ;
171166
172167 private int syntaxKindExtends ;
@@ -180,24 +175,11 @@ public class TypeScriptASTConverter {
180175 private static final Pattern WHITESPACE_END_PAREN =
181176 Pattern .compile ("^" + WHITESPACE_CHAR + "*\\ )" );
182177
183- TypeScriptASTConverter (JsonObject nodeFlags , JsonObject syntaxKinds ) {
184- this .nodeFlags = nodeFlags ;
185- this .syntaxKinds = syntaxKinds ;
186- makeEnumIdMap (nodeFlags , nodeFlagMap );
187- makeEnumIdMap (syntaxKinds , syntaxKindMap );
178+ TypeScriptASTConverter (TypeScriptParserMetadata metadata ) {
179+ this .metadata = metadata ;
188180 this .syntaxKindExtends = getSyntaxKind ("ExtendsKeyword" );
189181 }
190182
191- /** Builds a mapping from ID to name given a TypeScript enum object. */
192- private void makeEnumIdMap (JsonObject enumObject , Map <Integer , String > idToName ) {
193- for (Map .Entry <String , JsonElement > entry : enumObject .entrySet ()) {
194- JsonPrimitive prim = entry .getValue ().getAsJsonPrimitive ();
195- if (prim .isNumber () && !idToName .containsKey (prim .getAsInt ())) {
196- idToName .put (prim .getAsInt (), entry .getKey ());
197- }
198- }
199- }
200-
201183 /**
202184 * Convert the given TypeScript AST (which was parsed from {@code source}) into a parser {@link
203185 * Result}.
@@ -1617,7 +1599,7 @@ private Node convertMappedType(JsonObject node, SourceLocation loc) throws Parse
16171599 private Node convertMetaProperty (JsonObject node , SourceLocation loc ) throws ParseError {
16181600 Position metaStart = loc .getStart ();
16191601 String keywordKind =
1620- syntaxKinds .get (node .getAsJsonPrimitive ("keywordToken" ).getAsInt () + "" ).getAsString ();
1602+ metadata . getSyntaxKinds () .get (node .getAsJsonPrimitive ("keywordToken" ).getAsInt () + "" ).getAsString ();
16211603 String identifier = keywordKind .equals ("ImportKeyword" ) ? "import" : "new" ;
16221604 Position metaEnd =
16231605 new Position (
@@ -1995,7 +1977,7 @@ private Node convertPrefixUnaryExpression(JsonObject node, SourceLocation loc) t
19951977
19961978 private String getOperator (JsonObject node ) throws ParseError {
19971979 int operatorId = node .get ("operator" ).getAsInt ();
1998- switch (syntaxKindMap .get (operatorId )) {
1980+ switch (metadata . getSyntaxKindMap () .get (operatorId )) {
19991981 case "PlusPlusToken" :
20001982 return "++" ;
20011983 case "MinusMinusToken" :
@@ -2219,7 +2201,7 @@ private Node convertTypeOfExpression(JsonObject node, SourceLocation loc) throws
22192201 }
22202202
22212203 private Node convertTypeOperator (JsonObject node , SourceLocation loc ) throws ParseError {
2222- String operator = syntaxKinds .get ("" + node .get ("operator" ).getAsInt ()).getAsString ();
2204+ String operator = metadata . getSyntaxKinds () .get ("" + node .get ("operator" ).getAsInt ()).getAsString ();
22232205 if (operator .equals ("KeyOfKeyword" )) {
22242206 return new UnaryTypeExpr (loc , UnaryTypeExpr .Kind .Keyof , convertChildAsType (node , "type" ));
22252207 }
@@ -2537,7 +2519,7 @@ private int getMemberModifierKeywords(JsonObject node) {
25372519 * <tt>ts.NodeFlags</tt> in enum.
25382520 */
25392521 private boolean hasFlag (JsonObject node , String flagName ) {
2540- JsonElement flagDescriptor = this .nodeFlags .get (flagName );
2522+ JsonElement flagDescriptor = this .metadata . getNodeFlags () .get (flagName );
25412523 if (flagDescriptor == null ) {
25422524 throw new RuntimeException (
25432525 "Incompatible version of TypeScript installed. Missing node flag " + flagName );
@@ -2552,7 +2534,7 @@ private boolean hasFlag(JsonObject node, String flagName) {
25522534
25532535 /** Gets the numeric value of the syntax kind enum with the given name. */
25542536 private int getSyntaxKind (String syntaxKind ) {
2555- JsonElement descriptor = this .syntaxKinds .get (syntaxKind );
2537+ JsonElement descriptor = this .metadata . getSyntaxKinds () .get (syntaxKind );
25562538 if (descriptor == null ) {
25572539 throw new RuntimeException (
25582540 "Incompatible version of TypeScript installed. Missing syntax kind " + syntaxKind );
@@ -2581,7 +2563,7 @@ private String getKind(JsonElement node) {
25812563 if (node instanceof JsonObject ) {
25822564 JsonElement kind = ((JsonObject ) node ).get ("kind" );
25832565 if (kind instanceof JsonPrimitive && ((JsonPrimitive ) kind ).isNumber ())
2584- return syntaxKindMap .get (kind .getAsInt ());
2566+ return metadata . getSyntaxKindMap () .get (kind .getAsInt ());
25852567 }
25862568 return null ;
25872569 }
0 commit comments