Skip to content

Commit

Permalink
Workaround for false positive validation error #214
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Dec 16, 2024
1 parent ed26d42 commit e9ddf3e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bbj-vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bbj-vscode/src/language/bbj-lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class BbjLexer extends DefaultLexer {
return super.tokenize(text);
}

private prepareLineSplitter(text: string): string {
protected prepareLineSplitter(text: string): string {
const windowsEol = text.includes('\r\n');
const lines = text.split(/\r?\n/g);
for (let i = 0; i < lines.length - 1; i++) {
Expand Down
2 changes: 1 addition & 1 deletion bbj-vscode/src/language/bbj-token-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class BBjTokenBuilder extends DefaultTokenBuilder {
const token: TokenType = {
name: terminal.name,
// Add more exceptional tokens here if an explicit line break token is needed
PATTERN: this.regexPatternFunction(/,(?=(\r?\n|;))/i),
PATTERN: this.regexPatternFunction(/,(?=(\r?\n|;))/),
LINE_BREAKS: true
};
return token;
Expand Down
5 changes: 4 additions & 1 deletion bbj-vscode/src/language/bbj-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ export class BBjValidator {
}

checkSymbolicLabelRef(ele: SymbolicLabelRef, accept: ValidationAcceptor): void {
if (ele.$cstNode?.text.search(/\s/) !== -1) {
const nodeText = ele.$cstNode?.text;
// currently when using ':', see #214, node text may be shifted, that why
// we check for the first character to avoid false positives
if (nodeText && nodeText.startsWith('*') && nodeText.search(/\s/) !== -1) {
accept('error', 'Symbolic label reference may not contain whitespace.', { node: ele });
}
}
Expand Down
10 changes: 10 additions & 0 deletions bbj-vscode/test/bbj-test-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BBjGeneratedModule, BBjGeneratedSharedModule } from "../src/language/ge
import { registerValidationChecks } from "../src/language/bbj-validator.js";
import { JavaInteropService } from "../src/language/java-interop.js";
import { JavaClass, JavaMethod } from "../src/language/generated/ast.js";
import { BbjLexer } from "../src/language/bbj-lexer.js";

export function createBBjTestServices(context: DefaultSharedModuleContext): {
shared: LangiumSharedServices,
Expand All @@ -27,11 +28,20 @@ export function createBBjTestServices(context: DefaultSharedModuleContext): {
}

export const BBjTestModule: Module<BBjServices, PartialLangiumServices & DeepPartial<BBjAddedServices>> = {
parser: {
Lexer: (services) => new TestableBBjLexer(services)
},
java: {
JavaInteropService: (services) => new JavaInteropTestService(services)
}
}

export class TestableBBjLexer extends BbjLexer {
public prepareLineSplitter(text: string): string {
return super.prepareLineSplitter(text)
}
}

class JavaInteropTestService extends JavaInteropService {
protected readonly index: () => IndexManager;
private indexed: boolean = false;
Expand Down
22 changes: 21 additions & 1 deletion bbj-vscode/test/lexer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmptyFileSystem } from "langium";
import { expandToString } from "langium/generate";
import { createBBjTestServices } from "./bbj-test-module.js";
import { createBBjTestServices, TestableBBjLexer } from "./bbj-test-module.js";
import { describe, test, expect } from "vitest";

const services = createBBjTestServices(EmptyFileSystem);
Expand Down Expand Up @@ -29,4 +29,24 @@ describe('Lexer tests', () => {
expect(result.tokens[2].startOffset).toBe(afterIndex);
});

test('Joins split lines preserve offset with empty line', () => {
const text = `
if sys = "1" then
: goto *NEXT
: goto *BREAK
PRINT "After"
`;
const expectedSplitJoin = `
if sys = "1" then goto *NEXT goto *BREAK
PRINT "After"
`;
const tokenizedText = (lexer as TestableBBjLexer).prepareLineSplitter(text);
expect(tokenizedText.length).toBe(expectedSplitJoin.length);
expect(tokenizedText).toBe(expectedSplitJoin);
});
});
2 changes: 1 addition & 1 deletion bbj-vscode/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ describe('Parser Tests', () => {
expectNoValidationErrors(result);
});

test.skip('Switch-Default with semicolon', async () => {
test('Switch-Default with semicolon', async () => {
const result = await parse(`
LET t = 123
SWITCH t; CASE 1; PRINT "1"; BREAK; CASE 2; PRINT "2"; BREAK; CASE DEFAULT; PRINT "default"; SWEND
Expand Down

0 comments on commit e9ddf3e

Please sign in to comment.