[dart2js] Pull recipe syntax constants into shared library

Change-Id: Ibed00f517934ccfd5c0a05c6daedb72966b4ac01
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106723
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
diff --git a/sdk/lib/_internal/js_runtime/lib/rti.dart b/sdk/lib/_internal/js_runtime/lib/rti.dart
index fe11d73..b53dcc8 100644
--- a/sdk/lib/_internal/js_runtime/lib/rti.dart
+++ b/sdk/lib/_internal/js_runtime/lib/rti.dart
@@ -11,6 +11,8 @@
 
 import 'dart:_js_embedded_names' show RtiUniverseFieldNames, RTI_UNIVERSE;
 
+import 'dart:_recipe_syntax';
+
 /// An Rti object represents both a type (e.g `Map<int, String>`) and a type
 /// environment (`Map<int, String>` binds `Map.K=int` and `Map.V=String`).
 ///
@@ -728,42 +730,41 @@
     int i = 0;
     while (i < source.length) {
       int ch = charCodeAt(source, i);
-      if (isDigit(ch)) {
+      if (Recipe.isDigit(ch)) {
         i = handleDigit(i + 1, ch, source, stack);
-      } else if (isIdentifierStart(ch)) {
+      } else if (Recipe.isIdentifierStart(ch)) {
         i = handleIdentifer(parser, i, source, stack, false);
-      } else if (ch == $PERIOD) {
+      } else if (ch == Recipe.period) {
         i = handleIdentifer(parser, i, source, stack, true);
       } else {
         i++;
         switch (ch) {
-          case $COMMA:
-            // ignored
+          case Recipe.noOp:
             break;
 
-          case $SEMICOLON:
+          case Recipe.toType:
             push(stack,
                 toType(universe(parser), environment(parser), pop(stack)));
             break;
 
-          case $AT:
+          case Recipe.pushDynamic:
             push(stack, _Universe._lookupDynamicRti(universe(parser)));
             break;
 
-          case $TILDE:
+          case Recipe.pushVoid:
             push(stack, _Universe._lookupVoidRti(universe(parser)));
             break;
 
-          case $LT:
+          case Recipe.startTypeArguments:
             push(stack, position(parser));
             setPosition(parser, _Utils.arrayLength(stack));
             break;
 
-          case $GT:
+          case Recipe.endTypeArguments:
             handleTypeArguments(parser, stack);
             break;
 
-          case $AMPERSAND:
+          case Recipe.extensionOp:
             handleExtendedOperations(parser, stack);
             break;
 
@@ -777,11 +778,11 @@
   }
 
   static int handleDigit(int i, int digit, String source, Object stack) {
-    int value = digit - $0;
+    int value = Recipe.digitValue(digit);
     for (; i < source.length; i++) {
       int ch = charCodeAt(source, i);
-      if (!isDigit(ch)) break;
-      value = value * 10 + ch - $0;
+      if (!Recipe.isDigit(ch)) break;
+      value = value * 10 + Recipe.digitValue(ch);
     }
     push(stack, value);
     return i;
@@ -792,10 +793,10 @@
     int i = start + 1;
     for (; i < source.length; i++) {
       int ch = charCodeAt(source, i);
-      if (ch == $PERIOD) {
+      if (ch == Recipe.period) {
         if (hasPeriod) break;
         hasPeriod = true;
-      } else if (isIdentifierStart(ch) || isDigit(ch)) {
+      } else if (Recipe.isIdentifierStart(ch) || Recipe.isDigit(ch)) {
         // Accept.
       } else {
         break;
@@ -899,34 +900,6 @@
     }
     throw AssertionError('Bad index $index for $environment');
   }
-
-  static bool isDigit(int ch) => ch >= $0 && ch <= $9;
-  static bool isIdentifierStart(int ch) =>
-      (ch >= $A && ch <= $Z) ||
-      (ch >= $a && ch <= $z) ||
-      (ch == $_) ||
-      (ch == $$);
-
-  static const int $$ = 0x24;
-  static const int $AMPERSAND = 0x26;
-  static const int $PLUS = 0x2B;
-  static const int $COMMA = 0x2C;
-  static const int $PERIOD = 0x2E;
-  static const int $0 = 0x30;
-  static const int $9 = 0x39;
-  static const int $SEMICOLON = 0x3B;
-  static const int $LT = 0x3C;
-  static const int $GT = 0x3E;
-  static const int $QUESTION = 0x3F;
-  static const int $AT = 0x40;
-  static const int $A = 0x41;
-  static const int $Z = 0x5A;
-  static const int $LBRACKET = 0x5B;
-  static const int $RBRACKET = 0x5D;
-  static const int $a = $A + 32;
-  static const int $z = $Z + 32;
-  static const int $_ = 0x5F;
-  static const int $TILDE = 0x7E;
 }
 
 /// Represents the set of supertypes and type variable bindings for a given
diff --git a/sdk/lib/_internal/js_runtime/lib/shared/recipe_syntax.dart b/sdk/lib/_internal/js_runtime/lib/shared/recipe_syntax.dart
new file mode 100644
index 0000000..78d0975
--- /dev/null
+++ b/sdk/lib/_internal/js_runtime/lib/shared/recipe_syntax.dart
@@ -0,0 +1,67 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/// Constants and predicates used for encoding and decoding type recipes.
+///
+/// This library is shared between the compiler and the runtime system.
+library dart2js._recipe_syntax;
+
+abstract class Recipe {
+  Recipe._();
+
+  // Operators.
+
+  static const int noOp = _comma;
+
+  static const int toType = _semicolon;
+
+  static const int pushDynamic = _at;
+  static const int pushVoid = _tilde;
+  static const int wrapFutureOr = _formfeed;
+
+  static const int startTypeArguments = _lessThan;
+  static const int endTypeArguments = _greaterThan;
+
+  static const int extensionOp = _ampersand;
+
+  // Number and name components.
+
+  static bool isDigit(int code) => code >= _digit0 && code <= _digit9;
+  static int digitValue(int code) => code - _digit0;
+
+  static bool isIdentifierStart(int ch) =>
+      (((ch | 32) - _lowercaseA) & 0xffff) < 26 ||
+      (ch == _underscore) ||
+      (ch == _dollar);
+
+  static const int period = _period;
+
+  // Private names.
+
+  static const int _formfeed = 0x0C; // '\f' in string literal.
+
+  static const int _dollar = 0x24;
+  static const int _ampersand = 0x26;
+  static const int _plus = 0x2B;
+  static const int _comma = 0x2C;
+  static const int _period = 0x2E;
+  static const int _digit0 = 0x30;
+  static const int _digit9 = 0x39;
+  static const int _semicolon = 0x3B;
+  static const int _lessThan = 0x3C;
+  static const int _greaterThan = 0x3E;
+  static const int _question = 0x3f;
+  static const int _at = 0x40;
+
+  static const int _underscore = 0x5F;
+  static const int _lowercaseA = 0x61;
+  static const int _tilde = 0x7E;
+
+  static const int _leftParen = 0x28;
+  static const int _rightParen = 0x29;
+  static const int _leftBracket = 0x5B;
+  static const int _rightBracket = 0x5D;
+  static const int _leftBrace = 0x7B;
+  static const int _rightBrace = 0x7D;
+}
diff --git a/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart b/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart
index f848a09..cfd6fa6 100644
--- a/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart
+++ b/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart
@@ -181,6 +181,11 @@
       categories: "",
       documented: false,
       platforms: DART2JS_PLATFORM),
+  "_recipe_syntax": const LibraryInfo(
+      "_internal/js_runtime/lib/shared/recipe_syntax.dart",
+      categories: "",
+      documented: false,
+      platforms: DART2JS_PLATFORM),
   "_metadata": const LibraryInfo("html/html_common/metadata.dart",
       categories: "", documented: false, platforms: DART2JS_PLATFORM),
 };
@@ -293,26 +298,26 @@
       1,
       "Experimental",
       "This library is experimental and will likely change or be removed\n"
-      "in future versions.");
+          "in future versions.");
 
   static const Maturity UNSTABLE = const Maturity(
       2,
       "Unstable",
       "This library is in still changing and have not yet endured\n"
-      "sufficient real-world testing.\n"
-      "Backwards-compatibility is NOT guaranteed.");
+          "sufficient real-world testing.\n"
+          "Backwards-compatibility is NOT guaranteed.");
 
   static const Maturity WEB_STABLE = const Maturity(
       3,
       "Web Stable",
       "This library is tracking the DOM evolution as defined by WC3.\n"
-      "Backwards-compatibility is NOT guaranteed.");
+          "Backwards-compatibility is NOT guaranteed.");
 
   static const Maturity STABLE = const Maturity(
       4,
       "Stable",
       "The library is stable. API backwards-compatibility is guaranteed.\n"
-      "However implementation details might change.");
+          "However implementation details might change.");
 
   static const Maturity LOCKED = const Maturity(5, "Locked",
       "This library will not change except when serious bugs are encountered.");
