Skip to content

Commit 9ac42d9

Browse files
authored
Fix data type rule type computation (#1552)
1 parent 7374f07 commit 9ac42d9

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/langium/src/parser/langium-parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { ValueConverter } from './value-converter.js';
1616
import { defaultParserErrorProvider, EmbeddedActionsParser, LLkLookaheadStrategy } from 'chevrotain';
1717
import { LLStarLookaheadStrategy } from 'chevrotain-allstar';
1818
import { isAssignment, isCrossReference, isKeyword } from '../languages/generated/ast.js';
19-
import { getExplicitRuleType } from '../utils/grammar-utils.js';
19+
import { getExplicitRuleType, isDataTypeRule } from '../utils/grammar-utils.js';
2020
import { assignMandatoryProperties, getContainerOfType, linkContentToContainer } from '../utils/ast-utils.js';
2121
import { CstNodeBuilder } from './cst-node-builder.js';
2222

@@ -218,7 +218,7 @@ export class LangiumParser extends AbstractLangiumParser {
218218
private computeRuleType(rule: ParserRule): string | symbol | undefined {
219219
if (rule.fragment) {
220220
return undefined;
221-
} else if (rule.dataType) {
221+
} else if (isDataTypeRule(rule)) {
222222
return DatatypeSymbol;
223223
} else {
224224
const explicit = getExplicitRuleType(rule);

packages/langium/test/parser/langium-parser-builder.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,47 @@ describe('Parser calls value converter', () => {
508508
// Any value that cannot be parsed correctly is automatically false
509509
expectEqual('d 2022-Peach', false);
510510
});
511+
512+
test('Enums are correctly parsed with types', async () => {
513+
const parser = await parserFromGrammar(`
514+
grammar Test
515+
516+
entry Main:
517+
value=Enum;
518+
519+
Enum returns Enum: 'A' | 'B' | 'C';
520+
type Enum: 'A' | 'B' | 'C';
521+
522+
hidden terminal WS: /\\s+/;
523+
`);
524+
525+
const result = parser.parse('A');
526+
expect(result.lexerErrors.length).toBe(0);
527+
expect(result.parserErrors.length).toBe(0);
528+
const value = result.value as unknown as { value: string };
529+
expect(value.value).toBeTypeOf('string');
530+
expect(value.value).toBe('A');
531+
});
532+
533+
test('Enums are correctly parsed without types', async () => {
534+
const parser = await parserFromGrammar(`
535+
grammar Test
536+
537+
entry Main:
538+
value=Enum;
539+
540+
Enum returns string: 'A' | 'B' | 'C';
541+
542+
hidden terminal WS: /\\s+/;
543+
`);
544+
545+
const result = parser.parse('A');
546+
expect(result.lexerErrors.length).toBe(0);
547+
expect(result.parserErrors.length).toBe(0);
548+
const value = result.value as unknown as { value: string };
549+
expect(value.value).toBeTypeOf('string');
550+
expect(value.value).toBe('A');
551+
});
511552
});
512553

513554
// Constructs a grammar w/ a special token-builder to support multi-mode lexing

0 commit comments

Comments
 (0)