Skip to content

Commit

Permalink
Fix data type rule type computation (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored Jun 19, 2024
1 parent 7374f07 commit 9ac42d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/langium/src/parser/langium-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { ValueConverter } from './value-converter.js';
import { defaultParserErrorProvider, EmbeddedActionsParser, LLkLookaheadStrategy } from 'chevrotain';
import { LLStarLookaheadStrategy } from 'chevrotain-allstar';
import { isAssignment, isCrossReference, isKeyword } from '../languages/generated/ast.js';
import { getExplicitRuleType } from '../utils/grammar-utils.js';
import { getExplicitRuleType, isDataTypeRule } from '../utils/grammar-utils.js';
import { assignMandatoryProperties, getContainerOfType, linkContentToContainer } from '../utils/ast-utils.js';
import { CstNodeBuilder } from './cst-node-builder.js';

Expand Down Expand Up @@ -218,7 +218,7 @@ export class LangiumParser extends AbstractLangiumParser {
private computeRuleType(rule: ParserRule): string | symbol | undefined {
if (rule.fragment) {
return undefined;
} else if (rule.dataType) {
} else if (isDataTypeRule(rule)) {
return DatatypeSymbol;
} else {
const explicit = getExplicitRuleType(rule);
Expand Down
41 changes: 41 additions & 0 deletions packages/langium/test/parser/langium-parser-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,47 @@ describe('Parser calls value converter', () => {
// Any value that cannot be parsed correctly is automatically false
expectEqual('d 2022-Peach', false);
});

test('Enums are correctly parsed with types', async () => {
const parser = await parserFromGrammar(`
grammar Test
entry Main:
value=Enum;
Enum returns Enum: 'A' | 'B' | 'C';
type Enum: 'A' | 'B' | 'C';
hidden terminal WS: /\\s+/;
`);

const result = parser.parse('A');
expect(result.lexerErrors.length).toBe(0);
expect(result.parserErrors.length).toBe(0);
const value = result.value as unknown as { value: string };
expect(value.value).toBeTypeOf('string');
expect(value.value).toBe('A');
});

test('Enums are correctly parsed without types', async () => {
const parser = await parserFromGrammar(`
grammar Test
entry Main:
value=Enum;
Enum returns string: 'A' | 'B' | 'C';
hidden terminal WS: /\\s+/;
`);

const result = parser.parse('A');
expect(result.lexerErrors.length).toBe(0);
expect(result.parserErrors.length).toBe(0);
const value = result.value as unknown as { value: string };
expect(value.value).toBeTypeOf('string');
expect(value.value).toBe('A');
});
});

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

0 comments on commit 9ac42d9

Please sign in to comment.