-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new syntax for template strings (#1311)
### Summary of Changes - Template strings now use backticks as delimiters. Previously, they used double quotes like normal strings. - Template expressions are now enclosed in single curly braces. Previously, they were enclosed in double curly braces. --------- Co-authored-by: megalinter-bot <[email protected]>
- Loading branch information
1 parent
674cda8
commit 88295bc
Showing
42 changed files
with
300 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/safe-ds-lang/src/language/grammar/safe-ds-token-builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { DefaultTokenBuilder, GrammarAST, isTokenTypeArray } from 'langium'; | ||
import { TokenType, TokenVocabulary } from 'chevrotain'; | ||
|
||
// Inspired by https://eclipse-langium.github.io/langium-previews/pr-previews/pr-132/guides/multi-mode-lexing/ | ||
|
||
// Lexer modes | ||
const DEFAULT_MODE = 'default'; | ||
const TEMPLATE_STRING_MODE = 'template-string'; | ||
|
||
// Tokens | ||
const BLOCK_START = '{'; | ||
const BLOCK_END = '}'; | ||
const TEMPLATE_STRING_START = 'TEMPLATE_STRING_START'; | ||
const TEMPLATE_STRING_INNER = 'TEMPLATE_STRING_INNER'; | ||
const TEMPLATE_STRING_END = 'TEMPLATE_STRING_END'; | ||
|
||
export class SafeDsTokenBuilder extends DefaultTokenBuilder { | ||
override buildTokens(grammar: GrammarAST.Grammar, options?: { caseInsensitive?: boolean }): TokenVocabulary { | ||
const tokenTypes = super.buildTokens(grammar, options); | ||
|
||
if (isTokenTypeArray(tokenTypes)) { | ||
const defaultModeTokens = tokenTypes.filter( | ||
(token) => ![TEMPLATE_STRING_INNER, TEMPLATE_STRING_END].includes(token.name), | ||
); | ||
const templateStringModeTokens = tokenTypes.filter((token) => ![BLOCK_END].includes(token.name)); | ||
|
||
return { | ||
modes: { | ||
[DEFAULT_MODE]: defaultModeTokens, | ||
[TEMPLATE_STRING_MODE]: templateStringModeTokens, | ||
}, | ||
defaultMode: DEFAULT_MODE, | ||
}; | ||
} else { | ||
/* c8 ignore next 2 */ | ||
throw new Error('Invalid TokenVocabulary received from DefaultTokenBuilder.'); | ||
} | ||
} | ||
|
||
protected override buildKeywordToken( | ||
keyword: GrammarAST.Keyword, | ||
terminalTokens: TokenType[], | ||
caseInsensitive: boolean, | ||
): TokenType { | ||
let tokenType = super.buildKeywordToken(keyword, terminalTokens, caseInsensitive); | ||
|
||
if (tokenType.name === BLOCK_START) { | ||
// Enter default mode (for map literals and block lambdas) | ||
tokenType.PUSH_MODE = DEFAULT_MODE; | ||
} else if (tokenType.name === BLOCK_END) { | ||
// Return to previous mode | ||
tokenType.POP_MODE = true; | ||
|
||
// BLOCK_END has TEMPLATE_STRING_INNER and TEMPLATE_STRING_END as longer alternatives, which are not valid | ||
// in the default mode. | ||
delete tokenType.LONGER_ALT; | ||
} | ||
|
||
return tokenType; | ||
} | ||
|
||
protected override buildTerminalToken(terminal: GrammarAST.TerminalRule): TokenType { | ||
let tokenType = super.buildTerminalToken(terminal); | ||
|
||
if (tokenType.name === TEMPLATE_STRING_START) { | ||
// Enter template string mode | ||
tokenType.PUSH_MODE = TEMPLATE_STRING_MODE; | ||
} else if (tokenType.name === TEMPLATE_STRING_END) { | ||
// Return to previous mode | ||
tokenType.POP_MODE = true; | ||
} | ||
|
||
return tokenType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.