diff --git a/sdk/lib/libraries.json b/sdk/lib/libraries.json
index 4a0f788..30197aa2 100644
--- a/sdk/lib/libraries.json
+++ b/sdk/lib/libraries.json
@@ -205,6 +205,9 @@
       "html_common": {
         "uri": "html/html_common/html_common_dart2js.dart"
       },
+      "_recipe_syntax": {
+        "uri": "_internal/js_runtime/lib/shared/recipe_syntax.dart"
+      },
       "_native_typed_data": {
         "uri": "_internal/js_runtime/lib/native_typed_data.dart"
       },
@@ -386,48 +389,18 @@
   },
   "dart2js_server": {
     "libraries": {
-      "_native_typed_data": {
-        "uri": "_internal/js_runtime/lib/native_typed_data.dart"
-      },
-      "_js_names": {
-        "uri": "_internal/js_runtime/lib/js_names.dart"
-      },
-      "core": {
-        "patches": "_internal/js_runtime/lib/core_patch.dart",
-        "uri": "core/core.dart"
-      },
       "async": {
         "patches": "_internal/js_runtime/lib/async_patch.dart",
         "uri": "async/async.dart"
       },
-      "collection": {
-        "patches": "_internal/js_runtime/lib/collection_patch.dart",
-        "uri": "collection/collection.dart"
-      },
-      "js_util": {
-        "uri": "js_util/dart2js/js_util_dart2js.dart"
-      },
-      "typed_data": {
-        "patches": "_internal/js_runtime/lib/typed_data_patch.dart",
-        "uri": "typed_data/typed_data.dart"
-      },
-      "_interceptors": {
-        "uri": "_internal/js_runtime/lib/interceptors.dart"
-      },
-      "developer": {
-        "patches": "_internal/js_runtime/lib/developer_patch.dart",
-        "uri": "developer/developer.dart"
-      },
-      "isolate": {
-        "patches": "_internal/js_runtime/lib/isolate_patch.dart",
-        "supported": false,
-        "uri": "isolate/isolate.dart"
-      },
       "mirrors": {
         "patches": "_internal/js_runtime/lib/mirrors_patch_cfe.dart",
         "supported": false,
         "uri": "mirrors/mirrors.dart"
       },
+      "_interceptors": {
+        "uri": "_internal/js_runtime/lib/interceptors.dart"
+      },
       "_js_embedded_names": {
         "uri": "_internal/js_runtime/lib/shared/embedded_names.dart"
       },
@@ -440,13 +413,58 @@
         "patches": "_internal/js_runtime/lib/internal_patch.dart",
         "uri": "internal/internal.dart"
       },
+      "_async_await_error_codes": {
+        "uri": "_internal/js_runtime/lib/shared/async_await_error_codes.dart"
+      },
+      "_http": {
+        "uri": "_http/http.dart"
+      },
+      "_js_helper": {
+        "uri": "_internal/js_runtime/lib/js_helper.dart"
+      },
+      "_js_primitives": {
+        "uri": "_internal/js_runtime/lib/js_primitives.dart"
+      },
+      "js": {
+        "uri": "js/dart2js/js_dart2js.dart"
+      },
+      "_recipe_syntax": {
+        "uri": "_internal/js_runtime/lib/shared/recipe_syntax.dart"
+      },
+      "_native_typed_data": {
+        "uri": "_internal/js_runtime/lib/native_typed_data.dart"
+      },
+      "core": {
+        "patches": "_internal/js_runtime/lib/core_patch.dart",
+        "uri": "core/core.dart"
+      },
+      "_js_names": {
+        "uri": "_internal/js_runtime/lib/js_names.dart"
+      },
+      "js_util": {
+        "uri": "js_util/dart2js/js_util_dart2js.dart"
+      },
+      "collection": {
+        "patches": "_internal/js_runtime/lib/collection_patch.dart",
+        "uri": "collection/collection.dart"
+      },
+      "typed_data": {
+        "patches": "_internal/js_runtime/lib/typed_data_patch.dart",
+        "uri": "typed_data/typed_data.dart"
+      },
+      "isolate": {
+        "patches": "_internal/js_runtime/lib/isolate_patch.dart",
+        "supported": false,
+        "uri": "isolate/isolate.dart"
+      },
+      "developer": {
+        "patches": "_internal/js_runtime/lib/developer_patch.dart",
+        "uri": "developer/developer.dart"
+      },
       "_js": {
         "patches": "js/_js_server.dart",
         "uri": "js/_js.dart"
       },
-      "_async_await_error_codes": {
-        "uri": "_internal/js_runtime/lib/shared/async_await_error_codes.dart"
-      },
       "convert": {
         "patches": "_internal/js_runtime/lib/convert_patch.dart",
         "uri": "convert/convert.dart"
@@ -458,21 +476,9 @@
       "_foreign_helper": {
         "uri": "_internal/js_runtime/lib/foreign_helper.dart"
       },
-      "_http": {
-        "uri": "_http/http.dart"
-      },
-      "_js_primitives": {
-        "uri": "_internal/js_runtime/lib/js_primitives.dart"
-      },
-      "_js_helper": {
-        "uri": "_internal/js_runtime/lib/js_helper.dart"
-      },
       "_rti": {
         "uri": "_internal/js_runtime/lib/rti.dart"
-      },
-      "js": {
-        "uri": "js/dart2js/js_dart2js.dart"
       }
     }
   }
-}
+}
\ No newline at end of file
diff --git a/sdk/lib/libraries.yaml b/sdk/lib/libraries.yaml
index 7a5520c..8eea310 100644
--- a/sdk/lib/libraries.yaml
+++ b/sdk/lib/libraries.yaml
@@ -266,6 +266,9 @@
     _async_await_error_codes:
       uri: "_internal/js_runtime/lib/shared/async_await_error_codes.dart"
 
+    _recipe_syntax:
+      uri: "_internal/js_runtime/lib/shared/recipe_syntax.dart"
+
     _metadata:
       uri: "html/html_common/metadata.dart"
 
@@ -358,6 +361,9 @@
     _async_await_error_codes:
       uri: "_internal/js_runtime/lib/shared/async_await_error_codes.dart"
 
+    _recipe_syntax:
+      uri: "_internal/js_runtime/lib/shared/recipe_syntax.dart"
+
 dartdevc:
     libraries:
       _runtime: