ParsingOptions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 class ScriptParsingOptions : IParsingOptions
  29. {
  30. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  31. {
  32. AllowReturnOutsideFunction = true,
  33. RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
  34. };
  35. public static readonly ScriptParsingOptions Default = new();
  36. /// <summary>
  37. /// Gets or sets whether to allow return statements at the top level.
  38. /// Defaults to <see langword="true"/>.
  39. /// </summary>
  40. public bool AllowReturnOutsideFunction { get; init; } = _defaultParserOptions.AllowReturnOutsideFunction;
  41. /// <inheritdoc/>
  42. public bool? CompileRegex { get; init; }
  43. /// <inheritdoc/>
  44. public TimeSpan? RegexTimeout { get; init; }
  45. /// <inheritdoc/>
  46. public bool Tolerant { get; init; } = _defaultParserOptions.Tolerant;
  47. internal ParserOptions ApplyTo(ParserOptions parserOptions, RegExpParseMode defaultRegExpParseMode, TimeSpan defaultRegexTimeout) => parserOptions with
  48. {
  49. AllowReturnOutsideFunction = AllowReturnOutsideFunction,
  50. RegExpParseMode = CompileRegex is null
  51. ? defaultRegExpParseMode
  52. : (CompileRegex.Value ? RegExpParseMode.AdaptToCompiled : RegExpParseMode.AdaptToInterpreted),
  53. RegexTimeout = RegexTimeout ?? defaultRegexTimeout,
  54. Tolerant = Tolerant,
  55. };
  56. internal ParserOptions GetParserOptions() => ReferenceEquals(this, Default)
  57. ? _defaultParserOptions
  58. : ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, _defaultParserOptions.RegexTimeout);
  59. internal ParserOptions GetParserOptions(Options engineOptions)
  60. => ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, engineOptions.Constraints.RegexTimeout);
  61. }
  62. public sealed record class ModuleParsingOptions : IParsingOptions
  63. {
  64. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  65. {
  66. RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
  67. };
  68. public static readonly ModuleParsingOptions Default = new();
  69. /// <inheritdoc/>
  70. public bool? CompileRegex { get; init; }
  71. /// <inheritdoc/>
  72. public TimeSpan? RegexTimeout { get; init; }
  73. /// <inheritdoc/>
  74. public bool Tolerant { get; init; } = _defaultParserOptions.Tolerant;
  75. internal ParserOptions ApplyTo(ParserOptions baseOptions, RegExpParseMode defaultRegExpParseMode, TimeSpan defaultRegexTimeout) => baseOptions with
  76. {
  77. RegExpParseMode = CompileRegex is null
  78. ? defaultRegExpParseMode
  79. : (CompileRegex.Value ? RegExpParseMode.AdaptToCompiled : RegExpParseMode.AdaptToInterpreted),
  80. RegexTimeout = RegexTimeout ?? defaultRegexTimeout,
  81. Tolerant = Tolerant,
  82. };
  83. internal ParserOptions GetParserOptions() => ReferenceEquals(this, Default)
  84. ? _defaultParserOptions
  85. : ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, _defaultParserOptions.RegexTimeout);
  86. internal ParserOptions GetParserOptions(Options engineOptions)
  87. => ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, engineOptions.Constraints.RegexTimeout);
  88. }