Skip to content

Commit

Permalink
Add parser and transpile functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Sep 24, 2021
1 parent d71e614 commit f8a6c1d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,46 @@ export class NullCoalescingExpression extends Expression {
}
}

export class RegexLiteralExpression extends Expression {
public constructor(
public tokens: {
regexLiteral: Token;
}
) {
super();
}

public get range() {
return this.tokens.regexLiteral.range;
}

public transpile(state: BrsTranspileState): TranspileResult {
let text = this.tokens.regexLiteral?.text ?? '';
let flags = '';
//get any flags from the end
const flagMatch = /\/([a-z]+)$/i.exec(text);
if (flagMatch) {
text = text.substring(0, flagMatch.index + 1);
flags = flagMatch[1];
}
//remove leading and trailing slashes
const pattern = text.substring(1, text.length - 1);

return [
state.sourceNode(this.tokens.regexLiteral, [
'CreateObject("roRegex", ',
`"${pattern}", `,
`"${flags}"`,
')'
])
];
}

walk(visitor: WalkVisitor, options: WalkOptions) {
//nothing to walk
}
}

// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
type ExpressionValue = string | number | boolean | Expression | ExpressionValue[] | { [key: string]: ExpressionValue };

Expand Down
9 changes: 9 additions & 0 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { Logger } from '../Logger';
import { isAnnotationExpression, isCallExpression, isCallfuncExpression, isClassMethodStatement, isCommentStatement, isDottedGetExpression, isIfStatement, isIndexedGetExpression, isVariableExpression } from '../astUtils/reflection';
import { createVisitor, WalkMode } from '../astUtils/visitors';
import { createStringLiteral, createToken } from '../astUtils/creators';
import { RegexLiteralExpression } from '.';

export class Parser {
/**
Expand Down Expand Up @@ -1396,6 +1397,12 @@ export class Parser {
return new NullCoalescingExpression(test, questionQuestionToken, alternate);
}

private regexLiteralExpression() {
return new RegexLiteralExpression({
regexLiteral: this.advance()
});
}

private templateString(isTagged: boolean): TemplateStringExpression | TaggedTemplateStringExpression {
this.warnIfNotBrighterScriptMode('template string');

Expand Down Expand Up @@ -2544,6 +2551,8 @@ export class Parser {
return new VariableExpression(token, this.currentNamespaceName);
case this.checkAny(TokenKind.Function, TokenKind.Sub):
return this.anonymousFunction();
case this.check(TokenKind.RegexLiteral):
return this.regexLiteralExpression();
case this.check(TokenKind.Comment):
return new CommentStatement([this.advance()]);
default:
Expand Down
54 changes: 54 additions & 0 deletions src/parser/tests/expression/RegexLiteralExpression.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Program } from '../../../Program';
import { standardizePath as s } from '../../../util';
import { getTestTranspile } from '../../../testHelpers.spec';

describe('RegexLiteralExpression', () => {
let rootDir = s`${process.cwd()}/rootDir`;
let program: Program;
let testTranspile = getTestTranspile(() => [program, rootDir]);

beforeEach(() => {
program = new Program({ rootDir: rootDir });
});
afterEach(() => {
program.dispose();
});

describe('transpile', () => {
it('captures flags', () => {
testTranspile(`
sub main()
print /hello/gi
end sub
`, `
sub main()
print CreateObject("roRegex", "hello", "gi")
end sub
`);
});

it('handles when no flags', () => {
testTranspile(`
sub main()
print /hello/
end sub
`, `
sub main()
print CreateObject("roRegex", "hello", "")
end sub
`);
});

it('handles weird escapes', () => {
testTranspile(`
sub main()
print /\\r\\n\\//
end sub
`, `
sub main()
print CreateObject("roRegex", "\\r\\n\\/", "")
end sub
`);
});
});
});

0 comments on commit f8a6c1d

Please sign in to comment.