Skip to content

Commit

Permalink
Detect \n and \t as whitespace
Browse files Browse the repository at this point in the history
Previously, splitting a terminal rule into separate "spaces/tabs"
and "newlines" terminals would cause the "newlines" one to be detected
as a comment when generating TextMate grammar
  • Loading branch information
aabounegm committed Jul 19, 2024
1 parent aacd348 commit 46b074a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/langium/src/utils/grammar-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { AstNode, CstNode } from '../syntax-tree.js';
import { isCompositeCstNode } from '../syntax-tree.js';
import { getContainerOfType, streamAllContents } from './ast-utils.js';
import { streamCst } from './cst-utils.js';
import { escapeRegExp } from './regexp-utils.js';
import { escapeRegExp, isWhitespace } from './regexp-utils.js';

/**
* Returns the entry rule of the given grammar, if any. If the grammar file does not contain an entry rule,
Expand Down Expand Up @@ -92,7 +92,7 @@ export function getCrossReferenceTerminal(crossRef: ast.CrossReference): ast.Abs
* that contains visible characters is considered a comment.
*/
export function isCommentTerminal(terminalRule: ast.TerminalRule): boolean {
return terminalRule.hidden && !terminalRegex(terminalRule).test(' ');
return terminalRule.hidden && !isWhitespace(terminalRegex(terminalRule));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/langium/src/utils/regexp-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function isMultilineComment(regexp: RegExp | string): boolean {

export function isWhitespace(value: RegExp | string): boolean {
const regexp = typeof value === 'string' ? new RegExp(value) : value;
return regexp.test(' ');
return [' ', '\n', '\t'].some((ws) => regexp.test(ws));
}

export function escapeRegExp(value: string): string {
Expand Down

0 comments on commit 46b074a

Please sign in to comment.