|
5 | 5 | ******************************************************************************/ |
6 | 6 |
|
7 | 7 | import type { TokenType, TokenVocabulary } from 'chevrotain'; |
8 | | -import type { AstNode, CstNode, GenericAstNode, Grammar, GrammarAST, LangiumParser, TokenBuilderOptions } from 'langium'; |
| 8 | +import type { AstNode, CstNode, GenericAstNode, Grammar, GrammarAST, LangiumParser, ParseResult, TokenBuilderOptions } from 'langium'; |
9 | 9 | import { EmptyFileSystem, DefaultTokenBuilder } from 'langium'; |
10 | 10 | import { describe, expect, test, onTestFailed, beforeEach } from 'vitest'; |
11 | 11 | import { createLangiumGrammarServices, createServicesForGrammar } from 'langium/grammar'; |
@@ -852,7 +852,31 @@ describe('Unassigned data type rules', () => { |
852 | 852 | expect(parseResult.lexerErrors).toHaveLength(0); |
853 | 853 | expect(parseResult.parserErrors).toHaveLength(0); |
854 | 854 | }); |
| 855 | +}); |
| 856 | + |
| 857 | +describe('Parsing with lookbehind tokens', () => { |
| 858 | + test('Parser Success / Failure with positive lookbehind', async () => { |
| 859 | + await testLookbehind(true, 'AB', 'CB'); |
| 860 | + }); |
| 861 | + |
| 862 | + test('Parser Success / Failure with negative lookbehind', async () => { |
| 863 | + await testLookbehind(false, 'CB', 'AB'); |
| 864 | + }); |
855 | 865 |
|
| 866 | + async function testLookbehind(positive: boolean, success: string, failure: string): Promise<void> { |
| 867 | + const parser = await parserFromGrammar(` |
| 868 | + grammar test |
| 869 | + entry Main: ('A' | 'C') b=B; |
| 870 | + terminal B: (?<${positive ? '=' : '!'}'A')'B'; |
| 871 | + hidden terminal WS: /\\s+/; |
| 872 | + ` |
| 873 | + ); |
| 874 | + const validResult = parser.parse(success) as ParseResult<GenericAstNode>; |
| 875 | + expect(validResult.value.b).toEqual('B'); |
| 876 | + const invalidResult = parser.parse(failure); |
| 877 | + expect(invalidResult.lexerErrors).toHaveLength(1); |
| 878 | + expect(invalidResult.parserErrors).toHaveLength(1); |
| 879 | + } |
856 | 880 | }); |
857 | 881 |
|
858 | 882 | async function parserFromGrammar(grammar: string): Promise<LangiumParser> { |
|
0 commit comments