ParsingOptions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Text.RegularExpressions;
  2. namespace Jint
  3. {
  4. public interface IParsingOptions
  5. {
  6. /// <summary>
  7. /// Gets or sets whether to create compiled <see cref="Regex"/> instances when adapting regular expressions.
  8. /// Defaults to <see langword="null"/>, which means that in the case of non-prepared scripts and modules
  9. /// regular expressions will be interpreted, otherwise they will be compiled.
  10. /// </summary>
  11. bool? CompileRegex { get; init; }
  12. /// <summary>
  13. /// Gets or sets the default timeout for created <see cref="Regex"/> instances.
  14. /// Defaults to <see langword="null"/>, which means that in the case of non-prepared scripts and modules
  15. /// the <see cref="Options.ConstraintOptions.RegexTimeout"/> setting should apply,
  16. /// otherwise the default of the <see cref="ParserOptions.RegexTimeout"/> setting (10 seconds).
  17. /// </summary>
  18. /// <remarks>
  19. /// Please note that <see cref="Options.ConstraintOptions.RegexTimeout"/> setting will be ignored
  20. /// if this option is set to a value other than <see langword="null"/>.
  21. /// </remarks>
  22. TimeSpan? RegexTimeout { get; init; }
  23. /// <summary>
  24. /// Gets or sets whether to parse the source code in tolerant mode.
  25. /// Defaults to <see langword="false"/>.
  26. /// </summary>
  27. bool Tolerant { get; init; }
  28. }
  29. public sealed record class ScriptParsingOptions : IParsingOptions
  30. {
  31. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  32. {
  33. AllowReturnOutsideFunction = 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. }
  90. }