2323import java .nio .file .Paths ;
2424import java .nio .file .SimpleFileVisitor ;
2525import java .nio .file .attribute .BasicFileAttributes ;
26+ import java .util .Collection ;
2627import java .util .List ;
2728import java .util .Set ;
2829import java .util .concurrent .atomic .AtomicInteger ;
2930
3031import org .eclipse .jdt .annotation .NonNullByDefault ;
32+ import org .eclipse .jdt .annotation .Nullable ;
3133import org .eclipse .tm4e .core .Data ;
34+ import org .eclipse .tm4e .core .internal .grammar .raw .IRawRule ;
3235import org .eclipse .tm4e .core .internal .grammar .raw .RawGrammar ;
3336import org .eclipse .tm4e .core .internal .grammar .raw .RawGrammarReader ;
37+ import org .eclipse .tm4e .core .internal .oniguruma .OnigRegExp ;
3438import org .eclipse .tm4e .core .internal .utils .ResourceUtils ;
3539import org .junit .jupiter .api .MethodOrderer ;
3640import org .junit .jupiter .api .Test ;
@@ -197,6 +201,33 @@ void testParseYAML() throws Exception {
197201 }
198202 }
199203
204+ private void assertParseablePattern (final @ Nullable String pattern ) {
205+ if (pattern == null )
206+ return ;
207+ try {
208+ assertNotNull (new OnigRegExp (pattern ));
209+ } catch (final RuntimeException ex ) {
210+ final var msg = ex .getMessage ();
211+ if (msg != null && msg .contains ("invalid backref number/name" )) {
212+ // ignore
213+ } else
214+ throw ex ;
215+ }
216+ }
217+
218+ private void assertParseablePatterns (final @ Nullable Collection <IRawRule > patterns ) {
219+ if (patterns == null || patterns .isEmpty ())
220+ return ;
221+
222+ for (final var rule : patterns ) {
223+ assertParseablePattern (rule .getBegin ());
224+ assertParseablePattern (rule .getEnd ());
225+ assertParseablePattern (rule .getMatch ());
226+ assertParseablePattern (rule .getWhile ());
227+ assertParseablePatterns (rule .getPatterns ());
228+ }
229+ }
230+
200231 @ Test
201232 @ NonNullByDefault ({})
202233 void testLanguagePackGrammars () throws IOException {
@@ -210,9 +241,12 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
210241 final var grammar = TMParserJSON .INSTANCE .parse (input , RawGrammarReader .OBJECT_FACTORY );
211242 count .incrementAndGet ();
212243 assertFalse (grammar .getScopeName ().isBlank ());
213- assertFalse (castNonNull (grammar .getPatterns ()).isEmpty ());
214244 assertNotNull (grammar .getFileTypes ());
215245 assertNotNull (grammar .getRepository ());
246+
247+ final var patterns = castNonNull (grammar .getPatterns ());
248+ assertFalse (patterns .isEmpty ());
249+ assertParseablePatterns (patterns );
216250 } catch (final Exception ex ) {
217251 throw new RuntimeException (ex );
218252 }
0 commit comments