ParsingOptions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Text.RegularExpressions;
  2. namespace Jint;
  3. public interface IParsingOptions
  4. {
  5. /// <summary>
  6. /// Gets or sets whether to create compiled <see cref="Regex"/> instances when adapting regular expressions.
  7. /// Defaults to <see langword="null"/>, which means that in the case of non-prepared scripts and modules
  8. /// regular expressions will be interpreted, otherwise they will be compiled.
  9. /// </summary>
  10. bool? CompileRegex { get; init; }
  11. /// <summary>
  12. /// Gets or sets the default timeout for created <see cref="Regex"/> instances.
  13. /// Defaults to <see langword="null"/>, which means that in the case of non-prepared scripts and modules
  14. /// the <see cref="Options.ConstraintOptions.RegexTimeout"/> setting should apply,
  15. /// otherwise the default of the <see cref="ParserOptions.RegexTimeout"/> setting (10 seconds).
  16. /// </summary>
  17. /// <remarks>
  18. /// Please note that <see cref="Options.ConstraintOptions.RegexTimeout"/> setting will be ignored
  19. /// if this option is set to a value other than <see langword="null"/>.
  20. /// </remarks>
  21. TimeSpan? RegexTimeout { get; init; }
  22. /// <summary>
  23. /// Gets or sets whether to parse the source code in tolerant mode.
  24. /// Defaults to <see langword="false"/>.
  25. /// </summary>
  26. bool Tolerant { get; init; }
  27. }
  28. public sealed record ScriptParsingOptions : IParsingOptions
  29. {
  30. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  31. {
  32. AllowReturnOutsideFunction = true,
  33. AllowTopLevelUsing = true,
  34. RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
  35. };
  36. public static readonly ScriptParsingOptions Default = new();
  37. /// <summary>
  38. /// Gets or sets whether to allow return statements at the top level.
  39. /// Defaults to <see langword="true"/>.
  40. /// </summary>
  41. public bool AllowReturnOutsideFunction { get; init; } = _defaultParserOptions.AllowReturnOutsideFunction;
  42. /// <inheritdoc/>
  43. public bool? CompileRegex { get; init; }
  44. /// <inheritdoc/>
  45. public TimeSpan? RegexTimeout { get; init; }
  46. /// <inheritdoc/>
  47. public bool Tolerant { get; init; } = _defaultParserOptions.Tolerant;
  48. internal ParserOptions ApplyTo(ParserOptions parserOptions, RegExpParseMode defaultRegExpParseMode, TimeSpan defaultRegexTimeout) => parserOptions with
  49. {
  50. AllowReturnOutsideFunction = AllowReturnOutsideFunction,
  51. RegExpParseMode = CompileRegex is null
  52. ? defaultRegExpParseMode
  53. : (CompileRegex.Value ? RegExpParseMode.AdaptToCompiled : RegExpParseMode.AdaptToInterpreted),
  54. RegexTimeout = RegexTimeout ?? defaultRegexTimeout,
  55. Tolerant = Tolerant,
  56. };
  57. internal ParserOptions GetParserOptions() => ReferenceEquals(this, Default)
  58. ? _defaultParserOptions
  59. : ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, _defaultParserOptions.RegexTimeout);
  60. internal ParserOptions GetParserOptions(Options engineOptions)
  61. => ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, engineOptions.Constraints.RegexTimeout);
  62. }
  63. public sealed record class ModuleParsingOptions : IParsingOptions
  64. {
  65. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  66. {
  67. RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
  68. };
  69. public static readonly ModuleParsingOptions Default = new();
  70. /// <inheritdoc/>
  71. public bool? CompileRegex { get; init; }
  72. /// <inheritdoc/>
  73. public TimeSpan? RegexTimeout { get; init; }
  74. /// <inheritdoc/>
  75. public bool Tolerant { get; init; } = _defaultParserOptions.Tolerant;
  76. internal ParserOptions ApplyTo(ParserOptions baseOptions, RegExpParseMode defaultRegExpParseMode, TimeSpan defaultRegexTimeout) => baseOptions with
  77. {
  78. RegExpParseMode = CompileRegex is null
  79. ? defaultRegExpParseMode
  80. : (CompileRegex.Value ? RegExpParseMode.AdaptToCompiled : RegExpParseMode.AdaptToInterpreted),
  81. RegexTimeout = RegexTimeout ?? defaultRegexTimeout,
  82. Tolerant = Tolerant,
  83. };
  84. internal ParserOptions GetParserOptions() => ReferenceEquals(this, Default)
  85. ? _defaultParserOptions
  86. : ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, _defaultParserOptions.RegexTimeout);
  87. internal ParserOptions GetParserOptions(Options engineOptions)
  88. => ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, engineOptions.Constraints.RegexTimeout);
  89. }